Chapter 20.3.3. Auto-Detect User Language Preference

The turbogears.i18n.get_locale() function tries to guess the user’s locale. It looks for locale information in several places: in the session, the HTTP Accept-Language header, and finally the i18n.default_locale configuration option. If none of these places contains a locale, TurboGears will fall back to a default locale of “en.”

If the default lookup algorithm doesn’t suit you (for instance, if you store locale information in the database), you can specify your own algorithm by assigning a lookup function to the i18n.get_locale configuration variable. The function is parameterless and returns a locale name.

Example:

def get_locale_from_db(): user = identity.current.user if user and ...

Get Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.