From 10e7a8776332ac2969d97cb43cc7f0a960c1bb5e Mon Sep 17 00:00:00 2001 From: Abhinav Tarigoppula Date: Sat, 14 Nov 2026 21:22:46 +0530 Subject: [PATCH] fix: translate dropdown options in the language chosen at startup The thumbnail size, settings (file path, tag click action, theme and splash) and field type options were translated in class bodies, which run when the module is imported, before the language from the settings is applied. They always showed English. Store the translation keys and look them up when the comboboxes are filled. --- .../controllers/edit_field_template_modal.py | 6 +-- src/tagstudio/qt/controllers/main_window.py | 13 +++--- src/tagstudio/qt/mixed/settings_panel.py | 42 +++++++++-------- tests/qt/test_translated_options.py | 46 +++++++++++++++++++ 4 files changed, 79 insertions(+), 28 deletions(-) create mode 100644 tests/qt/test_translated_options.py diff --git a/src/tagstudio/qt/controllers/edit_field_template_modal.py b/src/tagstudio/qt/controllers/edit_field_template_modal.py index f8ebaf990c..a8915bec39 100644 --- a/src/tagstudio/qt/controllers/edit_field_template_modal.py +++ b/src/tagstudio/qt/controllers/edit_field_template_modal.py @@ -19,8 +19,8 @@ # TODO: Use newer MVC style guidelines class EditFieldTemplateModal(EditFieldTemplateModalView): field_type_map: dict[str, str] = { - "TextFieldTemplate": Translations["field_type.text"], - "DatetimeFieldTemplate": Translations["field_type.datetime"], + "TextFieldTemplate": "field_type.text", + "DatetimeFieldTemplate": "field_type.datetime", } DEFAULT_TYPE_INDEX = 0 @@ -32,7 +32,7 @@ def __init__(self, field_template: BaseFieldTemplate | None = None) -> None: self.old_field_type: str = "" for k, v in EditFieldTemplateModal.field_type_map.items(): - self._type_combobox.addItem(v, k) + self._type_combobox.addItem(Translations[v], k) self.__connect_callbacks() self.set_field_template(field_template) diff --git a/src/tagstudio/qt/controllers/main_window.py b/src/tagstudio/qt/controllers/main_window.py index aa040597be..ffb733c6cd 100644 --- a/src/tagstudio/qt/controllers/main_window.py +++ b/src/tagstudio/qt/controllers/main_window.py @@ -454,12 +454,13 @@ def rebuild_open_recent_library_menu( # View Component class MainWindow(QMainWindow): + # (translation key, size): the label is looked up when the combobox is filled. THUMB_SIZES: list[tuple[str, int]] = [ - (Translations["home.thumbnail_size.extra_large"], 256), - (Translations["home.thumbnail_size.large"], 192), - (Translations["home.thumbnail_size.medium"], 128), - (Translations["home.thumbnail_size.small"], 96), - (Translations["home.thumbnail_size.mini"], 76), + ("home.thumbnail_size.extra_large", 256), + ("home.thumbnail_size.large", 192), + ("home.thumbnail_size.medium", 128), + ("home.thumbnail_size.small", 96), + ("home.thumbnail_size.mini", 76), ] def __init__(self, driver: QtDriver, parent: QWidget | None = None) -> None: @@ -650,7 +651,7 @@ def setup_extra_input_bar(self): self.thumb_size_combobox.setMaximumWidth(352) self.extra_input_layout.addWidget(self.thumb_size_combobox) for size in MainWindow.THUMB_SIZES: - self.thumb_size_combobox.addItem(size[0], size[1]) + self.thumb_size_combobox.addItem(Translations[size[0]], size[1]) self.thumb_size_combobox.setCurrentIndex(2) # Default: Medium self.central_layout.addLayout(self.extra_input_layout, 5, 0, 1, 1) diff --git a/src/tagstudio/qt/mixed/settings_panel.py b/src/tagstudio/qt/mixed/settings_panel.py index b78d1d3a48..7c1452c3f3 100644 --- a/src/tagstudio/qt/mixed/settings_panel.py +++ b/src/tagstudio/qt/mixed/settings_panel.py @@ -42,31 +42,33 @@ class SettingsPanel(ModalContent): driver: QtDriver + # Translation keys, looked up when the comboboxes are filled so the labels use the + # language chosen at startup rather than the one active when this module was imported. filepath_option_map: dict[ShowFilepathOption, str] = { - ShowFilepathOption.SHOW_FULL_PATHS: Translations["settings.filepath.option.full"], - ShowFilepathOption.SHOW_RELATIVE_PATHS: Translations["settings.filepath.option.relative"], - ShowFilepathOption.SHOW_FILENAMES_ONLY: Translations["settings.filepath.option.name"], + ShowFilepathOption.SHOW_FULL_PATHS: "settings.filepath.option.full", + ShowFilepathOption.SHOW_RELATIVE_PATHS: "settings.filepath.option.relative", + ShowFilepathOption.SHOW_FILENAMES_ONLY: "settings.filepath.option.name", } theme_map: dict[Theme, str] = { - Theme.SYSTEM: Translations["settings.theme.system"], - Theme.DARK: Translations["settings.theme.dark"], - Theme.LIGHT: Translations["settings.theme.light"], + Theme.SYSTEM: "settings.theme.system", + Theme.DARK: "settings.theme.dark", + Theme.LIGHT: "settings.theme.light", } splash_map: dict[Splash, str] = { - Splash.DEFAULT: Translations["settings.splash.option.default"], - Splash.RANDOM: Translations["settings.splash.option.random"], - Splash.CLASSIC: Translations["settings.splash.option.classic"], - Splash.GOO_GEARS: Translations["settings.splash.option.goo_gears"], - Splash.NINETY_FIVE: Translations["settings.splash.option.ninety_five"], - Splash.AURORA: Translations["settings.splash.option.aurora"], + Splash.DEFAULT: "settings.splash.option.default", + Splash.RANDOM: "settings.splash.option.random", + Splash.CLASSIC: "settings.splash.option.classic", + Splash.GOO_GEARS: "settings.splash.option.goo_gears", + Splash.NINETY_FIVE: "settings.splash.option.ninety_five", + Splash.AURORA: "settings.splash.option.aurora", } tag_click_action_map: dict[TagClickActionOption, str] = { - TagClickActionOption.OPEN_EDIT: Translations["settings.tag_click_action.open_edit"], - TagClickActionOption.SET_SEARCH: Translations["settings.tag_click_action.set_search"], - TagClickActionOption.ADD_TO_SEARCH: Translations["settings.tag_click_action.add_to_search"], + TagClickActionOption.OPEN_EDIT: "settings.tag_click_action.open_edit", + TagClickActionOption.SET_SEARCH: "settings.tag_click_action.set_search", + TagClickActionOption.ADD_TO_SEARCH: "settings.tag_click_action.add_to_search", } date_format_map: dict[str, str] = { @@ -194,7 +196,7 @@ def on_page_size_changed(): # Show Filepath self.filepath_combobox = QComboBox() for k in SettingsPanel.filepath_option_map: - self.filepath_combobox.addItem(SettingsPanel.filepath_option_map[k], k) + self.filepath_combobox.addItem(Translations[SettingsPanel.filepath_option_map[k]], k) filepath_option: ShowFilepathOption = self.driver.settings.show_filepath if filepath_option not in SettingsPanel.filepath_option_map: filepath_option = ShowFilepathOption.DEFAULT @@ -206,7 +208,9 @@ def on_page_size_changed(): # Tag Click Action self.tag_click_action_combobox = QComboBox() for k in SettingsPanel.tag_click_action_map: - self.tag_click_action_combobox.addItem(SettingsPanel.tag_click_action_map[k], k) + self.tag_click_action_combobox.addItem( + Translations[SettingsPanel.tag_click_action_map[k]], k + ) tag_click_action = self.driver.settings.tag_click_action if tag_click_action not in SettingsPanel.tag_click_action_map: tag_click_action = TagClickActionOption.DEFAULT @@ -375,7 +379,7 @@ def __build_appearance_settings(self): # Dark Mode self.theme_combobox = QComboBox() for k in SettingsPanel.theme_map: - self.theme_combobox.addItem(SettingsPanel.theme_map[k], k) + self.theme_combobox.addItem(Translations[SettingsPanel.theme_map[k]], k) theme = self.driver.settings.theme if theme not in SettingsPanel.theme_map: theme = Theme.DEFAULT @@ -386,7 +390,7 @@ def __build_appearance_settings(self): # Splash Screen self.splash_combobox = QComboBox() for k in SettingsPanel.splash_map: - self.splash_combobox.addItem(SettingsPanel.splash_map[k], k) + self.splash_combobox.addItem(Translations[SettingsPanel.splash_map[k]], k) splash = self.driver.settings.splash if splash not in SettingsPanel.splash_map: splash = Splash.DEFAULT diff --git a/tests/qt/test_translated_options.py b/tests/qt/test_translated_options.py new file mode 100644 index 0000000000..03159a84f7 --- /dev/null +++ b/tests/qt/test_translated_options.py @@ -0,0 +1,46 @@ +# SPDX-FileCopyrightText: (c) TagStudio Contributors +# SPDX-License-Identifier: MIT + +# pyright: reportPrivateUsage=false + +from collections.abc import Iterator + +import pytest +from PySide6.QtWidgets import QComboBox +from pytestqt.qtbot import QtBot + +from tagstudio.i18n.translations import DEFAULT_TRANSLATION, Translations +from tagstudio.qt.controllers.edit_field_template_modal import EditFieldTemplateModal +from tagstudio.qt.mixed.settings_panel import SettingsPanel +from tagstudio.qt.qt_driver import QtDriver + + +@pytest.fixture +def french() -> Iterator[None]: + # Like the app, switch language only after these modules have been imported. + Translations.change_language("fr") + yield + Translations.change_language(DEFAULT_TRANSLATION) + + +def items(combobox: QComboBox) -> list[str]: + return [combobox.itemText(i) for i in range(combobox.count())] + + +def test_settings_options_use_the_current_language(qtbot: QtBot, qt_driver: QtDriver, french: None): + # qt_driver applies the language from its settings, so request french after it. + panel = SettingsPanel(qt_driver) + qtbot.addWidget(panel) + + assert "Sombre" in items(panel.theme_combobox) + assert "Aléatoire" in items(panel.splash_combobox) + assert "Afficher le Chemin Complet" in items(panel.filepath_combobox) + assert "Modifier le Tag" in items(panel.tag_click_action_combobox) + + +@pytest.mark.usefixtures("french") +def test_field_type_options_use_the_current_language(qtbot: QtBot): + modal = EditFieldTemplateModal() + qtbot.addWidget(modal) + + assert "Texte" in items(modal._type_combobox)