0

我实际上正在编写一个多语言应用程序,但在获取不同语言的资源时遇到了问题。我的应用程序目前正在使用一种带有 json 文件的语言。例如我得到:

{
  "Resorts" : [
    {
      "name": "Walt Disney World Resort",
      "slug": "waltdisneyworldresort",
      "description": "Explorez des terres d'enchantement infini, où vos fantasmes deviennent réalité !"
    },
    {
      "name": "Disneyland Resort",
      "slug": "disneylandresort",
      "description": "Regardez les rêves devenir réalité dans l'endroit le plus heureux de la planète !"
    },
  ]
}

我开始使用l10n,我有我的文件 app_en.arb:

{
  "@@locale": "en",

  "resortDesc": "English description",
  "@resortDesc": {
    "description": "Description of the resort that will be displayed on the cards in the Resort Selection screen",
  }
}

和 app_fr.arb:

{
  "@@locale": "fr",

  "resortDesc": "Description en Français",
  "@resortName": {
    "description": "Description du resort qui sera affiché sur la page 'Resort Selection'",
  }
}

但这意味着我需要使用以下内容来调用我的文本:

AppLocalizations.of(context)!.resortDesc

当我从我的 json 中调用我的文本时:

Future<void> readJsonDatas() async {
  final String res = await rootBundle.loadString("assets/datas.json");
  final data = await jsonDecode(res);
  setState(() {
    datas = data;
  });
  await datas;
  for(var data in datas['Resorts']) {
    parks.add(
        Resort(name: data['name'], imagePath: data['image'], slug: data['slug'], bgImagePath: data['bg_image'], description: data['description'], parks: data['parks'] ?? [], lat: data['latitude'], long: data['longitude'])
    );
  }
  currentResort = parks[0]; // Default park
}

你知道我可以在 app_en.arb 和 app_fr.arb 中做什么来获取我的数据:

AppLocalizations.of(context)!.datas['slug'].description

或者我应该怎么做?非常感谢您的提示,祝您有美好的一天!

4

0 回答 0