搜索插件、代码仓与 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.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。