4

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;

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Any ideas on how to override the plugins generated texts?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.