After spending several hours on that issue, I thought it might be a good idea 
to document these
gotchas. As 1.0/Internationalization has the status "official", I can't add it 
myself.

fs


Custom validators with localized error messages
-----------------------------------------------

If you write custom validators and use _("...") to localize your messages you
will get a TypeError exception when starting your app ("TypeError: Error when
calling the metaclass bases - expected string or buffer").

You need to add a small "i18n hack" to your validator as `demonstrated by 
Christoph Zwerschke`_::

     class NoWar(validators.FancyValidator):
          def _(s): return s
          gettextargs = {'domain': 'messages'}
          messages = {'war': _("Don't mention the war!")}

          def validate_python(self, value, state):
              if 'war' in value:
                  raise validators.Invalid(
                      self.message('war', state), value, state)

Please note that you need at least TurboGears 1.0.2 and FormEncode 0.7 for
localized error messages.

.. _demonstrated by Christoph Zwerschke: 
http://groups.google.com/group/turbogears/msg/61831c58b4684ac4


Doing i18n without using TurboGears session module
--------------------------------------------------

You don't need to use the session module from turbogears to get translated
messages. You can insert a custom function which returns a locale string like
"de" into the TurboGears config which controls which locale is used when
TurboGears needs to translated something.

The configuration change should take place in your start script create by
"tg-admin quickstart" (e.g. "start-foo.py" in your top-level project directory).
Assuming a very simple scenario where you support only one language, this may be
as easy as this::

     $ cat start-foo.py

     (...)
     from foo.controllers import Root

     config.update({'i18n.get_locale' : lambda: "de"})

     start_server(Root())

After this modification, all localized strings should be displayed in German.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears Docs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears-docs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to