language_helper 0.2.5+1 icon indicating copy to clipboard operation
language_helper: ^0.2.5+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:

final languageHelper = LanguageHelper.instance;

languageHelper.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. It will decrease the app performances.
    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.translate('Hello @name', params {'name', 'World'});
// Hello World

or

final text = languageHelper.translate('Hello @{name}', params {'name', 'World'});
// Hello World

You can also translate to specific language with toCode parameter:

final text = languageHelper.translate('Hello', toCode: LanguageCodes.en);

Use extension:

final text = 'Hello'.tr;

or

final text = 'Hello @{name}, @name'.trP({'name' : 'World'});
// Hello World, World

or use full version:

final text = 'Hello @{name}, @name'.trF(params: {'name' : 'World'}, toCode: LanguageCodes.en);

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 LanguageBuilder(builder: (context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Hello'.tr),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Hello'.tr),
              ElevatedButton(
                onPressed: () {
                  languageHelper.change(LanguageCodes.vi);
                },
                child: Text('Change language'.tr),
              ),
            ],
          ),
        ),
      ),
    );
  });
}
  • For specific widget:
LanguageBuilder(
    builder: (context) {
        return Text('Hello'.tr);
    },
),

You can analyze the missing texts for all language with this function:

languageHelper.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 the initialCode if it is not set, if the Devicelocale is unavailable, it will use the first language in data insteads.
  • No matter how many LanguageBuilder that you use, the plugin only rebuilds the outest (the root) widget of LanguageBuilder, so it improves a lot performance. And all LanguageBuilder widgets will be rebuilt at the same time. This setting can be changed with forceRebuild parameter in both initial for global setting and LanguageBuilder 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.
0
likes
140
pub points
68%
popularity

Publisher

verified publisher iconpub.vursin.com

Make it easier for you to implement multiple languages into your app.

Repository (GitHub)

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

flutter, language_code, shared_preferences

More

Packages that depend on language_helper