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

源码仓库

使用此插件的项目

暂无相关数据。