Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Feature/guessing language via accept language header #184

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
29 changes: 23 additions & 6 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,33 @@ def selectLanguage( self, path ):
tmppath = [ urlparse.unquote( x ) for x in tmppath.lower().strip("/").split("/") ]
if len( tmppath )>0 and tmppath[0] in conf["viur.availableLanguages"]+list( conf["viur.languageAliasMap"].keys() ):
self.language = tmppath[0]
return( path[ len( tmppath[0])+1: ] ) #Return the path stripped by its language segment
return path[ len( tmppath[0])+1: ] # Return the path stripped by its language segment
else: # This URL doesnt contain an language prefix, try to read it from session
if session.current.getLanguage():
self.language = session.current.getLanguage()
elif "X-Appengine-Country" in self.request.headers.keys():
sessionLang = session.current.getLanguage()
if sessionLang:
self.language = sessionLang
return path
if "Accept-Language" in self.request.headers:
acceptLangHeader = self.request.headers["Accept-Language"]
if acceptLangHeader:
acceptLangHeader = acceptLangHeader.split(",")
# we only accept up to seven language entries here.
for possibleLang in acceptLangHeader[:7]:
lng = possibleLang.split("-")[0]
if lng in conf["viur.availableLanguages"] or lng in conf["viur.languageAliasMap"]:
self.language = lng
return path
elif ";" in lng:
lng, weight = lng.split(";")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one more small thing. This Line can also fail. And you don't use weight at all. So also use lng.split(";")[0] - which doesn't break if there are more than two colons in that string

if lng in conf["viur.availableLanguages"] or lng in conf["viur.languageAliasMap"]:
self.language = lng
return path
if "X-Appengine-Country" in self.request.headers.keys():
lng = self.request.headers["X-Appengine-Country"].lower()
if lng in conf["viur.availableLanguages"] or lng in conf["viur.languageAliasMap"]:
self.language = lng
return( path )

return path
return path

def processRequest( self, path, *args, **kwargs ): #Bring up the enviroment for this request, handle errors
self.internalRequest = False
Expand Down