Edgewall Software

Opened 11 years ago

Last modified 9 years ago

#11338 new enhancement

Switch language based on URL prefix — at Version 4

Reported by: saintger@… 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 (last modified by anonymous)

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 = 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)

Change History (5)

comment:1 by Ryan J Ollos, 11 years ago

If possible, would you kindly create a patch as described in TracDev/SubmittingPatches?

comment:2 by Olemis Lang <olemis+trac@…>, 11 years ago

… if you need some help writing tests then I could help with that .

comment:3 by Olemis Lang <olemis+trac@…>, 11 years ago

Cc: olemis+trac@… added

comment:4 by anonymous, 11 years ago

Description: modified (diff)

by SaintGermain <saintger@…>, 11 years ago

Attachment: t11338_language_prefix.diff added

Patch to set language based on URL prefix

Note: See TracTickets for help on using tickets.