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

插件详情v2.3.0

csv_localizations

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

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
103
喜欢数
8
Pub 分数
150

安装插件

flutter pub add csv_localizations

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个轻量级的 Flutter 包,用于使用 CSV 文件管理应用程序翻译,基于 LocalizationsDelegate 框架构建。它使开发者能够通过读取存储在应用程序资源中的 CSV 文件,动态加载和切换语言翻译。该包通过提供一种简洁、结构化的方式来组织不同语言的键值对,简化了国际化,无需复杂的设置或样板代码。非常适合需要简单本地化且开销最小的应用程序。

适合场景

  • 管理多语言应用内容
  • 从CSV文件加载翻译
  • 支持动态语言切换
  • 简化翻译工作流程
  • 与现有基于资产的本地化集成
  • 轻松与翻译人员协作

主要特点

  • 基于CSV的翻译存储
  • 基于 LocalizationsDelegate 构建
  • 最小配置要求
  • 支持多个平台
  • 与 Flutter 资产的轻松集成
  • 动态语言加载

适合对象

  • 小型到中型应用
  • 具有非技术翻译人员的团队
  • 优先考虑简单性的项目
  • 具有静态翻译内容的应用程序
  • 开发者避免使用复杂的国际化解决方案
  • 使用CSV进行数据管理的项目

使用建议

  • 不太适合大规模翻译项目
  • 需要手动管理CSV文件
  • 对复数和格式化的支持有限
  • 无内置的翻译编辑工具
  • 最适合简单的键值翻译
  • 不处理运行时翻译更新

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

csv_localizations

A minimal CSV localization package for Flutter.

Store translations for multiple languages in a single CSV file.

One language per column - one translation per row.

Install

Add csc_localizations and flutter_localizations to your pubspec.yaml.

dependencies:
  flutter_localizations:
    sdk: flutter
  csv_localizations: <last-version>

Add a single CSV file asset to your pubspec.yaml.

flutter:
  assets:
    - assets/translations.csv

Add CsvLocalizationsDelegate and supported locales to MaterialApp.

MaterialApp(
  localizationsDelegates: [
    // delegate from flutter_localization
    ...GlobalMaterialLocalizations.delegates,
    // delegate from csv_localizations
    CsvLocalizationsDelegate(path: 'assets/translations.csv'),
  ],
  supportedLocales: [
    Locale('en'),
    Locale('nb'),
  ],
}
Note on iOS

Add supported locales to ios/Runner/Info.plist as described here

Example:

<key>CFBundleLocalizations</key>
<array>
	<string>en</string>
	<string>nb</string>
</array>

Format

A CSV file is a simple table-as-a-text-file with comma separated values as columns and new lines as rows.

In our case columns represents translations for a specific language and rows represent translations for a given key.

First row are supported language/country codes. First column are keys for localized values.

Example table:

key en nb
Hi Hi Hei
Dog Dog Hund
Cat Cat Cat

Tip (1) wrap multiline strings in quotation marks

Example CSV:

key,en,nb
Hi,Hi,Hei
Dog,Dog,Hund
Cat,Cat,Katt
my_img,assets/en.png,assets/nb.png

Tip (2) keys can point to local assets like images

API

Translate text using:

CsvLocalizations.instance.string('Hi')

Or add a String extension:

extension LocalizedString on String {
  String tr(BuildContext context) => CsvLocalizations.instance.string(this);
}

We don't want to pollute the String API by default

Now you could easily translate strings like this:

'Hi'.tr(context)

Note if a value does not exists for a given key, we return the key itself

We also support num, int and double via:

CsvLocalizations.instance.numValue('my_num')

CsvLocalizations.instance.intValue('my_int')

CsvLocalizations.instance.doubleValue('my_double')

Configurations

We support all the configurations from the csv package.

You can set it via CsvLocalizationsConfigurations when creating the CsvLocalizationsDelegate.

Here are the default configurations:

  const CsvLocalizationsConfigurations({
    this.fieldDelimiter = ',',
    this.textDelimiter = '"',
    this.textEndDelimiter = '"',
    this.eol = '\n',
    this.convertEmptyTo = '',
    this.allowInvalid = true,
    this.shouldParseNumbers = true,
  });

Example

See example

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。