flutterpack_localize 1.0.1
flutterpack_localize: ^1.0.1 copied to clipboard
A powerful and flexible internationalization package for Flutter apps with interpolation, pluralization, and persistence.
π¦ flutterpack_localize #
A simple, powerful, and lazy-loading i18n package for Flutter apps, inspired by Angular's internationalization.
Supports dynamic language switching, nested keys, pluralization, and parameter interpolation.
β¨ Features #
β
Nested JSON keys
β
Pluralization support
β
Interpolation (e.g., {name})
β
Lazy loading from asset files
β
Caching to avoid redundant loads
β
Persistence of the selected locale
β
Per-call control over persistence (changeLocaleWithPreference)
β
Simple tr() helper function
π Getting Started #
1οΈβ£ Install #
Add to your pubspec.yaml:
dependencies:
localize:
git:
url: https://github.com/FlutterPack/flutterpack_localize.git
Or once published to pub.dev:
dependencies:
flutterpack_localize: ^1.0.1
2οΈβ£ Create Translation Files #
Inside your assets/i18n folder, create JSON files:
en.json:
{
"auth": {
"login": "Login"
},
"greeting": "Hello {name}!",
"items": {
"zero": "No items",
"one": "One item",
"other": "{count} items"
}
}
fr.json:
{
"auth": {
"login": "Connexion"
},
"greeting": "Bonjour {name}!",
"items": {
"zero": "Aucun Γ©lΓ©ment",
"one": "Un Γ©lΓ©ment",
"other": "{count} Γ©lΓ©ments"
}
}
3οΈβ£ Configure pubspec.yaml assets #
flutter:
assets:
- assets/i18n/
π οΈ Usage Example #
Third party (Optional) #
flutter pub add flutter_localizations --sdk=flutter
Initialize #
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await FlutterPackLocalizeConfig.init(
const Locale('en'),
persistLanguage: true,
fileLoader: LocalizeFileLoader('assets/i18n'),
);
runApp(MyApp());
}
Provide Delegates and Reactive Locale #
Example 1:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: LocalizeConfig.currentLocale,
builder: (context, locale, _) {
return ValueListenableBuilder(
valueListenable: FlutterPackLocalizeConfig.translationsNotifier,
builder: (context, _, __) {
return MaterialApp(
locale: locale,
supportedLocales: [
Locale('en'),
Locale('fr'),
],
localizationsDelegates: [
FlutterPackLocalizeConfig.delegate(),
/// Optional
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: HomePage(),
);
},
);
},
);
}
}
Example 2:
Future<void> main() async {
// Initialize widget binding
WidgetsFlutterBinding.ensureInitialized();
// Initialize Localize (default to English)
await FlutterPackLocalizeConfig.init(
const Locale('en'),
persistLanguage: true,
fileLoader: FlutterPackLocalizeFileLoader('assets/i18n'),
);
// Run the app
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<Locale>(
valueListenable: FlutterPackLocalizeConfig.currentLocale,
builder: (context, locale, _) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Localization',
enableLog: true,
builder: EasyLoading.init(),
onGenerateRoute: onGenerateRoute,
// β¨ Localization
locale: locale,
supportedLocales: const [
Locale('en'),
Locale('fr')
],
localizationsDelegates: [
FlutterPackLocalizeConfig.delegate(),
/// Optional
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
);
},
);
}
}
Translating Text #
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
Text(tr(context, 'auth.login')),
Text(tr(context, 'greeting', params: {'name': 'Alice'})),
Text(tr(context, 'items', plural: 3)),
ElevatedButton(
onPressed: () {
FlutterPackLocalizeConfig.changeLocale(const Locale('fr'));
},
child: Text('Switch to French'),
),
],
);
}
}
π Per-Call Persistence Control #
Sometimes you may want user A to persist language selection but user B to not.
Use changeLocaleWithPreference:
// Switch to French and persist this choice
await FlutterPackLocalizeConfig.changeLocaleWithPreference(
const Locale('fr'),
persist: true,
);
// Switch to English only for this session
await FlutterPackLocalizeConfig.changeLocaleWithPreference(
const Locale('en'),
persist: false,
);
π§© API Reference #
FlutterPackLocalizeConfig.init
Initializes localization:
await FlutterPackLocalizeConfig.init(
Locale('en'),
persistLanguage: true,
fileLoader: FlutterPackLocalizeFileLoader('assets/i18n'),
);
-
persistLanguage: true β automatically remembers the userβs last selected language.
-
If persistLanguage is false, it always starts with your defaultLocale.
FlutterPackLocalizeConfig.changeLocale
Switches language (respecting persistLanguage):
await FlutterPackLocalizeConfig.changeLocale(Locale('fr'));
FlutterPackLocalizeConfig.changeLocaleWithPreference
Switches language and specifies whether to persist this change:
await FlutterPackLocalizeConfig.changeLocaleWithPreference(
Locale('fr'),
persist: true,
);
tr()
Helper for translating:
tr(context, 'auth.login')
tr(context, 'greeting', params: {'name': 'Alice'})
tr(context, 'items', plural: 2)
π Comparison With Other Packages #
Hereβs a quick look at how localize compares to popular Flutter i18n solutions:
| Feature | localize | intl | easy_localization | get |
|---|---|---|---|---|
| Format | JSON nested keys | ARB files | JSON / CSV | JSON |
| Nested JSON keys | β Yes (btn.add) | β No (you must generate keys manually) | β Yes | β Yes |
| Code generation required | β None | β Yes | β No | β No |
| Lazy loading | β Yes | β No | β οΈ Partial | β No |
| Caching | β In-memory | β No | β οΈ Limited | β οΈ Limited |
| Pluralization | β Supported | β Supported | β Supported | β οΈ Limited |
| Interpolation | β
{name} syntax |
β
{name} syntax |
β
{name} syntax |
β
{name} syntax |
| Dynamic locale switching | β Yes | β οΈ Manual rebuild | β Yes | β Yes |
| Per-call persistence control | β Yes | β No | β No | β No |
| Dependencies | Minimal | Medium | Medium | Medium |
| Learning curve | Super low | Medium | Medium | Low |
π― When Should You Use flutterpack_localize? #
Choose flutterpack_localize if:
- You want Angular-style simplicity without build steps.
- You need lazy loading and caching to save memory.
- You prefer full control over persistence for each user.
- You like small, clean, auditable codebases.
- You want to ship multilingual apps fast with minimal boilerplate.
β οΈ Known Limitations #
- No right-to-left (RTL) helpers (yet).
- No ICU plural rules (advanced pluralization logic).
- Currently loads from asset bundles (not remote URLs).
If youβd like to contribute or request features, please open an issue or pull request!
π Contributing #
Contributions are welcome!
Feel free to:
- Submit pull requests
- Report bugs
- Suggest improvements
π‘ Example Apps #
We recommend you create a small demo app to see flutterpack_localize in action before integrating into production.
π« Support #
Questions or need help?
Open an issue or contact the maintainer.