sai_l10n_generator 1.0.1 sai_l10n_generator: ^1.0.1 copied to clipboard
sai_l10n_generator is a code generator for Flutter projects that automatically detects .tr strings and generates localization files in GetX format. Ideal for projects needing multi-language support.
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'
),
],
),
),
);
}
}