i18n: add "context" param to _(), and use it from qml
fixes https://github.com/spesmilo/electrum/issues/8323 from issue: > Currently, translatable strings from QML are assigned a `context` > by `lupdate`, which is then also used by the conversion to `gettext`. > This `context` must be used when translating such a string. This results in > strings that are unique to QML to not be translated, due to a missing > `context` parameter which we do not take into account in electrum.
This commit is contained in:
@@ -42,7 +42,7 @@ class ElectrumTranslator(QTranslator):
|
|||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
|
|
||||||
def translate(self, context, source_text, disambiguation, n):
|
def translate(self, context, source_text, disambiguation, n):
|
||||||
return _(source_text)
|
return _(source_text, context=context)
|
||||||
|
|
||||||
class ElectrumGui(BaseElectrumGui, Logger):
|
class ElectrumGui(BaseElectrumGui, Logger):
|
||||||
|
|
||||||
|
|||||||
@@ -53,11 +53,22 @@ else:
|
|||||||
# note: f-strings cannot be translated! see https://stackoverflow.com/q/49797658
|
# note: f-strings cannot be translated! see https://stackoverflow.com/q/49797658
|
||||||
# So this does not work: _(f"My name: {name}")
|
# So this does not work: _(f"My name: {name}")
|
||||||
# instead use .format: _("My name: {}").format(name)
|
# instead use .format: _("My name: {}").format(name)
|
||||||
def _(x: str) -> str:
|
def _(msg: str, *, context=None) -> str:
|
||||||
if x == "":
|
if msg == "":
|
||||||
return "" # empty string must not be translated. see #7158
|
return "" # empty string must not be translated. see #7158
|
||||||
global language
|
global language
|
||||||
return language.gettext(x)
|
if context:
|
||||||
|
contexts = [context]
|
||||||
|
if context[-1] != "|": # try with both "|" suffix and without
|
||||||
|
contexts.append(context + "|")
|
||||||
|
else:
|
||||||
|
contexts.append(context[:-1])
|
||||||
|
for ctx in contexts:
|
||||||
|
out = language.pgettext(ctx, msg)
|
||||||
|
if out != msg: # found non-trivial translation
|
||||||
|
return out
|
||||||
|
# else try without context
|
||||||
|
return language.gettext(msg)
|
||||||
|
|
||||||
|
|
||||||
def set_language(x: Optional[str]) -> None:
|
def set_language(x: Optional[str]) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user