I want to display one string in its original source (untranslated)
version and in its translated version site by site without duplicating
the string in the python source code?
UnboundLocalError: local variable '_' referenced before assignment
The question is if this can be solved somehow or if there is an alternative approach.
On Aug 17, 2023, at 10:02 AM, c.buhtz--- via Python-list <[email protected]> wrote:mockup.
X-Post: https://stackoverflow.com/q/76913082/4865723
I want to display one string in its original source (untranslated) version and in its translated version site by site without duplicating the string in the python source code?
It wouldn't be a big deal if it is only one word.
print('The translated string "{}" is originally "{}".'.format(_('Hello'), 'Hello'))
But in my situation it is a multi line string containing multiple paragraphs. It is a full text. I don't want to duplicate that string.
# Imagine 'Hello' as a 50x70 characters multi line string.
original = 'Hello'
translated = _('Hello')
print('The translated string "{}" is originally "{}".'.format(translated, original))
I do use the "class based API" of GNU gettext. My current approach, which is not working, is to somehow (how!?) disable (or mask) the translation function "_()" temporarily.
But as described in the stackoverflow question (see first line of this mail) this do not work.
def foobar(translate):
if not translate:
# I try to mask the global _() builtins-function
def _(txt):
return txt
return _('Hello')
if __name__ == '__main__':
# To ilustrate that _() is part of "builtins" namespace
print(_('No problem.'))
print('The translated string "{}" is originally "{}".'
.format(foobar(True), foobar(False)))
This is the output:
Traceback (most recent call last):
File "/home/user/ownCloud/_transfer/./z.py", line 27, in <module>
.format(foobar(True), foobar(False)))
File "/home/user/ownCloud/_transfer/./z.py", line 19, in foobar
return _('Hello')
UnboundLocalError: local variable '_' referenced before assignment
The full MWE can be found at stackoverflow (https://stackoverflow.com/q/76913082/4865723).
The question is if this can be solved somehow or if there is an alternative approach.
The "_()" function is installed in the builtins namespace because of gettext class based API. This is nice.
Maybe I can somehow manipulate that builtins namespace? I tried to import builtins and played around with it but couldn't find a way to do it.
Thanks
Christian Buhtz
PS: This is IMHO not relevant for my question but if someone is interested the connection to productive code can be found in this issue: https://github.com/bit-team/backintime/issues/1473 There I describe what I want to achive and also provide a GUI
--
https://mail.python.org/mailman/listinfo/python-list
I want to display one string in its original source (untranslated)
version and in its translated version site by site without duplicating
the string in the python source code?
You could solve it by defining _() locally like so:
def foobar(translate):
_ = gettext.gettext
def orig_and_trans(msg):
return (_(msg), msg)
Hello Mirko,
thanks for reply.
Am 17.08.2023 18:19 schrieb Mirko via Python-list:
You could solve it by defining _() locally like so:
def foobar(translate):
_ = gettext.gettext
I see no way to do that. It is not the "class based API" of gettext installing _() into the builtins namespace.
My users are able to configure the language of their UI explicit. It
is a full application.
def orig_and_trans(msg):
return (_(msg), msg)
This will be ignored by GNU gettext utils (xgettext in my case) will
ignore this line because they do not know what "msg" is. The string
"hello" won't appear in the pot-file.
def foobar(translate):
if not translate:
# I try to mask the global _() builtins-function
def _(txt):
return txt
return _('Hello')
On 17 Aug 2023, at 15:01, c.buhtz--- via Python-list <[email protected]> wrote:
I want to display one string in its original source (untranslated) version and in its translated version site by site without duplicating the string in the python source code?
It wouldn't be a big deal if it is only one word.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 153:56:29 |
| Calls: | 12,091 |
| Calls today: | 4 |
| Files: | 15,000 |
| Messages: | 6,517,674 |