首页 > 解决方案 > 如何在颤振中使用 l10n 本地化翻译从 JSON 文件填充的列表?

问题描述

我有很长的项目列表,这些项目使用 JSON 文件中写入的数据填充。我在 JSON 中有一组类别,然后在我的应用程序中将它们制成一个列表。

"categories": [
    { "id" : 1, "name" : "Category number 1" , "icon" : "assets/icons/risk.png"},
    { "id" : 2, "name" : "Some random Cat Name For Testing That is Long" , "icon" : "assets/icons/risk.png"},
    { "id" : 3, "name" : "Cat3" , "icon" : "assets/icons/risk.png", "details": ""},
    { "id" : 4, "name" : "Cat4" , "icon" : "assets/icons/risk.png"},
    { "id" : 4, "name" : "Cat4" , "icon" : "assets/icons/risk.png"},
    { "id" : 4, "name" : "Cat4" , "icon" : "assets/icons/risk.png"},
    { "id" : 4, "name" : "Cat4" , "icon" : "assets/icons/risk.png"},
    { "id" : 4, "name" : "Cat4" , "icon" : "assets/icons/risk.png"},
    { "id" : 4, "name" : "Cat4" , "icon" : "assets/icons/risk.png"},

...(列表继续这样)。

数据通过以下方式加载到应用程序中:

void getData() async{
  final read = await rootBundle.loadString('assets/strings/risk_categories.json');
  final data = await json.decode(read);

  setState(() {
    if(data != null){
      _items = data["categories"];
      _description = data["description"].toString();
    }
  });
}

我的问题是,我怎样才能使这些数据容易受到语言变化的影响。我尝试将它以相同的格式放在 .arb 文件中,但它坚持将字符串作为值并且不会接受我的数组。有没有办法在 arb 或其他东西中搜索相同的值?我在这里错过了什么吗?

谢谢您的帮助!

标签: jsonflutterlocalization

解决方案


推荐阅读