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. # Please leave the coding line in this file to prevent xgettext complaining.
import gettext import gettext
import os
import re import re
import threading from typing import Optional, Union
from typing import Any
langs = sorted( langs = sorted(
[ [
@ -133,55 +133,39 @@ def lang_to_disk_lang(lang: str) -> str:
return re.match("(.*)_", lang).group(1) return re.match("(.*)_", lang).group(1)
threadLocal = threading.local() # the currently set interface language
currentLang = "en"
# global defaults # the current translation catalog
currentLang: Any = None current_catalog: Optional[
currentTranslation: Any = None Union[gettext.NullTranslations, gettext.GNUTranslations]
locale_folder: str = "" ] = None
# path to locale folder
def localTranslation() -> Any: locale_folder = ""
"Return the translation local to this thread, or the default."
if getattr(threadLocal, "currentTranslation", None):
return threadLocal.currentTranslation
else:
return currentTranslation
def _(str: str) -> str: 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: 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: def set_lang(lang: str, locale_dir: str) -> None:
trans = gettext.translation("anki", locale_dir, languages=[lang], fallback=True) global currentLang, current_catalog, locale_folder
if local: gettext_dir = os.path.join(locale_dir, "gettext")
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
currentLang = lang
def getLang() -> str: current_catalog = gettext.translation(
"Return the language local to this thread, or the default." "anki", gettext_dir, languages=[lang], fallback=True
if getattr(threadLocal, "currentLang", None): )
return threadLocal.currentLang locale_folder = locale_dir
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)

View file

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

View file

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

View file

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