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
|
host = https://www.transifex.com
|
||||||
|
|
||||||
[o:rare-1:p:rare:r:placeholder-ts]
|
[o:rare-1:p:rare:r:placeholder-ts]
|
||||||
file_filter = rare/resources/languages/<lang>.ts
|
file_filter = rare/resources/languages/rare_<lang>.ts
|
||||||
source_file = rare/resources/languages/translation_source.ts
|
source_file = rare/resources/languages/source.ts
|
||||||
source_lang = en_US
|
source_lang = en
|
||||||
type = QT
|
type = QT
|
||||||
minimum_perc = 50
|
minimum_perc = 50
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
cwd="$(pwd)"
|
cwd="$(pwd)"
|
||||||
cd "$(dirname "$0")"/.. || exit
|
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
|
cd "$cwd" || exit
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import locale
|
||||||
import sys
|
import sys
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
|
|
||||||
|
@ -33,9 +34,9 @@ class RareSettings(QWidget, Ui_RareSettings):
|
||||||
|
|
||||||
# Select lang
|
# Select lang
|
||||||
language = self.settings.value(*options.language)
|
language = self.settings.value(*options.language)
|
||||||
self.lang_select.addItem(self.tr("System default"), "")
|
self.lang_select.addItem(self.tr("System default"), options.language.default)
|
||||||
for locale, title in get_translations():
|
for lang_code, title in get_translations():
|
||||||
self.lang_select.addItem(title, locale)
|
self.lang_select.addItem(title, lang_code)
|
||||||
if (index := self.lang_select.findData(language, Qt.UserRole)) > 0:
|
if (index := self.lang_select.findData(language, Qt.UserRole)) > 0:
|
||||||
self.lang_select.setCurrentIndex(index)
|
self.lang_select.setCurrentIndex(index)
|
||||||
else:
|
else:
|
||||||
|
@ -212,8 +213,8 @@ class RareSettings(QWidget, Ui_RareSettings):
|
||||||
self.settings.remove(options.window_size.key)
|
self.settings.remove(options.window_size.key)
|
||||||
|
|
||||||
def on_lang_changed(self, index: int):
|
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)
|
self.settings.remove(options.language.key)
|
||||||
return
|
else:
|
||||||
language = self.lang_select.itemData(index, Qt.UserRole)
|
self.settings.setValue(options.language.key, lang_code)
|
||||||
self.settings.setValue(options.language.key, language)
|
|
||||||
|
|
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], ...]:
|
def get_translations() -> Tuple[Tuple[str, str], ...]:
|
||||||
langs = []
|
langs = []
|
||||||
for i in os.listdir(os.path.join(resources_path, "languages")):
|
for i in os.listdir(os.path.join(resources_path, "languages")):
|
||||||
if i.endswith(".qm") and not i.startswith("qt_"):
|
if i.endswith(".qm") and i.startswith("rare_"):
|
||||||
locale = QLocale(i.split(".")[0])
|
locale = QLocale(i.removesuffix(".qm").removeprefix("rare_"))
|
||||||
langs.append((locale.name(), f"{locale.nativeLanguageName()} ({locale.nativeCountryName()})"))
|
langs.append((locale.name(), f"{locale.nativeLanguageName()} ({locale.nativeCountryName()})"))
|
||||||
return tuple(langs)
|
return tuple(langs)
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,18 @@ import traceback
|
||||||
from argparse import Namespace
|
from argparse import Namespace
|
||||||
|
|
||||||
import legendary
|
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.QtGui import QIcon
|
||||||
from PyQt5.QtWidgets import QApplication, QMessageBox
|
from PyQt5.QtWidgets import QApplication, QMessageBox
|
||||||
|
|
||||||
|
@ -103,9 +114,9 @@ class RareApp(QApplication):
|
||||||
|
|
||||||
self.settings = QSettings(self)
|
self.settings = QSettings(self)
|
||||||
|
|
||||||
# Translator
|
# # Translator
|
||||||
self.translator = QTranslator(self)
|
# self.translator = QTranslator(self)
|
||||||
self.qt_translator = QTranslator(self)
|
# self.qt_translator = QTranslator(self)
|
||||||
|
|
||||||
# Style
|
# Style
|
||||||
# lk: this is a bit silly but serves well until we have a class
|
# 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):
|
def load_translator(self, lang: str):
|
||||||
# translator for qt stuff
|
# translator for qt stuff
|
||||||
if self.qt_translator.load(f"qt_{lang}", os.path.join(paths.resources_path, "languages")):
|
locale = QLocale(lang)
|
||||||
self.logger.debug("Using translation for locale: %s", lang)
|
self.logger.info("Using locale: %s", locale.name())
|
||||||
else:
|
translations = {
|
||||||
self.logger.info("Couldn't find translation for locale: %s", lang)
|
"qtbase": QLibraryInfo.location(QLibraryInfo.TranslationsPath),
|
||||||
self.installTranslator(self.qt_translator)
|
"rare": os.path.join(paths.resources_path, "languages"),
|
||||||
|
}
|
||||||
if self.translator.load(lang, os.path.join(paths.resources_path, "languages")):
|
for filename, path in translations.items():
|
||||||
self.logger.info("Using translation for locale: %s", lang)
|
translator = QTranslator(self)
|
||||||
else:
|
if translator.load(locale, filename, "_", path):
|
||||||
self.logger.info("Couldn't find translation for locale: %s", lang)
|
self.logger.debug("Loaded translation file: %s", translator.filePath())
|
||||||
self.installTranslator(self.translator)
|
self.installTranslator(translator)
|
||||||
|
else:
|
||||||
|
self.logger.info("Couldn't find translation for locale: %s", locale.name())
|
||||||
|
translator.deleteLater()
|
||||||
|
|
Loading…
Reference in a new issue