0

I am using the Flutter Intl plugin to generate an arb (JSON) file for internationalization. Content of file intl_en.arb is JSON like this:

{
  "msg1": "message..",
  "msg2": "message..",
  "msg3": "message.."
}

I want to auto-generate all the keys of the JSON to class. The content of the class should look like this:

class AllText {
  static const String msg1 = 'msg1';
  static const String msg2 = 'msg2';
  static const String msg3 = 'msg3';
}

So I can use GetX internationalization like this:

Text(AllText.msg1.tr)

Is there a plugin to generate the class like this from the arb file?

(I don't want to use Intl, just need it to generate JSON file so I can use it in GetX)

Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
dante
  • 984
  • 3
  • 10
  • 24

1 Answers1

0

Update Jun 22nd:

For the generation from the arb file to class, you are looking for this package flutter_localizations. The config is straightforward and guided in the official documentation on i18n.

If you are using Android Studio/ IntelliJ IDEA like me, there's a package called flutter_intl (Check here if you use VS Code) that allows you to generate the class in a few clicks.


For i18n integration with GetX, simply copy the content of the arb files to a Map, then config it as below:

For example, here's an arb file containing the keys:

{
  "msg1": "message",
  "msg2": "message",
  "msg3": "message"
}

Declare variables representing your desired locales:

const Map<String, String> en = {
  "msg1": "message",
  "msg2": "message",
  "msg3": "message"
};

const Map<String, String> fr = {
  "msg1": "message_other",
  "msg2": "message_other",
  "msg3": "message_other"
};

Then, create a class extending Translations from GetX

class LocalizationService extends Translations {

  // Default locale
  static final locale = Locale('en', 'US');

  // fallback locale saves the day when the locale gets in trouble
  static final fallbackLocale = Locale('fr', 'FR');

  // Your supported language here
  static final langs = [
    'English',
    'France',
  ];

  // Supported locales (same order as above)
  static final locales = [
    Locale('en', 'US'),
    Locale('fr', 'FR'),
  ];

  // Keys and their translations
  // Translations are defined with `en` and `fr` variable above
  @override
  Map<String, Map<String, String>> get keys => {
        'en_US': en,
        'tr_TR': fr,
      };


  // Use the following method to update your locale
  // Gets locale from language and updates the locale
  void changeLocale(String lang) {
    final locale = _getLocaleFromLanguage(lang);
    Get.updateLocale(locale);
  }

  // Finds language in `langs` list and returns it as Locale
  Locale _getLocaleFromLanguage(String lang) {
    for (int i = 0; i < langs.length; i++) {
      if (lang == langs[i]) return locales[i];
    }
    return Get.locale;
  }
}

Finally, define the locale in your GetMaterialApp:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      home: HomePage(),
      locale: LocalizationService.locale,
      fallbackLocale: LocalizationService.fallbackLocale,
      translations: LocalizationService(),
    );
  }
}

Use the locale as below:

Text('msg1'.tr) // The `tr` extension is from `GetX` as well
Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
Bach
  • 2,928
  • 1
  • 6
  • 16
  • The point is to generate the class contains all keys so when you use it, you know the key is correct, no worry about typo. Not how to use GetX. – dante Jul 21 '21 at 15:28
  • @dante Updated my answer, I think it's what you're looking for – Bach Jul 22 '21 at 02:10