FormEncode i18n translation integration in TurboGears

Gregor Horvath <gh-LfrpOBifzZo+ytmZ3SK/[email protected]>
Newsgroups gmane.comp.python.formencode,gmane.comp.web.turbogears
Organization Ing. Gregor Horvath, Industrieberatung & Softwareentwicklung
Message-ID <[email protected]>
Gregor Horvath schrieb:
> 
> The problem is that as long as I understand there is one message.mo file
> for a TG project. This includes the costum messages of a TG Project. Now
> I have a FormEncode.mo for the Formencode validators messages (and
> probably another Validators.mo or similar for TG's validators). How can
> I merge those files, or how can TG's translation function be instructed
> to use all *.mo files instead just the messages.mo?
> 
> Is there a mechansim I am not aware of or has this to be done?
> 

I checked the TG source and did not find a standard way, except that it
is possible to give the domain (and with it the filename
                     ) to the gettext function.

Therefore it is possible to create a message.mo file for the costum
messages of a specific TG Project (no change in this process), and in
the same directory (locales) a FormEncode.mo which includes the
translations of the standard FormEncode messages and a TG.mo for TG's
standard validator messages.
TG should install FormEncode.mo and TG.mo automatically with tg-admin
quickstart.

To enable this there must be a way of telling FormEncode to call TG's
gettext function with the argument domain=FormEncode (so the
FormEncode.mo file is used for translation instead of the standard
messages.mo)

I therefore propose a new class attribute: formencode.Validator.gettextargs
For standalone FormEncode this attribute is {}. (Because standard
gettext function does not allow additional parameters)

<formencode.api.py>
class Validator(declarative.Declarative):
  [....]
  gettextargs = {}
  [...]

  def message(self, msgName, state, **kw):
        #determine translation function
        try:
            trans = state._
        except AttributeError:
            try:
                import __builtin__
                trans = __builtin__._
            except AttributeError:
                trans = _stdtrans

        try:
            return trans(self._messages[msgName], **self.gettextargs) % kw
        except KeyError, e:
            raise KeyError(
                "Key not found (%s) for %r=%r %% %r (from: %s)"
                % (e, msgName, self._messages.get(msgName), kw,
                   ', '.join(self._messages.keys())))

</formencode.api.py>


TG has to setup this argument in turbogears.validators.py:

<turbogears.validators.py>
....
from formencode.validators import *
....

Validator.gettextargs = {'domain':'FormEncode'}

</turbogears.validators.py>



Is this concept OK?

The protoype works.
If it's OK I am going to make a patch to TG's trunk and apply the
changes to http://svn.colorstudy.com/FormEncode/branches/gettext-enabled/

--
Greg



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.