Skip to content

Instantly share code, notes, and snippets.

@roccogalluzzo
Created June 21, 2010 07:40
Show Gist options
  • Save roccogalluzzo/446548 to your computer and use it in GitHub Desktop.
Save roccogalluzzo/446548 to your computer and use it in GitHub Desktop.
from routes import Mapper
class CustomMapper(Mapper):
def match(self, url= None, environ= None):
if not url and not environ:
raise RoutesException('URL or environ must be provided')
if not url:
url = environ['PATH_INFO']
url, lang = self._get_language(url)
result = self._match(url, environ)
if result[0]:
result[0]['lang'] = lang
if self.debug:
return result[0], result[1], result[2]
if isinstance(result[0], dict) or result[0]:
return result[0]
return None
def routematch(self, url=None, environ=None):
if not url and not environ:
raise RoutesException('URL or environ must be provided')
if not url:
url = environ['PATH_INFO']
url, lang = self._get_language(url)
result = self._match(url, environ)
if result[0]:
result[0]['_lang'] = lang
if self.debug:
return result[0], result[1], result[2]
if isinstance(result[0], dict) or result[0]:
return result[0], result[1]
return None
def detect_language(self, url):
langs = ['it','en']
if len(url) > 3 and url[3]=='/' and url[1:3] in langs[1:]:
return url[3:], url[1:3]
return url, langs[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment