Exemple #1
0
def setupLangAndBackend(
    pm: ProfileManager,
    app: QApplication,
    force: Optional[str] = None,
    firstTime: bool = False,
) -> RustBackend:
    global _qtrans
    try:
        locale.setlocale(locale.LC_ALL, "")
    except:
        pass

    # add _ and ngettext globals used by legacy code
    def fn__(arg) -> None:  # type: ignore
        print("".join(traceback.format_stack()[-2]))
        print("_ global will break in the future; please see anki/lang.py")
        return arg

    def fn_ngettext(a, b, c) -> None:  # type: ignore
        print("".join(traceback.format_stack()[-2]))
        print(
            "ngettext global will break in the future; please see anki/lang.py"
        )
        return b

    builtins.__dict__["_"] = fn__
    builtins.__dict__["ngettext"] = fn_ngettext

    # get lang and normalize into ja/zh-CN form
    if firstTime:
        lang = pm.meta["defaultLang"]
    else:
        lang = force or pm.meta["defaultLang"]
    lang = anki.lang.lang_to_disk_lang(lang)

    if not firstTime:
        # set active language
        anki.lang.set_lang(lang)

    # switch direction for RTL languages
    if anki.lang.is_rtl(lang):
        app.setLayoutDirection(Qt.RightToLeft)
    else:
        app.setLayoutDirection(Qt.LeftToRight)

    # load qt translations
    _qtrans = QTranslator()

    from aqt.utils import aqt_data_folder

    if isMac and getattr(sys, "frozen", False):
        qt_dir = os.path.abspath(
            os.path.join(aqt_data_folder(), "..", "qt_translations"))
    else:
        qt_dir = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
    qt_lang = lang.replace("-", "_")
    if _qtrans.load(f"qtbase_{qt_lang}", qt_dir):
        app.installTranslator(_qtrans)

    return anki.lang.current_i18n
Exemple #2
0
 def current_lang_index(self) -> int:
     codes = [x[1] for x in anki.lang.langs]
     lang = anki.lang.current_lang
     if lang in anki.lang.compatMap:
         lang = anki.lang.compatMap[lang]
     else:
         lang = lang.replace("-", "_")
     try:
         return codes.index(lang)
     except:
         return codes.index("en_US")
Exemple #3
0
def setupLangAndBackend(
    pm: ProfileManager,
    app: QApplication,
    force: Optional[str] = None,
    firstTime: bool = False,
) -> RustBackend:
    global _qtrans
    try:
        locale.setlocale(locale.LC_ALL, "")
    except:
        pass

    # add _ and ngettext globals used by legacy code
    def fn__(arg):
        print("".join(traceback.format_stack()[-2]))
        print("_ global will break in the future; please see anki/lang.py")
        return arg

    def fn_ngettext(a, b, c):
        print("".join(traceback.format_stack()[-2]))
        print(
            "ngettext global will break in the future; please see anki/lang.py"
        )
        return b

    builtins.__dict__["_"] = fn__
    builtins.__dict__["ngettext"] = fn_ngettext

    # get lang and normalize into ja/zh-CN form
    if firstTime:
        lang = pm.meta["defaultLang"]
    else:
        lang = force or pm.meta["defaultLang"]
    lang = anki.lang.lang_to_disk_lang(lang)

    ldir = locale_dir()
    if not firstTime:
        # set active language
        anki.lang.set_lang(lang, ldir)

    # switch direction for RTL languages
    if anki.lang.is_rtl(lang):
        app.setLayoutDirection(Qt.RightToLeft)
    else:
        app.setLayoutDirection(Qt.LeftToRight)

    # load qt translations
    _qtrans = QTranslator()
    qt_dir = os.path.join(ldir, "qt")
    qt_lang = lang.replace("-", "_")
    if _qtrans.load("qtbase_" + qt_lang, qt_dir):
        app.installTranslator(_qtrans)

    return anki.lang.current_i18n
Exemple #4
0
def setupLangAndBackend(pm: ProfileManager,
                        app: QApplication,
                        force: Optional[str] = None) -> RustBackend:
    global _qtrans
    try:
        locale.setlocale(locale.LC_ALL, "")
    except:
        pass

    # add _ and ngettext globals used by legacy code
    def fn__(arg):
        print(
            "accessing _ without importing from anki.lang will break in the future"
        )
        print("".join(traceback.format_stack()[-2]))
        from anki.lang import _

        return _(arg)

    def fn_ngettext(a, b, c):
        print(
            "accessing ngettext without importing from anki.lang will break in the future"
        )
        print("".join(traceback.format_stack()[-2]))
        from anki.lang import ngettext

        return ngettext(a, b, c)

    builtins.__dict__["_"] = fn__
    builtins.__dict__["ngettext"] = fn_ngettext

    # get lang and normalize into ja/zh-CN form
    lang = force or pm.meta["defaultLang"]
    lang = anki.lang.lang_to_disk_lang(lang)

    # load gettext catalog
    ldir = locale_dir()
    anki.lang.set_lang(lang, ldir)

    # switch direction for RTL languages
    if anki.lang.is_rtl(lang):
        app.setLayoutDirection(Qt.RightToLeft)
    else:
        app.setLayoutDirection(Qt.LeftToRight)

    # load qt translations
    _qtrans = QTranslator()
    qt_dir = os.path.join(ldir, "qt")
    qt_lang = lang.replace("-", "_")
    if _qtrans.load("qtbase_" + qt_lang, qt_dir):
        app.installTranslator(_qtrans)

    return anki.lang.current_i18n