3

我做了一个 Flutter 插件,我需要添加 Internationalization。我已经像通常为我的 Flutter 应用程序所做的那样遵循本教程:Flutter 1.22+ 中的国际化

但是使用 Flutter 插件没有 MaterialApp 所以我不能添加这个:

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

那么,有没有办法将国际化添加到我的 Flutter 插件中,以便我可以在我的插件中使用它?

Translations.of(context).title;
4

2 回答 2

2

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

于 2020-11-25T13:12:08.417 回答
-1

这不是什么大事......只需按照此处此处此处的步骤操作

或者,如果您愿意,您可以使用谷歌翻译服务。在这里,您s=不必做任何事情,但您必须为每种语言付费。如果您有开发者帐户,则可以在 Play 控制台中找到它。

于 2020-11-19T11:57:32.027 回答