# flutter_i18n 0.35.1

i18n made easy for Flutter. With flutter_i18n you can make your app international, using just a simple .json, .xml, .yaml or .toml file!




## Installing
```
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'flutter_i18n:0.35.1'
```
Published: 2024-02-09T17:37:53.753646Z
Expected archive SHA-256: c823a9c9e32d2166d0ce8fa60b63459648a55f29a17c0d5dbf66fe4c9e05f1c2

Available in cache; verification: upstream_checksum
## Direct dependencies
- flutter: map[sdk:flutter]
- flutter_localizations: map[sdk:flutter]
- flutter_web_plugins: map[sdk:flutter]
- http: ^1.1.2
- intl: >=0.17.0-0 <=0.19.0
- logging: ^1.2.0
- path: ^1.8.3
- toml: ^0.15.0
- xml2json: ^6.2.2
- yaml: ^3.1.2

## Package README (untrusted upstream documentation)
# flutter_i18n
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
I18n made easy, for Flutter!

<!-- Badges -->
[![Pub Package](https://img.shields.io/pub/v/flutter_i18n.svg)](https://pub.dev/packages/flutter_i18n)
[![GitHub Actions](https://github.com/ilteoood/flutter_i18n/workflows/Publish%20plugin/badge.svg)](https://github.com/ilteoood/flutter_i18n/actions)

------------------------------------------------

## Table of contents

* [Why should you use flutter_i18n?](#why-should-you-use-flutter_i18n)
* [Loaders](#loaders)
  * [FileTranslationLoader](#filetranslationloader-configuration)
  * [NetworkFileTranslationLoader](#networkfiletranslationloader-configuration)
  * [NamespaceFileTranslationLoader](#namespacefiletranslationloader-configuration)
  * [E2EFileTranslationLoader](#e2efiletranslationloader-configuration)
* [flutter_i18n in action](#flutter_i18n-in-action)
* [Plugins](#plugins)
* [Contributors](#contributors-)

## Why should you use flutter_i18n?

The main goal of *flutter_i18n* is to simplify the i18n process in Flutter.
I would like to recreate the same experience that you have with the Angular i18n: simple *json* files, one for each language that you want to support.


## Loaders

Loader is a class which loads your translations from specific source. 
You can easy override loader and create your own.

Available loaders:

| Class name | Purpose |  
| --- | --- |
| `FileTranslationLoader` |  Loads translation files from JSON, YAML, TOML or XML format |
| `LocalTranslationLoader` |  Is a copy of `FileTranslationLoader`, but used to loads the files from the device storage instead of assets folder | 
| `NetworkFileTranslationLoader` | Loads translations from the remote resource | 
| `NamespaceFileTranslationLoader` | Loads translations from separate files |
| `E2EFileTranslationLoader` | Special loader for solving isolates problem with flutter drive |

### `FileTranslationLoader` configuration

To use this library, you must create a folder in your project's root: the `basePath`. Some examples:

> /assets/flutter_i18n (the default one)
>
> /assets/i18n
>
> /assets/locales

Inside this folder, you'll put the *json*, *yaml*, *xml* or *toml* files containing the translated keys. You have different options:

- If you want to specify the country code

    > `basePath`/{languageCode}_{countryCode}.json

- If you want to specify the script code

    > `basePath`/{languageCode}_{scriptCode}.json

- If you want to specify both

    > `basePath`/{languageCode}_{scriptCode}_${countryCode}_.json

- otherwise

    > `basePath`/{languageCode}.json

If the *json* file is not available, we will look for a *yaml* file with the same name. In case both exist, the *json* file will be used.

Of course, you must declare the subtree in your ***pubspec.yaml*** as assets:

```yaml
flutter:
  assets:
    - {basePath}
```

The next step consist in the configuration of the *localizationsDelegates*; to use *flutter_i18n*, you should configure as follows:

```dart
localizationsDelegates: [
        FlutterI18nDelegate(
          translationLoader: FileTranslationLoader(...parameters...),
          missingTranslationHandler: (key, locale) {
            print("--- Missing Key: $key, languageCode: ${locale.languageCode}");
          },
        ),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
],
builder: FlutterI18n.rootAppBuilder() //If you want to support RTL.
```

Below you can find the name and description of the accepted parameters.

The ***useCountryCode*** parameter depends on the *json* configuration:
- if you used the pattern {languageCode}_{countryCode}, ***useCountryCode*** must be **true**
- if you used the pattern {languageCode}, ***useCountryCode*** must be **false**

The ***fallbackFile*** parameter was entroduces with the version **0.1.0** and provide a default language, used when the translation for the current running system is not provided. This should contain the name of a valid *json* file in *assets* folder.

The ***basePath*** parameter is optionally used to set the base path for translations. If this option is not set, the default path will be `assets/flutter_i18n`. This path must be the same path as the one defined in your ***pubspec.yaml***.

The ***forcedLocale*** parameter is optionally used to force a locale instead finding the system one.

The ***decodeStrategies*** parameters is optionally used to choose witch kind of file you want to load. By default JSON, YAML and XML are enabled. If you use only one format, you can speed-up the bootstrap process using only the one you need.

If there isn't any translation available for the required key, even in the fallback file, the same key is returned.

### `NetworkFileTranslationLoader` configuration

Behaviour of this loader very similar as `FileTranslationLoader`. The main difference that we load translations from `NetworkAssetBundle` instead of `CachingAssetBundle`.

Below you can find the name and description of the accepted parameters.

The ***baseUri*** parameter provide base Uri for your remote translations.

The ***useCountryCode*** parameter depends on the *json* configuration:
- if you used the pattern {languageCode}_{countryCode}, ***useCountryCode*** must be **true**
- if you used the pattern {languageCode}, ***useCountryCode*** must be **false**

The ***fallbackFile*** parameter provide a default language, used when the translation for the current running system is not provided.

The ***forcedLocale*** parameter is optionally used to force a locale instead finding the system one.

For example if your translation files located at 
`https://example.com/static/en.json` you should configure as follows:

```dart
MaterialApp(
  localizationsDelegates: [
        FlutterI18nDelegate(translationLoader: 
          NetworkFileTranslationLoader(baseUri: Uri.https("example.com", "static")),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
  ],
)
```

### `NamespaceFileTranslationLoader` configuration

Behaviour of this loader very similar as `FileTranslationLoader`. The main difference that we load translations from separate files per each language.

For example `FileTranslationLoader` format:

> /assets/flutter_i18n/en.json
>
> /assets/flutter_i18n/it.json

`NamespaceFileTranslationLoader` format:

> /assets/flutter_i18n/en/home_screen.json
>
> /assets/flutter_i18n/en/about_screen.json
>
> /assets/flutter_i18n/it/home_screen.json
>
> /assets/flutter_i18n/it/about_screen.json

Example configuration:

```dart
MaterialApp(
  localizationsDelegates: [
        FlutterI18nDelegate(translationLoader: 
          NamespaceFileTranslationLoader(namespaces: ["home_screen", "about_screen"]),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
  ],
)
```

Below you can find the name and description of the accepted parameters.

The ***namespaces*** provide a list of filenames for the specific language directory.

The ***useCountryCode*** parameter depends on the *json* configuration:
- if you used the pattern {languageCode}_{countryCode}, ***useCountryCode*** must be **true**
- if you used the pattern {languageCode}, ***useCountryCode*** must be **false**

The ***fallbackDir*** provide a default language directory, used when the translation for the current running system is not provided.

The ***basePath*** parameter is optionally used to set the base path for translations. If this option is not set, the default path will be `assets/flutter_i18n`. This path must be the same path as the one defined in your ***pubspec.yaml***.

The ***forcedLocale*** parameter is optionally used to force a locale instead finding the system one.

### `E2EFileTranslationLoader` configuration

The same as `FileTranslationLoader` configuration. This loader can be used for solving problem with flutter drive testing.
It removes using separate isolate for loading translations (detailed issue described here: [issues/24703](https://github.com/flutter/flutter/issues/24703)).

The ***useE2E*** parameter:
- if you are in flutter drive testing mode – must be **true**
- if you are in normal mode – must be **false**, in this case `FileTranslationLoader` will be used

## flutter_i18n in action

After the configuration steps, the only thing to do is invoke the following method:

```dart
FlutterI18n.translate(buildContext, "your.key")
```

Where:
- *buildContext* is the *BuildContext* instance of the widget
- *your.key* is the key to translate

Other examples of use:

Force a language to be loaded at run-time:
```dart
await FlutterI18n.refresh(buildContext, languageCode, {countryCode});
```

Plural translations:
```dart
FlutterI18n.plural(buildContext, "your.key", pluralValue);
```

Text widget shorthand:
```dart
I18nText("your.key", child: Text(""))
I18nText("your.key", translationParams: {"user": "Flutter lover"})
I18nPlural("clicked.times", 1)
I18nPlural("clicked.times", 2, child: Text(""))
```

If you need to listen the translation loading status, you can use:
- ```FlutterI18n.retrieveLoadingStream``` method, that allows you to listen to every status change
- ```FlutterI18n.retrieveLoadedStream``` method, that allows you to listen when the translation is loaded

For more informations and details, read the [CHANGELOG.md](CHANGELOG.md).

## Utilities

Using *flutter pub run flutter_i18n* inside the root of your project you can enjoy some utilities that will help you with the translations files management.

### Commands

#### Validate

This command is used to validate all the translations files inside the project.

```sh
> flutter pub run flutter_i18n validate
[flutter_i18n DEBUG]: I've found assets/i18n/en.yaml
[flutter_i18n DEBUG]: I've found assets/i18n/it.json
[flutter_i18n DEBUG]: I've found assets/i18n/es.xml
[flutter_i18n DEBUG]: I've found assets/i18n_namespace/en/common.json
[flutter_i18n DEBUG]: I've found assets/i18n_namespace/en/home.yaml
[flutter_i18n DEBUG]: I've found assets/i18n_namespace/ua/common.json
[flutter_i18n DEBUG]: I've found assets/i18n_namespace/ua/home.json
[flutter_i18n INFO]: YAML file loaded for en
[flutter_i18n INFO]: Valid file: assets/i18n/en.yaml
[flutter_i18n INFO]: JSON file loaded for it
[flutter_i18n INFO]: Valid file: assets/i18n/it.json
[flutter_i18n INFO]: XML file loaded for es
[flutter_i18n INFO]: Valid file: assets/i18n/es.xml
[flutter_i18n INFO]: JSON file loaded for common
[flutter_i18n INFO]: Valid file: assets/i18n_namespace/en/common.json
[flutter_i18n INFO]: YAML file loaded for home
[flutter_i18n INFO]: Valid file: assets/i18n_namespace/en/home.yaml
[flutter_i18n INFO]: JSON file loaded for common
[flutter_i18n INFO]: Valid file: assets/i18n_namespace/ua/common.json
[flutter_i18n INFO]: JSON file loaded for home
[flutter_i18n INFO]: Valid file: assets/i18n_namespace/ua/home.json
```

#### Diff

This command is used to find the differences between the keys of the desired translation files.

```sh
> flutter pub run flutter_i18n diff en.yaml it.json
[flutter_i18n INFO]: [en.yaml, it.json]
[flutter_i18n INFO]: YAML file loaded for en
[flutter_i18n INFO]: JSON file loaded for it
[flutter_i18n ERROR]: The compared dictionary doesn't contain the key >title
```

## Plugins

| Plugin | Description |
|---------|-------------|
| [flutter_i18n_locize](https://github.com/defint/flutter_i18n_locize) | Easy integration locize.io to flutter_i18n. |

## Do you like my work?
<p>
    <a href="https://www.patreon.com/ilteoood">
        <img align="center" alt="patreon" src="https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.vercel.app%2Fapi%3Fusername%3Dilteoood%26type%3Dpatrons&style=for-the-badge">
        </img>
    </a>
    or
    <a href="https://www.buymeacoffee.com/ilteoood">
        <img align="center" alt="buy-me-a-coffee" src="https://img.shields.io/badge/-buy_me_a%C2%A0coffee-gray?logo=buy-me-a-coffee">
        </img>
    </a>
</p>

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
  <tr>
    <td align="center"><a href="https://github.com/defint"><img src="https://avatars2.githubusercontent.com/u/1523848?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Anton</b></sub></a><br /><a href="#infra-defint" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/ilteoood/flutter_i18n/commits?author=defint" title="Tests">⚠️</a> <a href="https://github.com/ilteoood/flutter_i18n/commits?author=defint" title="Code">💻</a></td>
    <td align="center"><a href="https://lucasvienna.dev"><img src="https://avatars1.githubusercontent.com/u/1761244?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Lucas Vienna</b></sub></a><br /><a href="https://github.com/ilteoood/flutter_i18n/commits?author=Avyiel" title="Code">💻</a></td>
    <td align="center"><a href="https://github.com/jlcool"><img src="https://avatars3.githubusercontent.com/u/381375?v=4?s=100" width="100px;" alt=""/><br /><sub><b>jlcool</b></sub></a><br /><a href="https://github.com/ilteoood/flutter_i18n/commits?author=jlcool" title="Code">💻</a></td>
    <td align="center"><a href="https://github.com/juumixx"><img src="https://avatars1.githubusercontent.com/u/5200179?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Julian</b></sub></a><br /><a href="https://github.com/ilteoood/flutter_i18n/commits?author=juumixx" title="Code">💻</a> <a href="#ideas-juumixx" title="Ideas, Planning, & Feedback">🤔</a></td>
    <td align="center"><a href="https://andreygordeev.com"><img src="https://avatars1.githubusercontent.com/u/2883524?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Andrey Gordeev</b></sub></a><br /><a href="https://github.com/ilteoood/flutter_i18n/commits?author=agordeev" title="Code">💻</a> <a href="https://github.com/ilteoood/flutter_i18n/issues?q=author%3Aagordeev" title="Bug reports">🐛</a></td>
    <td align="center"><a href="https://github.com/Amir-P"><img src="https://avatars.githubusercontent.com/u/8766492?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Amir Panahandeh</b></sub></a><br /><a href="https://github.com/ilteoood/flutter_i18n/issues?q=author%3AAmir-P" title="Bug reports">🐛</a> <a href="https://github.com/ilteoood/flutter_i18n/commits?author=Amir-P" title="Code">💻</a></td>
  </tr>
</table>

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

## Changelog
## [0.2.0]

*flutter_i18n* now manage strings that contain parameters; an example can be: "Hello, {user}!"
For a correct translation, you must use the third parameter of the *translate* method, a *Map<String, String>* where:
- the keys are the placeholders used in the *.json* file (i.e. user)
- the values are what you want to display

## [0.3.0]

*flutter_i18n* now supports language change at runtime. To use it, you ***must*** invoke the method
```sh
await FlutterI18n.refresh(buildContext, languageCode, {countryCode});
```

***NOTE***: *countryCode* is optional.

## [0.4.0]

*flutter_i18n* now supports plurals. To use it, invoke the method:
```sh
FlutterI18n.plural(buildContext, "your.key", pluralValue);
```

Where pluralValue is the integer value that will be used to determinate the plural form of the translation.

Here is an example of configuration of the *.json* file:

```sh
"clicked": {
    "times-0": "You clicked {times} times!",
    "times-1": "You clicked {time} time!",
    "times-2": "You clicked {times} times!"
  }
```

With this configuration, you must invoke the *plural* method as follow:

```sh
FlutterI18n.plural(buildContext, "clicked.times", pluralValue);
```

FlutterI18n will choose the right key using the value of pluralValue: it will match the last key with a value <= of pluralValue.


## [0.5.0]

*flutter_i18n* now supports the `basePath` configuration.
The default one is: `assets/flutter_i18n`.
To configure it, use the third optional parameter of `FlutterI18nDelegate`.

## [0.5.1]

Print in log when a key is not found.

## [0.5.2]

Fix for bug #10.

## [0.6.0]

Exposed new method that return the locale used by the library, as requested in #16:

```sh
FlutterI18n.currentLocale();
```

***NOTE***: the ***refresh*** method now accept a *Locale* as second parameter, instead of two strings.
***NOTE***: the constructor of *FlutterI18nDelegate* now accept only named parameter.

## [0.6.1]

Fix for bug #22

## [0.6.2]

Fix for the delegate

## [0.6.3]

Fix for #17

## [0.6.4]

Fix for #41

## [0.7.0]

Fix for #48 and implemented feature for #49.
From this version of *FlutterI18n*, you can use as 4th parameter the ***forcedLanguage*** parameter, in order to avoid to call the *refresh* method on application startup.

## [0.8.0]

Support for *yml* language files

## [0.8.1]

Improved *json* and *yml* loading

## [0.8.2]

Bugfix and code improvement

## [0.8.3]

Library will print debug messages only in development mode

## [0.9.0]

Added a new optional parameter to the translate method: *fallbackKey*.
You can use it when the translation *key* is not found.

## [0.10.0]

Added stateless widgets in order to avoid to deal with BuildContext.
```dart
I18nText("your.key", Text(""))
I18nText("your.key", Text(""), translationParams: {"user": "Flutter lover"})
I18nPlural("clicked.times", 2, Text(""))
```

## [0.11.0]

New translation mechanism, with different and customizable loaders provided:

* FileTranslationLoader
* NetworkFileTranslationLoader
* E2EFileTranslationLoader

## [0.11.1]

Plural translator improvements

## [0.12.0]

New loader provided: ```NamespaceFileTranslationLoader```

## [0.13.0]

In the I18nText and I18nPlural widgets, the child parameter is now optional.
The default value of the child parameter is now ```Text("")```

## [0.13.1]

Fix for #83.

## [0.13.2]

Fixed immutable warnings.

## [0.14.0]

New feature: file translation decode strategies.
New translation strategy provided, able to load XML file.

## [0.15.0]

New feature: missing key are now retrieved from the fallback file.

## [0.16.0]

Full support for supportedLocales

## [0.16.1]

Support for decode strategies on other loaders

## [0.16.2]

Added missing translation handler, according to [issue/67](https://github.com/ilteoood/flutter_i18n/issues/67)

## [0.16.3]

Removed cache parameter from loadString method, resolve [issue/116](https://github.com/ilteoood/flutter_i18n/issues/116)

## [0.17.0]

RTL support.
Test fix for [issue/115](https://github.com/ilteoood/flutter_i18n/issues/115)

## [0.18.0]

New loader provided:

* LocalTranslationLoader

## [0.19.0]

Support for Flutter Web.
Note: due to dart:io bug, NetworkFileTranslationLoader can't be used.

## [0.19.1]

Removed useless parameter from LocalTranslationLoader and fixed delegate load method.

## [0.19.2]

Plugins and better Flutter Web support.

## [0.19.3]

Removed dart:io for better Flutter Web support.

## [0.19.4]

Better dartdocs

## [0.20.0]

Loading and loaded stream, in order to detect loading translations status

## [0.20.1]

Fix for streams listened twice

## [0.21.0]

Introducing validate and diff actions

## [0.21.1]

Fix for #150

## [0.22.0]

Initial Flutter 2 support:
- intl upgraded to version 0.17.0
- example app upgraded to Android X
- null safe migration

## [0.22.1]

Nullsafe support + static analysis fixes

## [0.22.2]

Dartfmt

## [0.22.3]

Fix for #159

## [0.22.4]

Fix for #167
Dependencies upgrade

## [0.30.0]

Support for script code
Support for toml format
Fix for #173

## [0.31.0]
Fix for #179

## [0.31.1]
Fix for #184

## [0.32.0]
Fix for #186
Recursively merge the translation map with the fallback map

## [0.32.1]
Fix for #189

## [0.32.2]
Fix for #190

## [0.32.3]
Fix for #197 and #196

## [0.32.4]
Dependencies upgrade

## [0.32.5]
Dependencies upgrade

## [0.33.0]
Support for Dart 3 and Flutter 3.10
Fix for #204

## [0.34.0]
Upgrade http@1.1.0, logging@1.2.0, xml2json@6.2.0, yaml@3.1.2, path@1.8.3
Fix: deprecation warnings

## [0.35.0]
Upgrade intl@0.18.1, xml2json@6.2.2, toml@0.15.0, http@1.1.2
Fix: deprecation warnings

## [0.35.1]
Change intl version to '>=0.17.0-0 <=0.19.0'






 
