Flutter i18n插件无法自动更改语言

时间:2019-10-10 07:57:53

标签: flutter internationalization

安装Flutter i18n plugin后,我添加了3种语言.arb文件。当我添加localeResolutionCallback:时,可以得到此语言页面。但是,如果没有localeResolutionCallback:,它总是英文。更改设备语言时可以使应用程序自动更改语言吗?

lib / main.dart:

...
import 'package:hctodo/generated/i18n.dart';
import 'app.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() {
  ...
  return runApp(CupertinoApp(
      localizationsDelegates: [
        S.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalMaterialLocalizations.delegate
      ],
      supportedLocales: S.delegate.supportedLocales,
//      localeResolutionCallback: S.delegate.resolution(fallback: new Locale("ja", "JP")),
      home: MyApp()));
}

1 个答案:

答案 0 :(得分:0)

尝试一下, 首先声明支持的语言环境:

import 'dart:ui' as ui;   
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      supportedLocales: [
        const Locale('en'), // English
        const Locale('he'), // Hebrew
        const Locale('de'), // Hebrew
        // ... other locales the app supports
      ],
      home: MyHomePage(title: ''),
    );
  }
}

然后var _sysLng = ui.window.locale.languageCode; 在您的其他文件中