flutter 如何在GetX中翻译提示文本?

31moq8wy  于 2022-12-19  发布在  Flutter
关注(0)|答案(2)|浏览(143)

我想翻译我的提示文本,但我不知道如何翻译。有什么方法可以翻译它吗?

InternationalPhoneNumberInput(
    searchBoxDecoration: InputDecoration(
        hintText: 'country_code',)) // How can I translate this in GetX?
4c8rllxm

4c8rllxm1#

首先,您需要准备GetMaterialApp

GetMaterialApp(
    translationsKeys:AppTranslation.translationsKeys,
    locale: Get.deviceLocale,
    fallbackLocale: Locale("en" , "US").
    title: "Application"
    initialRoute: Routes.HOME,
    defaultTransition: Transition.fade,
    onGenerateRoute:RouteGenerator.generateRoute,)

然后创建一个AppTranslation.dart

//AppTranslation.dart
abstract class AppTranslation {
  static Map<String, Map<String, String>> translationsKeys = {
    "en_US": enUS,
    "fr": fr
  };
}

final Map<String, String> enUS = {
  'greeting': 'Hello, How are you?',
  'day': "Awesome day..."
};

final Map<String, String> fr = {
  'greeting': "Salut comment allez-vous?",
  'day': "Super journée..."
};

要更改区域设置:

Locale locale = new Locale(languageCode); //languageCode=en_US or fr
Get.updateLocale(locale);

你可以这样称呼翻译文本:

Text(
    'greeting'.tr,
)
dhxwm5r4

dhxwm5r42#

hintText: ("greeting".tr).toString()

相关问题