Rare: Improve translation handling
* Remove base Qt translations from repo, load translations from Qt itself * Prefix translation `qm` files with `rare_`. * Rename `translation_source.ts` to simply `source.ts` * If the selected language matches the system local, remove the option from the configuration.
This commit is contained in:
parent
f088fc95b6
commit
8a3bdbdd91
|
@ -2,9 +2,9 @@
|
|||
host = https://www.transifex.com
|
||||
|
||||
[o:rare-1:p:rare:r:placeholder-ts]
|
||||
file_filter = rare/resources/languages/<lang>.ts
|
||||
source_file = rare/resources/languages/translation_source.ts
|
||||
source_lang = en_US
|
||||
file_filter = rare/resources/languages/rare_<lang>.ts
|
||||
source_file = rare/resources/languages/source.ts
|
||||
source_lang = en
|
||||
type = QT
|
||||
minimum_perc = 50
|
||||
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
cwd="$(pwd)"
|
||||
cd "$(dirname "$0")"/.. || exit
|
||||
|
||||
pylupdate5 -noobsolete $(find rare/ -iname "*.py") -ts rare/resources/languages/translation_source.ts
|
||||
#py_files=$(find rare -iname "*.py" -not -path rare/ui)
|
||||
#ui_files=$(find rare/ui -iname "*.ui")
|
||||
|
||||
pylupdate5 -noobsolete $(find rare/ -iname "*.py") -ts rare/resources/languages/source.ts
|
||||
|
||||
cd "$cwd" || exit
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
import locale
|
||||
import sys
|
||||
from logging import getLogger
|
||||
|
||||
|
@ -33,9 +34,9 @@ class RareSettings(QWidget, Ui_RareSettings):
|
|||
|
||||
# Select lang
|
||||
language = self.settings.value(*options.language)
|
||||
self.lang_select.addItem(self.tr("System default"), "")
|
||||
for locale, title in get_translations():
|
||||
self.lang_select.addItem(title, locale)
|
||||
self.lang_select.addItem(self.tr("System default"), options.language.default)
|
||||
for lang_code, title in get_translations():
|
||||
self.lang_select.addItem(title, lang_code)
|
||||
if (index := self.lang_select.findData(language, Qt.UserRole)) > 0:
|
||||
self.lang_select.setCurrentIndex(index)
|
||||
else:
|
||||
|
@ -212,8 +213,8 @@ class RareSettings(QWidget, Ui_RareSettings):
|
|||
self.settings.remove(options.window_size.key)
|
||||
|
||||
def on_lang_changed(self, index: int):
|
||||
if not index:
|
||||
lang_code = self.lang_select.itemData(index, Qt.UserRole)
|
||||
if lang_code == locale.getlocale()[0]:
|
||||
self.settings.remove(options.language.key)
|
||||
return
|
||||
language = self.lang_select.itemData(index, Qt.UserRole)
|
||||
self.settings.setValue(options.language.key, language)
|
||||
else:
|
||||
self.settings.setValue(options.language.key, lang_code)
|
||||
|
|
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load diff
|
@ -135,8 +135,8 @@ def get_style_sheets() -> Iterable[str]:
|
|||
def get_translations() -> Tuple[Tuple[str, str], ...]:
|
||||
langs = []
|
||||
for i in os.listdir(os.path.join(resources_path, "languages")):
|
||||
if i.endswith(".qm") and not i.startswith("qt_"):
|
||||
locale = QLocale(i.split(".")[0])
|
||||
if i.endswith(".qm") and i.startswith("rare_"):
|
||||
locale = QLocale(i.removesuffix(".qm").removeprefix("rare_"))
|
||||
langs.append((locale.name(), f"{locale.nativeLanguageName()} ({locale.nativeCountryName()})"))
|
||||
return tuple(langs)
|
||||
|
||||
|
|
|
@ -7,7 +7,18 @@ import traceback
|
|||
from argparse import Namespace
|
||||
|
||||
import legendary
|
||||
from PyQt5.QtCore import QSettings, QTranslator, QT_VERSION_STR, PYQT_VERSION_STR, QObject, pyqtSignal, pyqtSlot, Qt
|
||||
from PyQt5.QtCore import (
|
||||
QSettings,
|
||||
QTranslator,
|
||||
QT_VERSION_STR,
|
||||
PYQT_VERSION_STR,
|
||||
QObject,
|
||||
pyqtSignal,
|
||||
pyqtSlot,
|
||||
Qt,
|
||||
QLibraryInfo,
|
||||
QLocale,
|
||||
)
|
||||
from PyQt5.QtGui import QIcon
|
||||
from PyQt5.QtWidgets import QApplication, QMessageBox
|
||||
|
||||
|
@ -103,9 +114,9 @@ class RareApp(QApplication):
|
|||
|
||||
self.settings = QSettings(self)
|
||||
|
||||
# Translator
|
||||
self.translator = QTranslator(self)
|
||||
self.qt_translator = QTranslator(self)
|
||||
# # Translator
|
||||
# self.translator = QTranslator(self)
|
||||
# self.qt_translator = QTranslator(self)
|
||||
|
||||
# Style
|
||||
# lk: this is a bit silly but serves well until we have a class
|
||||
|
@ -130,14 +141,17 @@ class RareApp(QApplication):
|
|||
|
||||
def load_translator(self, lang: str):
|
||||
# translator for qt stuff
|
||||
if self.qt_translator.load(f"qt_{lang}", os.path.join(paths.resources_path, "languages")):
|
||||
self.logger.debug("Using translation for locale: %s", lang)
|
||||
else:
|
||||
self.logger.info("Couldn't find translation for locale: %s", lang)
|
||||
self.installTranslator(self.qt_translator)
|
||||
|
||||
if self.translator.load(lang, os.path.join(paths.resources_path, "languages")):
|
||||
self.logger.info("Using translation for locale: %s", lang)
|
||||
else:
|
||||
self.logger.info("Couldn't find translation for locale: %s", lang)
|
||||
self.installTranslator(self.translator)
|
||||
locale = QLocale(lang)
|
||||
self.logger.info("Using locale: %s", locale.name())
|
||||
translations = {
|
||||
"qtbase": QLibraryInfo.location(QLibraryInfo.TranslationsPath),
|
||||
"rare": os.path.join(paths.resources_path, "languages"),
|
||||
}
|
||||
for filename, path in translations.items():
|
||||
translator = QTranslator(self)
|
||||
if translator.load(locale, filename, "_", path):
|
||||
self.logger.debug("Loaded translation file: %s", translator.filePath())
|
||||
self.installTranslator(translator)
|
||||
else:
|
||||
self.logger.info("Couldn't find translation for locale: %s", locale.name())
|
||||
translator.deleteLater()
|
||||
|
|
Loading…
Reference in a new issue