我想获取本地手机语言, 简体中文还是英文, 但是localeResolutionCallback这个函数在ios下一直返回en但是手机时简体中文的, 这个问题怎么解决?
flutter信息:
Flutter 1.12.13+hotfix.9 • channel stable •
https://github.com/flutter/flutter.git
Framework • revision f139b11009 (4 weeks ago) • 2020-03-30 13:57:30 -0700
Engine • revision af51afceb8
Tools • Dart 2.7.2
代码如下:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
localeResolutionCallback: (locale, locales) {
print('cacasasc, ${locale.languageCode}');
if (locale.languageCode == 'zh') {
return Locale('zh', 'CN');
} else {
return Locale('en', 'EN');
}
}
);
}
}
日志:
flutter: cacasasc, en
回答
在xcode里添加简体中文的支持
工程-PROJECT-info->Localizations,点“+”,选择(Chinese(Simplified))添加简体中文,英文Xcode自带有(English),所以不需要再次添加。(点击添加弹出语言菜单,即可选择你所需要的语言)
var lan =uni.getStorageSync('language');
var val='';
if(lan){
if(lan=='English'||lan=='英文'){
val='en-us'
}else{
val='zh-cn'
}
}else if(!lan){
var i=uni.getSystemInfoSync().language.toLowerCase();
if(i=='zh'||i=='zh-cn'||i=='zh-tw'||i=='zh-hk'){
val='zh-cn'
}else{
val='en-us'
}
}