durusmail: quixote-users: Quixote and gettext()
Quixote and gettext()
2004-10-06
2004-10-06
2004-10-06
Quixote and gettext()
Daniele Varrazzo
2004-10-06
> Seems that python's _ method (the builtin one, via the install() method)
> is only for strings, but in the second case the "Hello" is no longer a
> string.

> What is your method to i10n/i18n Quixote Applications?
I am writing a multilingual application with Quixote and i didn't get this
trouble. I use Quixote 1.0 with Python 2.3.4 under windows 2000.

I wanted language selection through an URL segment, so i wrote a
MultiTranslation class encapsulating a dict of GNUTranslations instances that
get selected through a function taking no argument and returning the "languages"
argument of gettext.translation().
The application url pace is something like
http://host/appname/[XX/]entity/[id/]verb
where XX is a language code. While parsing the url, if i find the language
selector i add a "language" property to the request object (of course, no entity
name must be 2 chars long). My application's selection function performs the
selection by issuing a get_request() and reading if the language property was
set.

>>> from quixote import enable_ptl
>>> enable_ptl()
>>> import espweb
>>> espweb.translation.set_lang_function(lambda: ['it'])
>>> espweb.translation.gettext("Creation date")
'Data di creazione'
>>> espweb.translation.gettext(htmltext("Creation date"))
'Data di creazione'

The end of the psalm is a call to GNUTranslations.gettext - that of course is
aliased  as _ . I don't use the one provided with install: try to alias it by
hand with something like
    from mymodule import translation
    _ = translation.gettext

>>> t = espweb.translation._translations['it']
>>> t

>>> t.gettext(htmltext("Creation date"))
'Data di creazione'

My .po file is utf-8 encoded, but the initial EF BB BF sequence creates trouble
to the msgfmt.py tool, so i avoid it. The strings can be returned as unicode:
>>> print t.ugettext(htmltext("If no user is specified, a new realm will be
created"))
Se non e specificato alcun utente verra creato un nuovo reame

Now, the unicode issue... I really hoped for unicode support in Quixote 1.1!
- i read the gettext string through gettext instead of ugettext, so i have 8-bit
strings.;
- i encoded the backend database (postgresql through psycopg) in unicode but
read it as an 8-bit database, getting the utf-8 encoding of the strings;
- i set response.set_content_type("text/html; charset=utf-8") to my pages.

The second point is ugly: i'd like unicode strings from the db of course. But by
now it's not an issue because the library i wrote is only used in the web
interface. So, there is a
#psycopg.extensions.register_type(psycopg.extensions.UNICODE)
waiting to be uncommented...

Regards,

Daniele


reply