3

I am actually coding a multi-language app and I have a problem to get my resources on different languages. My app is currently working on one language with a json file. For example I got :

{
  "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 !"
    },
  ]
}

I started using l10n and I have my files 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",
  }
}

and app_fr.arb:

{
  "@@locale": "fr",

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

But this means that I need to call my texts with something like:

AppLocalizations.of(context)!.resortDesc

While I was calling my texts from my json as:

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
}

Do you have any idea what I could do in app_en.arb and app_fr.arb to get my datas as:

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

Or how I should do it ? Many thanks for your tips, Have a great day !

Akainn Z
  • 51
  • 5

0 Answers0