我在一个显示字符串的最小应用程序中添加了所需的几行配置:
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体变得任意复杂,行为保持不变(i18n与警告一起工作)。但是,如果我向AppBar添加一个简单的Scaffold (即使加宽不需要转换),整个应用程序就会崩溃。(例如,在上面的片段中去掉注释)
错误摘要:
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中。不知道怎么解开这个谜团。
编辑
我刚刚发现的另一件奇怪的事情是:在--release模式下运行相同的应用程序可以消除所有的错误和警告;一切都被正确翻译,包括AppBar。
仍然想知道为什么它会在--debug模式下崩溃。
发布于 2019-09-13 08:53:26
问题不在于样板代码或配置。
错误的含义是不支持“默认系统字符串”的语言"it_“。(特别是,由于AppBar ->的某些默认工具提示而崩溃,这就是如果显示AppBar就会崩溃的原因)。
正如所报告的在这个问答中,您可以通过实现自己的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这个快速修复将把所有默认的系统字符串设置为英语,但是您的“自定义”本地化应该可以工作。
发布于 2022-08-31 22:44:43
另外,使用can DefaultMaterialLocalizations而不是MaterialLocalizations和override并不是真的,但是您也需要检查这个以获得更多详细信息( https://api.flutter.dev/flutter/material/MaterialApp/localizationsDelegates.html )
https://stackoverflow.com/questions/57902361
复制相似问题