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

套件詳情v0.7.0

json_localizations

一個基於 LocalizationsDelegate 的最小化 JSON 地方化套件。

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

安裝套件

flutter pub add json_localizations

套件介紹

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

一個基於 Flutter 的 LocalizationsDelegate 輕量級 JSON 地區化套件。它透過從 JSON 檔案載入翻譯,支援多種語言與動態語言切換,便於管理地區化字串。設計簡潔,與 Flutter 的國際化系統無縫整合,讓開發者能維護清晰、易讀的翻譯檔案,無需複雜設定。適用於需要簡單多語支援且開銷最小的應用。

適用情境

  • 多語言應用支援
  • 動態語言切換
  • 簡單翻譯檔案管理
  • 將翻譯嵌入資源
  • 輕量級定位解決方案
  • 可自訂區域設定回退

主要特色

  • 基於 JSON 的翻譯檔案
  • 擴展 LocalizationsDelegate
  • 支援多種語言環境
  • 輕鬆整合資產
  • 最小的樣板程式碼
  • 備用區域設定處理

適合對象

  • 小型到中型應用
  • 尋求簡潔的開發者
  • 具有靜態翻譯的專案
  • 使用 JSON 進行本地化的團隊
  • 需要基本國際化功能的應用
  • 快速原型設計

使用建議

  • 不適合大規模翻譯工作流程
  • 無內建的翻譯編輯工具
  • 手動 JSON 檔案管理
  • 有限的複數支援
  • 無自動金鑰提取
  • 需要手動設定區域設定

原始 README

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

json_localizations

A minimal JSON localization package for Flutter.

Use a JSON file/object per language to represent key/value pairs for localizing your app.

Install

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

dependencies:
  flutter_localizations:
    sdk: flutter
  json_localizations:
Add a JSON file per language

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

flutter:
  assets:
    - assets/json_translations/

The JSON 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.json file.

Add JsonLocalizationsDelegate to MaterialApp

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

MaterialApp(
  localizationsDelegates: [
    // delegate from flutter_localization
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
    // delegate from json_localizations
    JsonLocalizationsDelegate('assets/json_translations'),
  ],
  supportedLocales: [
    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>

Format

Example JSON file:

{
	"hello": "hello",
	"bye": "bye",
	"items": [ "one", "two", "three" ],
	"count": 1
}

Tip: You can store any json type given a key, like a string, an array of strings, or a number

API

Get a value for the current language using:

JsonLocalizations.of(context)?.value('hello')

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

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

So you could write:

'hello'.tr(context)

Example

See example.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。