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.
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.
First, I added the GetX package to my pubspec.yaml file:
dependencies:
get: ^4.6.5I 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',
}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(),
);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 KoreanTo display the translated text in the UI, I used text.tr inside the widget where I wanted the translation. For example:
Text('santa'.tr),

