我正在将应用程序的一些小部件移动到单独的 Flutter 包中。这些小部件使用AppLocalization显示本地化字符串。我想定义包内的字符串。
包中没有 MaterialApp 形式的“入口点”。
到目前为止,我使用的是 via 的翻译AppLocalizations.of(context),但是context现在由主应用程序提供。我的理解是,应用程序现在加载应用程序中定义的翻译文件(上下文来自何处)而不是包。有什么方法可以指定使用哪些翻译?如果没有,如何在不包含 MaterialApp 的 Flutter 包中实现本地化字符串?
我在 flutter-web 应用程序中切换语言时遇到问题。我将所需的依赖项添加到了package.yml.
dependencies:
flutter:
sdk: flutter
intl: ^0.16.1
flutter_localizations:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
intl_translation: ^0.17.9
Run Code Online (Sandbox Code Playgroud)
然后我初始化了intlMaterialApp 中的东西。
[...]
child: MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
S.delegate,
],
supportedLocales: S.delegate.supportedLocales,
[...]
Run Code Online (Sandbox Code Playgroud)
这是我希望更改区域设置的代码。
Text(S.of(context).language),
RaisedButton(
child: Text("deutsch"),
onPressed: () {
S.load(Locale('de'));
},
),
RaisedButton(
child: Text("englisch"),
onPressed: () {
S.load(Locale('en'));
},
Run Code Online (Sandbox Code Playgroud)
这是arb包含翻译的两个文件。
{
"language": "Deutsch"
}
Run Code Online (Sandbox Code Playgroud)
{
"language": "English"
}
Run Code Online (Sandbox Code Playgroud)
一切都编译完毕,我可以访问这些S.of(context)类。我确信我错过了什么。因为如果我在 Android 模拟器中启动,语言切换也不起作用。
在模拟器中切换系统区域设置会更改语言。
我将不胜感激任何帮助或提示。
到目前为止,我使用的是动态字符串,如本文的解决方案所示: Flutter 国际化 - 动态字符串
下面是一个例子:
AppLocalizations.of(context).userAge(18)
Run Code Online (Sandbox Code Playgroud)
在 AppLocalizations.dart 上:
userAge(age) => Intl.message(
"My age is $age",
name: "userAge",
args: [age]);
// Return "My age is 18"
Run Code Online (Sandbox Code Playgroud)
但后来我读了这篇关于颤振国际化的文章:https : //medium.com/flutter-community/flutter-internationalization-the-easy-way-using-provider-and-json-c47caa4212b2 其中展示了如何使用 json 文件进行本地化字符串的资源文件。它看起来更方便,所以我更喜欢使用这种方法,但不知道如何从具有动态值的 json 文件中获取字符串。
有什么解决办法吗?
运行Flutter Intl: Initialize生成lib/l10n/intl_en.arb. 然而,由于我的应用程序中有很多字符串,我希望有多个相同语言的 arb 文件。
为了更清楚,请考虑以下文件夹结构
-->lib
--> screens
--> screen_a.dart
--> screen_b.dart
Run Code Online (Sandbox Code Playgroud)
相应地,我的l10n文件夹结构如下:
--> l10n
--> screen_a
--> intl_en.dart
--> intl_fr_FR.dart
--> screen_b
--> intl_en.dart
--> intl_fr_FR.dart
Run Code Online (Sandbox Code Playgroud)
我如何使用 flutter intl 插件实现这一点?
提前致谢!
已转移到 Flutter 2,并正在将我们的翻译直接转移为 .arb 格式,正如现在建议的那样。然而,遇到了一个问题,即生成的本地化翻译文件无法理解何时应该使用类型double,而是使用int,这会在代码中使用时导致类型错误。似乎没有任何方法可以指定特定占位符替换是 adouble而不是int。
ARB 翻译文件
"hours": "{hours,plural, =1{Hour}other{Hours}}",
"@hours": {
"placeholders": {
"hours": {}
}
},
Run Code Online (Sandbox Code Playgroud)
尝试向占位符添加类型信息,但似乎对生成影响不大(使用flutter gen-l10n脚本)
"hours": "{hours,plural, =1{Hour}other{Hours}}",
"@hours": {
"placeholders": {
"hours": {
"type: "double" <========== THIS SEEMS TO HAVE NO IMPACT (and/or may not be valid)
}
}
},
Run Code Online (Sandbox Code Playgroud)
生成本地化翻译.dart
/// In en, this message translates to:
/// **'{hours,plural, =1{Hour}other{Hours}}'**
String hours(int hours); <========== THIS NEEDS TO BE A DOUBLE …Run Code Online (Sandbox Code Playgroud) 我做了一个 Flutter 插件,我需要添加国际化。我已经按照本教程进行操作,就像我通常对 Flutter 应用程序所做的那样:Internationalization in Flutter 1.22+
但是对于 Flutter 插件,没有 MaterialApp,所以我无法添加这个:
MaterialApp(
localizationsDelegates: Translations.localizationsDelegates,
supportedLocales: Translations.supportedLocales
)
Run Code Online (Sandbox Code Playgroud)
那么,有没有办法将国际化添加到我的 Flutter 插件中,以便我可以在我的插件中使用它?
Translations.of(context).title;
Run Code Online (Sandbox Code Playgroud) 我正在使用 Localizely 的 Flutter Intl 插件来本地化我的应用程序。我为我想要的语言生成了 arb 文件并开始介绍翻译。
例如:
{
"documentsSection": "All the documents",
"favouritesSection": "Favourites",
"newsSection": "News",
"settingsSection": "Settings"
}
Run Code Online (Sandbox Code Playgroud)
每次我想本地化我使用的文本时:
S.of(context).favouritesSection;
Run Code Online (Sandbox Code Playgroud)
它完美地工作。
但是,当我有这样的列表时:
List<Strings> sectionTitles = ["documentsSection","favouritesSection","newsSection","settingsSection"]
Run Code Online (Sandbox Code Playgroud)
我在这样一个 itemBuilder 循环中:
itemBuilder: (context, index) {
String sectionName = sectionTitles[index];
return Text(
S.of(context).sectionName,
),
},
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,因为“sectionName”不是 arb 文件中的键。但我认为代码表达了我想要实现的目标。可能有人可以帮助我。提前致谢。
我正在创建一个新的 Flutter UI 组件,其中包含选择和获取有关产品的更多信息。
我希望这个组件也支持 RTL,所以我需要获取当前的语言环境语言方向,这将使我知道选择形状的哪些角将被圆角。
的LTR形状代码是这样的
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(35),
topLeft: Radius.circular(35),
),
)
Run Code Online (Sandbox Code Playgroud)
的RTL形状码会像
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(35),
topRight: Radius.circular(35),
),
)
Run Code Online (Sandbox Code Playgroud)
我知道 intl 提供了获取特定文本方向的功能,而我想获取当前选择语言环境的默认方向,因此如果当前语言环境是阿拉伯语、波斯语或任何其他从右到左的语言,我将返回RLT组件。我不知道具体该怎么做。
android internationalization right-to-left flutter flutter-intl