Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Remember, there's a big difference between kneeling down and bending over. -- Frank Zappa


devel / comp.lang.python / GNU gettext: Print string translated and untranslated at the same time

SubjectAuthor
* GNU gettext: Print string translated and untranslated at the same timec.buhtz
`- Re: GNU gettext: Print string translated and untranslated at the sameGreg Ewing

1
GNU gettext: Print string translated and untranslated at the same time

<mailman.235.1692280745.23016.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23887&group=comp.lang.python#23887

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: c.bu...@posteo.jp
Newsgroups: comp.lang.python
Subject: GNU gettext: Print string translated and untranslated at the same time
Date: Thu, 17 Aug 2023 07:10:32 +0000
Lines: 68
Message-ID: <mailman.235.1692280745.23016.python-list@python.org>
References: <2ecc54498066930d20ada6343bf4374d@posteo.de>
Reply-To: python-list@python.org
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII;
format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de hKeq8lUO5q6gwN0JQ74s9gv32UqLmgycmsw6hku4HW+w==
Cancel-Lock: sha1:pCfLl8GemREzxcMTVtoJrQPY8Tg= sha256:kWks6y2r0n2IdCu0+nwe0FU1FMLF25y32BOpugQhHnQ=
Return-Path: <c.buhtz@posteo.jp>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url-ip:140.82/16': 0.03;
'def': 0.04; 'traceback': 0.04; '(most': 0.05; 'containing': 0.05;
'gui': 0.05; 'last):': 0.05; 'variable': 0.05; 'string': 0.07;
'27,': 0.09; 'code?': 0.09; 'describe': 0.09; 'namespace': 0.09;
'originally': 0.09; 'output:': 0.09; 'text.': 0.09; 'url:github':
0.14; 'import': 0.15; 'url-ip:140/8': 0.15; 'api.': 0.16;
'approach,': 0.16; 'approach.': 0.16; 'skip:. 20': 0.16; 'solved':
0.16; 'subject:GNU': 0.16; 'subject:string': 0.16; 'python': 0.16;
'gnu': 0.19; 'to:addr:python-list': 0.20; 'maybe': 0.22;
'version': 0.23; 'code': 0.23; 'installed': 0.23; 'received:de':
0.23; 'tried': 0.26; 'local': 0.27; 'function': 0.27; 'question':
0.32; 'christian': 0.32; 'manipulate': 0.32; "wouldn't": 0.32;
'but': 0.32; 'there': 0.33; 'someone': 0.34; 'skip:" 20': 0.34;
'work.': 0.34; 'display': 0.36; 'source': 0.36; 'class': 0.37;
'file': 0.38; 'way': 0.38; 'thanks': 0.38; 'use': 0.39; '(see':
0.40; "couldn't": 0.40; 'productive': 0.40; 'situation': 0.40;
'want': 0.40; 'try': 0.40; 'provide': 0.60; 'connection': 0.61;
'skip:b 10': 0.63; 'full': 0.64; 'imagine': 0.64; 'url:q': 0.67;
'interested': 0.68; 'ps:': 0.69; 'translated': 0.69; 'working,':
0.69; 'site': 0.70; 'received:185': 0.71; 'global': 0.73; 'deal':
0.73; 'relevant': 0.73; 'reply-to:no real name:2**0': 0.78;
'header:Reply-To:1': 0.79; 'characters': 0.84; 'multi': 0.84;
'referenced': 0.84; 'subject: \n ': 0.84; 'word.': 0.91;
'duplicate': 0.93
Mail-Reply-To: python-list@python.org
X-Mailman-Approved-At: Thu, 17 Aug 2023 09:59:05 -0400
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <2ecc54498066930d20ada6343bf4374d@posteo.de>
 by: c.bu...@posteo.jp - Thu, 17 Aug 2023 07:10 UTC

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 mockup.

Re: GNU gettext: Print string translated and untranslated at the same time

<kk8ginFm6o7U1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23898&group=comp.lang.python#23898

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: greg.ew...@canterbury.ac.nz (Greg Ewing)
Newsgroups: comp.lang.python
Subject: Re: GNU gettext: Print string translated and untranslated at the same
time
Date: Fri, 18 Aug 2023 18:14:44 +1200
Lines: 25
Message-ID: <kk8ginFm6o7U1@mid.individual.net>
References: <2ecc54498066930d20ada6343bf4374d@posteo.de>
<mailman.235.1692280745.23016.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net 9S2RpDR8krfLrvQyKiXdsA1egYEG49K9FrV/NxiP8IpPDhXqHL
Cancel-Lock: sha1:xy1MxYJb9MkYKxy8oP3r19GdvlE= sha256:UfrJg7RSSIHQONGnJC+D/fBWru3+Ta3w0l4splWwH8s=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:91.0)
Gecko/20100101 Thunderbird/91.3.2
Content-Language: en-US
In-Reply-To: <mailman.235.1692280745.23016.python-list@python.org>
 by: Greg Ewing - Fri, 18 Aug 2023 06:14 UTC

On 17/08/23 7:10 pm, c.buhtz@posteo.jp wrote:
> def foobar(translate):
>     if not translate:
>         # I try to mask the global _() builtins-function
>         def _(txt):
>             return txt
>
>     return _('Hello')

This causes _ to become a local that is left undefined on one
branch of the if. You need to ensure it's always defined. Here's
one way that should work:

gttran = _

def foobar(translate):
def _(txt):
if translate:
return gttran(txt)
else:
return txt
return _('Hello')

--
Greg

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor