Excel 本地化
一个自动从 CSV 和 Excel 文件生成 Flutter 本地化资源的工具。
这尤其有用,因为任何团队成员都可以编辑 CSV/Excel 文件,随后通过终端命令将翻译导入项目。基本变量(字符串和整数)受支持,但性别和复数均不计划支持。如果您需要此类功能,请考虑使用 arb_generator。
请注意,插件仅支持 null safety。
特别感谢激励:点击这里
入门
要使用 excel_localization 包,请在 CSV 或 Excel 文件中提供您的翻译。对于 CSV 文件,已测试分隔符 , 和 ; 可正常工作。
在这里您可以看到 CSV 示例,说明用户需要如何安排数据
| 键 | fr | en | en_GB | hi |
|---|---|---|---|---|
| 纯文本 | Bonjour le monde! | Hello world! | Hello world! | हैलो वर्ल्ड! |
| 欢迎 | Bienvenu %name$s! | Welcome %name$s! | Welcome %name$s! | स्वागत%name$s! |
| favoriteColor | Quelle est votre couleur préférée? | What is your favorite color? | What is your favourite colour? | आपका पसंदीदा रंग कौनसा है? |
可以通过附加国家/地区代码来指定某个地区的可本地化项。
添加依赖
dependencies:
flutter_localizations:
sdk: flutter
dev_dependencies:
excel_localization:
请注意,excel_localization 需要 dart sdk >= 2.12。
定义设置
excel_localization 的设置必须在您项目的 pubspec.yaml 文件中设置。input_file_path 是唯一必需的参数。
excel_localization:
input_file_path: "lang.csv"
output_dir: "lib"
file_name: "i18n"
class_name: "I18n"
delimiter: ","
start_index: 1
depend_on_context: true
use_single_quotes: false
replace_no_break_spaces: false
expose_get_string: false
expose_loca_strings: false
expose_locale_maps: false
generate_comments: false
comment_languages: []
| 设置 | 默认值 | 描述 |
|---|---|---|
| input_file_path | 不适用 | 必需。输入 CSV/Excel 文件的路径。 |
| output_dir | lib | 生成输出文件的目录。 |
| file_name | i18n | 生成文件的文件名。 |
| class_name | I18n | 生成文件的类名。 |
| delimiter | , | 仅限 CSV 文件:用于分隔输入 CSV 文件中列的分隔符。 |
| start_index | 1 | 翻译开始的列索引(即默认语言的列索引)。 |
| depend_on_context | 真 | 生成的本地化项是否应依赖于 BuildContext。 |
| use_single_quotes | 假 | 生成的文件的字符串是否应使用单引号或双引号。 |
| replace_no_break_spaces | 假 | 是否将不间断空格 (\u00A0) 替换为普通空格 (\u0020)。 |
| expose_get_string | 假 | 是否公开 getString 方法的默认值。 |
| expose_loca_strings | 假 | 是否公开 locaStrings getter 的默认值。 |
| expose_locale_maps | 假 | 是否公开 localeMaps getter 的默认值。 |
| generate_comments | 假 | 是否应使用文档注释来显示翻译。 |
| comment_languages | [] | 在注释中显示的语言。空 -> 使用所有语言。 |
运行包
确保您当前的工作目录是项目根目录。
flutter pub get
flutter pub run excel_localization
更新 iOS Info.plist
对于 iOS,需要使用应用支持的语言数组更新 ios/Runner/Info.plist。
<key>CFBundleLocalizations</key>
<array>
<string>fr</string>
<string>en</string>
<string>hi</string>
</array>
有关更多信息,请参阅 国际化 Flutter 应用。
使用 i18n 生成的文件
该包使用您的输入文件在您提供的 output_dir 中生成一个名为 file_name 的文件。以下示例使用默认的 class_name I18n,并依赖于 BuildContext。
首先,将 I18nDelegate 添加到您的 delegates 中
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: I18n.languageKey, //<== This is require if you like to call words without context
localizationsDelegates: [
const I18nDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: I18nDelegate.supportedLocals,
home: Home(),
);
}
}
然后使用生成的 I18n 类!
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('excel localization sample"'),
),
body: Center(
child: Column(
children: <Widget>[
Text(I18n.of(context).plainText),
Text(I18n.of(context).welcome(name: 'Simran')),
Text(I18n.of(context).favoriteColor),
// To access variable without con the way is given below just replace of(context) to translate keyword
// Make sure you add navigatorkey parameter in MaterialApp or GetMaterialApp
Text(I18n.translate.plainText),
Text(I18n.translate.welcome(name: 'Simran')),
Text(I18n.translate.favoriteColor),
],
),
),
);
}
有关更多信息,请参阅 示例。
Material 本地化
支持未包含在 GlobalMaterialLocalizations 中的语言(例如 ga 或 cy)需要添加 material 本地化类和 delegate。尽管这超出了本包的范围,但在代码生成期间会向控制台记录警告。 更多信息。
规则和功能
Locale
Locale 在顶行指定,并期望以 en 或 en_US 的形式提供。
默认语言
start_index 列被视为默认语言。这意味着:
- 如果此列没有值,则将使用 loca 键。
- 如果另一种语言没有给定键的翻译,将使用默认语言的值。
Keys
每个 loca 键必须以小写字母开头,之后任何小写字母、大写字母、数字或下划线的组合都是有效的。
变量
为了在 loca 字符串中包含变量,它们需要写成 %<VAR NAME>$<VAR TYPE> 的格式。目前只支持整数和字符串作为变量类型。
- %myVariable$d(
d代表整数) - %myVariable$s(
s代表字符串)
所有变量都是必需的。考虑示例中的 `welcome` 键。生成的函数签名是:
String welcome({
required String name,
})
请注意,如果变量名以数字开头,生成的变量名将是 var<VAR NAME>。因此,例如,%1$d 将变成 var1。
