Skip to main content

Stars Forks Issues MIT License

Translate JSON

About The Project

This is a command line tool to translate all string values in a JSON file to multiple languages using the Google Cloud Translate API.

Getting Started

Prerequisites

To run this program you need to have python 3.9 installed on your machine.

Installation

  1. To use this you need to have a google translate API key
  2. Install the package
    pip install translate-json
    

Usage

You can use this program as a command line tool.

usage: translate-json [-h] -f FILE -s SOURCE_LANGUAGE [-o OUT] -l LANGS [LANGS ...] -k KEY

Translate all string values in a JSON file to multiple languages using the google translate API.

optional arguments:
  -h, --help            show this help message and exit
  -f FILE, --file FILE  The input file path
  -s SOURCE_LANGUAGE, --source-language SOURCE_LANGUAGE
                        The source language code
  -o OUT, --out OUT     The output directory path, will contain the created translation files
  -l LANGS [LANGS ...], --langs LANGS [LANGS ...]
                        List of target languages you want to translate the file to
  -k KEY, --key KEY     Google translate API key, don't set this if you already have a key set as the environement
                        variable "GOOGLE_TRANSLATE_TOKEN"

CLI Example

A file called source.json contains the following values

{
   "title": "Hello World",
   "tags": {
      "lara": ["Video games", "swimming", "Interrupt routines"],
      "david": [
         {
            "value": "Baseball",
            "count": 5
         },
         "Climbing trees"
      ]
   },
   "example": {
      "title": "This will be translated",
      "ok": true
   }
}

To translate this file to dutch and french, you can run this command

translate-json -f source.json -s en -o ./translations/ -l nl fr -k YOUR_API_KEY

After running the command, the translations folder will contain two files: source.nl.json and source.fr.json.

{
   "title": "Bonjour le monde",
   "tags": {
      "lara": [
         "Jeux vidéo",
         "la natation",
         "Routines d'interruption"
      ],
      "david": [
         {
               "value": "Base-ball",
               "count": 5
         },
         "Grimper aux arbres"
      ]
   },
   "example": {
      "title": "Cela sera traduit",
      "ok": true
   }
}
{
    "title": "Hallo Wereld",
    "tags": {
        "lara": [
            "Computerspellen",
            "zwemmen",
            "Routines onderbreken"
        ],
        "david": [
            {
                "value": "Basketbal",
                "count": 5
            },
            "Bomen klimmen"
        ]
    },
    "example": {
        "title": "Dit wordt vertaald",
        "ok": true
    }
}

Example

You can also import this library and use it in your code

from translate_json.translate import translate_all


if __name__=="__main__":
   # you must set the google cloud translate API key as an environment variable before running this program
   translate_all('source.json', 'en', ['nl', 'de', 'fr'], './dist/')

Contributing

Any contributions you make are greatly appreciated

License

Distributed under the MIT License. See LICENSE for more information.

Metadata

Release files for translate-json 0.0.2

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

Source distribution (sdist)

Source distribution for translate-json 0.0.2
File Size Uploaded Provenance Actions
translate_json-0.0.2.tar.gz 7.1 kB Without provenance Details for translate_json-0.0.2.tar.gz

Built distribution (wheel)

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

Total release size: 14.1 kB

Release files / translate_json-0.0.2.tar.gz

Download URL translate_json-0.0.2.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a5766e89aa0db29272df88a1cfbfd20478a306841f42cc6a441084d3723667fa
BLAKE2b-256 checksum
How to use checksums
598043b6728c6b828500840363b2269eaf1f3115ef4f454f0f7622b8ac52745e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

Release files / translate_json-0.0.2-py3-none-any.whl

Download URL translate_json-0.0.2-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
176839385833a2a410c73211b6a77fe27014abc4a7eae73860c44a8a95f53e56
BLAKE2b-256 checksum
How to use checksums
9ed7d61e2a168bcab67b1ff72e654c7b6a7f023081971e6a612762304d1b16f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

Security for translate-json 0.0.2

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.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

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