【问题标题】:How to use easy_localization in a dependent flutter package?如何在依赖的flutter包中使用easy_localization?
【发布时间】:2020-12-16 02:51:57
【问题描述】:

我在我的应用程序中使用了 easy_localization,一切正常。 但是,我的应用程序所依赖的包之一(内部包)也有翻译,我想在这个包中使用 easy_translation。 如何配置我的主应用程序和包,以便它们都有自己的翻译?

这是我的主应用程序的配置:

  EasyLocalization(
    supportedLocales: AppConfig.SUPPORTED_LANG.split(',').map((e) => Locale.fromSubtags(languageCode: e)).toList(),
    path: 'lib/resources/assets/translations',
    useOnlyLangCode: true,
    assetLoader: CodegenLoader(),
    startLocale: Locale(Intl.getCurrentLocale()),
    fallbackLocale: Locale.fromSubtags(languageCode: AppConfig.DEFAULT_LANG),
    child: MyApp(),
  ),

我的主应用程序的所有翻译都在 CodegenLoader 类中。我想为我的包裹做同样的事情。

问候

【问题讨论】:

  • 你有没有解决这个问题?
  • 我没有使用那个包。只是一个简单的带有 intl 的 LocalizationsDelegate。
  • 是的,我以后可能也会这样做。现在,我有一个 hacky 解决方法。我会将其作为答案发布,因为它可能对某人有所帮助。

标签: flutter flutter-dependencies


【解决方案1】:

目前,我认为这是不可能的。但是有一种解决方法可能会对某些人有所帮助:

  1. easy_localization 添加到您的pubspec.yaml
  2. 在包代码中像往常一样使用 tr() 函数
  3. 将您在中使用的翻译键添加到主应用程序的翻译文件中
    • 示例:如果您在包中使用翻译密钥welcome,请将"welcome": "Welcome to the App!" 添加到主应用的en.json

当然,这是 hacky,如果主应用程序不使用 easy_localization,它就不起作用,但也许直到找到更好的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2019-09-12
    • 2021-01-13
    • 2023-02-24
    • 2021-07-31
    • 1970-01-01
    • 2019-12-12
    相关资源
    最近更新 更多