首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颤振应用程序使用AppBar Jetbrains插件的样板构建多语言i18n

颤振应用程序使用AppBar Jetbrains插件的样板构建多语言i18n
EN

Stack Overflow用户
提问于 2019-09-12 08:07:44
回答 2查看 2.7K关注 0票数 3

我为Android在正式文件之后使用了颤振正式文件插件。

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

代码语言:javascript
复制
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),
          ],
        ),
      ),
    );
  }
}

这是有效的(字符串实际上被翻译成我的电话系统语言)。但是,每次调用构建时都会发出警告:

代码语言:javascript
复制
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 (即使加宽不需要转换),整个应用程序就会崩溃。(例如,在上面的片段中去掉注释)

错误摘要:

代码语言:javascript
复制
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模式下崩溃。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-13 08:53:26

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

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

正如所报告的在这个问答中,您可以通过实现自己的LocalizationsDelegate来解决这个问题。

全固定

代码语言:javascript
复制
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)
}

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

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

快速修复

一种仓促的方式,以检查这是否会对您的工作之前,实现57覆盖。

准确地复制以下代码:

代码语言:javascript
复制
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区域设置。

最后,将其添加到委托列表中。

代码语言:javascript
复制
localizationsDelegates: [S.delegate, FallbackLocalizationDelegate()],  // <- append here

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

票数 10
EN

Stack Overflow用户

发布于 2022-08-31 22:44:43

另外,使用can DefaultMaterialLocalizations而不是MaterialLocalizations和override并不是真的,但是您也需要检查这个以获得更多详细信息( https://api.flutter.dev/flutter/material/MaterialApp/localizationsDelegates.html )

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57902361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档