flutterdartinternationalizationflutter-intl

Internationalisation in Flutter: how do you get all keys from one select?


Ok, maybe the question is not very precise. But, I figured out that the only way to have something similar to Android's <string-array> for translation in Flutter is to use select. For instance,

"pronoun": "{gender, select, male{he} female{she} other{they}}",
"@pronoun": {
  "description": "A gendered message",
  "placeholders": {
    "gender": {
      "type": "String"
    }
  }
}

Now, how can I get the array of keys that are used in creating this definition? In the upper case, the answer should be [male, female, other].


Solution

  • You have to define the enum yourself.

    The docs say:

    It is possible to use a Dart enum as the choice and as the key in cases, but note that we will process this by truncating toString() of the enum and using just the name part. We will do this for any class or strings that are passed, since we can't actually identify if something is an enum or not.

    Your dart code could look like:

    enum Gender{
      male,
      female,
      nonbinary;
    }
    
    var birthdaycake = Translations.of(context).birthdaycake(Gender.female.toString())
    

    arb-file:

     "birthdaycake": "{gender, select, male{his} female{her} nonbinary{their} other{please use the enum 'Gender'. this should not happen.}} Birthdaycake",