npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-i18n-whisper

v3.0.0

Published

A package to translate JSON files using Google Translate API

Downloads

485

Readme

json-i18n-whisper

A Node.js package to translate JSON files using Google Cloud Translate API. This tool helps you quickly translate your JSON language files into multiple languages, making internationalization of your projects easier.

Translation Process

Features

  • Translates JSON files to multiple languages simultaneously
  • Supports nested JSON structures
  • Estimates translation cost before processing
  • Uses Google Cloud Translate API for accurate translations
  • Handles special cases for languages like Hebrew and Arabic

This package is part of Safe Deal localization codebase. For a wise & fast shopping experience, check out Safe Deal. Learn more about Safe Deal here.

Installation

You can install this package globally using npm or yarn:

npm install -g json-i18n-whisper
# or
yarn global add json-i18n-whisper

Prerequisites

  • A Google Cloud Platform account with the Translate API enabled
  • A Google Cloud API key with access to the Translate API

Usage

Command Line Interface

You can run the script using the following command:

translate-json <inputLang> <targetLangs> [apiKey]
  • <inputLang>: The source language code (e.g., 'en' for English)
  • <targetLangs>: Comma-separated list of target language codes (e.g., 'fr,es,de')
  • [apiKey]: (Optional) Your Google Cloud Translate API key. If not provided, the script will look for the GOOGLE_TRANSLATE_API_KEY environment variable.

Examples:

  1. Using an environment variable:

    export GOOGLE_TRANSLATE_API_KEY=your_api_key_here
    translate-json en fr,es,de
  2. Providing the API key as an argument:

    translate-json en fr,es,de your_api_key_here

This will translate the en.json file to French, Spanish, and German, creating fr.json, es.json, and de.json files.

Importing the Function Directly

You can also import the translateJson function directly in your Node.js code:

const translateJson = require("json-i18n-whisper");

translateJson("en", "fr,es", "your_api_key_here");

How it works

  1. The tool reads the input JSON file (e.g., en.json)
  2. It estimates the translation cost based on the text length and number of target languages
  3. The JSON is recursively traversed, and all string values are collected for translation
  4. Translations are performed in batches using the Google Cloud Translate API, which is chosen for its superior quality and speed compared to GPT-4o and other LLM or translation services.
  5. The translated strings are then reassembled into the original JSON structure
  6. Separate output files are created for each target language

Cost Estimation

Before translation begins, the tool provides an estimated cost based on the current Google Cloud Translate API pricing. This helps you make an informed decision before proceeding with large translations.

Notes

  • This package exclusively uses the Google Cloud Translate API. Make sure you have the necessary API key and billing set up in your Google Cloud Console.
  • The actual cost may vary slightly from the estimate due to factors like text complexity and API pricing changes.
  • For languages like Hebrew, Arabic, Persian, and Urdu, diacritical marks are automatically removed from the translations to improve readability.

Testing

This package uses Jest for testing. To run the tests, follow these steps:

npm install
# or
yarn
  1. Run the tests:
npm test
# or
yarn test

The test suite includes unit tests for core functions and integration tests that mock the Google Cloud Translate API responses.

License

MIT