flutter_easy_localization 1.0.2
flutter_easy_localization: ^1.0.2 copied to clipboard
A powerful and flexible localization package for Flutter with runtime language switching, translation caching, plural/gender rules, and missing translation detection.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_easy_localization/flutter_easy_localization.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize EasyLocalization
await EasyLocalization.init(
supportedLanguages: ['en', 'hi', 'es', 'fr'],
defaultLanguage: 'en',
translationsPath: 'assets/translations',
fallbackLanguage: 'en',
enableDebugMode: true,
);
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
// Listen to language controller changes
EasyLocalization.instance.controller.addListener(_onLanguageChanged);
}
@override
void dispose() {
EasyLocalization.instance.controller.removeListener(_onLanguageChanged);
super.dispose();
}
void _onLanguageChanged() {
// Rebuild entire app when language changes
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
// Add unique key that changes when language changes
key: ValueKey(EasyLocalization.instance.currentLanguage),
title: 'app_title'.tr(),
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int itemCount = 0;
String userName = 'Developer';
final TextEditingController _nameController = TextEditingController();
@override
void initState() {
super.initState();
_nameController.text = userName;
}
@override
void dispose() {
_nameController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('welcome'.tr()),
elevation: 2,
actions: const [
// Language selector dropdown
Padding(
padding: EdgeInsets.symmetric(horizontal: 8.0),
child: LanguageSelector(
languageNames: {
'en': 'English 🇺🇸',
'hi': 'हिंदी 🇮🇳',
'es': 'Español 🇪🇸',
'fr': 'Français 🇫🇷',
},
),
),
],
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Welcome Card
_buildWelcomeCard(),
const SizedBox(height: 20),
// Name Input Card
_buildNameInputCard(),
const SizedBox(height: 20),
// Plural Demo Card
_buildPluralDemoCard(),
const SizedBox(height: 20),
// Features Card
_buildFeaturesCard(),
const SizedBox(height: 20),
// Alternative Selectors Card
_buildAlternativeSelectorsCard(),
const SizedBox(height: 20),
],
),
),
);
}
Widget _buildWelcomeCard() {
return Card(
elevation: 3,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'hello_user'.tr(params: {'name': userName}),
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Text('description'.tr(), style: const TextStyle(fontSize: 16)),
],
),
),
);
}
Widget _buildNameInputCard() {
return Card(
elevation: 3,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'change_name'.tr(),
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
TextField(
controller: _nameController,
decoration: InputDecoration(
labelText: 'enter_name'.tr(),
border: const OutlineInputBorder(),
suffixIcon: IconButton(
icon: const Icon(Icons.check),
onPressed: () {
setState(() {
userName = _nameController.text;
});
},
),
),
),
],
),
),
);
}
Widget _buildPluralDemoCard() {
return Card(
elevation: 3,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'plural_demo'.tr(),
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
// Display plural text
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.blue.shade50,
borderRadius: BorderRadius.circular(8),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.shopping_cart, size: 20),
const SizedBox(width: 12),
Text(
'items_count'.plural(count: itemCount),
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
],
),
),
const SizedBox(height: 16),
// Counter controls
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Minus button
IconButton.filled(
onPressed: itemCount > 0
? () {
setState(() {
itemCount--;
});
}
: null,
icon: const Icon(Icons.remove),
iconSize: 24,
),
const SizedBox(width: 20),
// Count display
Container(
padding: const EdgeInsets.symmetric(
horizontal: 24,
vertical: 8,
),
decoration: BoxDecoration(
border: Border.all(color: Colors.blue, width: 2),
borderRadius: BorderRadius.circular(8),
),
child: Text(
itemCount.toString(),
style: const TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(width: 20),
// Plus button
IconButton.filled(
onPressed: () {
setState(() {
itemCount++;
});
},
icon: const Icon(Icons.add),
iconSize: 24,
),
],
),
],
),
),
);
}
Widget _buildFeaturesCard() {
return Card(
elevation: 3,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'features'.tr(),
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
_buildFeatureItem('feature1'.tr()),
_buildFeatureItem('feature2'.tr()),
_buildFeatureItem('feature3'.tr()),
_buildFeatureItem('feature4'.tr()),
],
),
),
);
}
Widget _buildFeatureItem(String text) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
children: [
const Icon(Icons.check_circle, color: Colors.green, size: 20),
const SizedBox(width: 8),
Expanded(child: Text(text, style: const TextStyle(fontSize: 15))),
],
),
);
}
Widget _buildAlternativeSelectorsCard() {
return Card(
elevation: 3,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'other_selectors'.tr(),
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
// Button selector
const LanguageButtonSelector(
languageNames: {'en': 'EN', 'hi': 'हि', 'es': 'ES', 'fr': 'FR'},
),
const SizedBox(height: 12),
// Dialog button
ElevatedButton.icon(
onPressed: () => showLanguageDialog(context),
icon: const Icon(Icons.language),
label: Text('show_dialog'.tr()),
),
const SizedBox(height: 8),
// Bottom sheet button
ElevatedButton.icon(
onPressed: () => showLanguageBottomSheet(context),
icon: const Icon(Icons.menu),
label: Text('show_bottom_sheet'.tr()),
),
const SizedBox(height: 12),
// Current language info
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.green.shade50,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Colors.green.shade200),
),
child: Row(
children: [
const Icon(Icons.info_outline, color: Colors.green),
const SizedBox(width: 8),
Expanded(
child: Text(
'Current Language: ${EasyLocalization.instance.currentLanguage.toUpperCase()}',
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
),
],
),
),
],
),
),
);
}
}