[英]Requested the Locale of a context that does not include a Localizations ancestor
I am trying to get the Locale
of the user's phone at app start.我正在尝试在应用程序启动时获取用户手机的
Locale
。
I have this Widget
tree in the runApp()
method :我在
runApp()
方法中有这个Widget
树:
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: Locale(Localizations.localeOf(context).languageCode), // This crashes
localizationsDelegates: [
const LocalizationDelegate(), // My custom delegate to get translations
CountryLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
Locale("en"),
Locale("fr"),
],
debugShowCheckedModeBanner: false,
home: Scaffold(
resizeToAvoidBottomInset: false,
body: HomePage(),
)
);
}
the line locale: Locale(Localizations.localeOf(context).languageCode)
cause a crash with :行
locale: Locale(Localizations.localeOf(context).languageCode)
导致崩溃:
Requested the Locale of a context that does not include a Localizations ancestor.
I simply want to bind this locale to my Delegate
without going further in app.我只是想将此语言环境绑定到我的
Delegate
,而无需在 app.xml 中进一步操作。
So far, i put locale: Locale("en")
to read my en.json
file from my LocalizationDelegate
.到目前为止,我把
locale: Locale("en")
从我的LocalizationDelegate
读取我的en.json
文件。
You have to use a MaterialApp or CupertinoApp to use Locales.您必须使用 MaterialApp 或 CupertinoApp 才能使用语言环境。
For ex,例如,
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: const <Locale>[
Locale('de'),
Locale('en'),
Locale('es')
],
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const YourHomeWidget(), // All Localized Widgets must be placed inside home widget.
);
}
}
NOTE: Flutter Localization automatically detect system language and apply this settings to your application.注意:Flutter Localization 会自动检测系统语言并将此设置应用于您的应用程序。
I also answer because @Jaydipsinh is right but the error i was making is that i thought i needed this line :我也回答是因为@Jaydipsinh 是对的,但我犯的错误是我认为我需要这一行:
locale: Locale(Localizations.localeOf(context).languageCode)
Whereas it's useless.而它是无用的。
You simply have to put supportedLocales
values and if they match the phone's locale, flutter automatically use the langage for the given delegates
, for example :您只需输入
supportedLocales
值,如果它们与手机的语言环境匹配,flutter 会自动使用给定delegates
的语言,例如:
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
Also, you can retrieve the locale by using : String locale = Platform.localeName.split('_')[0];
此外,您可以使用以下方法检索语言环境:
String locale = Platform.localeName.split('_')[0];
with no crash at app start.应用程序启动时没有崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.