Fast localization solution for flutter apps using Dart's Map
. (no context
needed for every single localization).
Add the following to your dependencies
in pubspec.yaml
fast_localization: <last_version>
import 'package:flutter/material.dart';
import 'package:fast_localization/fast_localization.dart';
void main() async {
final en = {
"title": "Demo",
"welcome": "Hello World!",
};
final ar = {
"title": "عرض",
"welcome": "أهلاً بالعالم!",
};
final locales = {
Locale('en'): en,
Locale('ar'): ar,
};
await Localization.load(locales);
runApp(LocalizationApp(
title: () => Localization.translate('title'),
home: () => HomeScreen(),
));
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
...
);
}
}
When you want the home
and title
of MaterialApp
only.
MaterialApp(title: 'App Name', home: HomeScreen());
Check out example/fast_localization.dart
You need to install flutter_localizations
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fast_localization/fast_localization.dart';
void main() async {
final en = {
"title": "Demo",
"welcome": "Hello World!",
};
final ar = {
"title": "عرض",
"welcome": "أهلاً بالعالم!",
};
final locales = {
Locale('en'): en,
Locale('ar'): ar,
};
await Localization.load(locales);
runApp(LocalizationApp(
child: (context) => MyApp(),
));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
...
);
}
}
When you want to use own MaterialApp
, CupertinoApp
, or WidgetsApp
.
Check out example/flexible.dart
Argument | Type | Example | Required |
---|---|---|---|
title | Function => String | () => t('title') | Yes |
home | Function => Widget | () => HomeScreen() | Yes |
theme | ThemeData | ThemeData.dark() | No |
Argument | Type | Example | Required |
---|---|---|---|
child | Function(BuildContext) => Widget | (context) => MyApp() | Yes |
You need to use either
Minimal
orFlexible
Function | Parameters | Returns |
---|---|---|
Localization.load(...) |
(Map<Locale, Map<String, dynamic>> languages) | Future |
Localization.changeLocale(...) |
(Locale locale, [BuildContext context]) | Future |
Localization.translate(...) |
(String key, [Map<String, String> params]) | String |
Localization.t(...) (Alias) |
(String key, [Map<String, String> params]) | String |
Field | Type | Description |
---|---|---|
Localization.locale |
Locale | Get current locale |
Localization.supportedLocales |
List < Locale > | Get supported locales |
Localization.languageCountry |
String | Get current locale to string, e.g. 'en_US' |
There is a global alias for
Localization.translate(key, params)
which ist(key, params)
!
final en = {
"title": "Demo",
"welcome": "Hello World!",
};
Usage:
Localization.translate('welcome');
Localization.t('welcome');
t('welcome');
final en = {
"app": {
"name": "Demo",
"description": "A demo app",
},
};
Usage:
Localization.translate('app.name');
Localization.t('app.name');
t('app.name');
final en = {
"info": {
"name": "My name is {{name}}",
},
};
Usage:
Localization.translate('info.name', {"name": "Hasan"});
Localization.t('info.name', {"name": "Hasan"});
t('info.name', {"name": "Hasan"});
- Locale fallback
- Parameters support
- Nested localizations
- Auto save/load local
- Language code only support
Locale('en')
- Language and country code support
Locale('en', 'US')
- Add tests
PRs are always welcome and appreciated!