flutterdartlocalizationintlarb

Flutter l10n using arb: logic translations


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?


Solution

  • 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"
            }
        }
    }