Opened 11 years ago
Last modified 9 years ago
#11338 new enhancement
Switch language based on URL prefix — at Initial Version
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | normal | Milestone: | undecided |
Component: | i18n | Version: | 1.0.1 |
Severity: | trivial | Keywords: | patch |
Cc: | olemis+trac@…, leho@… | Branch: | |
Release Notes: | |||
API Changes: | |||
Internal Changes: |
Description
Hello,
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 RequestDispatcher
method
_get_locale:
def _get_locale(self, req): if has_babel: language_prefix_regexp = re.compile('^/([A-Za-z\-]+)/') match = self.language_prefix_regexp.match(req.base_path) if req.base_path else None language_prefix = match.groups()[0] 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)