p-hatan/ mirror

pub repository

mirror.p-hatan.com

flutter_i18n

0.7.0

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

Download archive

Readme

flutter_i18n

I18n made easy, for Flutter!


Why you should 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.

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 files containing the translated keys. You have two options:

  • If you want to specify the country code

    basePath/{languageCode}_{countryCode}.json

  • otherwise

    basePath/{languageCode}.json

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

flutter:
  assets:
    - {basePath}

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

localizationsDelegates: [
        FlutterI18nDelegate({useCountryCode = false, fallbackFile, basePath = "assets/flutter_i18n"}),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
],

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.

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

flutter_i18n in action

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

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:

await FlutterI18n.refresh(buildContext, languageCode, {countryCode});
FlutterI18n.plural(buildContext, "your.key", pluralValue);

For more informations, read the CHANGELOG.md.

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

await FlutterI18n.refresh(buildContext, languageCode, {countryCode});

NOTE: countryCode is optional.

[0.4.0]

flutter_i18n now supports plurals. To use it, invoke the method:

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:

"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:

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:

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.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add flutter_i18n
pubspec.yaml
name: flutter_i18n
description: i18n made easy for Flutter. With flutter_i18n you can make your app international, using just a simple .json file!
version: 0.7.0
author: Matteo Pietro Dazzi <matteopietro.dazzi@gmail.com>
homepage: https://github.com/ilteoood/flutter_i18n

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

dev_dependencies:
  flutter_test:
    sdk: flutter

environment:
  sdk: '>=1.19.0 <3.0.0'

Version history

VersionPublishedArchiveSHA-256
0.0.12018-08-26T21:04:01.985130ZDownloadbc62ed0c0f91a86e96cc6a15234bceef1f4bb3e0679c18355bbc2befca6b4262
0.0.22018-08-27T21:33:02.280068ZDownload53d2a591e69524257b5a0f1c1d654cbb10483fac084653fed61717816776baee
0.0.32018-09-10T21:11:31.961228ZDownload4f262b0dcfadb17197dfeac13783d2b4d8bf1299a3cc080868b5418765c3d320
0.1.02018-09-21T19:16:57.681831ZDownloadc8a7e81b5c5f65c8ba90f09891b5d5cb02bfec437fa882a9a7f131aae580768b
0.2.02018-09-23T14:14:30.542962ZDownload74c6b69cee950b04364ee8656e51483e26a23b93eed9f8b41b2ff2fa59045ed4
0.3.02018-10-29T20:48:00.709676ZDownload4eba6520eb9854985f0840d373f1c06cc4593566ec4fa9d9d51ed21bdef1c244
0.4.02018-11-03T14:10:38.525501ZDownload32557618f1d8ad93d69dbd16069ab7ce3d489298ab4226e44904a92815ba7775
0.5.02018-11-14T19:39:03.673200ZDownload2b118d2661ad2747e050b650e5272495e9117bf2e3a3e6cf70c6edb284b206e5
0.5.12018-12-17T20:08:16.235361ZDownload7cfdec9ffd014d0b5eaa16e8d1dd4391f0f35173afb863c3366bc14c944888b4
0.5.22019-01-07T20:26:07.209874ZDownloade3fcf08125f97a91f28d66114c51bbcf6462072c750494ed254c9f35bd0ebfcc
0.6.02019-03-05T21:47:52.259604ZDownload65c9167c0303b5d5f981c2875fbd189e70087795ce1ffa319c0fa6b173f84f62
0.6.12019-03-09T12:43:10.817899ZDownloadde342cf3c49b4d3c5c3563b990e3cb8b3e9590c38ec09db0e0ce0d38547fb15e
0.6.22019-03-11T20:58:43.533934ZDownload620a8242ee5064c233de261963d2cbe8b9155181d40882c5d920b439d85d4929
0.6.32019-03-17T10:39:47.917692ZDownload95d2ec261e11f0f8378b1c4637d61db307fe36732e73624cf7e767f03ca2f65e
0.6.42019-08-11T09:36:54.640786ZDownload9b0231b638d58411ba8ccaf1179a49af1415b379e65c8e6b9000f42e950f9de4
0.7.02019-09-30T19:59:44.413347ZDownload4e21b4d8e37caf16caf9d0ed39356b874f62b352db466b6a6343e7a2d8850d78
0.8.02019-10-26T18:16:49.864494ZDownload88a9bab0d755846e2b97115ac149abf9b0a6c5631a05937d28be3b74fa622bff
0.8.12019-11-15T21:46:15.098385ZDownload61dc2387aeea13885a3429f11c5d88f410a59c8e9954044c768a554da69d4a4c
0.8.22019-11-16T17:47:44.166275ZDownload4f1217f14e43d46c91409dce53712177f7afb4424ba7052f6a0d62f7d0a0305c
0.8.32019-12-29T15:58:31.061423ZDownload411807a576669f6707c417935e731e5348e13f9eedfb6560b34dc232e1403fd6
0.9.02020-03-01T17:30:55.038990ZDownload84b7bb766128447374e524ed725a455193988855945bc2e99724c901376f1f2e
0.10.02020-03-02T22:44:36.244631ZDownload79bb2c69e7dc2d4b1105b101557b68a256a1c6f9bedb8ba3bd8720bdc59b5f61
0.10.12020-03-06T18:55:36.352844ZDownloadf862efcd027d422da060f6bb85aee129d108e84e4c92e355bde58c5f1c113a8d
0.11.02020-03-11T21:01:08.014565ZDownload575ab3688ee14f7704b83b697cd6ea22fe91a55dde7ecef8585f8db3f085f14d
0.11.12020-03-12T19:15:20.076435ZDownload5ea048981cb55ba0eed017489f54e64d696406f9db34ca63d2f7894cd3f7e32e
0.12.02020-03-16T13:32:17.272413ZDownload8b93a2858d70fe0a452652f893f7b9265084caade6fc6457e180ae9146d0bc7e
0.13.02020-03-24T20:01:03.075417ZDownloadc6391b80ecc8286bd1a3fa8d308fa9db53d78c9365c5ed04028e246c5f6c927c
0.13.12020-04-24T12:15:29.332125ZDownloadc48f9f5e96efbec443f17a9aff0f5967abb18d30c5f7838f8d8d45395d8b6bd5
0.13.22020-04-24T20:07:25.746303ZDownloadfb23273aa9e341aa9f70085791466f3ae2eeda0b1005364512d25199436f6aea
0.14.02020-04-30T21:13:02.731418ZDownload8eda74b3265a33bcfd9a4d432acc4727f2a602bfeb6e677ddddd95878223c255
0.15.02020-05-09T07:26:16.927216ZDownload0b8452f6960d0c167b11e18d3bb1dedb4e126f44cccb94b49b8dbaafccb81b80
0.16.02020-06-02T16:15:42.681833ZDownload3821b4a29b43189fef0af337c67a56d9caf2602e360cd888c1d419609cd770cb
0.16.12020-06-09T10:02:54.456251ZDownload92d94b691e4f5dab29aa939dd925aad1cdcfdfd9f50c5fb81e52663fa179e702
0.16.22020-06-10T20:22:43.008226ZDownloada1578783187f227a2779f68eeb60833d45b0a20c259b16524a5904b67ae42e4d
0.16.32020-07-13T12:01:33.990171ZDownload480a953bded3f7490d6ebe1d44acc853ba406a584e7c0030a692412f8217aeab
0.17.02020-07-15T20:30:54.684133ZDownloade087afc13af6cde948524c129f93d1c5875cf38d40033775eb6e7fce215858bc
0.18.02020-07-16T19:42:31.250028ZDownloadd1659c8b5a61cd01b5ada821afcf4dbc7032799e386503993cc72d0c7a4d2f18
0.19.02020-08-10T19:35:23.270589ZDownloadfa476f0a9b209b03e7751850d1760ca5d648d87514e7858dbbcc90968279426c
0.19.12020-08-18T20:49:33.901023ZDownloadf0a5815e272921d688032fddabe50091990c4d7efb2d4a20635955ec1fcac246
0.19.22020-09-12T14:45:15.540359ZDownload6ec2e77e93ed5947692f57eb2b8e3c6e93d72f37f0888d8c6bf7fbb829199745
0.19.32020-09-30T18:35:18.789660ZDownload548e327f8446a697e9a37a9e722d7894224528ceba882448e54d08fe7cb56326
0.19.42020-10-01T18:18:59.742503ZDownload9532ebbceaabc3407977fccd8c68b63e1387e782fbf218c9c89a0de7bb6e732e
0.20.02020-10-12T09:32:56.185497ZDownloadecef39ae7cb0789bf5a2f2ca1866b547a209b14df1449bc514ab85d614defb5b
0.20.12020-10-24T16:51:39.378330ZDownload9b1507fd2d33e644d260c0cbbb1e323346040f55855603a13dadd7bf4ad995fa
0.21.0-intl.nullsafety2021-01-03T17:06:40.866045ZDownload3baaed18086595be3f49603cf1450bace1fd6d369e240901409717d011dbd00a
0.21.02021-02-04T20:39:15.270189ZDownload5322101af1f6873d5b64a59832e861f371e05ccb46d96d14fabf947e68054f68
0.21.12021-03-01T18:28:08.831602ZDownload139750e7c5535ee9e90331d4cd64138b767bbb26000e699d5106cf3a5351ea63
0.22.02021-03-04T14:22:13.601224ZDownload9f2455cc84cccb0462b8e63514ee22f1b22bebadcc1df9ee2592d30d53cabd5a
0.22.12021-03-04T19:45:01.343111ZDownload0ff3150a2ff3fdd54bb5ec82fdfd880449243846bbe9a55c9f2edd3518420d8c
0.22.22021-03-04T21:57:43.767274ZDownload4bf68422a53cea26ed015bcb13a1620fac5e861cd5cdd4cad9ab87950c75d59b
0.22.32021-03-06T18:49:14.826855ZDownload94aed8693b37bf7f37a299d131c62d713b6ac516229d03f4afde4c8c3b20f07b
0.22.42021-06-19T07:13:59.653739ZDownloadae96d3f630390dc5590b6bafef373aff342f651da5d1d146b57ff858b833d5ad
0.30.02021-08-14T11:01:21.377254ZDownload5f2203e50eff6523c689566cdd93422a1f2c862ebc6ae9ee46f27b4c8b1b5418
0.31.02021-11-25T09:39:38.064342ZDownload454d2c4e446a39eed80988358ea60e6053a19947e7ce274681412d484556810d
0.31.12022-01-15T16:26:48.153911ZDownload3dc20ca94bf447ce2dbe365b914c9846e600e61fa4c96a08a1aa0f4f8a4ebcf8
0.32.02022-02-11T12:49:48.063881ZDownload3f498993ed0d185b04a573dbec52d6ecf0b420f878cf04b3ec2179c2a2ddc25b
0.32.12022-03-13T12:30:55.997174ZDownloadd0dbf49b68316d0e5e902e0e15dd18863995a1c71af14ab7941983a9ea6bac44
0.32.22022-03-30T07:41:58.419889ZDownload3eeb3d57d9c5f7e3079e9d35ebce60416ce52e4738d0d9563aedf8e5a469725c
0.32.32022-06-05T16:51:11.946632ZDownload143b397a1c58ef0fa5f37231136f1c5ed157142706eb38c44cbcac22ca58d269
0.32.42022-06-08T21:13:32.882435ZDownload5f453b2533939c20042902e0ecf12f0a6941e647f839b9add7a5f6c0a625af66
0.33.02023-05-11T06:47:40.838456ZDownloadb71fe887697686368c93e4d2257ecdb3c35fe38686a6fbf3902d6355c3f20363
0.34.02023-08-15T09:00:38.856101ZDownloadc7e9752c5dea38963bed26513c25a6e5db70c110fc2c68b84694aafa37b29636
0.35.02023-12-29T11:34:51.623582ZDownload091af6970be8036c37121b8a683bc392d0182786186ab8fe2cf20403aebdcfa6
0.35.12024-02-09T17:37:53.753646ZDownloadc823a9c9e32d2166d0ce8fa60b63459648a55f29a17c0d5dbf66fe4c9e05f1c2
0.36.02024-06-11T20:14:01.501110ZDownload0e88d479c11c4d3d46b2610fb7e94a13ac34fbf5d3adc265afb1645361ec5062
0.36.12024-07-06T14:56:19.137292ZDownloadeed5e5d26a66e2da7a2cbbc9a9c558d8469f58c907672334bbac30d7a74663bc
0.36.22024-07-10T20:20:50.137389ZDownload37334bfabd05655895dc0c90a38022e4e588eefb848ffc9d382f37439a44be46
0.36.32025-05-20T18:18:43.036545ZDownloadfe2bd740fe522d9dd791850baa6cba492f00c283f3c0b0012995d538ce66570c
0.37.02025-10-28T21:33:17.634937ZDownload9c2e4cbfe35db52c0071371202804551932a331a7ea31338c65ec133ea9f56b8
0.37.12025-12-18T18:00:34.741620ZDownloadaf7d625e4bfd04764d1554a6411d349dd62d4dc204105cc819a81a35a77bd9b6

Dependencies

flutter { "sdk": "flutter" }

flutter_localizations { "sdk": "flutter" }