Search code examples
flutterpackagelocalizationgit-submodules

Flutter: How use flutter_localizations in package


I plan to move some common function / page to "flutter package"

But how to handle localization in these package ? I try to follow step https://docs.flutter.dev/development/accessibility-and-localization/internationalization

But it seem app-level localization, How to add localization in submodule/package ?

Update:

I tried to add flutter_intl / flutter_localizations in submodule, let it work like app-level. I also add module delegate in localizationsDelegates like this

    return MaterialApp(
      localizationsDelegates: const [
        S.delegate,
        module_a.S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ], ...

After test, here is result:

  1. it can build.
  2. but it seem not use submodule's translation. it always show english at this case.
  3. If I copy translate items from submodule to app, it seem work. it look like app translate string will override submodule's translate string

Am I use correct way to implement translation in flutter sub-module ?


Solution

  • I am not able to test your code, but I hope this helps.

    Regarding

    1. but it seem not use submodule's translation. it always show english at this case.

    As stated in MaterialApp's documentation:

    ///  * [supportedLocales], which must be specified along with
    ///    [localizationsDelegates].
    

    If I do not specify the supportedLocales, my app is only shown in english.

    Regarding

    1. If I copy translate items from submodule to app, it seem work. it look like app translate string will override submodule's translate string

    I think you are right, that the first S.type, in this case the S.delegate, is selected, because both S.delegate and module_a.S.delegate have the same LocalizationsDelegate.type=S.

    Something similar can be found in MaterialApp's documentation:

    /// Constructing a [MaterialApp] with a `FooLocalizationsDelegate` overrides
    /// the automatically included delegate for [MaterialLocalizations] because
    /// only the first delegate of each [LocalizationsDelegate.type] is used and
    /// the automatically included delegates are added to the end of the app's
    /// [localizationsDelegates] list.
    

    I split my flutter project into different packages, and the solution I choose is to generate a separate delegate class for each package (module).

    Example for a module l10n.yaml:

    arb-dir: lib/l10n
    template-arb-file: app_en.arb
    output-class: moduleALocalizations
    output-localization-file: module_a_localizations.dart
    synthetic-package: false # important, if you want to import the delegate in another package or app
    output-dir: lib/l10n/generated # this is optional, if synthetic-package=false and not stated, defaults to arb-dir location
    

    I set output-class and output-localization-file to the class and filename I want to use for the module. The delegate is added as to the [localizationsDelegates] as usual.