This repository helps users to make localized json file(to 102 countries language based on google translator API) from a json file.
The countries available for localization are listed in ./src/lang_code.js
. You can decide which countries you want to translate in the file by adding countries of langsExplain
object to langsToTranslate
array in ./src/edit-lang-coverage.js
. The countries list is referenced here.
- Linux like OS
- Node.js
This repo is only working on Linux os based machine. On Windows with terminal it may not work because some bash commands will not work on Windows OS which don't have. Node.js should be installed before to run.
After download this repository, in the directory node modules should be installed. Type
npm install
- Install node modules by typing
npm install
at the repository directory./
. - Open
./src/lang_code.js
and./src/edit-lang-coverage.js
. Add countries to thelangsToTranslate
array in./src/edit-lang-coverage.js
from thelangsExplain
object at./src/lang_code.js
. The countries withinlangsToTranslate
array will be the countries you want to target for translating. LeavinglangsToTranslate
empty will translate to every langauge by default. - Then execute
exe.sh <json_file_you_to_localize>
file by typing./exe.sh test.json
at the repositories./
directory on terminal.
Here is already an example .json file, so we can execute translating without preparing .json file. local_obj.json
is an example json file to test the repository. Type for example,
./exe local_obj.json
The file is at ./src
. You can add,
// ./src/edit-lang-coverage.js
const langsToTranslate = [langsExplain.fr, langsExplain.ko /*add more languages from langsExplain here*/]
or you can leave langsToTranslate
empty to translate all langauges,
// ./src/edit-lang-coverage.js
const langsToTranslate = []
You can localize multiple josn files.
- Execute
exe.sh <json_file_you_to_localize1> <json_file_you_to_localize2>
file by typing./exe.sh test1.json test2.json
on current directory on terminal.
It localize texts without html tag files. Also texts inside of more than 2 tags can be localized.
Short briefing orders the program executed.
- Command takes into
exe.sh
.exe.sh
executeindex.js
file.
- ex)
./exe.sh local_obj.json
- .json file is parsed at
index.js
. Parsed json is like,{"key": {"key1": "value"}}
- Each key and object is deep copied into
srcObj
byaddStr
function to scan what to regex. - With the object at 3, each value is added into a string
srcStr
bymakeyKeyPathReturnSrc
function. Also each key is pushed intokeyArr
. Array of key is like, `['key/key1', 'key/key2'] - With keys array at 4, each set is made into an object.
- ex)
{"key/key1" : value}
- With the string
srcStr
at 4, translate module translates it.
- ex)
'frenchValue1 \n frenchValue2'
- With the translated string at 6, it will splited into an array by line feed. For this array, the orders of it are same to orders of
keyArr
at 4.
- ex)
['frenchValue1', 'frenchValue2']
- With each line at 6, it replaces each value of the object at 5 by
objValWithKeyPath
function and make new object. In case of complicated example,frenchValue2
will be omitted andfrenchValue1
will be replaced tofrenchValue
.
- ex)
{"key/key1" : frenchValue}
- With the object at 8, its each key will read each one of the object at 2 and replace each value to translated one by
putStrIn
function.
- ex)
{"key": {"key1": "frenchValue"}}
- With the object at 9, new json file is written.
By orders above, each json key and value are splited and rejoined. It is because for avoiding request traffic error of google translate module. Whenever translating each values with short term like 0.01 seconds, the google server shuts down the requests from same IP. So all values are joined as one string and translated and then splited.