Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Jackenmen/44d2b83c24b6a2a9481a2b3b3546cb57 to your computer and use it in GitHub Desktop.
Save Jackenmen/44d2b83c24b6a2a9481a2b3b3546cb57 to your computer and use it in GitHub Desktop.
A script that fixes po files that were generated by redgettext<3.3. See https://github.com/Cog-Creators/redgettext/issues/1
from pathlib import Path
from typing import Dict
import polib
from lxml import etree
def _str_field(self, *args, **kwargs):
ret = _str_field_orig(self, *args, **kwargs)
if len(ret) > 1:
if ret[0] == 'msgid ""':
ret[1] = f"msgid {ret[1]}"
ret.pop(0)
elif ret[0] == 'msgstr ""':
ret[1] = f"msgstr {ret[1]}"
ret.pop(0)
actual_ret = []
idx = -1
for line in ret:
if line == '"\\n"':
actual_ret[idx] = f'{actual_ret[idx][:-1]}\\n"'
else:
actual_ret.append(line)
idx += 1
return actual_ret
def wrap(text, width=70, **kwargs):
initial_indent = kwargs.get("initial_indent")
if initial_indent is not None and initial_indent == "#: ":
return _wrap_orig(text, width, **kwargs)
if initial_indent is not None:
return f"{initial_indent}{text}"
return _wrap_orig(text, 99999999, **kwargs)
_str_field_orig = polib._BaseEntry._str_field
polib._BaseEntry._str_field = _str_field
_wrap_orig = polib.wrap
polib.wrap = wrap
with open("redbot-tmx.tmx", "rb") as fp:
tmx = etree.fromstring(fp.read())
tm: Dict[str, Dict[str, str]] = {}
for tu in tmx[1]:
orig_string = tu.find("tuv[@{http://www.w3.org/XML/1998/namespace}lang='en']/seg").text
tm.setdefault(orig_string, {})
for tuv in tu:
tm[orig_string][tuv.attrib["{http://www.w3.org/XML/1998/namespace}lang"]] = polib.unescape(tuv.find("seg").text)
PO_NAME_TO_LANG_NAME = {
"af-ZA": "af",
"ar-SA": "ar",
"bg-BG": "bg",
"bs-BA": "bs",
"ca-ES": "ca",
"cs-CZ": "cs",
"da-DK": "da",
"de-DE": "de",
"el-GR": "el",
"en-PT": "en-PT",
"es-ES": "es-ES",
"fi-FI": "fi",
"fr-FR": "fr",
"he-IL": "he",
"hu-HU": "hu",
"id-ID": "id",
"it-IT": "it",
"ja-JP": "ja",
"ko-KR": "ko",
"nb-NO": "nb",
"nl-NL": "nl",
"pl-PL": "pl",
"pt-BR": "pt-BR",
"pt-PT": "pt-PT",
"ro-RO": "ro",
"ru-RU": "ru",
"sk-SK": "sk",
"sr-CS": "sr-CS",
"sr-SP": "sr",
"sv-SE": "sv-SE",
"tr-TR": "tr",
"uk-UA": "uk",
"vi-VN": "vi",
"zh-CN": "zh-CN",
"zh-HK": "zh-HK",
"zh-TW": "zh-TW",
}
# for path in [Path("Red-DiscordBot/redbot/core/locales/tr-TR.po")]:
for path in Path("Red-DiscordBot/redbot").glob("**/locales/*.po"):
lang_name = PO_NAME_TO_LANG_NAME.get(path.stem)
if lang_name is None:
if path.stem not in ("debugging", "en-US", "lol-US"):
print(f"couldn't find matching lang for {path.stem}")
continue
po = polib.pofile(str(path))
changed = False
for entry in po:
if entry.translated():
continue
translation = tm.get(polib.escape(entry.msgid), {}).get(lang_name)
if translation is None:
continue
entry.msgstr = translation
changed = True
if changed:
po.save(str(path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment