Kirubel
Kirubel

Reputation: 1627

Flutter Localizations with variables

I'm trying to localize my Flutter app by following the documentation.

What I'm trying to achieve is that while building widgets dynamically, I wanted to translate the data which comes from my model. This is what I've tried so far

List.generate(services.length, (index) {
  final Service service = services[index];
  return Material(
    borderRadius: BorderRadius.circular(10.0),
    child: Text(
      AppLocalizations.of(context).{{service.title}} // Here I wanted to translate the service title
    ),
  );
}

How can I achieve this in Flutter? or any other possible method to translate dynamic contents.

Upvotes: 7

Views: 4393

Answers (1)

Mol0ko
Mol0ko

Reputation: 3298

Dynamic translation is not supported by Flutter localization packages. You can try to integrate google translation API in you app. But I strongly convinced that it should be a server-side feature and Flutter client should obtain already translated messages in your models. To achieve this, for example, you can use http-headers with your device locale to tell client's language to server.

Also you need to use arguments for Intl messages according to this guide. Here is an example of message with String argument in AppLocalizations:

  String someLocalizedString(String argument) => Intl.message(
        'Localized part - $argument',
        name: 'someLocalizedString',
        locale: localeName,
        args: [argument],
      );

This string inside .arb file:

  "someLocalizedString": "Localized part - {argument}",
  "@someLocalizedString": {
    "type": "text",
    "placeholders": {
      "argument": {}
    }
  }

Upvotes: 9

Related Questions