[Patch] Switch language based on URL prefix
|Reported by:||Owned by:|
Description (last modified by )
I would like to propose my first patch to Trac. The target is to switch language according to this principle: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-language-preference
I worked with Trac version 1.0.1 on a Debian Testing.
I modified the file trac/web/main.py and class
def _get_locale(self, req): if has_babel: language_prefix_regexp = re.compile('^/([A-Za-z\-]+)/') match = language_prefix_regexp.match(req.base_path) if req.base_path else None language_prefix = match.groups() if match else None preferred = req.session.get('language') default = self.env.config.get('trac', 'default_language','') negotiated = get_negotiated_locale([language_prefix, preferred, default] + req.languages) self.log.debug("Negotiated locale: %s -> %s", preferred, negotiated) return negotiated
Basically it changes the language if there is a language prefix. Of course it may be a problem for site which doesn't use language prefix but which have an URL similar to a language prefix. So we may need to make that an option (but I don't know yet how to do that)