搜尋套件、儲存庫與 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>

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。