[gettext] How to change language at run-time

dieter dieter at handshake.de
Fri Jun 16 02:23:08 EDT 2017


pozz <pozzugno at gmail.com> writes:
> I know I can load multiple gettext.translation:
>
>   it = gettext.translation('test', localedir="locale", languages=["it"])
>   es = gettext.translation('test', localedir="locale", languages=["es"])
>
> and install one translation at run-time when I want at a later time
> (when the user selects a new language):
>
>   it.install()
> or
>   es.install()
>
>
> However the problem is that strings already translated are not
> translated again when a new translation is installed.  So they stay at
> the language selected during start-up and don't change after a new
> install().

I know the internationalization/localisation of the web application framework
"Zope". There, it is completely natural, that the individual request
determines the target language.

This is handled as follows: things to be localized are represented
by so called "message_id"s. They behave somehow as unicode objects, but,
of course, they are more complex; especially, they may encapsulate parameters
to be incorparated in the translation.
When a "message_id" is localized, the target language is taken from
the context (i.e. the current request). It returns a unicode string
but does not change the "message_id".




More information about the Python-list mailing list