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

localization 封面
插件详情v2.1.1

localization

用于简化包翻译的包。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.5万
喜欢数
194
Pub 分数
125

安装插件

flutter pub add localization

插件介绍

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

本地化包通过提供一种简化的方式来管理翻译,从而简化了Flutter应用中的国际化。它允许开发者高效地组织语言文件,并动态切换区域设置,从而为多语言用户提升用户体验。该包支持所有主要平台——Android、iOS、Web、macOS、Windows和Linux,确保在整个应用生命周期中保持一致的翻译处理。该包设计简洁易用,减少了样板代码并提高了可维护性。

适合场景

  • 多语言应用程序开发
  • 动态语言切换
  • 集中式翻译管理
  • 跨平台本地化
  • 基于用户偏好选择语言
  • 与现有 Flutter 项目的轻松集成

主要特点

  • 简单翻译文件结构
  • 自动检测区域设置
  • 支持复数形式
  • 易于使用的键值翻译系统
  • 内置回退机制
  • 平台无关的设计

适合对象

  • 开发全球应用的开发者
  • 管理多种语言的团队
  • 需要频繁翻译更新的项目
  • 针对不同地区受众的应用
  • Flutter 初学者学习国际化
  • 项目具有严格的本地化时间表

使用建议

  • 需要手动设置翻译文件
  • 有限的内置翻译工具
  • 与外部翻译服务无实时同步
  • 最适合静态翻译
  • 大规模翻译工作流可能需要额外的工具
  • 不处理运行时翻译获取

原始 README

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

Localization

Package to simplify in-app translation.

Install

Use the Localization package together with flutter_localization.

Add in your pubspec:

dependencies:
  flutter_localizations: 
    sdk: flutter
  localization: <last-version>

flutter:

  # json files directory
  assets:
    - lib/i18n/

Now, add the delegate in MaterialApp or CupertinoApp and define a path where the translation json files will be:

 @override
  Widget build(BuildContext context) {
    // set json file directory
    // default value is ['lib/i18n']
    LocalJsonLocalization.delegate.directories = ['lib/i18n'];
    
    //or set a map of translations
    MapLocalization.delegate.translations = {
      Locale('en', 'US'): {
        'welcome-text': 'This text is in english',
      },
      Locale('es', 'ES'): {
        'welcome-text': 'Este texto esta en español',
      },
      Locale('pt', 'BR'): {
        'welcome-text': 'Este texto está em português',
      },
    };

    return MaterialApp(
      localizationsDelegates: [
        // delegate from flutter_localization
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        
        // delegate from localization package.
        //json-file
        LocalJsonLocalization.delegate,
        //or map
        MapLocalization.delegate,
      ],
      home: HomePage(),
    );
  }

Json files

The json file pattern must have the name of the locale and its content must be a json of key and value ONLY. Create the files in the directory configured (LocalJsonLocalization.delegate.directories):

lib/i18n/en_US.json
lib/i18n/es_ES.json
lib/i18n/pt_BR.json

See an example of a translation json file:

en_US.json

{
  "welcome-text": "This text is in english"
}

es_ES.json

{
  "welcome-text": "Este texto esta en español"
}

pt_BR.json

{
  "welcome-text": "Este texto está em português"
}

Using

For convenience, the i18n() method has been added to the String class via extension. So just add the translation json file key as string and use i18n() method to bring up the translation.

String text = 'welcome-text'.i18n();
print(text) // prints 'This text is in english'

We can also work with arguments for translation. Use %s notion:

{
  "welcome-text": "Welcome, %s"
}
String text = 'welcome-text'.i18n(['Peter']);
print(text); // Welcome, Peter

The %s notation can also be retrieved positionally. Just use %s0, %s1, %s2...

You could plularization your Strings. Use %b{true:false} notion, where left is a string value when condition is true and on the right is a value when condition is false:

{
  "person-text": "Welcome, %b{people:person}"
}
final count = 2;
String text = 'person-text'.i18n(
        [], //args is a required positional parameter, if you don't gave a %s notation give a empty list []
        conditions: [count > 1]);
print(text); // Welcome, people

THAT`S IT!

Additional settings

After installation, the Localization package is fully integrated into Flutter and can reflect changes made natively by the SDK. Here are some examples of configurations that we will be able to do directly in MaterialApp or CupertinoApp.

Add supported languages

This setting is important to tell Flutter which languages your app is prepared to work with. We can do this by simply adding the Locale in the supportedLocales property:

return MaterialApp(
  supportedLocales: [
    Locale('en', 'US'),
    Locale('es', 'ES'),
    Locale('pt', 'BR'),
  ],
  ...
);
Locale resolution

Often we will not have to make some decisions about what to do when the device language is not supported by our app or work with a different translation for different countries (eg pt_BR(Brazil) and pt_PT(Portugal). For these and other decisions we can use the dsdsk property to create a resolution strategy:

return MaterialApp(
  localeResolutionCallback: (locale, supportedLocales) {
      if (supportedLocales.contains(locale)) {
        return locale;
      }

      // define pt_BR as default when de language code is 'pt'
      if (locale?.languageCode == 'pt') {
        return Locale('pt', 'BR');
      }

      // default language
      return Locale('en', 'US');
  },
  ...
);

Localization UI

Web version | Download now

We have an application to help you configure your translation keys. The project is also open-source, so be fine if you want to help it evolve!

localization ui

Features and bugs

Localization package is constantly growing. Let us know what you think of all this. If you agree, give a Star in that repository representing that you are signing and consenting to the proposed standard.

Questions and Problems

The issues channel is open for questions, to report problems and suggestions, do not hesitate to use this communication channel.

LET'S BE REFERENCES TOGETHER

所属分类

源码仓库

使用此插件的项目