/json-deepl-translate

Translate json files with deepl API

Primary LanguagePythonGNU Lesser General Public License v2.1LGPL-2.1

json-deepl-translate

Translate json files with deepl API

Usually multi-language projects developed with a javascript framework (Angular, Vue...) base their translations on json files with different nesting levels. This small project allows you to generate new files for other languages while keeping the same structure.

  • Works with Python3
python main.py /path/to/file/en_US.json --locale es --output es_ES.json --indent 4 --sleep 0.5

See more options with python main.py --help

Install

  1. Create virtual environment

  2. Install dependencies:

pip install -r requirements.txt
  1. Create an .env file with:
DEEPL_AUTH_KEY=your-key-here

You can get a free deepl developer account in https://www.deepl.com/pro-checkout/account (Credit card needed)

Usage

  1. Execute the command with the file path and the language you want to generate
python main.py /home/user/my_project/locales/en_US.json --locale ES --output es_ES.json

The script will create an es_ES.json file in the same folder as the source file.

Optional parameters

-l, --locale    Language target to translate. Defaults to "en"
-o, --output    Output file name. Defaults to "en.json
-s, --sleep     Sleep time between API calls. Defaults to 0.01s
-i, --indent    Output file indentation spaces. Defaults to 2
--skip          Keys to skip (they won't be translated)
--keep          Keys to keep (they will be not changed from existing value)
--cache         Uses a cache file name to prevent re-requesting to API. Defaults to ".cache/[locale].json"
--override      Override existing file witout prompting

Example file

Translate the example file /tests/data/en_US.json to spanish:

python main.py tests/data/en_US.json --locale ES --output es_ES.json --skip lorem ipsum

API usage

You can check your API usage with

curl -H "Authorization: DeepL-Auth-Key YOUR-API-KEY-HERE" https://api-free.deepl.com/v2/usage

Tests

Run tests with:

python -m unittest discover

License

This repository is available under GNU LESSER GENERAL PUBLIC LICENSE v2.1 (LGPL). See details.