Pad*_*ado 3 android jetbrains-ide internationalization android-studio flutter
我按照官方文档使用 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),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是有效的(该字符串实际上被翻译成我的手机系统语言)。但每次调用构建时都会引发警告:
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.
Run Code Online (Sandbox Code Playgroud)
尽管所有 arb 文件都已正确配置S.delegate.supportedLocales。[en_, it_]
我可以使Scaffold主体任意复杂,而行为保持不变(i18n 带有警告)。但是如果我添加一个简单的AppBar(即使widged不需要翻译)到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
Run Code Online (Sandbox Code Playgroud)
该框架抱怨我AppBar没有本地化小部件祖先。但它实际上有,因为它位于MaterialApp. 不知道如何解开这个谜团。
编辑
我刚刚发现的另一个奇怪的事情:在模式下运行相同的应用程序--release可以消除所有错误和警告;一切都被正确翻译,包括AppBar.
仍然想知道为什么它在模式下崩溃--debug。
Pad*_*ado 15
问题不在于样板代码或配置。
该错误的含义是“默认系统字符串”不支持语言“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)
}
Run Code Online (Sandbox Code Playgroud)
然后将您的委托添加到委托列表中:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [S.delegate, MaterialLocalizationItDelegate()], // <- append here
supportedLocales: S.delegate.supportedLocales,
home: MyHomePage(),
);
}
}
Run Code Online (Sandbox Code Playgroud)
在实施 57 个覆盖之前,检查这是否适合您的仓促方法。
准确复制这段代码:
class FallbackLocalizationDelegate extends LocalizationsDelegate<MaterialLocalizations> {
@override
bool isSupported(Locale locale) => true;
@override
Future<MaterialLocalizations> load(Locale locale) async => DefaultMaterialLocalizations();
@override
bool shouldReload(_) => false;
}
Run Code Online (Sandbox Code Playgroud)
上面的类声称支持所有语言环境 ( isSupported(Locale locale) => true) 并仅返回默认的 en_US 语言环境。
最后将其添加到您的代表列表中。
localizationsDelegates: [S.delegate, FallbackLocalizationDelegate()], // <- append here
Run Code Online (Sandbox Code Playgroud)
此快速修复会将所有默认系统字符串设置为英语,但您的“自定义”本地化应该可以工作。
| 归档时间: |
|
| 查看次数: |
3845 次 |
| 最近记录: |