signals_translator 0.0.1 copy "signals_translator: ^0.0.1" to clipboard
signals_translator: ^0.0.1 copied to clipboard

Translate your project 100% using signals

Until this package reaches 1.0.0, every version update could contain breaking changes

Signal translator #

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 the way it's reactive system works. This is not because of the package itself, but I'm all aboard the signal train. This package currently fits all my needs, but it does not have a lot of features (yet)

License #

Licensed under a modified MIT License (with a no-reselling clause).

Features #

  • Supports singular only, does not support pluralization

  • Only supports JSON translation files

  • Does not support dates

  • Translates using signals

  • Get current language from the device

  • Ability to set the language manually

  • Also ability to set system language

Getting started #

important #

  1. All the components that use translations must use some kind of Watch functionality as described in the Signals package.
  2. Text widgets cannot be constants anymore, since their Signals. Although this completely logical, it might have some impact on performance.

Steps #

  1. In root, create a folder called assets/translations/
  2. 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"
  }
}
  1. Add the following to your pubspec.yaml file:
dependencies:
  signals_translator: ^0.0.1
  
  [...]
  
  flutter:
  assets:
    - assets/translations/

Then run flutter pub get to install the package.

  1. 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'), 
  )
);

  1. To set the language, you can use the setLanguage method of the SignalTranslator class. This method takes a String parameter that represents the language code (e.g. en, fr, etc.).
SignalTranslator.setLanguage('en');

and watch the translation change in the widget.

Full example #

See the 'example' folder for a complete example.

Additional features #

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

Credits #

All this is possible because of the best state management package out there: Signals. Real credits should go there :)

0
likes
0
points
345
downloads

Publisher

verified publishernathantaal.nl

Weekly Downloads

Translate your project 100% using signals

License

unknown (license)

Dependencies

flutter, shared_preferences, signals

More

Packages that depend on signals_translator