flutter internationalization guide code example

Example: flutter internationalization

class AppLocalizations {  final Locale locale;  AppLocalizations(this.locale);  // Helper method to keep the code in the widgets concise  // Localizations are accessed using an InheritedWidget "of" syntax  static AppLocalizations of(BuildContext context) {    return Localizations.of<AppLocalizations>(context, AppLocalizations);  }  // Static member to have a simple access to the delegate from the MaterialApp  static const LocalizationsDelegate<AppLocalizations> delegate =  _AppLocalizationsDelegate();  Map<String, String> _localizedStrings;  Future<bool> load() async {    // Load the language JSON file from the "lang" folder    String jsonString =    await rootBundle.loadString('i18n/${locale.languageCode}.json');    Map<String, dynamic> jsonMap = json.decode(jsonString);    _localizedStrings = jsonMap.map((key, value) {      return MapEntry(key, value.toString());    });    return true;  }  // This method will be called from every widget which needs a localized text  String translate(String key) {    return _localizedStrings[key];  }}

Tags:

Misc Example