Flutter 国际化:如何访问 json 中的嵌套数据

Oot*_*oto 5 internationalization flutter

我想知道如何访问 json 中的嵌套数据。

AppLocalizations.of(context).translate('Information.about');
Run Code Online (Sandbox Code Playgroud)

en.json

{  
  "Information" : {
      "about": "About"
  }

}
Run Code Online (Sandbox Code Playgroud)

我尝试了上面的方法,但它无法访问嵌套数据。

这是翻译方法。

  class AppLocalizations {
      final Locale locale;

  AppLocalizations(this.locale);

  static AppLocalizations of(BuildContext context) {
    return Localizations.of<AppLocalizations>(context, AppLocalizations);
  }

  // Static member to get access to the delegate from 'main.dart' file
  static const LocalizationsDelegate<AppLocalizations> delegate = _AppLocalizationsDelegate();

  Map<String, String> _localizedValues;

  Future<bool> load() async {
    // Load a language JSON file from the 'i18n' folder
    String value = await rootBundle.loadString('i18n/${locale.languageCode}.json');
    Map<String, dynamic> jsonMap = jsonDecode(value);
    _localizedValues = jsonMap.map((key, value) {
      return MapEntry(key, value.toString());
    });
    return true;
  }

  String translate(String key) {
    // Returns a localized text
    return _localizedValues[key];
  }
}
Run Code Online (Sandbox Code Playgroud)

Mor*_*rez 2

尝试这个:

\n\n

第一种方法:

\n\n
AppLocalizations.of(context).translate(\xe2\x80\x9cabout\xe2\x80\x9d);\n
Run Code Online (Sandbox Code Playgroud)\n\n

并将翻译功能更改为:

\n\n
  String translate(String key) {\n// Returns a localized text\n  return _localizedValues[\xe2\x80\x9cInformation\xe2\x80\x9d][key];\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者你可以这样做:

\n\n

第二种方法:

\n\n
AppLocalizations.of(context).translate(\xe2\x80\x9dInformation\xe2\x80\x9d,\xe2\x80\x9cabout\xe2\x80\x9d);\n
Run Code Online (Sandbox Code Playgroud)\n\n

并将翻译功能更改为:

\n\n
 String translate(String parentkey, String nestedKey) {\n// Returns a localized text\n  return _localizedValues[parentKey][nestedKey];\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n

可能会有所帮助。

\n\n

另外,是一篇学习如何解析复杂 json 文件的好文章

\n\n

更新的答案:

\n\n

尝试代码后,我明白了问题所在。

\n\n

问题是你的 _localizedValues["Information"] 将是一个字符串而不是地图,因为我们将值转换为 value.toString() ,这就是为什么你不能使用第二个键,因为返回的对象不是 Map 而是它\'一个字符串。

\n\n

所以 _localizedValues["Information"] 是“{about: About}”。

\n\n

要解决该问题,请使用以下代码:

\n\n
  Map<String, dynamic> _localizedValues; //your values are not String anymore and we use dynamic instead\nFuture<bool> load() async {\n    // Load a language JSON file from the \'i18n\' folder\n    String value = await rootBundle.loadString(\'i18n/${locale.languageCode}.json\');\n    Map<String, dynamic> jsonMap = jsonDecode(value);\n    _localizedValues = jsonMap.map((key, value) {\n      return MapEntry(key, value); //not value.toString() so the value will be a map\n    });\n    return true;\n  }\nString translate(String parentkey, String nestedKey) {\n    // Returns a localized text\n      return _localizedValues[parentKey][nestedKey];\n     }\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后你必须从下面的代码中获取“关于”:

\n\n
AppLocalizations.of(context).translate(\'Information\',\'about\');\n
Run Code Online (Sandbox Code Playgroud)\n