gpt4 book ai didi

android - Flutter 应用程序崩溃使用 i18n Jetbrains 插件的样板构建具有多语言的 AppBar

转载 作者:行者123 更新时间:2023-12-05 00:13:47 25 4
gpt4 key购买 nike

official documentation 之后,我正在为 Android Studio 使用 Flutter i18n 插件。 .

我在一个显示字符串的最小应用程序中添加了一些配置行:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [S.delegate],
supportedLocales: S.delegate.supportedLocales,
home: MyHomePage(),
);
}
}

class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
// appBar: AppBar(
// title: Text("Hard-coded English string"),
// ),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(S.of(context).string_that_should_be_internationalized),
],
),
),
);
}
}

这是有效的(字符串实际上被翻译成我的电话系统语言)。但是每次调用构建都会引发警告:
Warning: This application's locale, it_, is not supported by all of its
localization delegates.
> A MaterialLocalizations delegate that supports the it_ locale was not found.

尽管 S.delegate.supportedLocales正在 [en_, it_]并且所有 arb 文件都已正确配置。

我可以制作 Scaffold body 任意复杂,行为保持不变(i18n 与警告一起工作)。但是如果我添加一个简单的 AppBar (即使 widged 不需要翻译)到 Scaffold整个应用程序崩溃。 (例如,在上面的 fragment 中取消注释)

错误摘要:
I/flutter (11411): No MaterialLocalizations found.
I/flutter (11411): AppBar widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.
I/flutter (11411): Localizations are used to generate many different messages, labels,and abbreviations which are used
I/flutter (11411): by the material library.
I/flutter (11411): To introduce a MaterialLocalizations, either use a MaterialApp at the root of your application to
I/flutter (11411): include them automatically, or add a Localization widget with a MaterialLocalizations delegate.
I/flutter (11411): The specific widget that could not find a MaterialLocalizations ancestor was:
I/flutter (11411): AppBar

框架提示我的 AppBar没有本地化小部件祖先。但它实际上有,因为它在 MaterialApp 内.不知道如何解开这个谜。

Full log

编辑
我刚刚发现的另一个奇怪的事情:在 --release 中运行相同的应用程序模式摆脱了所有的错误和警告;一切都正确翻译,包括 AppBar .
仍然想知道为什么它会在 --debug 中崩溃模式。

最佳答案

问题不在于样板代码或配置。

该错误的含义是“默认系统字符串”不支持语言“it_”。 (特别是由于 AppBar 的某些默认工具提示而导致崩溃 -> 这就是如果显示 AppBar 时它会崩溃的原因)。

据报道in this Q&A您可以通过实现自己的 LocalizationsDelegate 来解决问题.

完全修复

class MaterialLocalizationItDelegate extends LocalizationsDelegate<MaterialLocalizations> {
/// Here list supported country and language codes
@override
bool isSupported(Locale locale) => locale.languageCode == "it";
/// Here create an instance of your [MaterialLocalizations] subclass
@override
Future<MaterialLocalizations> load(Locale locale) async => MaterialLocalizationIt();
@override
bool shouldReload(_) => false;
}

class MaterialLocalizationIt extends MaterialLocalizations {
// alt-enter on intellij and implement many overrides (somthing like 57)
}

然后将您的代表附加到代表列表中:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [S.delegate, MaterialLocalizationItDelegate()], // <- append here
supportedLocales: S.delegate.supportedLocales,
home: MyHomePage(),
);
}
}

快速解决

在实现 57 次覆盖之前,一种仓促的方法来检查这是否适合您。
完全复制此代码:

class FallbackLocalizationDelegate extends LocalizationsDelegate<MaterialLocalizations> {
@override
bool isSupported(Locale locale) => true;
@override
Future<MaterialLocalizations> load(Locale locale) async => DefaultMaterialLocalizations();
@override
bool shouldReload(_) => false;
}

上面的类声称支持所有语言环境( isSupported(Locale locale) => true )并简单地返回默认的 en_US 语言环境。
最后将此添加到您的代表列表中。

localizationsDelegates: [S.delegate, FallbackLocalizationDelegate()],  // <- append here

此快速修复会将所有默认系统字符串设置为英语,但您的“自定义”本地化应该可以工作。

关于android - Flutter 应用程序崩溃使用 i18n Jetbrains 插件的样板构建具有多语言的 AppBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57902361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com