[spesmilo/electrum] Module-level internationalized strings always return the english value (#4621)

Something like https://github.com/spesmilo/electrum/blob/master/electrum/gui/qt/installwizard.py#L23

«`python
MSG_ENTER_PASSWORD = _(«Choose a password to encrypt your wallet keys.») + ‘\n’\
+ _(«Leave this field empty if you want to disable encryption.»)
«`

will immediately be evaluated on import. However, the language is not set at that point, it only happens right before the GUI starts up.

Possible solutions:
1. Move such definitions to an class method

2. Make `_()` return an object that has a `__str__` method which handles the translation. This way the translation will be evaluated each time this is used in a string context

«`python
class FutureI18nString(object):
def __init__(self, template):
self.template = [template]

def __add__(self, right):
self.template.append(right)

def __str__(self):
return ».join([i18n.language.gettext(t) for t in self.template])
«`

3. Change the definition to be a lambda function instead, that has to be evaluated, each time it is used:

«`python
MSG_ENTER_PASSWORD = lambda: _(«Choose a password to encrypt your wallet keys.») + ‘\n’\
+ _(«Leave this field empty if you want to disable encryption.»)

print(MSG_ENTER_PASSWORD())
«`

Добавить комментарий