跳到主要内容

使用翻译

此项目依赖于 flutter_localizations 并遵循 Flutter 的官方国际化指南

添加字符串

  1. 要添加新的可本地化字符串,请打开 lib/l10n/arb/app_en.arb 中的 app_en.arb 文件。
{
"@@locale": "en",
"counterAppBarTitle": "Counter",
"@counterAppBarTitle": {
"description": "Text shown in the AppBar of the Counter Page"
}
}
  1. 然后添加新的键/值和描述
{
"@@locale": "en",
"counterAppBarTitle": "Counter",
"@counterAppBarTitle": {
"description": "Text shown in the AppBar of the Counter Page"
},
"helloWorld": "Hello World",
"@helloWorld": {
"description": "Hello World Text"
}
}
  1. 使用新字符串
import 'package:google_news_template/l10n/l10n.dart';


Widget build(BuildContext context) {
final l10n = context.l10n;
return Text(l10n.helloWorld);
}

添加支持的语言环境

更新 ios/Runner/Info.plist 文件中的 CFBundleLocalizations 数组以包含新的语言环境。例如

    ...

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

...

添加翻译

  1. 对于每个支持的语言环境,在 lib/l10n/arb 中添加一个新的 ARB 文件。
├── l10n
│ ├── arb
│ │ ├── app_en.arb
│ │ └── app_es.arb
  1. 将翻译后的字符串添加到每个 .arb 文件

app_en.arb

{
"@@locale": "en",
"counterAppBarTitle": "Counter",
"@counterAppBarTitle": {
"description": "Text shown in the AppBar of the Counter Page"
}
}

app_es.arb

{
"@@locale": "es",
"counterAppBarTitle": "Contador",
"@counterAppBarTitle": {
"description": "Texto mostrado en la AppBar de la página del contador"
}
}

文本方向性

当用户更改其语言设置时,Flutter 会自动支持从右到左的语言。如 Flutter 国际化指南 中所述,无需额外的配置或代码来支持应用程序中的文本方向性。