This Python script utilizes the OpenAI API to translate a JSON file into multiple target languages. To use the script, you will need to have an OpenAI API key and specify the path to the source JSON file.
To set up the script, you will need to create a config.py
file in the same directory as the script. In this file, you should define your OpenAI API key as a string variable API_KEY
. Additionally, you can define an optional string variable SOURCE_PATH
to specify the path of the source JSON file. If SOURCE_PATH
is not defined, the script will prompt you to enter the path when you run it.
API_KEY = "your_api_key_here"
SOURCE_PATH = "path_to_source_json_file_here" # Optional
Note: This script uses OpenAI's GPT-3.5-Turbo model for translation, which may incur costs if you exceed the monthly free tier limit. Please refer to OpenAI's pricing page for more information on pricing. During my tests, converting a JSON file of 25 lines in all 40 the languages of the script costed about 0.07 USD.
This script requires the following dependencies to be installed:
openai
(installation instructions can be found here)concurrent.futures
(should be included in Python standard library)
To run the script, save it as json_translator.py
and run the following command in the terminal:
python json_translator.py
Follow the prompts to enter the path to the input JSON file (if SOURCE_PATH
is not defined in config.py
). If you need more languages or remove some, just edit the array languages
in the script.
The translated JSON files will be saved in the same directory as the input file with the language code as the filename extension (e.g. it-IT.json
, en-US.json
, etc.). If a translated file already exists, the script will only translate the missing lines, saving time and API tokens.
This script is particularly useful in combination with the Flutter localization library easy_localization, which allows you to easily add translations to your Flutter app. Simply use the translated JSON files generated by this script as your localization files, and you can easily add support for multiple languages in your app.
This script is released under the MIT License. See the LICENSE file for more information.