GithubHelp home page GithubHelp logo

oldaczom / translate-json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yxor/translate-json

0.0 0.0 0.0 14 KB

📓 Translate string values in a JSON document using the Google Cloud Translation API

License: MIT License

Shell 0.78% Python 99.22%

translate-json's Introduction

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.

translate-json's People

Contributors

yxor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.