UNPKG

mojito-rb-gen

Version:

Localization utility that converts .properties files into JS/JSON resource bundles

124 lines (85 loc) 3.44 kB
# mojito-rb-gen Localization utility that converts `.properties` files into `JSON`/`JS` resource bundles. # Install `npm install -g mojito-rb-gen` # Usage Each `.properties` files in the source directory will be converted into a `JSON` or `JS` resource bundle. One of the `.properties` file is the source resource bundle (by default `en.properties`, use `--source-bundle` to override) and contains the source strings that are usually modified during development. Other files contain the localized strings. The `JSON`/`JS` localized resource bundles are generated by merging the source strings with the translated strings from the `.properties` files. This ensure that the localized resource bundles contain all the strings required by the application even if the translations are not yet available. ``` Usage: mojito-rb-gen [OPTIONS] [ARGS] Options: -s, --source-directory [PATH] Source directory (Default is .) -o, --output-directory [PATH] Output directory (Default is .) -b, --source-boundle [STRING] Source bundle file (Default is en.properties) -n, --use-namespaces BOOLEAN Use namespaces when parsing properties files -t, --output-type [STRING] Output type: json, js (Default is json) -w, --watch BOOLEAN Watch the source directory for changes and rebuild resource bundles --js-variable [STRING] Varaible name used to generate Javascript file (Default is MESSAGES) -k, --no-color Omit color from output --debug Show debug information -h, --help Display help and usage details ``` # Merge logic With source bundle: `en.properties` ```properties # Group 1 Key 1 Comment group1.key1 = Group 1 Key 1 group1.key2 = Group 1 Key 2 group2.key1 = Group 1 Key 1 ``` and localized properties: `fr.properties` ```properties group1.key2 = Group 1 Key 2 (fr) outdated = Outdated ``` The merge output in `JSON` will be: ```json {"group1.key1":"Group 1 Key 1","group1.key2":"Group 1 Key 2 (fr)","group2.key1":"Group 2 Key 1","outdated":"Outdated"} ``` # Examples ## Output types ### JSON ``` sh $ mojito-rb-gen -s examples/src/ -o examples/out/json/ ``` ``` json {"group1.key1":"Group 1 Key 1","group1.key2":"Group 1 Key 2 (fr)","group2.key1":"Group 2 Key 1","outdated":"Outdated"} ``` ### Javascript ```sh $ mojito-rb-gen -s examples/src/ -o examples/out/js/default -t js ``` ```js MESSAGES = {"group1.key1":"Group 1 Key 1","group1.key2":"Group 1 Key 2 (fr)","group2.key1":"Group 2 Key 1","outdated":"Outdated"}; ``` ### Configure Javascript variable name ```sh $ mojito-rb-gen -s examples/src/ -o examples/out/js/var/ -t js --js-variable MY_MESSAGES ``` ```js MY_MESSAGES = {"group1.key1":"Group 1 Key 1","group1.key2":"Group 1 Key 2 (fr)","group2.key1":"Group 2 Key 1","outdated":"Outdated"}; ``` ### Use namespaces ```sh $ mojito-rb-gen -s examples/src/ -o examples/out/js/namespaces/ -t js --js-variable MY_MESSAGES_NS -n ``` ```js MY_MESSAGES_NS = {"group1":{"key1":"Group 1 Key 1","key2":"Group 1 Key 2 (fr)"},"group2":{"key1":"Group 2 Key 1"},"outdated":"Outdated"}; ``` ## Watch source directory for changes and rebuild resources bundles ```sh $ mojito-rb-gen -s examples/src/ -o out -w INFO: Start watching: examples/src/ INFO: en.properties changed, generate resource bundles ``` ### Use a different source bundle ```sh $ mojito-rb-gen -s examples/src/ -o out -b en-US.properties ```