json_localizations 0.1.0 copy "json_localizations: ^0.1.0" to clipboard
json_localizations: ^0.1.0 copied to clipboard

outdated

A minimal JSON localization package built on LocalizationsDelegate.

json_localizations #

A minimal JSON localization package for Flutter.

Use a JSON object / file per language to represent key / value pairs for localizing your app.

Also consider toml_localizations, yaml_localizations and csv_localizations.

Usage #

See example.

Install #

Add to your pubspec.yaml

dependencies:
  json_localizations:

Add a JSON file per language #

Add a JSON file per language you support in an asset path and describe it in your pubspec.yaml

flutter:
  assets:
    - {path}/{languageCode}.yaml

Or using a combination of language and country code

flutter:
  assets:
    - {path}/{languageCode-countryCode}.yaml

The JSON file name must match exactly the combination of language and country code described in supportedLocales.

That is Locale('en', 'US') must have a corresponding assetPath/en-US.json file.

Example JSON file
{
	"hello": "hello",
	"bye": "bye",
	"items": [ "one", "two", "three" ],
  "count": 1
}

Tip: You can store any json type given a key, like a string, an array of strings, or a number

MaterialApp #

Add JsonLocalizationsDelegate to MaterialApp and set supportedLocales using language/country codes.

MaterialApp(
  localizationsDelegates: [
    ... // global delegates
    JsonLocalizationsDelegate('assets/json_translations'),
  ],
  supportedLocales: [
    Locale('en'),
    Locale('nb'),
  ],
}

API #

Translate strings or a list of strings using value:

JsonLocalizations.of(context)?.value('hello')

We keep the API simple, but you can easily add an extension method to String like this:

extension LocalizedString on String {
  String tr(BuildContext context) => JsonLocalizations.of(context)!.value(this);
}

Note on iOS #

Add supported languages to ios/Runner/Info.plist as described here.

Example:

<key>CFBundleLocalizations</key>
<array>
	<string>en-GB</string>
	<string>en</string>
	<string>nb</string>
</array>
2
likes
0
pub points
49%
popularity

Publisher

verified publisherapptakk.com

A minimal JSON localization package built on LocalizationsDelegate.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on json_localizations