我正在尝试在我的应用程序中支持多种语言。我想在我的应用程序中支持两种语言:英语 (en) 和 Bahasa (id)。但是,我希望我的应用程序使用 Bahasa 作为默认语言。我尝试使用插件easy_localization来做到这一点。
这是我的 main.app 文件中的一些代码
return EasyLocalizationProvider(
data: data,
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: APP_NAME,
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
//app-specific localization
EasylocaLizationDelegate(
locale: data.locale,
path: 'assets/strings'
),
],
navigatorKey: locator<NavigationService>().navigatorKey,
supportedLocales: [ Locale('id', 'ID'), Locale('en', 'US')],
locale: data.savedLocale,
theme: ThemeData(
primaryColor: KaskuColor.primary,
accentColor: Color(0xFFCB0E00),
fontFamily: PRIMARY_FONT_FAMILY,
textTheme: TextTheme(
headline: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
title: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
body1: TextStyle(fontSize: 14.0),
),
primarySwatch: Colors.red,
cursorColor: KaskuColor.primary,
snackBarTheme: SnackBarThemeData(
backgroundColor: KaskuColor.snackBarColor
)
),
home: Splashscreen(),
routes: {
},
),
);
Run Code Online (Sandbox Code Playgroud)
有人能帮我吗?提前致谢!
您需要使用回调来设置默认语言。在您的 MaterialApp 小部件中添加localeListResolutionCallback如下:-
MaterialApp(
...
localeListResolutionCallback: (locales, supportedLocales) {
print('device locales=$locales supported locales=$supportedLocales');
for (Locale locale in locales) {
// if device language is supported by the app,
// just return it to set it as current app language
if (supportedLocales.contains(locale)) {
return locale;
}
}
// if device language is not supported by the app,
// the app will set it to english but return this to set to Bahasa instead
return Locale('id', 'ID');
},
supportedLocales: [Locale('id', 'ID'), Locale('en', 'US')],
locale: Locale('en', 'US'),
...
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6467 次 |
| 最近记录: |