Until this package reaches 1.0.0, every version update could contain breaking changes. I'll try to deprecate things a few versions ahead
For my own project, I needed a lightweight translation solution that could be used in Flutter.
I previously used the easy_localization package, but I didn't like how it made my app re-render.
This is not because of the package itself, but because I'm a Signals enthusiast.
This package currently fits all my needs, but it might not fit yours.
Licensed under a modified MIT License.
-
Supports singular only, does not support pluralization
-
Only supports JSON translation files
-
Does not support dates
-
Translates using signals
-
Display app in system language by default
-
Support setting a different language
-
Supports setting system language as default
- All the components that use translations must use some kind of Watch functionality as described in the Signals package.
- Text widgets cannot be constants anymore, since their Signals. Although this completely logical, it might have some impact on performance.
- In root, create a folder called
assets/translations/ - In the
assets/translations/folder, create a JSON file for each language you want to support. The name of the file should be the language code (e.g.en.json,fr.json, etc.). The content of the file should be a JSON object with key-value pairs for each translation:
{
"language": "English",
"translations": {
"example translation": "example translation",
"example translation 2": "example translation 2"
}
}- Add the following to your
pubspec.yamlfile:
dependencies:
signals_translator: ^0.0.1+2
[...]
flutter:
assets:
- assets/translations/
Then run flutter pub get to install the package.
- Add a widget that needs translating
import 'package:signals/signals_flutter.dart';
import 'package:signals_translator/signals_translator.dart';
Watch(
(context) => Text(
tl('example translation'),
)
);
- To set the language, you can use the
setLanguagemethod of theSignalTranslatorclass. This method takes aStringparameter that represents the language code (e.g.en,fr, etc.).
SignalTranslator().setLanguage('en');- To view the currently set language:
SignalTranslator().currentLocale;This is done automatically when the app starts, but if you want to build in a language selector, you can use this method to highlight the currently selected language.
See the 'example' folder for a complete example.
More features could be implemented. As my time is limited, feel free to open an issue and I will look into when I have time.
- Planned for Q3: a dropdown menu to select the language
All this is possible because of the best state management package out there: Signals. Credits should go there :)
Publish extension using dart pub publish --dry-run