Skip to main content

json-translate

CLI tool to translate JSON files automatically using external translation services like DeepL or AWS Translate

License Release Open issues Open PRs Contributors Commits

Normally, multilingual projects base their translations on JSON files with different levels of nesting. This small project makes it easy to generate new files for new languages while keeping the same structure.

json_translate deepl /path/to/input-file.json FR

# Input file:                    Output file:
# {                              {
#   "welcome": "Welcome!",         "welcome": "Bienvenue!",
#   "questions": {                 "questions": {
#     "sure": "Are you sure?"        "sure": "Êtes-vous sûr?"
#    }                             }
# }                              }

The supported languages depends on the service used:

Install

  1. Install dependencies:
    pip install json-translate
    
  2. To define the environmental variables, create an .env file in the root directory with the required variables, which will vary depending on the external service you will use:
    EXAMPLE_VARIABLE_NAME=example_variable_value
    

    You can also define them directly in your environment without having to create the .env file, it's up to you.

Use with DeepL

You can use DeepL with a free account or with a paid account.

  • Free account: You can get a free deepl developer account in https://www.deepl.com/pro-checkout/account (Credit card needed). Once you have registered, you must include your auth key in the .env file:

    DEEPL_AUTH_KEY=your-key-here
    
  • Paid account: If you have a paid account, in addition to including your auth key, you must include the API endpoint in the file.

    DEEPL_AUTH_KEY=your-key-here
    DEEPL_API_ENDPOINT=https://api.deepl.com/v2/translate
    

Use with AWS Translate

Login on your AWS account and obtain your credentials and place them in the .env file:

AWS_REGION_NAME=your-aws-region
AWS_ACCESS_KEY_ID=your-aws-access-key
AWS_SECRET_ACCESS_KEY=your-aws-secret

How to use

Execute the command with the service, the file path and the language you want to generate:

json_translate <service> <source> <locale>
# Example: json_translate aws locales/en.json ES

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

Required parameters

service               Translation service to use. Can be "deepl" or "aws"
source                Path to source file (must be a json file)
target_locale         Translation target language code

Optional parameters

-sl, --source-locale  Source language translating from (Required for AWS).
-o, --output          Output file name. Defaults to "<target_locale>.json" (ex: en.json)
-e, --extend          Extend the existing translation file (defined by <output>)
-s, --sleep           Sleep time between API calls. Defaults to 0.01 (seconds)
-i, --indent          Output file indentation spaces. Defaults to 2
--encoding            Input & output file encoding. Defaults to UTF-8
--skip                Keys to skip in the json file (they won't be translated)
--log                 Display translations as they are being translated
--override            Force override on output file

DeepL options

-g, --glossary        Glossary ID to use when translating (Optional)

AWS Translate options

--formality           Set formality level to FORMAL
--profanity           Mask profane words and phrases

Note that sleep, indentation and encoding can also be defined with variables in the .env file but they are overwritten with the values of the command:

SLEEP_BETWEEN_API_CALLS=0.01
INDENTATION_DEFAULT=2
ENCODING=utf-8

Example file

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

json_translate deepl tests/data/en_US.json ES --skip lorem ipsum --log

DeepL 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

Contributing

See the contributing guide for detailed instructions on how to get started.

License

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

Metadata

Release files for json-translate 0.4.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-translate 0.4.4
File Size Uploaded Provenance Actions
json_translate-0.4.4.tar.gz 21.4 kB Without provenance Details for json_translate-0.4.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for json-translate 0.4.4
File Interpreter ABI Platform Actions
json_translate-0.4.4-py3-none-any.whl Python 3 none any Details for json_translate-0.4.4-py3-none-any.whl

Total release size: 44.5 kB

Release files / json_translate-0.4.4.tar.gz

Download URL json_translate-0.4.4.tar.gz
Size 21.4 kB
Tags Source
SHA-256 checksum
How to use checksums
db6e5bef718e0f1108e06325c0f22c7008918dbed1aabd019b9f39f65a653e85
BLAKE2b-256 checksum
How to use checksums
a3d0f2c53d9145ea50d542cf3c5ae1e425dfe9a2b1fe468c2301048298989640
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / json_translate-0.4.4-py3-none-any.whl

Download URL json_translate-0.4.4-py3-none-any.whl
Size 23.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6d8658e9e7c40b9e285f3971c2dbdb2e03172a84e23ba46a367500109fccec74
BLAKE2b-256 checksum
How to use checksums
c13e76defae3b6b31576e276c7eb79f4dfb51cdd045c67a6e75a3fe9cca52dc1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Security for json-translate 0.4.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page