gnue-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnue-dev] gettext & codeset


From: Dmitry Sorokin
Subject: [Gnue-dev] gettext & codeset
Date: Sun, 17 Nov 2002 00:45:34 +0300 (MSK)

Hello all,

Below is the answer from one of the python gettext authors.

I was looking into gettext thing as i was trying to avoid to have 2
translation catalogs for 2 platforms.

It is available but require to change

common/GBaseApp.py from:

gettext.translation('gnue',GConfig.getInstalledBase('install_prefix')+'/translations',languages=lang).install()

   to:

gettext.translation('gnue',GConfig.getInstalledBase('install_prefix')+'/translations',languages=lang).install(unicode)


It gives then a lot more unicode errors on the login, splashscreen, menu
initialising stages.

Then i found that  bind_textdomain_codeset (from GNU gettext) would work
for our _('msg') like setting appropriate encoding in site.py but
it is missed in python gettext module.

Now will look into Martin's answer myself.


Dmitry


-------------



Dmitry Sorokin <address@hidden> writes:

> how to reach what GNU gettext bind_textdomain_codeset function is for
> with python gettext module?

Dmitry,

I recommend to use ugettext instead, or wrap it
appropriately. ugettext will always return Unicode objects (assuming
that the codeset of the .mo file was properly declared in the .mo
header).

> I'd like to use translation catalog which is in koi8-r encoding with gui
> that expects either koi8-r or cp1251.

Then I recommend to do something like this

catalog = gettext.translation("domainname")
runtime_charset = "cp1251"
def _(msg):
  return catalog.ugettext(msg).encode(runtime_charset)

Changing runtime_charset will then arrange for _ to return
differently-encoded messages.

> The problem is that we are trying to leave encoding = "ascii" in
> site.py as it is by default and assign needed encoding at run time.

That is a good idea.

> Without bind_textdomain_codeset we need to put something like
> _('Text String').encode(neededencoding) in too many places

Not necessarily. Even without ugettext, you could always do

def _(msg):
  gettext.dgettext(domain).decode("koi8-r").encode("cp1251")

That, of course, requires you to hard-code the codeset of the catalog,
which is undesirable - ugettext finds out the codeset of the catalog
dynamically.

Remember: _ is just a function. This function can do any computation
you want.

> Sorry for not asking in newsgroup but personally.

That is ok (in this specific case :-).

If you think bind_textdomain_codeset is a better interface than
ugettext, I encourage you to enhance gettext.py in this respect.  I
would add another dictionary, next to _localedirs; say,
_localecodesets. Then, add a gettext_codeset method to GNUTranslation,
which does recoding if necessary, and use that in dgettext if a
_localecodeset has been set for the domain.

You might also consider adding a codeset argument to install, which
would (if recoding is needed) install a lambda function like

   def install(self, unicode=0, codeset = None):
         import __builtin__
        if unicode:
           func = self.ugettext
        elif codeset and codeset != self.charset:
           def func(msg):
             self.gettext_codeset(msg, codeset)
        else:
           func
        __builtin__.__dict__['_'] = func

If you do implement these enhancements, don't hesitate to contribute
them back!

Regards,
Martin






reply via email to

[Prev in Thread] Current Thread [Next in Thread]