Cander
Cander

Reputation: 23

flutter ! About getting phone language settings?

I want to get the local mobile phone language, simplified Chinese or English, but the function localeResolutionCallback always returns en under ios but the mobile phone is simplified Chinese, how to solve this problem?

flutter information:

Flutter (Channel stable, v1.12.13+hotfix.9, on Mac OS X 10.15.3 19D76,
    locale zh-Hans-CN)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 11.4.1)
[✓] Android Studio (version 3.6)
[!] IntelliJ IDEA Community Edition (version 2020.1)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[!] VS Code (version 1.44.2)
    ✗ Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (1 available)

The newly created app also has this problem:

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'),
      supportedLocales: [//传入支持的语种数组
        const Locale('en', 'EN'), // English
        const Locale('zh', 'CN'), //
      ],
      localeResolutionCallback: (locale, locales) {
        print('cacasasc, ${locale.languageCode} ${locales}');
        if (locale.languageCode == 'zh') {
          return Locale('zh', 'CN');
        } else {
          return Locale('en', 'EN');
        }
      },
    );
  }
}

log:

flutter: cacasasc, en

Upvotes: 2

Views: 1023

Answers (1)

Stewie Griffin
Stewie Griffin

Reputation: 5608

This is supposed to work fine on Android.

For IOS, you need to do something very simple. Open Info.plist which is located in project_name/ios/Runner/Info.plist and add the languages you want to support like this:

  <key>CFBundleLocalizations</key>
  <array>
    <string>English</string>
    <string>es</string>
    <string>zh</string>
  </array>

So, each language will be between string tags and the whole code will be above <key>CFBundleIdentifier</key>.

Upvotes: 2

Related Questions