Last active
May 31, 2024 14:37
-
-
Save stracker-phil/d1d874a299883ff540966a402793f616 to your computer and use it in GitHub Desktop.
Python script to translate a GlotPress JSON export to another language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
import os | |
from openai import OpenAI | |
# URL: https://gist.github.com/stracker-phil/d1d874a299883ff540966a402793f616 | |
# | |
# Usage: | |
# 1. Download the current translations as JSON format from the internal GlotPress site. | |
# 2. Adjust the settings below | |
# - Insert your OpenAI API key | |
# - Specify the input- and output files | |
# - Customize the destination language details | |
# 3. Set PREVIEW to True to review the first 10 translations in the console. | |
# 4. When results are as expected, set PREVIEW to False and run again. | |
# 5. Review the output file in a text editor and adjust typos, if needed. | |
# 6. Iport the final JSON file to the internal GlotPress site. | |
# ---------------------------------------------------------------------------- | |
openai_api_key = "sk-proj-..." | |
input_file = os.path.expanduser("~/Downloads/glotpress-export.json") | |
output_file = os.path.expanduser("~/Downloads/ai-translated.json") | |
short_lang = "DE" | |
full_lang = "informal German" | |
tonality_change = "from formal to informal" | |
tonylity_sample = '"Sie" with "du"' | |
PREVIEW = False | |
ai_model = "gpt-4o" # Recommended: Either "gpt-3.5" or "gpt-4o" | |
# ---------------------------------------------------------------------------- | |
translator_role = f""" | |
Role: Act as a professional translator, specialized in translating WordPress plugins from English to {full_lang}. | |
Task: Translate the provided user input to {full_lang}. | |
Process: | |
1. The user will provide an english input string. | |
2. Translate this string to {full_lang}. | |
3. Pay attention to correct case, grammar and preserve eventual placeholders. | |
Output: Reply with the translated string and nothing else. No quotes, wrappers, descriptions. | |
""" | |
adjustor_role = f""" | |
Role: Act as a professional translator, specialized in translating WordPress plugins to {full_lang}. | |
Task: Adjust the tonality of the user input to {full_lang}. | |
Process: | |
1. The user will provide a string that might have a wrong tonality. | |
2. Only adjust the tonality {tonality_change}, eg. by replacing {tonylity_sample}. | |
3. Preserve all other aspects, like the sentence structure, other words, eventual placeholders. | |
Output: Reply with the adjusted string and nothing else. No quotes, wrappers, descriptions. | |
""" | |
ai_client = OpenAI( | |
api_key = os.environ.get("OPENAI_API_KEY", openai_api_key), | |
) | |
def load_translations(file: str): | |
""" | |
Load the JSON data from the file and return them as object. | |
""" | |
with open(file, 'r', encoding='utf-8') as f: | |
data = json.load(f) | |
# PREVIEW: Only return 10 items. | |
if PREVIEW: | |
data = dict(list(data.items())[:10]) | |
return data | |
def save_translations(file: str, data): | |
""" | |
Save the modified data to a new file. | |
""" | |
with open(file, 'w', encoding='utf-8') as f: | |
json.dump(data, f, ensure_ascii=False, indent=4) | |
def translate_text(english, orig_tonality): | |
""" | |
Use the OpenAI API to translate text from English. | |
""" | |
if orig_tonality: | |
prompt = orig_tonality | |
role = adjustor_role | |
else: | |
prompt = english | |
role = translator_role | |
response = ai_client.chat.completions.create( | |
model = ai_model, | |
messages = [ | |
{ "role": "system", "content": role }, | |
{ "role": "user", "content": prompt } | |
] | |
) | |
resp_choice = response.choices[0] | |
translation = resp_choice.message.content.strip() | |
print(f"\n-> {prompt}\n<- {translation}") | |
return translation | |
def modify_translations(data): | |
""" | |
Modify/fix the translations by translating using the OpenAI API. | |
""" | |
modified_data = {key: [translate_text(key, value[0])] for key, value in data.items()} | |
return modified_data | |
if __name__ == "__main__": | |
original = load_translations(input_file) | |
modified = modify_translations(original) | |
save_translations(output_file, modified) | |
print(f"\nAll done ✔︎\nNext:\n- Review the results in {output_file}\n- Import the translations to GlotPress") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment