Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jokester committed Oct 28, 2024
1 parent eaf5644 commit c632dc7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 44 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ babel-update-mo:
venv/bin/pybabel compile -d app/translations

babel-translate-po:
venv/bin/python app/scripts/fill_zh_translations.py
venv/bin/python app/scripts/fill_zh_translations.py
venv/bin/python app/scripts/fill_en_translations.py
66 changes: 66 additions & 0 deletions app/scripts/fill_en_translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import dotenv
from babel.messages.pofile import read_po, write_po
import openai
import logging

dotenv.load_dotenv()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

client = openai.Client()


def translate_text(text: str, target_language: str):
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": f"""You are a skillful multilanguage translatior specialized in UI i18n. Please translate the following text to {target_language}: {text}. Please only return the translated text and nothing else.""",
}
],
max_tokens=1000,
temperature=0,
)
return response.choices[0].message.content


def parse_and_translate_po(file_path: str, target_language: str, limit=0):
# Read the .po file
with open(file_path, "rb") as po_file:
catalog = read_po(po_file)

print(f"Translating {len(catalog)} messages to {target_language}")

translated_count = 0

# Translate each message
for message in catalog:
# print("message:", message.id, message.string)
if message.id and not message.string:
print(f"Translating '{message.id}'")
try:
translated_text = translate_text(str(message.id), target_language)
message.string = translated_text
logger.info(f"Translated '{message.id}' to '{message.string}'")
translated_count += 1
except Exception:
break
else:
print(f"Skipping '{message.id}'")
if limit and translated_count >= limit:
break

# Write the updated catalog back to the .po file
with open(file_path, "wb") as po_file:
write_po(po_file, catalog)


def po_path_for_language(language_code):
return f"app/translations/{language_code}/LC_MESSAGES/messages.po"


if __name__ == "__main__":
for lang in ["en"]:
po_file_path = po_path_for_language(lang)
parse_and_translate_po(po_file_path, lang, limit=500)
38 changes: 0 additions & 38 deletions app/scripts/fill_translation.py

This file was deleted.

10 changes: 5 additions & 5 deletions app/translations/en/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ msgstr "Moved"

#: app/apis/file.py:228
msgid "您没有权限修改文件名"
msgstr ""
msgstr "You do not have permission to modify the file name."

#: app/apis/file.py:230
msgid "改名成功"
Expand Down Expand Up @@ -109,19 +109,19 @@ msgstr "Complete"

#: app/apis/invitation.py:156
msgid "只能修改邀请角色等级比您低的邀请"
msgstr ""
msgstr "You can only modify invitations for roles lower than your own."

#: app/apis/invitation.py:163 app/models/user.py:383
msgid "邀请的角色等级需要比您低"
msgstr ""
msgstr "The role level of the invitee needs to be lower than yours."

#: app/apis/invitation.py:234
msgid "只能删除邀请角色等级比您低的邀请"
msgstr ""
msgstr "You can only delete invitations for roles that are lower than your level."

#: app/apis/me.py:180
msgid "修改成功,请重新登陆"
msgstr ""
msgstr "Modification successful, please log in again."

#: app/apis/project.py:125
msgid "完结项目成功"
Expand Down

0 comments on commit c632dc7

Please sign in to comment.