Skip to content

Commit 7e75087

Browse files
authored
fix: restore translate_formatted() method as format() (#830)
* fix: restore `translate_formatted()` method * fix: set "Create & Add" button text * refactor: rename "translate_formatted" to "format" * translations: replace invalid format key names with "{unknown_key}"
1 parent 07b7d40 commit 7e75087

18 files changed

+83
-57
lines changed

tagstudio/src/qt/modals/about.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def __init__(self, config_path):
4444
if ff_version["ffprobe"] is not None:
4545
ffprobe = '<span style="color:green">Found</span> (' + ff_version["ffprobe"] + ")"
4646
self.content_widget = QLabel(
47-
Translations["about.content"].format(
47+
Translations.format(
48+
"about.content",
4849
version=VERSION,
4950
branch=VERSION_BRANCH,
5051
config_path=config_path,

tagstudio/src/qt/modals/delete_unlinked.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def __init__(self, driver: "QtDriver", tracker: MissingRegistry):
3232
self.root_layout.setContentsMargins(6, 6, 6, 6)
3333

3434
self.desc_widget = QLabel(
35-
Translations["entries.unlinked.delete.confirm"].format(
35+
Translations.format(
36+
"entries.unlinked.delete.confirm",
3637
count=self.tracker.missing_file_entries_count,
3738
)
3839
)
@@ -65,8 +66,8 @@ def __init__(self, driver: "QtDriver", tracker: MissingRegistry):
6566

6667
def refresh_list(self):
6768
self.desc_widget.setText(
68-
Translations["entries.unlinked.delete.confirm"].format(
69-
count=self.tracker.missing_file_entries_count
69+
Translations.format(
70+
"entries.unlinked.delete.confirm", count=self.tracker.missing_file_entries_count
7071
)
7172
)
7273

tagstudio/src/qt/modals/drop_import.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ def ask_duplicates_choice(self):
131131
self.desc_widget.setText(
132132
Translations["drop_import.duplicates_choice.singular"]
133133
if len(self.duplicate_files) == 1
134-
else Translations["drop_import.duplicates_choice.plural"].format(
135-
count=len(self.duplicate_files)
134+
else Translations.format(
135+
"drop_import.duplicates_choice.plural", count=len(self.duplicate_files)
136136
)
137137
)
138138

@@ -154,11 +154,10 @@ def begin_transfer(self, choice: DuplicateChoice | None = None):
154154
return
155155

156156
def displayed_text(x):
157-
return Translations[
157+
return Translations.format(
158158
"drop_import.progress.label.singular"
159159
if x[0] + 1 == 1
160-
else "drop_import.progress.label.plural"
161-
].format(
160+
else "drop_import.progress.label.plural",
162161
count=x[0] + 1,
163162
suffix=f" {x[1]} {self.choice.value}" if self.choice else "",
164163
)

tagstudio/src/qt/modals/fix_dupes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ def set_dupe_count(self, count: int):
114114
self.dupe_count.setText(Translations["file.duplicates.matches_uninitialized"])
115115
elif count == 0:
116116
self.mirror_button.setDisabled(True)
117-
self.dupe_count.setText(Translations["file.duplicates.matches"].format(count=count))
117+
self.dupe_count.setText(Translations.format("file.duplicates.matches", count=count))
118118
else:
119119
self.mirror_button.setDisabled(False)
120-
self.dupe_count.setText(Translations["file.duplicates.matches"].format(count=count))
120+
self.dupe_count.setText(Translations.format("file.duplicates.matches", count=count))
121121

122122
@override
123123
def keyPressEvent(self, event: QtGui.QKeyEvent) -> None: # noqa N802

tagstudio/src/qt/modals/fix_unlinked.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def set_missing_count(self, count: int | None = None):
135135
self.search_button.setDisabled(self.missing_count == 0)
136136
self.delete_button.setDisabled(self.missing_count == 0)
137137
self.missing_count_label.setText(
138-
Translations["entries.unlinked.missing_count.some"].format(count=self.missing_count)
138+
Translations.format("entries.unlinked.missing_count.some", count=self.missing_count)
139139
)
140140

141141
@override

tagstudio/src/qt/modals/mirror_entities.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self, driver: "QtDriver", tracker: DupeRegistry):
3232
self.tracker = tracker
3333

3434
self.desc_widget = QLabel(
35-
Translations["entries.mirror.confirmation"].format(count=self.tracker.groups_count)
35+
Translations.format("entries.mirror.confirmation", count=self.tracker.groups_count)
3636
)
3737
self.desc_widget.setObjectName("descriptionLabel")
3838
self.desc_widget.setWordWrap(True)
@@ -63,7 +63,7 @@ def __init__(self, driver: "QtDriver", tracker: DupeRegistry):
6363

6464
def refresh_list(self):
6565
self.desc_widget.setText(
66-
Translations["entries.mirror.confirmation"].format(count=self.tracker.groups_count)
66+
Translations.format("entries.mirror.confirmation", count=self.tracker.groups_count)
6767
)
6868

6969
self.model.clear()
@@ -72,8 +72,8 @@ def refresh_list(self):
7272

7373
def mirror_entries(self):
7474
def displayed_text(x):
75-
return Translations["entries.mirror.label"].format(
76-
idx=x + 1, count=self.tracker.groups_count
75+
return Translations.format(
76+
"entries.mirror.label", idx=x + 1, count=self.tracker.groups_count
7777
)
7878

7979
pw = ProgressWidget(

tagstudio/src/qt/modals/relink_unlinked.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def __init__(self, tracker: MissingRegistry):
1818

1919
def repair_entries(self):
2020
def displayed_text(x):
21-
return Translations["entries.unlinked.relink.attempting"].format(
21+
return Translations.format(
22+
"entries.unlinked.relink.attempting",
2223
idx=x,
2324
missing_count=self.tracker.missing_file_entries_count,
2425
fixed_count=self.tracker.files_fixed_count,

tagstudio/src/qt/modals/tag_database.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ def delete_tag(self, tag: Tag):
6060
message_box = QMessageBox(
6161
QMessageBox.Question, # type: ignore
6262
Translations["tag.remove"],
63-
Translations["tag.confirm_delete"].format(
64-
tag_name=self.lib.tag_display_name(tag.id),
65-
),
63+
Translations.format("tag.confirm_delete", tag_name=self.lib.tag_display_name(tag.id)),
6664
QMessageBox.Ok | QMessageBox.Cancel, # type: ignore
6765
)
6866

tagstudio/src/qt/modals/tag_search.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ def set_driver(self, driver):
118118
"""Set the QtDriver for this search panel. Used for main window operations."""
119119
self.driver = driver
120120

121-
def build_create_button(self, query: str | None, key: str, format_args: dict):
121+
def build_create_button(self, query: str | None):
122122
"""Constructs a "Create & Add Tag" QPushButton."""
123-
create_button = QPushButton(Translations[key].format(**format_args), self)
123+
create_button = QPushButton(self)
124124
create_button.setFlat(True)
125125

126126
create_button.setMinimumSize(22, 22)
@@ -244,7 +244,8 @@ def update_tags(self, query: str | None = None):
244244

245245
# Add back the "Create & Add" button
246246
if query and query.strip():
247-
cb: QPushButton = self.build_create_button(query, "tag.create_add", {"query": query})
247+
cb: QPushButton = self.build_create_button(query)
248+
cb.setText(Translations.format("tag.create_add", query=query))
248249
with catch_warnings(record=True):
249250
cb.clicked.disconnect()
250251
cb.clicked.connect(lambda: self.create_and_add_tag(query or ""))

tagstudio/src/qt/translations.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from collections import defaultdict
12
from pathlib import Path
3+
from typing import Any
24

35
import structlog
46
import ujson
@@ -27,6 +29,23 @@ def change_language(self, lang: str):
2729
self._lang = lang
2830
self._strings = self.__get_translation_dict(lang)
2931

32+
def __format(self, text: str, **kwargs) -> str:
33+
try:
34+
return text.format(**kwargs)
35+
except (KeyError, ValueError):
36+
logger.error(
37+
"[Translations] Error while formatting translation.",
38+
text=text,
39+
kwargs=kwargs,
40+
language=self._lang,
41+
)
42+
params: defaultdict[str, Any] = defaultdict(lambda: "{unknown_key}")
43+
params.update(kwargs)
44+
return text.format_map(params)
45+
46+
def format(self, key: str, **kwargs) -> str:
47+
return self.__format(self[key], **kwargs)
48+
3049
def __getitem__(self, key: str) -> str:
3150
return self._strings.get(key) or self._default_strings.get(key) or f"[{key}]"
3251

0 commit comments

Comments
 (0)