Localization with Getx in Flutter
Hi Coders,
Hope you are doing great,
In this article we are gonna learn about how to do localization in Flutter using Getx
This is the 4th part of Getx Series and if you still didn't check other parts of this series then Please look at below topics
You can watch video on this topic on my Youtube Channel
👉 Localization with Getx in Flutter 👈
You will find the code below, and here i am only posting the file which are related to this topic and if you want to get other file then Please look other article which i linked above
👉 Localization with Getx in Flutter 👈
You will find the code below, and here i am only posting the file which are related to this topic and if you want to get other file then Please look other article which i linked above
main.dart
import 'package:codemicros_getx_series/app/routes/app_pages.dart';
import 'package:codemicros_getx_series/lang/languages.dart';
import 'package:flutter/material.dart';
import 'package:get/route_manager.dart';
main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
locale: const Locale('en', 'US'),
translations: LocalString(),
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
initialRoute: AppPages.initial,
getPages: AppPages.routes,
);
}
}
languages.dart
import 'package:get/get.dart';
class LocalString extends Translations {
@override
Map<String, Map<String, String>> get keys => {
"en_us": {
"With Rx": "With Rx",
"Without Rx": "Without Rx",
},
"hi_IN": {
"With Rx": "आरएक्स के साथ",
"Without Rx": "बिना आरएक्स के",
}
};
}
homepage_view.dart
import 'package:codemicros_getx_series/app/routes/app_routes.dart';
import 'package:codemicros_getx_series/app/views/home/homepage_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class HomepageView extends StatelessWidget {
const HomepageView({super.key});
@override
Widget build(BuildContext context) {
// HomepageController homepageController = Get.put(HomepageController());
return GetBuilder(
builder: (HomepageController homepageController) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"With Rx".tr,
style: const TextStyle(fontSize: 45.0),
),
Text(
"Without Rx".tr,
style: const TextStyle(fontSize: 45.0),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
/// hindi
ElevatedButton(
onPressed: () {
homepageController.changeLanguage(
lang: 'hi', countryCode: 'IN');
},
child: const Text("Hindi")),
// english
ElevatedButton(
onPressed: () {
homepageController.changeLanguage(
lang: 'en', countryCode: 'US');
},
child: const Text("English"))
],
)
],
),
),
floatingActionButton: const Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
// with RX
// ElevatedButton(
// onPressed: () {
// homepageController.updateCounterValue();
// },
// child: const Text("Add")),
// //WITHOUT rX
// ElevatedButton(
// onPressed: () {
// homepageController.updateCounter2Value();
// },
// child: const Text("Add without obx")),
],
),
);
},
);
}
}
homepage_controller.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class HomepageController extends GetxController {
final RxInt counter = 0.obs;
int counter2 = 0;
updateCounterValue() {
counter.value = counter.value + 1;
update([]);
}
updateCounter2Value() {
counter2 = counter2 + 1;
update();
}
changeLanguage({
required String lang,
required String countryCode,
}) {
Get.updateLocale(Locale(lang, countryCode));
}
}
If you have any questions then feel free to contact me in any social media platform, it will be my pleasure to help you out