get_localization 1.0.0 get_localization: ^1.0.0 copied to clipboard
Localize your app easily entirely in flutter using dart getters. No need for code generation.
get_localization
Localize your app easily entirely in flutter using dart getters. No need for code generation.
Get started #
Create your BaseLocalization class and the translation getters #
Don't forget to make it abstract
.
abstract class BaseLocalization extends Localization {
BaseLocalization({
@required String code,
@required String name,
String country,
}) : super(
code: code,
name: name,
country: country,
);
// Add your getters down here:
String get appName;
}
class EnglishLocalization extends BaseLocalization {
EnglishLocalization() : super(code: 'en', name: 'English');
String get appName => 'Example App';
}
class PortugueseLocalization extends BaseLocalization {
PortugueseLocalization() : super(code: 'pt', name: 'Português');
String get appName => 'App de Exemplo';
}
Dart-analyzer will tell you when a getter implementation is missing.
See example to a full example
Initialize the package #
void main() {
// Initialize the localization system. It's not necessary, but
// if you want to get notified about the system language as soon
// as changes, you need to call this method
Localization.init();
// Add your localizations. You can add them at runtime, but it's
// recommended to add it here, since it'll be called only once
Localization.localizations
..add(yourLocalization);
runApp(MyApp());
}
If you're using MaterialApp
(or WidgetsApp
, CupertinoApps
and related), you need to set the supportedLocales
:
return MaterialApp(
/// Add this line so the platform knows the supported languages
supportedLocales: Localization.localizations.toLocaleList(),
home: Home(),
);
Make sure to use Localization
in the main thread/isolator (main()
).
Get and set the current localization #
// Get the localization. Make sure to cast it to BaseLocalization
// or whatever your class name is
BaseLocalization localization = Localization.currentLocalization;
// Set the localization
Localization.currentLocalization = <localization-class here>;
Listen to events #
You can listen to when the localization change using Localization.onLocaleChanged
. It's usually used to update the app when the localization changes:
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
/// Listen to the change events and update the whole app.
/// You can use something like SharedPreferences to save the current language
/// and set it on initialization
Localization.onLocaleChanged.listen((event) => setState(() {}));
}
@override
Widget build(BuildContext context) {
BaseLocalization loc = Localization.currentLocalization;
return MaterialApp(
title: loc.appName,
/// Add this line so the platform knows the supported languages
supportedLocales: Localization.localizations.toLocaleList(),
home: Home(),
);
}
}
You can see a full-working app at example