gpt4 book ai didi

flutter - 在Flutter应用程序中设置默认语言

转载 作者:行者123 更新时间:2023-12-03 16:03:15 32 4
gpt4 key购买 nike

我正在尝试在我的应用程序中支持多种语言。我想在我的应用程序中支持两种语言:英语(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: {


},

),
);

有人能帮我吗?提前致谢!

最佳答案

您需要使用回调来设置默认语言。在您的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: localConfig.locale,
...
);

关于flutter - 在Flutter应用程序中设置默认语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59610200/

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