mirror of
https://github.com/knadh/listmonk.git
synced 2025-11-20 17:52:49 +08:00
Add i18n translation helper script.
This commit is contained in:
parent
81d05e4dd6
commit
deb41f8d1c
1 changed files with 55 additions and 0 deletions
55
scripts/translate-i18n.py
Normal file
55
scripts/translate-i18n.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import os
|
||||
import json
|
||||
from glob import glob
|
||||
from openai import OpenAI
|
||||
|
||||
client = OpenAI(
|
||||
# This is the default and can be omitted
|
||||
api_key=os.environ.get("OPENAI_API_KEY")
|
||||
)
|
||||
|
||||
# Keys to translate. If this is empty, all keys are translated.
|
||||
KEYS = []
|
||||
|
||||
DEFAULT_LANG = "en.json"
|
||||
DIR = os.path.normpath(os.path.join(
|
||||
os.path.dirname(os.path.abspath(__file__)), "../i18n"))
|
||||
BASE = json.loads(open(os.path.join(DIR, DEFAULT_LANG), "r").read())
|
||||
|
||||
|
||||
def translate(data, lang):
|
||||
completion = client.chat.completions.create(
|
||||
model="gpt-3.5-turbo-16k",
|
||||
messages=[
|
||||
{"role": "system", "content": "You are an i18n language pack translator for listmonk, a mailing list manager. Remember that context when translating."},
|
||||
{"role": "user",
|
||||
"content": "Translate the untranslated English strings in the following JSON language map to {}. Retain any technical terms or acronyms.".format(lang)},
|
||||
{"role": "user", "content": json.dumps(data)}
|
||||
# {"role": "user", "content": "Hello world good morning!"}
|
||||
]
|
||||
)
|
||||
|
||||
return json.loads(str(completion.choices[0].message.content))
|
||||
|
||||
|
||||
# Go through every i18n file.
|
||||
for f in glob(os.path.join(DIR, "*.json")):
|
||||
if os.path.basename(f) == DEFAULT_LANG:
|
||||
continue
|
||||
|
||||
print(os.path.basename(f))
|
||||
|
||||
data = json.loads(open(f, "r").read())
|
||||
|
||||
# Diff the entire file or only given keys.
|
||||
if KEYS:
|
||||
diff = {k: BASE[k] for k in KEYS}
|
||||
else:
|
||||
diff = {k: v for k, v in data.items() if BASE.get(k) == v}
|
||||
|
||||
new = translate(diff, data["_.name"])
|
||||
data.update(new)
|
||||
|
||||
with open(f, "w") as o:
|
||||
o.write(json.dumps(data, sort_keys=True,
|
||||
indent=4, ensure_ascii=False) + "\n")
|
||||
Loading…
Add table
Reference in a new issue