Flutter / l10n with dynamic parameters?

Viewed 722

How to use l10n with dynamic parameters? like this.

AppLocalizations.of(context)!['dynamic_string']; => Try defining the operator '[]'.

But the class is generated automatically so overwrite with each build. Thx , If anyone has a solution.

Have a good day !

2 Answers

You cannot. And should not. What is the chance that a random value is actually in your resources?

You can always branch depending on decisions in your program:

isUsersFirstLogin ? AppLocalizations.of(context)!.Hello 
                  : AppLocalizations.of(context)!.HelloAgain

If you absolutely need this feature of addressing random dynamic resources that are magically correct, there is a alternative solution described here:

https://flutter.dev/docs/development/accessibility-and-localization/internationalization#alternative-internationalization-workflows

You can do it by providing a placeholder in the .arb file like so:

"weekTitle": "Week {weekNumber}",
"@weekTitle": {
    "placeholders": {
        "weekNumber": {
            "type": "int"
        }
    }
}

Then in your code you can call it like this:

AppLocalizations.of(context).weekTitle(_calendarWeek)
Related