Flutter Translator is a package use for in-app localization with json file. More easier and faster to implement. This package is inspired by the flutter_localizations itself.
We’re not support the dynamic path and file name yet so the json files have to be at the assets/locales/ and the file name has to be localization_{languageCode}.json example: localization_en.json So the directory tree will look like this:
project_root_directory
|__ ...
|__ android
|__ assets
|__ fonts
|__ images
|__ locales
|__ localization_en.json
|__ localization_km.json
|__ localization_ja.json
|__ ios
|__ lib
|__ ...
And don’t for get to add asset path to the pubspec.yaml:
assets:
- assets/locales/
final TranslatorGenerator translator = TranslatorGenerator.instance;
@override
void initState() {
translator.init(
supportedLanguageCodes: ['en', 'km', 'ja'],
initLanguageCode: 'en',
);
translator.onTranslatedLanguage = _onTranslatedLanguage;
super.initState();
}
void _onTranslatedLanguage(Locale locale) {
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
supportedLocales: translator.supportedLocales,
localizationsDelegates: translator.localizationsDelegates,
home: HomeScreen(),
);
}
RaisedButton(
child: Text('English'),
onPressed: () {
translator.translate('en');
},
),
translator.getString(context, 'title');
translator.getLanguageName(languageCode: 'en'); // English
translator.getLanguageName(languageCode: 'km'); // ភាសាខ្មែរ
translator.getLanguageName(languageCode: 'ja'); // 日本語
translator.getLanguageName(); // get language name depend on current app locale
Author: channdara
Source Code: https://github.com/channdara/flutter_translator
#flutter #dart #mobile-apps