GithubHelp home page GithubHelp logo

arthurbcd / tr_extension Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 304 KB

License: MIT License

Java 0.70% Shell 1.40% Objective-C 0.87% Dart 30.04% C++ 31.62% C 2.74% CMake 27.39% Swift 2.53% Kotlin 0.19% HTML 2.52%

tr_extension's Introduction

tr_extension

Tr Extension is a smart and lightweight string translation through extension.

Features

  • ๐Ÿ› ๏ธ Simplified Setup: No widgets. Few lines, and you're done!
  • โ†ฉ๏ธ Fallback Pattern: Smart key matching. Be specific or generic.
  • ๐Ÿ”€ Argument Replacement: Empower your JSON. Handle plurals & arguments.
  • ๐Ÿ”„ Locale Auto-Sync: Set Locale. Reflect changes.
  • ๐ŸŒ Flutter Integration. Works with sdk widgets and flutter_localizations.
  • โšก๏ธ Hot-reload friendly: Update your translations without restarting your app.

Getting started ๐Ÿ”ฅ

Setup files

Add tr_extension to your pubspec.yaml:

dependencies:
  tr_extension: ^0.5.1

Add your translations folder path in your pubspec.yaml:

flutter:
  assets:
     - assets/translations/ #default

Note: When changing the path, you must also change the path in the TrDelegate factory constructor, and recompile the app.

Put your .json files in the folder with the name of the locale.

  • assets/translations/en_US.json.
  • assets/translations/pt-BR.json.

Separators allowed:_ , - , + , . , / , | , \ and space.

Each file should contain a map of key/value pairs. Where the key is the translation key and the value is the translation itself.

{
  "hello_world": "Hello World!"
}

The path can be changed within TrDelegate factory constructor.

Setup app

MaterialApp(
  localizationsDelegates: TrDelegate(path: ...).toList(), // <- includes flutter_localizations
  locale: context.locale, // <- auto state management
  supportedLocales: const [
    Locale('en', 'US'),
    Locale('pt', 'BR'),
  ],
  home: const Home(),
);

'flutter_localizations' delegates are included in .toList().

Usage

Add '.tr' or '.trn' on any String and it will replace with the respective key/value from your translations. Where:

  • tr: returns the same string key when not found.
  • trn: returns null when not found.
    Text('helloWorld'.tr); // print -> 'Hello World!'
    Text('helloUniverse'.trn ?? 'other'); // print -> 'other'

Fallback Pattern

The fallback pattern will first try to translate the whole string, if not found, it will atempt to look for the next fallback pattern:

  • [.tr]: 'a.b.c' -> 'a.b' -> 'a' -> 'a.b.c'.
  • [.trn]: 'a.b.c' -> 'a.b' -> 'a' -> null.

For the below translations:

{
  "form.invalid": "This field in invalid",
  "form.invalid.email": "Invalid email",
};

Will return:

'form.invalid.email'.tr // 'Invalid email'.
'form.invalid.name'.tr // 'This field in invalid'.
'form.invalid'.tr // 'This field in invalid'.

Argument Replacement

Super simplification of arguments and pluralization!

Suppose you have the json below:

{
    "user_title.{name}.male": "{name} is nominated for Best Actor",
    "user_title.{name}.female": "{name} is nominated for Best Actress",
    "user_title.{name}": "{name} is nominated for Best Actor/Actress",
    "user_description.male": "He is the favorite this year!",
    "user_description.female": "She is the favorite this year!",
    "user_description": "They are the favorite this year!",
    "user_oscars.{name}.0": "{name} still hasn't won an Oscar.",
    "user_oscars.{name}.1": "This is {name}'s first Oscar!",
    "user_oscars.{name}.{}": "{name} has won {} Oscars"
}

You can easily swap args in a smart combination of fallback matching:

  final name = 'Emma Stone';
  final gender = 'female';
  final oscars = 1;

  print('user_title.$name.$gender'.tr); // 'Emma Stone is nominated for Best Actress'
  print('user_description.$gender'.tr); // 'She is the favorite this year!'
  print('user_oscars.$name.$oscars'.tr); // 'This is Emma Stone's first Oscar!'

Instance methods

Use TrDelegate.instance or Localizations.of<TrDelegate>(context, TrDelegate) to get the instance of the delegate.

///Changes the language with the chosen [Locale].
.setLocale(Locale locale) // or context.setLocale(Locale locale)

///Manually configures translations. Although we recommend using json files as described above.
.setTranslations(Locale locale, Map translations)

And the following getters:

.translations //all parsed translations
.missingTranslations //all missing translations
.translationFiles //all json files
.locale //the current locale
.supportedLocales  //all supported locales

tr_extension's People

Contributors

arthurbcd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.