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

套件詳情v1.6.0

yaml_localizations

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

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
33
喜歡數
2
Pub 分數
150

安裝套件

flutter pub add yaml_localizations

套件介紹

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

一個輕量級的 Flutter 套件,用於使用 YAML 檔案管理應用程式翻譯,基於 Flutter 的 LocalizationsDelegate。它透過從 YAML 資源載入本地化字串,讓國際化變得簡單,無需複雜設定即可支援多種語言。此套件可無縫整合至所有主要平台的 Flutter 應用中,提供一種乾淨且易維護的本地化處理方式,幾乎不需要樣板程式碼。非常適合尋求簡潔替代方案、避免使用基於 JSON 或硬編碼翻譯系統的開發者。

適用情境

  • 多語言應用支援
  • 簡單的翻譯管理
  • 與現有 Flutter 項目整合
  • 輕量級定位解決方案
  • 動態語言切換

主要特色

  • 基於 YAML 的翻譯檔案
  • 基於 LocalizationsDelegate 建構
  • 跨平台相容性
  • 最小設定
  • 易於擴展和維護

適合對象

  • 小型到中型應用
  • 偏好 YAML 而非 JSON 的開發者
  • 需要快速國際化設定的專案
  • 避免使用複雜的本地化框架的團隊
  • 針對多個平台的應用程式

使用建議

  • 需要手動管理 YAML 檔案
  • 無內建的翻譯編輯工具
  • 僅限基於字串的翻譯
  • 不適合大規模翻譯工作流程
  • 無執行時翻譯更新

原始 README

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

yaml_localizations

A minimal YAML localization package for Flutter.

YAML is a human-readable, configuration file format with a minimal syntax, which allows you to represent strings as key/value pairs in addition to other types.

YAML is the format used by Flutter's pubspec.yaml file.

Install

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

dependencies:
  flutter_localizations:
    sdk: flutter
  yaml_localizations:
Add a YAML file per language

Add a YAML file per language you support in an asset path and describe it in your pubspec.yaml

flutter:
  assets:
    - assets/yaml_translations/

The YAML file name must match the language tag described in supportedLocales.

E.g. Locale('en', 'US') must have a corresponding assetPath/en-US.yaml file.

Add localizationDelegates and supportedLocales

Add YamlLocalizationsDelegate to MaterialApp and set supportedLocales using language/country codes.

MaterialApp(
  localizationsDelegates: [
    // global delegates
    ...GlobalMaterialLocalizations.delegates,
    // yaml localizations
    YamlLocalizationsDelegate(path: 'assets/yaml_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-GB</string>
	<string>en</string>
	<string>nb</string>
</array>

YAML format

Hi: Hi
Text: |
  There once was a tall man from Ealing
  Who got on a bus to Darjeeling
      It said on the door
      "Please don't sit on the floor"
  So he carefully sat on the ceiling
Long: >
  Wrapped text
  will be folded
  into a single
  paragraph

  Blank lines denote
  paragraph breaks

Tip: Yaml supports several ways of expressing strings. Use the vertical bar character to indicate that a string will span several lines. Use the greater-than character to break up long lines.

API

Translate strings using

YamlLocalizations.of(context)?.string('Hi')

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) => YamlLocalizations.of(context)!.string(this);
}

So you can use it like this:

'Hi'.tr(context)

Example

See example

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。