Search code examples
flutterdartflutter-getx

How can I translate hint text in GetX?


I want to translate my hint text, but I don't know how. Is there a way I can translate this?

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

Solution

  • First you need to prepare your GetMaterialApp:

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

    Then create a 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..."
    };
    

    To change the locale:

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

    And you can call translation text like that:

    Text(
        'greeting'.tr,
    )