Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Language Translation Implementation with GetX in Flutter

In this Flutter project, I implemented language translation using the GetX package to provide seamless support for multiple languages. The primary focus of this implementation was to handle text localization dynamically within the app.

Why Use GetX for Translation?

GetX makes it easy to manage localization in your Flutter app without much boilerplate code. It allows you to:

  • Automatically switch between languages.
  • Easily define translations for different languages.
  • Use simple syntax to update the language and display translated content.

Steps Taken to Implement Translation

1. Adding GetX Dependency

First, I added the GetX package to my pubspec.yaml file:

dependencies:
  get: ^4.6.5

2. Setting Up Translations

I created a translation file (ENLanguage) that holds all the text strings in English as the default language. Here’s an example of how I structured the translations:

class ENLanguage {
  static Map<String, String> en = {
    '_santa': 'Santa',
    '_doesn_t_know_you_like_i_do': 'Doesn’t know you like I do',
    '_i_ve_been_there_through_the_good_and_bad': 'I’ve been there through the good and bad',
    '_know_how_to_make_you_laugh': 'Know how to make you laugh',
    '_kiss_all_your_tears_away_babe': 'Kiss all your tears away, babe',
}

3. Initializing GetX Localization

In the main.dart file, I initialized GetX with the translations property in the GetMaterialApp widget. This step ensures that GetX can manage and switch between languages. Here's the setup:

GetMaterialApp(
      title: 'Translate DEMO',
      translations: Languages(),
      locale: Get.deviceLocale,
      fallbackLocale: const Locale('en', 'US'),
      home: const SplashScreen(),
    );

4. Switching Languages Dynamically

To switch languages, I used a button that toggles the language between English and Korean. The button updates the language using Get.updateLocale() method.

Get.updateLocale(Locale('ko', 'KR'));  // Switch to Korean

5. Using Translations in Widgets

To display the translated text in the UI, I used text.tr inside the widget where I wanted the translation. For example:

Text('santa'.tr),

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages