Internationalization with GetX State Management

 import "package:flutter/material.dart";

import "package:get/get.dart";
import "package:getx_state_management/Internationalization/MyController.datr.dart";
import "package:getx_state_management/Internationalization/translations.dart";

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: "Internationalization",
debugShowCheckedModeBanner: false,
translations: Messages(),
locale: const Locale("en", "US"),

/// default locale /// to get device locale Get.deviceLocale
fallbackLocale: const Locale("en", "US"),

/// fallbackLocale if wrong locale found
theme: ThemeData(
primarySwatch: Colors.grey,
),
home: Internationalization(),
);
}
}

class Internationalization extends StatelessWidget {
MyController2 myController2 = Get.put(MyController2());

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Internationalization",
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"hello, how are you ?".tr,
style:
const TextStyle(fontWeight: FontWeight.bold, fontSize: 30.0),
),
const SizedBox(height: 40.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
myController2.changeLanguage("hi", "IN");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Hindi",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
ElevatedButton(
onPressed: () {
myController2.changeLanguage("fr", "FR");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"French",
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.white),
),
),
ElevatedButton(
onPressed: () {
myController2.changeLanguage("pn", "PN");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Punjabi",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
ElevatedButton(
onPressed: () {
myController2.changeLanguage("ur", "UR");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Urdu",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
ElevatedButton(
onPressed: () {
myController2.changeLanguage("en", "US");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"English",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
ElevatedButton(
onPressed: () {
myController2.changeLanguage("ar", "AR");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Arabic",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
],
),
const SizedBox(height: 30.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
myController2.changeLanguage("tm", "TM");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Tamil",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
ElevatedButton(
onPressed: () {
myController2.changeLanguage("gj", "GJ");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Gujarati",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
ElevatedButton(
onPressed: () {
myController2.changeLanguage("mr", "MR");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Marathi",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
ElevatedButton(
onPressed: () {
myController2.changeLanguage("si", "SI");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Sindhi",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
ElevatedButton(
onPressed: () {
myController2.changeLanguage("tg", "TG");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Telugu",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
ElevatedButton(
onPressed: () {
myController2.changeLanguage("ml", "ML");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
child: const Text(
"Malayalam",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
],
),
],
),
),
);
}
}

import "package:get/get.dart";

class Messages extends Translations {
@override
Map<String, Map<String, String>> get keys => {
"en_US": {
"hello, how are you ?": "Hello, How are you ?",
},
"hi_IN": {
"hello, how are you ?": "नमस्ते, आप कैसे हैं ?",
},
"fr_FR": {
"hello, how are you ?": "bonjour comment allez-vous ?",
},
"ar_AR": {
"hello, how are you ?": "مرحبا، كيف حالك ؟",
},
"pn_PN": {
"hello, how are you ?": "ਹੈਲੋ ਤੁਸੀ ਕਿਵੇਂ ਹੋ ?",
},
"ur_UR": {
"hello, how are you ?": "ہیلو آپ کیسے ہیں ؟",
},
"tm_TM": {
"hello, how are you ?": "வணக்கம் எப்படி இருக்கிறாய் ?",
},
"gj_GJ": {
"hello, how are you ?": "હેલો, તમે કેમ છો?",
},
"mr_MR": {
"hello, how are you ?": "नमस्कार, कसे आहात?",
},
"si_SI": {
"hello, how are you ?": "سلام توهان ڪيئن آهيو ؟",
},
"tg_TG": {
"hello, how are you ?": "హలో, ఎలా ఉన్నారు?",
},
"ml_ML": {
"hello, how are you ?": "ഹലോ, നിങ്ങൾക്ക് സുഖമാണോ ?",
},
};
}

import "package:flutter/material.dart";
import "package:get/get.dart";

class MyController2 extends GetxController {
/// Here below, the Language code will store in "parameter1" and
/// the Country code will store in "parameter2"
void changeLanguage(var parameter1, var parameter2) {
var locale = Locale(parameter1, parameter2);
Get.updateLocale(locale);
}
}

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter