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.
flutter_easy_localization #
A powerful and flexible localization package for Flutter applications with runtime language switching, translation caching, and comprehensive plural/gender support.
Features #
- Runtime language switching without app restart
- Smart translation caching for optimal performance
- Missing translation detector in debug mode
- Comprehensive plural rules for 30+ languages
- Easy JSON/YAML integration
- Fallback language support
- Pre-built UI widgets (dropdown, buttons, dialogs)
- Type-safe string extensions
- Nested translation keys with dot notation
- Zero dependencies (except Flutter SDK)
Getting started #
Add to your pubspec.yaml:
dependencies:
flutter_easy_localization: ^1.0.2
Then run:
flutter pub get
Create JSON translation files in assets/translations/:
{
"welcome": "Welcome",
"hello_user": "Hello, {name}!",
"items_count": {
"zero": "No items",
"one": "1 item",
"other": "{count} items"
}
}
Add assets to pubspec.yaml:
flutter:
assets:
- assets/translations/
Usage #
Initialize in main.dart:
import 'package:flutter/material.dart';
import 'package:flutter_easy_localization/flutter_easy_localization.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await LanguageManager.init(
supportedLanguages: ['en', 'hi', 'es'],
defaultLanguage: 'en',
translationsPath: 'assets/translations',
fallbackLanguage: 'en',
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return TranslationBuilder(
builder: (context) => MaterialApp(
title: 'My App',
home: HomePage(),
),
);
}
}
Use translations in your widgets:
// Simple translation
Text('welcome'.tr())
// With parameters
Text('hello_user'.tr(params: {'name': 'John'}))
// Plural support
Text('items_count'.plural(count: 5))
// Switch language
await LanguageManager.instance.setLanguage('hi');
Pre-built language selector widget:
AppBar(
actions: [
LanguageSelector(
languageNames: {
'en': 'English 🇺🇸',
'hi': 'हिंदी 🇮🇳',
},
),
],
)