Search code examples
flutterlocalizationflutter-intl

Language structure problem in localization for flutter?


I am using localization in flutter.

this is the eng text: placed for $date

this is the translated text: $date का लागि राखिएको

as you can see the text has a variable (date) in it and as the grammar structure for the two language is different the variable has to be placed in different position.

AppLocalizations.of(context).placed_for + date.toString()
// The usual way of doing this does not work due to different placing of the date variable.

How do i handle such case? Do I have to write custom logic or is there already a way to handle such case.


Solution

  • I'm not used to work with the AppLocalizations itself, but it seems to work the same way as intl.

    If so, just edit your arb files to receive the desired parameters:

    "placed_for": "placed for {date}", 
    "placed_for": "{date}  का लागि राखिएको",
    

    then you should be able to use it in the following way:

    AppLocalizations.of(context).placed_for(date.toString())
    

    Edit:

    You also need to add the placeholder definition to your arb template. Example:

    {
      "helloWorld": "Hello {name}!",
      "@helloWorld": {
        "description": "The conventional newborn programmer greeting",
        "placeholders": {
          "name": {}
        }
      }
    }
    

    Then, after adding the placeholders, you can use the generated string using:

    AppLocalizations.of(context).helloWorld('Leandro'),
    

    PS. You can also add multiples placeholders