搜索插件、代码仓与 APP 分类

GitHub 代码仓erf/json_localizations

json_localizations

一个基于 LocalizationsDelegate 的最小化 YAML 本地化包

json_localizations 项目封面
星标
3
复刻
0
最近推送(UTC)
2025年7月7日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

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.

Erlend GitHub avatar
GitHub 作者

Erlend ↗

语言DartHTMLSwiftKotlinObjective-C

此仓库发布的插件

使用的依赖

依赖清单 4 项

  • flutter{"sdk":"flutter"}
  • flutter_test开发依赖{"sdk":"flutter"}
  • flutter_localizations开发依赖{"sdk":"flutter"}
  • flutter_lints开发依赖^6.0.0

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗