Coverage for graphica/gui/widget_translation.py: 91.0%
33 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-12 16:38 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-12 16:38 +0000
1"""組み立てた画面の文字を、あとからまとめて tr() で訳す(Designer の .ui で作った部品にも効かせるため)。
3日本語のときは何もしない。選択肢(QComboBox)は文字そのものを値として保存するものがあるので、ここでは
4番号やデータで値を持つと分かっているものだけを訳す(ほかは作る所で tr() を通す)。
5"""
6from PySide6.QtWidgets import (QAbstractButton, QComboBox, QDockWidget, QGroupBox, QLabel, QLineEdit, QTabWidget,
7 QToolBar, QWidget)
9from graphica.core.i18n import DEFAULT_LANGUAGE, get_language, tr
11# 選んだ番号で値を持つ選択肢(gui/axis_bindings.py の index)。表示を訳しても保存する値は変わらない
12INDEX_BOUND_COMBOS = ("x_major_tick_mode_combo", "y_major_tick_mode_combo")
15def _translate(text):
16 translated = tr(text)
17 return translated if translated != text else None
20def translate_widget_texts(root):
21 if get_language() == DEFAULT_LANGUAGE:
22 return
23 for widget in [root] + root.findChildren(QWidget):
24 tooltip = widget.toolTip()
25 if tooltip and (new := _translate(tooltip)):
26 widget.setToolTip(new)
27 if isinstance(widget, (QLabel, QAbstractButton)):
28 if (new := _translate(widget.text())):
29 widget.setText(new)
30 elif isinstance(widget, QGroupBox):
31 if (new := _translate(widget.title())): 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true
32 widget.setTitle(new)
33 elif isinstance(widget, (QDockWidget, QToolBar)):
34 if (new := _translate(widget.windowTitle())): 34 ↛ 35line 34 didn't jump to line 35 because the condition on line 34 was never true
35 widget.setWindowTitle(new)
36 elif isinstance(widget, QLineEdit):
37 if widget.placeholderText() and (new := _translate(widget.placeholderText())):
38 widget.setPlaceholderText(new)
39 elif isinstance(widget, QComboBox) and widget.objectName() in INDEX_BOUND_COMBOS:
40 for i in range(widget.count()):
41 if (new := _translate(widget.itemText(i))): 41 ↛ 40line 41 didn't jump to line 40 because the condition on line 41 was always true
42 widget.setItemText(i, new)
43 if isinstance(widget, QTabWidget):
44 for i in range(widget.count()):
45 if (new := _translate(widget.tabText(i))): 45 ↛ 44line 45 didn't jump to line 44 because the condition on line 45 was always true
46 widget.setTabText(i, new)