drop unused threadLocal and noHint i18n code

This commit is contained in:
Damien Elmes 2020-02-16 15:46:06 +10:00
parent 97b9b94fc7
commit 6c9e9eb330
4 changed files with 30 additions and 47 deletions

View file

@ -4,9 +4,9 @@
# Please leave the coding line in this file to prevent xgettext complaining.
import gettext
import os
import re
import threading
from typing import Any
from typing import Optional, Union
langs = sorted(
[
@ -133,55 +133,39 @@ def lang_to_disk_lang(lang: str) -> str:
return re.match("(.*)_", lang).group(1)
threadLocal = threading.local()
# the currently set interface language
currentLang = "en"
# global defaults
currentLang: Any = None
currentTranslation: Any = None
locale_folder: str = ""
# the current translation catalog
current_catalog: Optional[
Union[gettext.NullTranslations, gettext.GNUTranslations]
] = None
def localTranslation() -> Any:
"Return the translation local to this thread, or the default."
if getattr(threadLocal, "currentTranslation", None):
return threadLocal.currentTranslation
else:
return currentTranslation
# path to locale folder
locale_folder = ""
def _(str: str) -> str:
return localTranslation().gettext(str)
if current_catalog:
return current_catalog.gettext(str)
else:
return str
def ngettext(single: str, plural: str, n: int) -> str:
return localTranslation().ngettext(single, plural, n)
if current_catalog:
return current_catalog.ngettext(single, plural, n)
elif n == 1:
return single
return plural
def setLang(lang: str, locale_dir: str, local: bool = True) -> None:
trans = gettext.translation("anki", locale_dir, languages=[lang], fallback=True)
if local:
threadLocal.currentLang = lang
threadLocal.currentTranslation = trans
threadLocal.locale_folder = locale_dir
else:
global currentLang, currentTranslation, locale_folder
currentLang = lang
currentTranslation = trans
locale_folder = locale_dir
def set_lang(lang: str, locale_dir: str) -> None:
global currentLang, current_catalog, locale_folder
gettext_dir = os.path.join(locale_dir, "gettext")
def getLang() -> str:
"Return the language local to this thread, or the default."
if getattr(threadLocal, "currentLang", None):
return threadLocal.currentLang
else:
return currentLang
def noHint(str) -> str:
"Remove translation hint from end of string."
return re.sub(r"(^.*?)( ?\(.+?\))?$", "\\1", str)
if not currentTranslation:
setLang("en_US", locale_dir="", local=False)
currentLang = lang
current_catalog = gettext.translation(
"anki", gettext_dir, languages=[lang], fallback=True
)
locale_folder = locale_dir

View file

@ -175,8 +175,7 @@ def setupLang(
# load gettext catalog
ldir = locale_dir()
gettext_dir = os.path.join(ldir, "gettext")
anki.lang.setLang(lang, gettext_dir, local=False)
anki.lang.set_lang(lang, ldir)
# switch direction for RTL languages
if lang in ("he", "ar", "fa"):

View file

@ -59,7 +59,7 @@ class Preferences(QDialog):
def langIdx(self):
codes = [x[1] for x in anki.lang.langs]
try:
return codes.index(anki.lang.getLang())
return codes.index(anki.lang.currentLang)
except:
return codes.index("en_US")

View file

@ -442,7 +442,7 @@ please see:
sql = "update profiles set data = ? where name = ?"
self.db.execute(sql, self._pickle(self.meta), "_global")
self.db.commit()
anki.lang.setLang(code, locale_dir(), local=False)
anki.lang.set_lang(code, locale_dir())
# OpenGL
######################################################################