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

套件詳情v2.8.0

toml_localizations

一個基於 LocalizationsDelegate 的最小化 TOML 本地化套件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
94
喜歡數
3
Pub 分數
150

安裝套件

flutter pub add toml_localizations

套件介紹

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

一個輕量級的 Flutter 套件,用於使用 TOML(Tom 的明顯、最小語言)檔案管理應用程式翻譯。基於 Flutter 的 LocalizationsDelegate 建構,它透過從 TOML 檔案載入鍵值對,實現輕鬆整合本地化字串。該套件支援多種語系,並透過簡潔、易讀的格式簡化翻譯工作流程。非常適合尋求結構化且簡潔替代方案(而非 JSON 或 YAML)進行本地化的開發者。它可在所有主要的 Flutter 平台(包括 Android、iOS、Web、macOS、Windows 和 Linux)上運作。

適用情境

  • 管理多語言應用內容
  • 從 TOML 檔案載入翻譯
  • 簡化 Flutter 應用中的本地化設定
  • 支援動態語言切換
  • 整合結構化文字檔案進行翻譯

主要特色

  • 基於 TOML 的翻譯檔案
  • 最小依賴足跡
  • 基於 LocalizationsDelegate 建構
  • 跨平台支援
  • 易於閱讀的設定格式
  • 自動檢測區域設定

適合對象

  • 偏好 TOML 而非 JSON/YAML 的開發者
  • 需要輕量級本地化的專案
  • 使用結構化文字格式的團隊
  • 針對多種語言的應用
  • Flutter 應用需要簡單的翻譯管理

使用建議

  • 需要手動設定 TOML 檔案
  • 僅限基本字串翻譯
  • 無內建的翻譯編輯工具
  • TOML 解析可能不像 JSON 那樣熟悉
  • 不適用於複雜的本地化結構

原始 README

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

toml_localizations

A minimal TOML localization package for Flutter.

TOML is a minimal, easy to read, configuration file format, which allows you to represent strings (and other types) as key/value pairs.

Install

Add toml_localizations and flutter_localizations as dependencies to your pubspec.yaml.

dependencies:
  flutter_localizations:
    sdk: flutter
  toml_localizations: <last-version>
Add assets

Add a TOML file per language to a asset path and define it in your pubspec.yaml.

flutter:
  assets:
    - assets/toml_translations

The TOML file name must match exactly the combination of language and country code described in supportedLocales.

That is Locale('en', 'US') must have a corresponding assetPath/en-US.toml file.

Add localization delegate and supported locales

Add TomlLocalizationsDelegate to MaterialApp and set supportedLocales to supported language/country codes.

MaterialApp(
  localizationsDelegates: [
    // delegate from flutter_localization
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
    // toml localizations delegate
    TomlLocalizationsDelegate(path: 'assets/toml_translations')
  ],
  supportedLocales: [
    Locale('en', 'GB'),
    Locale('en', 'US'),
    Locale('en'),
    Locale('nb'),
  ],
}
Note on iOS

Add supported languages to ios/Runner/Info.plist as described here.

Example:

<key>CFBundleLocalizations</key>
<array>
	<string>en</string>
	<string>en-GB</string>
	<string>en-US</string>
	<string>nb</string>
</array>

Format

Example TOML file:

str = "The quick brown fox jumps over the lazy dog."

literal_str = 'C:\Users\nodejs\templates'

multiline_str = """\
The quick brown \
fox jumps over \
the lazy dog.\
"""

literal_multiline_str = '''
The first newline is
trimmed in raw strings.
   All other whitespace
   is preserved.
'''

list = [ 'one', 'two', 'three' ]

Tip: Toml supports several ways of expressing strings. See Toml documentation for more info.

API

Translate strings or other types using:

TomlLocalizations.of(context)!.value(this);

We keep the API simple, but you can easily add an extension method to String like this:

extension LocalizedString on String {
  String tr(BuildContext context) => TomlLocalizations.of(context)!.value(this);
}

Se you could translate strings like this:

'Hi'.tr(context)

Example

See example

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。