如何在flutter中动态翻译语言



我目前正在使用getx包来翻译该语言。我正在像这样手动翻译所有的文本,

class MyTranslations extends Translations {
@override
Map<String, Map<String, String>> get keys => {
'en': {
'title': 'Hello World %s',
},
'en_US': {
'title': 'Hello World from US',
},
'pt': {
'title': 'Olá de Portugal',
},
'pt_BR': {
'title': 'Olá do Brasil',
},
};
}

我的问题是,如何动态地翻译语言?我们可以使用getx包,还是任何包?

您可以使用GetX软件包进行如下更新:

RaisedButton(
child: Text('English'),
onPressed: () {
Get.updateLocale(Locale('en'));
},
),

RaisedButton(
child: Text('English'),
onPressed: () {
Get.updateLocale(Locale('pt_BR'));
},
),

您还必须将以下代码添加到GetMaterialApp:

GetMaterialApp(
title: 'Nome App',
debugShowCheckedModeBanner: false,
locale: Get.deviceLocale,
fallbackLocale: Locale('en'), // default language if it does not exist or returns invalid.
)

要更改代码,只需调用Get.updateLocale((函数;


ElevatedButton(
child: Text('English'),
onPressed: () {
Get.updateLocale(Locale('en'));
},
),

最新更新