以编程方式更改Flutter i18n应用程序的语言在iOS中不起作用 [英] programmatically change language of flutter i18n apps doesn't work in iOS
问题描述
我使用flutter_i18n插件(Android Studio)生成i18n.dart(class S
),而S.of(context).locale_msg
将返回语言环境字符串.主要代码如下所示.
单击主页中的按钮时,应通过调用onLocaleChange(locale)
以编程方式更改语言.它在Android模拟器中效果很好,但在iOS模拟器中不会更改语言.想知道我的代码怎么了吗?
I used flutter_i18n plugin (Android Studio) to generate i18n.dart(class S
) and S.of(context).locale_msg
will return the locale string. The main code is shown below.
Language should be changed programmatically by invoking onLocaleChange(locale)
when click the button in HomePage. It works well in Android simulator, but won't change language in iOS simulator. Wonder what's wrong with my code?
class _PaperMoonAppState extends State<PaperMoonApp> {
SpecifiedLocalizationDelegate _localeOverrideDelegate;
void onLocaleChange(Locale locale) {
setState(() {
if (appVars.appConfig.changeLanguage(locale)) {
_localeOverrideDelegate = new SpecifiedLocalizationDelegate(locale);
appVars.saveConfig(); //print save config file...
}
});
}
@override
void initState() {
SpecifiedLocalizationDelegate.onLocaleChange = this.onLocaleChange;
appVars.loadConfig().then((AppConfig _config) {
appVars.appConfig = _config;
setState(() {
_localeOverrideDelegate =
new SpecifiedLocalizationDelegate(appVars.appConfig.getLocale());
});
});
_localeOverrideDelegate =
new SpecifiedLocalizationDelegate(Locale('zh', ''));
super.initState();
}
@override
Widget build(BuildContext context) {
print(_localeOverrideDelegate.overriddenLocale);
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "Paper Moon",
color: Colors.blueAccent,
localizationsDelegates: [
_localeOverrideDelegate,
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
supportedLocales: const <Locale>[
Locale("ja", ""),
Locale("en", ""),
Locale("zh", ""),
],
localeResolutionCallback:
S.delegate.resolution(fallback: _localeOverrideDelegate.overriddenLocale),
home: HomePage(),
// routes: _buildRoutes(),
);
}
}
自定义LocalizationDelegate:
Custom LocalizationDelegate:
class SpecifiedLocalizationDelegate
extends LocalizationsDelegate<WidgetsLocalizations> {
//class static vars:
//onLocaleChange should be bind to MaterialApp function containing setState().
static LocaleChangeCallback onLocaleChange;
// for instance
final Locale overriddenLocale;
const SpecifiedLocalizationDelegate(this.overriddenLocale);
@override
bool isSupported(Locale locale) => overriddenLocale != null;
@override
Future<WidgetsLocalizations> load(Locale locale) =>
S.delegate.load(overriddenLocale);
@override
bool shouldReload(SpecifiedLocalizationDelegate old) => true;
}
推荐答案
根据您的代码,似乎唯一缺少的是:
Based on your code, the only thing that seems to be missing is this:
打开ios/Runner/Info.plist
并添加:
<key>CFBundleLocalizations</key>
<array>
<string>ja</string>
<string>en</string>
<string>zh</string>
</array>
据我所知,到目前为止(2019年3月),flutter尚未自动将支持的语言列表添加到此文件中.
As far I as know, by now (march/2019), flutter doesn't yet add automatically the list of supported languages to this file.
这篇关于以编程方式更改Flutter i18n应用程序的语言在iOS中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!