How to add Internationalization to a Flutter Plugin

Issue

I made a Flutter plugin and I need to add Internationalization.
I have follow this tutorial like I usually do for a my Flutter app : Internationalization in Flutter 1.22+

But with a Flutter plugin there is no MaterialApp so I can’t add this :

MaterialApp(
   localizationsDelegates: Translations.localizationsDelegates,
   supportedLocales: Translations.supportedLocales
)

So, is there a way to add Internationalization to my Flutter plugin so I can use this in my plugin ?

Translations.of(context).title;

Solution

So I found an answer in case someone need this :
You need to import the generated .dart file in your app to use it.

In l10n.yaml plugin file I have output-localization-file=translations.dart so I need to import this file in example/main.dart (or in any Flutter app using the plugin) :

import 'package:MinimalExampleInternationalization/l10n/translationsUpdate.dart';
and that’s where I need to add this code :

 MaterialApp(
   localizationsDelegates: Translations.localizationsDelegates,
   supportedLocales: Translations.supportedLocales
 )

If your Flutter app that import this plugin already has Internationalization then you can add multiple localizationsDelegates: and supportedLocales: like this :

 MaterialApp(
          localizationsDelegates: Translations.localizationsDelegates+TranslationsPlugin.localizationsDelegates,
          supportedLocales: Translations.supportedLocales+TranslationsPlugin.supportedLocales,
 )

Where Translations is the class generated by your app and TranslationsPlugin the class generated by your plugin.

Also note that right now there is a bug when generating the Internationalisation files with a Plugin so you can delete the l10n.yaml file and use this command instead : flutter gen-l10n --arb-dir=assets/l10n --template-arb-file=string_en.arb --output-localization-file=translations.dart --output-class=Translations --output-dir=lib/l10n --no-synthetic-package

More informations : Flutter Issue and Working Plugin Example with I18n

Answered By – zackattack

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *