A generation tool that builds a class to get your json locale paths from dart code.
Getting started
- Add dependencies in your
pubspec.yaml
:
dev_dependencies:
json_locale_generator: ^0.0.3
build_runner: ^2.0.4
- Configure the json files to generate code for in your
pubspec.yaml
:
json_to_dart:
sample_files:
# The file assets/en.json will be converted to dart code and values can be accessed via the Locale class
- source: assets/en.json
class_name: Locale
- source: assets/other.json
class_name: Other
- Execute
[dart|flutter] pub run build_runner build
or[dart|flutter] pub run json_locale_generator:generate
to run only json_locale_generator without other eventual builder packages. The filejsons.dart
will be generated intolib/jsons.dart
- Import
jsons.dart
and start using it:
import 'jsons.dart';
FlutterI18n.translate(context, Locale.core.app_title);
Features
Converts a json such as
{
"ok": "OK",
"core": {
"app_name": "WordSing Battle"
},
"home": {
"title": "No card in the deck"
}
}
To dart code where each path can be accessed easily.
For above example, doing GeneratedClass.core.app_name
will return the String 'core.app_name'