Re: FormCode i18n - TurboGears
Ian Bicking <[email protected]>
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
Gregor Horvath wrote:
> 1. Message translation time
>
> The straightforward and standard way of i18n would be to pack the
> strings in validators.py into the function _("some text").
> The problem is that FormEncode executes this function at import time of
> validators.py (because of __classinit__).
> Therefore it is not possible not change the language or the gettext
> function after the import with this approach.
> Also at this time TG's i18n module is not installed yet. TG integration
> is therefore not possible.
OK, I figured this wouldn't work so I'm glad you can confirm that.
> 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.
I think it can go something like this (in formencode.api):
try:
_
except NameError:
def _(msg): return msg
class Validator(object): ...
def message(self, msgName, state, **kw):
try:
msg = self._messages[msgName]
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())))
if hasattr(state, '_'):
msg = state._(msg)
else:
msg = _(msg)
return msg % kw
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
the messages (e.g., from a "get_i18n_messages" function or something)
that would be even better, but I don't know if there's conventions for
that sort of thing.
--
Ian Bicking | [email protected] | http://blog.ianbicking.org
-------------------------------------------------------------------------
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