Edgewall Software
Modify

Opened 6 years ago

Last modified 4 years ago

#11338 new enhancement

[Patch] Switch language based on URL prefix

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:

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)

Attachments (1)

t11338_language_prefix.diff (1.1 KB ) - added by SaintGermain <saintger@…> 6 years ago.
Patch to set language based on URL prefix

Download all attachments as: .zip

Change History (9)

comment:1 by Ryan J Ollos, 6 years ago

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

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

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

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

Cc: olemis+trac@… added

comment:4 by anonymous, 6 years ago

Description: modified (diff)

by SaintGermain <saintger@…>, 6 years ago

Attachment: t11338_language_prefix.diff added

Patch to set language based on URL prefix

comment:5 by SaintGermain <saintger@…>, 6 years ago

Ok I created a patch on the current 1.0.2dev changeset. Hope that it is OK !

comment:6 by lkraav <leho@…>, 6 years ago

Cc: leho@… added

comment:7 by Ryan J Ollos, 5 years ago

Keywords: patch added

comment:8 by figaro, 4 years ago

Summary: Switch language based on URL prefix[Patch] Switch language based on URL prefix

Modify Ticket

Change Properties
Set your email in Preferences
Action
as new The ticket will remain with no owner.
The ticket will be disowned. Next status will be 'new'.
as The resolution will be set. Next status will be 'closed'.
The owner will be changed from (none) to anonymous. Next status will be 'assigned'.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.