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

源码仓库

使用此插件的项目

暂无相关数据。