sai_l10n_generator 1.0.0 sai_l10n_generator: ^1.0.0 copied to clipboard
A custom Dart code generator for localization using GetX.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'core/utils/l10n/app_translations.dart'; // Import the generated localization file
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return GetMaterialApp(
// Use the GetX translations and specify the initial locale
translations: AppTranslation
.instance(), // The translation maps from app_translations.dart
locale: const Locale(
'en', 'US'), // Default locale (can be set to any supported language)
fallbackLocale: const Locale(
'en', 'US'), // Fallback to English if the locale is not supported
// Add the localization delegates for Material, Widgets, and Cupertino
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('hello'.tr), // Using GetX to translate 'hello'
actions: [
IconButton(
icon: const Icon(Icons.language),
onPressed: () {
// Switch between English and Arabic
Locale currentLocale = Get.locale!;
if (currentLocale.languageCode == 'en') {
Get.updateLocale(const Locale('ar', 'AR')); // Switch to Arabic
} else {
Get.updateLocale(const Locale('en', 'US')); // Switch to English
}
},
),
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('welcomeMessage'.tr), // GetX translation for 'welcomeMessage'
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
// Add functionality for button press
},
child: Text('clickMe'.tr), // GetX translation for 'clickMe'
),
],
),
),
);
}
}