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

插件详情v1.1.2

i18n_localizations

一个简单的 Flutter 国际化包,Angular TranslateService 用户会喜欢这个包。

近 30 天下载
17
喜欢数
5
Pub 分数
40

安装插件

flutter pub add i18n_localizations

插件介绍

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

一个轻量级的Flutter包,专为国际化设计,提供一种简单直观的方式来管理应用程序中的翻译。受Angular的TranslateService启发,它提供了一个易于使用的API,用于在不同语言之间切换并获取本地化字符串。非常适合那些希望获得简单解决方案而无需复杂设置的开发者。该包支持动态语言更改,并可无缝集成到Flutter项目中。对于熟悉Angular翻译模式的开发者尤其具有吸引力。

适合场景

  • 多语言应用支持
  • 动态语言切换
  • 简单的翻译管理
  • 网页和移动应用本地化
  • 与现有翻译文件的集成

主要特点

  • Angular TranslateService 启发的 API
  • 轻松切换语言
  • 支持 JSON 翻译文件
  • 最小的样板代码
  • 简洁清晰的语法

适合对象

  • Flutter 初学者
  • 从 Angular 迁移的开发者
  • 需要基本国际化支持的应用
  • 中小型翻译需求的项目
  • 使用基于 JSON 的翻译工作流的团队

使用建议

  • 与完整的国际化框架相比,高级功能有限
  • 无内置翻译文件生成
  • 手动处理缺失的键
  • 不适合大规模企业应用
  • 依赖外部JSON文件管理

原始 README

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

i18n_localizations

A flutter package for internationalization, it allows you to simply add the translation of the application according to the language of the device, it uses json files as angular. You only need to configure it properly to be able to translate your entire app.

Getting Started

Follow the next step for use i18n_localizations

Configuration

To use this library, you will need json file you can put there where you want, but don't forget to add them to your pubspec.yaml

assets:
  - {translateDir}/en.json
  - {translateDir}/fr.json
  - {translateDir}/{languageCode}.json
    or
  - {translateDir}/en_US.json
  - {translateDir}/fr_FR.json
  - {translateDir}/{languageCode}_${countryCode}.json

You need to initialize a I18nLocalizationsDelegate white a list of supportedLocales and a pathFile where the json file are store.

final I18nLocalizationsDelegate i18n = I18nLocalizationsDelegate(
      supportedLocales: [Locale('en'), Locale('fr')],
      pathFile: '{translateDir}'
  );
or
final I18nLocalizationsDelegate i18n = I18nLocalizationsDelegate(
      supportedLocales: [Locale('en', 'US'), Locale('fr', 'FR')],
      pathFile: '{translateDir}',
      haveCountryCode: true
  );

Then you need to configure the supportedLocales, localizationsDelegates and localeResolutionCallback of your MaterialApp or CupertinoApp

  supportedLocales: i18n.supportedLocales,
  localizationsDelegates: [
   i18n,
   GlobalMaterialLocalizations.delegate,
   GlobalWidgetsLocalizations.delegate
  ],
  localeResolutionCallback: (Locale locale, supportedLocales) {
    return i18n.localeResolutionCallback(locale);
  },

Result

// construct the i18nLocalisationsDelegate
final I18nLocalizationsDelegate i18n = I18nLocalizationsDelegate(
      supportedLocales: [Locale('en', 'US'), Locale('fr', 'FR')],
      pathFile: '{translateDir}'
  );
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'MyApp',
        onGenerateRoute: router.generateRoute,
        debugShowCheckedModeBanner: false,
        initialRoute: HomePageRoute,
        // set the supportedLocales for the app
        supportedLocales: i18n.supportedLocales,
        localizationsDelegates: [
         // don't forget to put the delegate
          i18n,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate
        ],
        localeResolutionCallback: (Locale locale, supportedLocales) {
           // get the default local when the device language is not supported, use the first one
          // from the list (English, in this case).
          return i18n.localeResolutionCallback(locale);
        },
    );
  }

Use

To use I18nLocalizations, just call :

I18nLocalizations.translate(context,"keySimple")
I18nLocalizations.translate(context,"key.child_one")
I18nLocalizations.translate(context,"key.child_two.grandchild")

I18nLocalizations.translate(context,"keyParams", params:{"key": "value"})

I18nLocalizations.translatePlural(context,"keyPlural", params:{"key":"6"})
I18nLocalizations.translatePlural(context,"keyPlural", params:{"key":"1"})
I18nLocalizations.translatePlural(context,"keyPlural", params:{"key":"not a number"})

And the json file will look like:

{
  "keySimple": "key simple",
  "key": {
    "child_one": "child 1",
    "child_two": {
      "grandchild": "grandchild of key"
    }
  },
  "keyParams": "keyParams have a params or a {key}",
  "keyPlural": {
    "none": "there are none key here",
    "one": "the value of our key is {key}",
    "many": "we have {key} keys"
  }
}

Note: there should be no space between the key and the parameter hooks

⚠️ Note on iOS

For translation to work on iOS you need to add supported locales to ios/Runner/Info.plist as described here.

Example:

<key>CFBundleLocalizations</key>
<array>
	<string>en</string>
	<string>fr</string>
</array>

所属分类

源码仓库

使用此插件的项目

暂无相关数据。