language_helper 0.2.1 language_helper: ^0.2.1 copied to clipboard
Make it easier for you to implement multiple languages into your app.
Language Helper #
Make it easier for you to implement multiple languages into your app.
Usage #
Create the data:
LanguageData data = {
LanguageCodes.en: {
'Hello': 'Hello',
'Change language': 'Change language',
},
LanguageCodes.vi: {
'Hello': 'Xin Chào',
'Change language': 'Thay đổi ngôn ngữ',
}
};
Initialize the data:
LanguageHelper.instance.initialize(
data: data,
initialCode: LanguageCodes.en, // Optional. Default is set to the device locale (if available) or the first language of [data]
useInitialCodeWhenUnavailable: false, // Optional. Default is set to false
forceRebuild: true, // Rebuild all the widgets instead of only root widgets
isAutoSave: true, // Auto save and reload the changed language
onChanged: (code) => print(code), // Call this function if the language is changed
isDebug: true, // Print debug log. Default is set to false
);
Get text:
final text = LanguageHelper.instance.translate('Hello @name', params {'name', 'World'});
// Hello World
or
final text = LanguageHelper.instance.translate('Hello @{name}', params {'name', 'World'});
// Hello World
Use extension:
final text = 'Hello'.tr;
or
final text = 'Hello @{name}, @name'.trP({'name' : 'World'});
// Hello World, World
Note: The ${param}
work in any case, the @param
only work if the text ends with a white space, the end of a line, or the end of a new line.
Use builder to rebuild the widgets automatically on change:
- For all widget in your app:
@override
Widget build(BuildContext context) {
return LanguageNotifier(builder: (context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Hello'.tr),
),
body: Center(
child: Column(
children: [
Text('Hello'.tr),
ElevatedButton(
onPressed: () {
LanguageHelper.instance.change(LanguageCodes.vi);
},
child: Text('Change language'.tr),
),
],
),
),
),
);
});
}
- For specific widget:
LanguageNotifier(
builder: (context) {
return Text('Hello'.tr);
},
),
You can analyze the missing texts for all language with this function:
LanguageHelper.instance.analyze();
This function will be automatically called in initial
when the isDebug
is true
.
Here is the result from the Example:
flutter: [Language Helper]
flutter: [Language Helper] ==================================================
flutter: [Language Helper]
flutter: [Language Helper] Analyze all languages to find the missing texts...
flutter: [Language Helper] Results:
flutter: [Language Helper] LanguageCodes.en:
flutter: [Language Helper] This text is missing in `en`
flutter: [Language Helper]
flutter: [Language Helper] LanguageCodes.vi:
flutter: [Language Helper] This text is missing in `vi`
flutter: [Language Helper]
flutter: [Language Helper] ==================================================
flutter: [Language Helper]
Additional Information #
- The app will try to use the
Devicelocale
to set theinitialCode
if it is not set, if theDevicelocale
is unavailable, it will use the first language indata
insteads. - No matter how many
LanguageNotifier
that you use, the plugin only rebuilds the outest (the root) widget ofLanguageNotifier
, so it improves a lot performance. And allLanguageNotifier
widgets will be rebuilt at the same time. This setting can be changed withforceRebuild
parameter in bothinitial
for global setting andLanguageNotifier
for local setting. - The
LanguageCodes
contains all the languages with additional information like name in English (name) and name in native language (nativeName). - This is the very first state so it may contain bugs or issues.
Note #
- The
${param}
work in any case, the@param
only work if the text ends with a white space, the end of a line, or the end of a new line.