1

in my flutter app, I've implemented localization using .arb files. in my case what I want to implement is something like this:

"arrangement_index": "{value}{value == 1? 'st' : 'nd'}",
"@arrangement_index": {
    "placeholders": {
        "value": {
            "type": "int"
        }
    }
}

so, with the generated translations, if I use:

AppLocalizations.of(context)!.arrangement_index(1) I should get: 1st AppLocalizations.of(context)!.arrangement_index(2) I should get: 2nd

this approach is not working, how can I get such a functionality?

Adnan
  • 906
  • 13
  • 30

1 Answers1

3

flutter_localizations uses the ICU syntax for parsing the texts from the .arb files and generating the Dart localizations.

Here's the bible for writing localizations: https://icu.unicode.org/design/formatting/messageformat/newsyntax

If you follow their docs and apply it to your specific case, this is what you should end up with:

"arrangement_index": "{value,select, 1 {1st} 2 {2nd} 3 {3rd} other{{value}th}}",
"@arrangement_index": {
    "placeholders": {
        "value": {
            "type": "int"
        }
    }
}
  • Thank you so much! I was not able to find a result by searching something like `Flutter arb` or `Flutter l10n`, the link you've sent is very hard to understand and that's normal as its the official documentation, instead for anyone coming to this answer, you can find very good resources by searching about `flutter intl icu`, and there is also [this amazing youtube video](https://www.youtube.com/watch?v=MQo32dQxxjg). – Adnan Jul 06 '22 at 11:41
  • 1
    an additional thing I want to know is how would I treat +20 numers? for example for 21 I wanna give the value `{count}st`, but by checking its ending number. is there a way to do that? – Adnan Jul 06 '22 at 11:59
  • Instead of the real value, I would just pass the count%10. So you would need to handle 0-9. – Dániel Rózsa Dec 20 '22 at 17:03