How to put a list of string in Flutter localizations?

Issue

I’m developen a flutter application and in order to internationalize it I’m using Flutter_localizations package. It works great and it is very easy to implement.

So, using this package I have a JSON file with some word with his translations as you can se here:

{
    
    "helloWorld" : "Hellow world!",
    "@helloWorld":{
        "description": "A programer greeting"
    },

    "team" : "Team",
    "@team":{
        "description": "Team text of buttom"
    },

    **"cart": {
        "one": "{{ count }} item in your Shopping cart",
        "other": "{{ count }} items in your Shopping cart"
    }**

    **"test" : ["test"],
    "@test":{
        "description": "car text of buttom"
    }**
}

However, I want to put a list of string as you can see in the key cart or in the key test, but flutter localizations doesn’t like something like that and it give me the error:

Generating synthetic localizations package failed with 2 error:

Exception: The value of "cart" is not a string.
Exception: The value of "test" is not a string.
exit code 1

Does anyone now how to put a list of string in order to translate that list using flutter localizations?

Solution

List also need to be defined as seperate list items for Flutter Localization like below inside .arb file:

  "yourTextList0": "I'm learning",
    "yourTextList1": "Good job",
    "yourTextList2": "Almost done",

Then you need to assign this values and create a list where you will use it.

Note: This example is for Flutter Intl localization implementation. If you use raw localization by only l10n.yaml option you may need to use different format and define the type as well:

"yourTextList[0]": "I'm learning",
"@yourTextList[0]": {
    "type": "text"
  },

 "yourTextList[1]": "Good job",
"@yourTextList[1]": {
    "type": "text"
  },

"yourTextList[2]": "Almost done",
"@yourTextList[2]": {
    "type": "text"
  },

Then use it like normal list:

DropdownButton(
                                value: yourDefaultValue,                                
                                onChanged: (value) {
                                  // have fun, do something here
                                },
                                items: S
                                    .of(context)
                                    .yourTextList
                                    .map((value) {
                                  return DropdownMenuItem(
                                      child: new Text(value,
                                          style: TextStyle(fontSize: 15)),
                                      value: value);
                                }).toList(),
 ),

Answered By – Elmar

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *