如何更改 Flutter intl 中的主要本地化文件
How to change the main localization file in Flutter intl
最初,主要本地化文件是intl_en.arb
如何以及在何处将主本地化文件更改为另一个本地化文件,例如 intl_ru.arb
您可以在 locale
上添加 languageCode
MaterialApp(
.
.
.
locale : Locale.fromSubtags(languageCode: 'ru')
);
完整示例:
String lang = '';
_getLang() async {
try {
String data = await getLang();
setState(() {
lang = data;
});
} catch (e) {
print(e);
}
}
@override
void initState() {
super.initState();
_getLang();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
onGenerateRoute: RouteGenerator.generateRoute,
localizationsDelegates: [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
locale: lang == 'English'
? Locale.fromSubtags(languageCode: 'en')
: Locale.fromSubtags(languageCode: 'ru'),
);
}
Future<String> getLang() async {
Future<SharedPreferences> _langPrefs = SharedPreferences.getInstance();
SharedPreferences prefs = await _langPrefs;
var lang = prefs.getString("lang");
if (lang == null) {
return null;
}
return lang;
}
最初,主要本地化文件是intl_en.arb 如何以及在何处将主本地化文件更改为另一个本地化文件,例如 intl_ru.arb
您可以在 locale
languageCode
MaterialApp(
.
.
.
locale : Locale.fromSubtags(languageCode: 'ru')
);
完整示例:
String lang = '';
_getLang() async {
try {
String data = await getLang();
setState(() {
lang = data;
});
} catch (e) {
print(e);
}
}
@override
void initState() {
super.initState();
_getLang();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
onGenerateRoute: RouteGenerator.generateRoute,
localizationsDelegates: [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
locale: lang == 'English'
? Locale.fromSubtags(languageCode: 'en')
: Locale.fromSubtags(languageCode: 'ru'),
);
}
Future<String> getLang() async {
Future<SharedPreferences> _langPrefs = SharedPreferences.getInstance();
SharedPreferences prefs = await _langPrefs;
var lang = prefs.getString("lang");
if (lang == null) {
return null;
}
return lang;
}