搜尋套件、儲存庫與 APP 分類

套件詳情v0.9.0

i18next

基於 i18next 標準的本地化格式化程式。它還不是完整的國際化工具,僅支援格式化功能。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
6萬
喜歡數
21
Pub 分數
160

安裝套件

flutter pub add i18next

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

i18next 是一個基於 i18next 標準的 Flutter/Dart 套件,提供本地化格式化功能。它專注於國際化中的格式化部分,支援帶佔位符和複數形式的結構化翻譯處理。雖然不是功能完整的國際化解決方案,但可在跨平台間保持一致的文本格式。專為需要標準化本地化模式的開發者設計,可與現有工作流程良好整合。該套件相容 Android、iOS、Web、macOS、Windows 與 Linux。

適用情境

  • 使用佔位符格式化翻譯後的字串
  • 處理翻譯中的複數形式
  • 支援多語言應用內容
  • 與基於 i18next 的後端系統整合
  • 簡化動態訊息渲染

主要特色

  • i18next 標準合規
  • 佔位符和複數支援
  • 跨平台相容性
  • 輕量級格式化引擎
  • 與現有翻譯檔案輕鬆整合

適合對象

  • 需要結構化本地化的應用
  • 熟悉 i18next 的開發者
  • 使用外部翻譯管理的項目
  • 需要在不同平台上保持一致格式的團隊
  • 面向前端的國際化

使用建議

  • 不包含完整的國際化工具
  • 需要外部翻譯檔案管理
  • 僅限格式化,不包含載入或切換區域設定
  • 不適合複雜的國際化工作流程
  • 依賴於翻譯資源的正確設置

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

i18next

build codecov

This is an adaptation of i18next standard for Dart with support for Flutter localization techniques. This package is still a work in progress. Mind that this is still a pre-1.0.0 so breaking changes may occur frequently.

  • [x] Support for variables

  • [x] Support for namespaces

  • [x] Support for context

  • [x] Support for simple plural forms (one or plural)

  • [x] Support for multiple plural forms (one, few, many, plural, ...)

  • [x] Plural and context fallbacks

  • [x] Locale and namespace fallbacks

  • [x] Get string or object tree

  • [x] Support for nesting

  • [x] Flutter's LocalizationsDelegate support

  • [x] Asset bundle localizations data source (retrieves from pubspec.yaml). See the example for more details.

  • [ ] Sprintf support

  • [ ] Resource caching

  • [ ] Custom post-processing

  • [ ] ~Retrieve resource files from server~ (won't do, see https://github.com/williamhjcho/i18next/issues/27)

Usage

Simples way to add the latest version is to run flutter pub add i18next Or you can declare it directly into your pubspec.yaml file:

dependencies:
  i18next: ^0.9.0 # (or the latest version available)

To use it with flutter's LocalizationsDelegate you first create I18NextLocalizationDelegate and register it in your WidgetsApp (MaterialApp or CupertinoApp).

I18NextLocalizationDelegate(
  locales: widget.locales,
  // this data source is from where the delegate will retrieve the localizations from (namespaces Map)
  dataSource: ...,
  // optional extra options can be added here
  options: I18NextOptions(...),
),

Then to access and use it, simply call

Widget build(BuildContext context) {
  // It finds the i18next instance on the widgets tree via `Localizations.of`
  I18Next.of(context).t(...);
  ...
}

But if you want to handle it yourself, then simply instantiate it:

I18Next(
  locale,
  // This store is from where i18next will attempt to retrieve the localizations from.
  resourceStore: ...,
  // Optional extra options can be added here
  options: I18NextOptions(...)
);

Syntax

For the simple and straightforward usages:

{
  "key": "Hello World!",
  "nested": {
    "key": "My nested key"
  }
}
i18next.t('key'); // 'Hello World!'
i18next.t('nested.key'); // 'My nested key'

// unmapped keys usually return themselves (when graceful fallback fails)
i18next.t('unspecifiedKey'); // 'unspecifiedKey'
{
  "key": "Hello {{name}}!",
  "grouped_key": "Hello {{grouped.name}}"
}
i18next.t('key', arguments: {'name': 'World'}); // 'Hello World!'
i18next.t('grouped_key', arguments: {'grouped': {'name': 'Grouped World'}}); // 'Hello Grouped World!'
{
  "nesting1": "1 $t(nesting2)",
  "nesting2": "2 $t(nesting3)",
  "nesting3": "3"
}
i18next.t('nesting1'); // "1 2 3"
{
  "key": "item",
  "key_plural": "items",
  "keyWithCount": "{{count}} item",
  "keyWithCount_plural": "{{count}} items"
}
i18next.t('key', count: 0); // 'items'
i18next.t('key', count: 1); // 'item'
i18next.t('key', count: 5); // 'items'
i18next.t('keyWithCount', count: 0); // '0 items'
i18next.t('keyWithCount', count: 1); // '1 item'
i18next.t('keyWithCount', count: 5); // '5 items'

There are also ways of dealing with locales with multiple plural: zero, one, few, many, others (key identifier) (Unsupported)

  • Contexts like gender, are marked via underscores
{
    "genderMessage": "They",
    "genderMessage_male": "Him",
    "genderMessage_female": "Her"
}
i18next.t('genderMessage'); // 'They'
i18next.t('genderMessage', context: 'male'); // 'Him'
i18next.t('genderMessage', context: 'female'); // 'Her'

And can be used with plurals

{
  "friend": "A friend",
  "friend_plural": "{{count}} friends",
  "friend_male": "A boyfriend",
  "friend_female": "A girlfriend",
  "friend_male_plural": "{{count}} boyfriends",
  "friend_female_plural": "{{count}} girlfriends"
}
i18next.t('friend'); // 'A friend'
i18next.t('friend', count: 1); // 'A friend'
i18next.t('friend', count: 100); // '100 friends'

i18next.t('friend', context: 'male', count: 1); // 'A boyfriend'
i18next.t('friend', context: 'female', count: 1); // 'A girlfriend'
i18next.t('friend', context: 'male', count: 100); // '100 boyfriends'
i18next.t('friend', context: 'female', count: 100); // '100 girlfriends'
{
  "key1": "{{text, uppercase}} just uppercased",
  "key2": "The current date is {{now, datetime(format:dd/MM/yyyy)}}",
  "key3": "Right now we are in {{now, datetime(format:MMM), uppercase}}."
}
i18next.t('key1', arguments: { 'text': 'my text' }); // 'MY TEXT just uppercased'
i18next.t('key2', arguments: { 'now': DateTime.now() }); // 'The current date is 21/01/2020'
i18next.t('key3', arguments: { 'now': DateTime.now() }); // 'Right now we are in JAN.'

There are other usages and possibilities as well, this is just an example of what is defined by this format.

  • Namespaces: A namespace can be thought of as logical groupings of different sets of translations. In a given namespace you could have a set of languages, each with their own set of keys. They can also be understood as separate files. For example:

    • common.json: Things that are reused everywhere, eg. Button labels 'save', 'cancel'
    • validation.json: All validation texts
    • glossary.json: Words we want to be reused consistently inside texts
// common.json
{
  "myKey": "This key is in common"
}

// feature.json
{
  "myKey": "This key is in my feature"
}
i18next.t('common:myKey'); // 'This key is in common'
i18next.t('feature:myKey'); // 'This key is in my feature'
  • Context/plural fallback mechanism:
{
  "friend": "A friend",
  "friend_female": "A girlfriend"
}
i18next.t('friend'); // 'A friend'

i18next.t('friend', count: 1); // 'A friend'
// It fallbacks to `friend` since `friend_plural` is not present
i18next.t('friend', count: 2); // 'A friend'

i18next.t('friend', context: 'female'); // 'A girlfriend'
// It fallbacks to `friend` since `friend_male` is not present
i18next.t('friend', context: 'male'); // 'A friend'

There is a way to also set the default namespace or a order of namespaces so a key knows where to start looking for the translation.

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案