gpt4 book ai didi

Flutter - 基于设备的动态语言

转载 作者:IT王子 更新时间:2023-10-29 06:38:06 24 4
gpt4 key购买 nike

我正在尝试使用 Localizations 获取移动国家代码和语言代码

Widget build(BuildContext context) {
Locale myLocale = Localizations.localeOf(context);
print(myLocale.countryCode);
print(myLocale.languageCode);
return MaterialApp(
title: 'Title',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LoginPage(),
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
Locale('es' 'ES'),
],
);
}

但返回“未找到本地化祖先”。有人知道执行此操作的正确方法是什么吗?

最佳答案

使用 LocaleResolutionCallback 获取设备语言环境:

 Widget build(BuildContext context) {
Locale myLocale ;

return MaterialApp(
title: 'Title',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LoginPage(),
localeResolutionCallback: (deviceLocale, supportedLocales) {
myLocale = deviceLocale ; // here you make your app language similar to device language , but you should check whether the localization is supported by your app
print(myLocale.countryCode);
print(myLocale.languageCode);
}
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
Locale('es' 'ES'),
],
);
}

关于Flutter - 基于设备的动态语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55042814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com