IQ.Pilot Release Commit @ 0798119
This commit is contained in:
3
selfdrive/ui/translations/README.md
Normal file
3
selfdrive/ui/translations/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Multilanguage
|
||||
|
||||
[](#)
|
||||
1130
selfdrive/ui/translations/app.pot
Normal file
1130
selfdrive/ui/translations/app.pot
Normal file
File diff suppressed because it is too large
Load Diff
1218
selfdrive/ui/translations/app_ar.po
Normal file
1218
selfdrive/ui/translations/app_ar.po
Normal file
File diff suppressed because it is too large
Load Diff
1221
selfdrive/ui/translations/app_de.po
Normal file
1221
selfdrive/ui/translations/app_de.po
Normal file
File diff suppressed because it is too large
Load Diff
1207
selfdrive/ui/translations/app_en.po
Normal file
1207
selfdrive/ui/translations/app_en.po
Normal file
File diff suppressed because it is too large
Load Diff
1225
selfdrive/ui/translations/app_es.po
Normal file
1225
selfdrive/ui/translations/app_es.po
Normal file
File diff suppressed because it is too large
Load Diff
1236
selfdrive/ui/translations/app_fr.po
Normal file
1236
selfdrive/ui/translations/app_fr.po
Normal file
File diff suppressed because it is too large
Load Diff
1197
selfdrive/ui/translations/app_ja.po
Normal file
1197
selfdrive/ui/translations/app_ja.po
Normal file
File diff suppressed because it is too large
Load Diff
1190
selfdrive/ui/translations/app_ko.po
Normal file
1190
selfdrive/ui/translations/app_ko.po
Normal file
File diff suppressed because it is too large
Load Diff
1220
selfdrive/ui/translations/app_pt-BR.po
Normal file
1220
selfdrive/ui/translations/app_pt-BR.po
Normal file
File diff suppressed because it is too large
Load Diff
1129
selfdrive/ui/translations/app_th.po
Normal file
1129
selfdrive/ui/translations/app_th.po
Normal file
File diff suppressed because it is too large
Load Diff
1210
selfdrive/ui/translations/app_tr.po
Normal file
1210
selfdrive/ui/translations/app_tr.po
Normal file
File diff suppressed because it is too large
Load Diff
1258
selfdrive/ui/translations/app_uk.po
Normal file
1258
selfdrive/ui/translations/app_uk.po
Normal file
File diff suppressed because it is too large
Load Diff
1174
selfdrive/ui/translations/app_zh-CHS.po
Normal file
1174
selfdrive/ui/translations/app_zh-CHS.po
Normal file
File diff suppressed because it is too large
Load Diff
1173
selfdrive/ui/translations/app_zh-CHT.po
Normal file
1173
selfdrive/ui/translations/app_zh-CHT.po
Normal file
File diff suppressed because it is too large
Load Diff
138
selfdrive/ui/translations/auto_translate.py
Executable file
138
selfdrive/ui/translations/auto_translate.py
Executable file
@@ -0,0 +1,138 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import xml.etree.ElementTree as ET
|
||||
from typing import cast
|
||||
|
||||
import requests
|
||||
|
||||
TRANSLATIONS_DIR = pathlib.Path(__file__).resolve().parent
|
||||
TRANSLATIONS_LANGUAGES = TRANSLATIONS_DIR / "languages.json"
|
||||
|
||||
OPENAI_MODEL = "gpt-4"
|
||||
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
||||
OPENAI_PROMPT = "You are a professional translator from English to {language} (ISO 639 language code). " + \
|
||||
"The following sentence or word is in the GUI of a software called openpilot, translate it accordingly."
|
||||
|
||||
|
||||
def get_language_files(languages: list[str] | None = None) -> dict[str, pathlib.Path]:
|
||||
files = {}
|
||||
|
||||
with open(TRANSLATIONS_LANGUAGES) as fp:
|
||||
language_dict = json.load(fp)
|
||||
|
||||
for filename in language_dict.values():
|
||||
path = TRANSLATIONS_DIR / f"{filename}.ts"
|
||||
language = path.stem
|
||||
|
||||
if languages is None or language in languages:
|
||||
files[language] = path
|
||||
|
||||
return files
|
||||
|
||||
|
||||
def translate_phrase(text: str, language: str) -> str:
|
||||
response = requests.post(
|
||||
"https://api.openai.com/v1/chat/completions",
|
||||
json={
|
||||
"model": OPENAI_MODEL,
|
||||
"messages": [
|
||||
{
|
||||
"role": "system",
|
||||
"content": OPENAI_PROMPT.format(language=language),
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": text,
|
||||
},
|
||||
],
|
||||
"temperature": 0.8,
|
||||
"max_tokens": 1024,
|
||||
"top_p": 1,
|
||||
},
|
||||
headers={
|
||||
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
)
|
||||
|
||||
if 400 <= response.status_code < 600:
|
||||
raise requests.HTTPError(f'Error {response.status_code}: {response.json()}', response=response)
|
||||
|
||||
data = response.json()
|
||||
|
||||
return cast(str, data["choices"][0]["message"]["content"])
|
||||
|
||||
|
||||
def translate_file(path: pathlib.Path, language: str, all_: bool) -> None:
|
||||
tree = ET.parse(path)
|
||||
|
||||
root = tree.getroot()
|
||||
|
||||
for context in root.findall("./context"):
|
||||
name = context.find("name")
|
||||
if name is None:
|
||||
raise ValueError("name not found")
|
||||
|
||||
print(f"Context: {name.text}")
|
||||
|
||||
for message in context.findall("./message"):
|
||||
source = message.find("source")
|
||||
translation = message.find("translation")
|
||||
|
||||
if source is None or translation is None:
|
||||
raise ValueError("source or translation not found")
|
||||
|
||||
if not all_ and translation.attrib.get("type") != "unfinished":
|
||||
continue
|
||||
|
||||
llm_translation = translate_phrase(cast(str, source.text), language)
|
||||
|
||||
print(f"Source: {source.text}\n" +
|
||||
f"Current translation: {translation.text}\n" +
|
||||
f"LLM translation: {llm_translation}")
|
||||
|
||||
translation.text = llm_translation
|
||||
|
||||
with path.open("w", encoding="utf-8") as fp:
|
||||
fp.write('<?xml version="1.0" encoding="utf-8"?>\n' +
|
||||
'<!DOCTYPE TS>\n' +
|
||||
ET.tostring(root, encoding="utf-8").decode())
|
||||
|
||||
|
||||
def main():
|
||||
arg_parser = argparse.ArgumentParser("Auto translate")
|
||||
|
||||
group = arg_parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument("-a", "--all-files", action="store_true", help="Translate all files")
|
||||
group.add_argument("-f", "--file", nargs="+", help="Translate the selected files. (Example: -f fr de)")
|
||||
|
||||
arg_parser.add_argument("-t", "--all-translations", action="store_true", default=False, help="Translate all sections. (Default: only unfinished)")
|
||||
|
||||
args = arg_parser.parse_args()
|
||||
|
||||
if OPENAI_API_KEY is None:
|
||||
print("OpenAI API key is missing. (Hint: use `export OPENAI_API_KEY=YOUR-KEY` before you run the script).\n" +
|
||||
"If you don't have one go to: https://beta.openai.com/account/api-keys.")
|
||||
exit(1)
|
||||
|
||||
files = get_language_files(None if args.all_files else args.file)
|
||||
|
||||
if args.file:
|
||||
missing_files = set(args.file) - set(files)
|
||||
if len(missing_files):
|
||||
print(f"No language files found: {missing_files}")
|
||||
exit(1)
|
||||
|
||||
print(f"Translation mode: {'all' if args.all_translations else 'only unfinished'}. Files: {list(files)}")
|
||||
|
||||
for lang, path in files.items():
|
||||
print(f"Translate {lang} ({path})")
|
||||
translate_file(path, lang, args.all_translations)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
108
selfdrive/ui/translations/create_badges.py
Executable file
108
selfdrive/ui/translations/create_badges.py
Executable file
@@ -0,0 +1,108 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
import os
|
||||
import requests
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
from openpilot.common.basedir import BASEDIR
|
||||
from openpilot.selfdrive.ui.update_translations import LANGUAGES_FILE, TRANSLATIONS_DIR
|
||||
|
||||
BADGE_HEIGHT = 20 + 8
|
||||
SHIELDS_URL = "https://img.shields.io/badge"
|
||||
|
||||
def parse_po_file(file_path):
|
||||
"""
|
||||
Parse a .po file and count total and unfinished translations.
|
||||
Returns: (total_translations, unfinished_translations)
|
||||
"""
|
||||
with open(file_path) as f:
|
||||
content = f.read()
|
||||
|
||||
total_translations = 0
|
||||
unfinished_translations = 0
|
||||
|
||||
# Split into entries (separated by blank lines)
|
||||
entries = content.split('\n\n')
|
||||
|
||||
for entry in entries:
|
||||
# Skip header entry (contains Project-Id-Version)
|
||||
if 'Project-Id-Version' in entry:
|
||||
continue
|
||||
|
||||
# Check if this entry has a msgid (translation entry)
|
||||
# After skipping header, any entry with msgid " is a translation
|
||||
# (both msgid "content" and msgid "" for multiline contain msgid ")
|
||||
if 'msgid "' not in entry:
|
||||
continue
|
||||
|
||||
total_translations += 1
|
||||
|
||||
# Check if msgstr is empty (unfinished translation)
|
||||
if 'msgstr ""' in entry:
|
||||
# Check if there are continuation lines with content after msgstr ""
|
||||
lines = entry.split('\n')
|
||||
msgstr_idx = None
|
||||
for i, line in enumerate(lines):
|
||||
if line.strip().startswith('msgstr ""'):
|
||||
msgstr_idx = i
|
||||
break
|
||||
|
||||
if msgstr_idx is not None:
|
||||
# Check if any continuation lines have content
|
||||
has_content = False
|
||||
for line in lines[msgstr_idx + 1:]:
|
||||
stripped = line.strip()
|
||||
# Continuation line with content
|
||||
if stripped.startswith('"') and len(stripped) > 2:
|
||||
has_content = True
|
||||
break
|
||||
# End of entry
|
||||
if stripped.startswith(('msgid', '#')) or not stripped:
|
||||
break
|
||||
|
||||
if not has_content:
|
||||
unfinished_translations += 1
|
||||
|
||||
return (total_translations, unfinished_translations)
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open(LANGUAGES_FILE) as f:
|
||||
translation_files = json.load(f)
|
||||
|
||||
badge_svg = []
|
||||
max_badge_width = 0 # keep track of max width to set parent element
|
||||
for idx, (name, file) in enumerate(translation_files.items()):
|
||||
po_file_path = os.path.join(str(TRANSLATIONS_DIR), f"app_{file}.po")
|
||||
|
||||
total_translations, unfinished_translations = parse_po_file(po_file_path)
|
||||
|
||||
percent_finished = int(100 - (unfinished_translations / total_translations * 100.)) if total_translations > 0 else 0
|
||||
color = f"rgb{(94, 188, 0) if percent_finished == 100 else (248, 255, 50) if percent_finished > 90 else (204, 55, 27)}"
|
||||
|
||||
# Download badge
|
||||
badge_label = f"LANGUAGE {name}"
|
||||
badge_message = f"{percent_finished}% complete"
|
||||
if unfinished_translations != 0:
|
||||
badge_message += f" ({unfinished_translations} unfinished)"
|
||||
|
||||
r = requests.get(f"{SHIELDS_URL}/{badge_label}-{badge_message}-{color}", timeout=10)
|
||||
assert r.status_code == 200, "Error downloading badge"
|
||||
content_svg = r.content.decode("utf-8")
|
||||
|
||||
xml = ET.fromstring(content_svg)
|
||||
assert "width" in xml.attrib
|
||||
max_badge_width = max(max_badge_width, int(xml.attrib["width"]))
|
||||
|
||||
# Make tag ids in each badge unique to combine them into one svg
|
||||
for tag in ("r", "s"):
|
||||
content_svg = content_svg.replace(f'id="{tag}"', f'id="{tag}{idx}"')
|
||||
content_svg = content_svg.replace(f'"url(#{tag})"', f'"url(#{tag}{idx})"')
|
||||
|
||||
badge_svg.extend([f'<g transform="translate(0, {idx * BADGE_HEIGHT})">', content_svg, "</g>"])
|
||||
|
||||
badge_svg.insert(0, '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ' +
|
||||
f'height="{len(translation_files) * BADGE_HEIGHT}" width="{max_badge_width}">')
|
||||
badge_svg.append("</svg>")
|
||||
|
||||
with open(os.path.join(BASEDIR, "translation_badge.svg"), "w") as badge_f:
|
||||
badge_f.write("\n".join(badge_svg))
|
||||
15
selfdrive/ui/translations/languages.json
Normal file
15
selfdrive/ui/translations/languages.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"English": "en",
|
||||
"Deutsch": "de",
|
||||
"Français": "fr",
|
||||
"Português": "pt-BR",
|
||||
"Español": "es",
|
||||
"Türkçe": "tr",
|
||||
"Українська": "uk",
|
||||
"العربية": "ar",
|
||||
"ไทย": "th",
|
||||
"中文(繁體)": "zh-CHT",
|
||||
"中文(简体)": "zh-CHS",
|
||||
"한국어": "ko",
|
||||
"日本語": "ja"
|
||||
}
|
||||
Reference in New Issue
Block a user