This Bash script automates the translation of JSON files using the Google Cloud Translation API. It supports translating JSON files from a source language to multiple target languages, with intelligent file tracking to only process recently modified files.
- Bash shell
jq
command-line JSON processor- Google Cloud Translation API access
- A Google Cloud Project with Translation API enabled
Make sure you have the following tools installed:
- Bash
jq
(JSON processor)curl
(for API requests)
On Ubuntu/Debian:
sudo apt-get update
sudo apt-get install jq curl
On macOS (using Homebrew):
brew install jq curl
Create a .env
file in the same directory as the script with the following variables:
GOOGLE_PROJECT_ID=your-google-cloud-project-id
GOOGLE_API_KEY=your-google-cloud-api-key
SOURCE_LANGUAGE=en
TARGET_LANGUAGES=es,fr,de
GOOGLE_PROJECT_ID
: Your Google Cloud project IDGOOGLE_API_KEY
: API key with Translation API permissionsSOURCE_LANGUAGE
: The language code of the original JSON files (e.g., 'en' for English)TARGET_LANGUAGES
: Comma-separated list of target language codes
en
: Englishes
: Spanishde
: Germanfr
: Frenchpt
: Portugueseru
: Russianja
: Japaneseko
: Koreanit
: Italiannl
: Dutchzh-CN
: Chinese (Simplified)zh-TW
: Chinese (Traditional)pl
: Polishtr
: Turkishar
: Arabic
chmod +x translate.sh
- Place your JSON files in the same directory as the script.
- Run the script:
./translate.sh
- Identifies JSON files in the current directory
- Creates a source language directory and copies original files
- Finds files modified since the last script run
- Translates strings within each JSON file
- Creates translated files in language-specific directories
- Tracks the last run time to optimize translation process
-
Initial run creates:
en/
directory with original fileses/
,fr/
,de/
directories with translated files
-
Subsequent runs will only translate files modified since the last run
- Requires an active Google Cloud Translation API key
- Translates only string values in JSON files
- Preserves original JSON structure
- Uses a tracking file to optimize translation process
- Ensure
.env
file is correctly configured - Check Google Cloud API key permissions
- Verify
jq
andcurl
are installed - Check network connectivity to Google Translation API
- Relies on Google Cloud Translation API (may incur costs)
- Works best with simple JSON structures
- May not perfectly translate context-dependent strings
[Specify your license here]
Contributions are welcome! Please submit pull requests or open issues on the project repository.