搜索插件、代码仓与 APP 分类

插件详情v0.6.0

multiple_localization

支持 intl 包的多个 abr 和消息本地化文件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
3.5万
喜欢数
28
Pub 分数
160

安装插件

flutter pub add multiple_localization

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

multiple_localization 包通过支持多个本地化文件(包括 .arb 文件)增强了 intl 包,使开发者能够更高效地管理不同语言和区域设置的翻译。它简化了在 Flutter 应用中组织和加载本地化消息的过程,尤其适用于大规模国际化需求。该包设计为与 Flutter 现有的国际化基础设施无缝协作,使翻译资源的维护和扩展更加容易。它支持所有主要平台:Android、iOS、Web、macOS、Windows 和 Linux。

适合场景

  • 在一个项目中管理多个语言文件
  • 简化大型应用的翻译工作流程
  • 支持动态区域设置切换
  • 按功能或模块组织翻译
  • 与现有的基于 intl 的本地化集成
  • 启用模块化本地化结构

主要特点

  • 支持多个 .arb 文件
  • 与 intl 包的无缝集成
  • 灵活的文件组织
  • 平台无关的设计
  • 通过 pub add 轻松设置
  • 自动消息加载

适合对象

  • 大规模国际化Flutter应用
  • 管理多个翻译文件的团队
  • 需要模块化本地化的项目
  • 使用 intl 进行国际化开发的开发者
  • 面向多样化全球受众的应用程序

使用建议

  • 需要熟悉 intl 包
  • 自定义文件路径需要额外配置
  • 可能因文件过多而增加构建复杂性
  • 最适合已经使用 intl 的项目
  • 不兼容非国际化定位方法

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

multiple_localization

pub package Analyze & Test innim lint

Support for multiple abr and messages localization files for intl package.

Problem

If you want to have multiple arb files and register separate delegate for each one of them, then you have a problem. Intl doesn't allow multiple initializeMessages calls. Only first one will be processed and only it's messages will be used. Every calls after the first one will be ignored.

Why do we need to have multiple arb files?

In common scenario - we don't. Just put all localization string in single file and enjoy. But if you want to add intl localization with arb files to separate package, and than use it in you project with it's own localization files - that's problem.

MultipleLocalizations supports using Localizations.override(delegates: [SomeLocalizationsDelegate(), ...]) widget, too.

Exactly for that situation this package was developed.

See article on Medium for more details - Localization for Dart package.

Usage

To use this package, add multiple_localization as a dependency in your pubspec.yaml file.

Than use MultipleLocalizations.load for your delegate load function, instead of call initializeMessages explicitly.

Example
class _AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
  const _AppLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) {
    return ['en', 'ru'].contains(locale.languageCode);
  }

  @override
  Future<AppLocalizations> load(Locale locale) {
    return MultipleLocalizations.load(
        initializeMessages, locale, (l) => AppLocalizations(l),
        setDefaultLocale: true);
  }

  @override
  bool shouldReload(LocalizationsDelegate<AppLocalizations> old) {
    return false;
  }
}
Override string from package in project

If you want to override string from package localization in project (for example to change some labels in package widgets) - you need to define string with required name in you app localization class.

Example.

If package has string messageFromPackageForOverride:

class PackageLocalizations {
  ...

  String get messageFromPackageForOverride =>
        Intl.message('Package', name: 'messageFromPackageForOverride');

Then you need to define messageFromPackageForOverride in you AppLocalizations:

class AppLocalizations {
  ...

  String get messageFromPackageForOverride =>
        Intl.message('App', name: 'messageFromPackageForOverride');

Make sure that you add the app localization delegate before the package localization delegate:

  ...
  const MaterialApp(
    localizationsDelegates: [
      AppLocalizations.delegate,
      PackageLocalizations.delegate,
      ...
    ],
    ...

Now, whenever package use messageFromPackageForOverride you will see App, not Package.

Set fallback locale

Also you can specify a fallback locale when loading application localization delegate. Use [fallbackLocale] parameter to set locale which will be used if some key not found for current locale. Only first call of [load] will set fallback locale, make sure that your app's localization delegate added as a first element of the delegates list.

Also pay attention that if you provide the [fallbackLocale], than all messages will be uploaded in memory on the start of application in addition to current locale.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。