没有上下文的颤振本地化 [英] Flutter localization without context

查看:106
本文介绍了没有上下文的颤振本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的应用本地化.我为支持的语言创建了所需的string.arb文件.

I am trying to localize my app in flutter. I created the needed string.arb files for the supported languages.

为什么AppLocalizations.of(context)需要上下文?

我只是想访问文件/语言环境文件/类中的命名字符串. 在应用程序中的某个时候,我建立了一个列表,稍后通过使用单独的类覆盖某些字段来填充它.

I simply want to access the named strings in the files/locales files/classes. At some point in the app I build a List and fill it later via overriding some fields with a separate class.

但是,此类没有上下文,但是我想在其中使用本地化的字符串. 我可以写一个方法让我输入的任何String都本地化吗?

However, this class has no context but I want to use localized strings in it. Can I write a method which gets me the Localization of whatever String I put in?

推荐答案

如果您不想使用软件包,那么这里有一个对我有用的解决方案.现在,我通常见过的AppLocalizations常见实现方式有这两行:

If you would prefer to not use a package then here is a solution that worked for me. Now the most common implementation of AppLocalizations I've seen usually has these two lines:

//.........
static const LocalizationsDelegate<AppLocalizations> delegate =
      _AppLocalizationsDelegate();

static AppLocalizations of(BuildContext context) {
  return Localizations.of<AppLocalizations>(context, AppLocalizations);
}
//.........

委托的实现如下所示:

class _AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
  const _AppLocalizationsDelegate();

  @override
  Future<AppLocalizations> load(Locale locale) async {
    AppLocalizations localizations = new AppLocalizations(locale);
    await localizations.load();

    return localizations;
  }

  //... the rest omitted for brevity
}

请注意,委托上的load方法将返回Future<AppLocalizations>.通常,从main调用一次load方法,然后再调用一次,因此您可以通过将AppLocalizations的静态实例添加到委托中来利用该方法.所以现在您的代表看起来像这样:

Notice the load method on the delegate returns a Future<AppLocalizations>. The load method is usually called once from main and never again so you can take advantage of that by adding a static instance of AppLocalizations to the delegate. So now your delegate would look like this:

class _AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
  const _AppLocalizationsDelegate();

  static AppLocalizations instance;

  @override
  Future<AppLocalizations> load(Locale locale) async {
    AppLocalizations localizations = new AppLocalizations(locale);
    await localizations.load();

    instance = localizations; // set the static instance here

    return localizations;
  }

  //... the rest omitted for brevity
}

然后在您的AppLocalizations类上,您将拥有:

Then on your AppLocalizations class you would now have:

//.........
static const LocalizationsDelegate<AppLocalizations> delegate =
      _AppLocalizationsDelegate();

static AppLocalizations of(BuildContext context) {
  return Localizations.of<AppLocalizations>(context, AppLocalizations);
}

static AppLocalizations get instance => _AppLocalizationsDelegate.instance; // add this
//.........

现在,在您的翻译助手方法中,您可以:

Now in your translate helper method you could have:

String tr(String key) {
    return AppLocalizations.instance.translate(key);
}

不需要上下文.

这篇关于没有上下文的颤振本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆