Re: FormCode i18n - TurboGears

Gregor Horvath <gh-LfrpOBifzZo+ytmZ3SK/[email protected]>
Newsgroups gmane.comp.python.formencode
Organization Ing. Gregor Horvath, Industrieberatung & Softwareentwicklung
Message-ID <[email protected]>
Ian Bicking schrieb:
> 
>>3. Translation in message
>>
>>Another approach is to translate the message on the fly with an outside
>>gettext function like it is done in the patch from January.
>>
>>The problem is that this does not work for using FormEncode standalone.
>>Also I do not see how to extract the messages automatically when there
>>are no _("") functions. Also one has to fiddle with the state object,
>>and TG has to supply it along with a gettext function.
> 
> 
> This seems like the best solution, though _() still confused me in the 
> previous patch.  I'd really like someone to revisit that (I won't be 
> able to), but that patch was still a bit fuzzy to me.
> 

TG installs a _ function in builtins.
I would prefer to determine that in the message function.
Then it is not necessary to submit a gettext function to the state object.
For standalone FormEncode I propose a fallback standard gettext
function. (gets the language from the local system)

import gettext
t = gettext.translation(domain='FormEncode', \
                            languages=None, \
                            fallback=True)
stdtrans = t.ugettext


class Validator(declarative.Declarative):

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

        try:
            return trans(self._messages[msgName]) % 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())))


With this FormEncode can be used:

* with a gettext function provided in the state object

* with a gettext function provided in the builtin namespace

* with a translation provided in the domain FormEncode for standalone
use, just place the compiled files in
/usr/share/locale/<lang>/LC_MESSAGES/FormEncode.mo

* without any translation (fallback in stdtrans returns the plain
string), just like now

> 
> Additionally _() can be used around message in validators.py etc, so 
> that extraction tools work, but that's generally all it will be used 
> for.  If there was a tool that could actually import the module and read 

ok. Then in validators.py a dummy _ function must be added

def _(s): return s


I have a working prototype of the concept working, if this is ok for you
I would make a patch and testing/finalization.

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