-
Notifications
You must be signed in to change notification settings - Fork 4
Feature/guessing language via accept language header #184
base: develop
Are you sure you want to change the base?
Changes from 1 commit
d6caa01
b8db83e
70b0b63
5a8ea8d
e53c744
7bc82c1
0f0d32a
e44afc7
45a5ef2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -360,18 +360,40 @@ def selectLanguage( self, path ): | |
elif conf["viur.languageMethod"] == "url": | ||
tmppath = urlparse.urlparse( path ).path | ||
tmppath = [ urlparse.unquote( x ) for x in tmppath.lower().strip("/").split("/") ] | ||
langSet = False | ||
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 | ||
langSet = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is obsole: You're setting langSet to True and return from the function afterwards. |
||
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(): | ||
langSet = True | ||
if not langSet and "Accept-Language" in self.request.headers: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The control-flow is botched. Instead of carrying the marker langSet, just return path in L371. Same thing at the end of this if block - if found return path. |
||
acceptLangHeader = self.request.headers["Accept-Language"] | ||
if acceptLangHeader: | ||
acceptLangHeader = acceptLangHeader.split(",") | ||
for possibleLang in acceptLangHeader[:7]: | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for try/except here. As you are not interested in regionEtc just call lng = possibleLang.split("-")[0] - which will always work |
||
lng, regionEtc = possibleLang.split("-", 1) | ||
except ValueError: | ||
lng = possibleLang | ||
|
||
if lng in conf["viur.availableLanguages"] or lng in conf["viur.languageAliasMap"]: | ||
self.language = lng | ||
langSet = True | ||
break | ||
elif ";" in lng: | ||
lng, weight = lng.split(";") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
langSet = True | ||
break | ||
if not langSet and "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 | ||
|
||
def processRequest( self, path, *args, **kwargs ): #Bring up the enviroment for this request, handle errors | ||
self.internalRequest = False | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line can be moved into the else part.