def selectLanguage( self, path ):
"""
Tries to select the best language for the current request.
"""
if translations is None:
# This project doesn't use the multi-language feature, nothing to do here
return( path )
if conf["viur.languageMethod"] == "session":
# We store the language inside the session, try to load it from there
if not session.current.getLanguage():
if "X-Appengine-Country" in self.request.headers.keys():
lng = self.request.headers["X-Appengine-Country"].lower()
if lng in conf["viur.availableLanguages"]+list( conf["viur.languageAliasMap"].keys() ):
session.current.setLanguage( lng )
self.language = lng
else:
session.current.setLanguage( conf["viur.defaultLanguage"] )
else:
self.language = session.current.getLanguage()
elif conf["viur.languageMethod"] == "domain":
host = self.request.host_url.lower()
host = host[ host.find("://")+3: ].strip(" /") #strip http(s)://
if host.startswith("www."):
host = host[ 4: ]
if host in conf["viur.domainLanguageMapping"].keys():
self.language = conf["viur.domainLanguageMapping"][ host ]
else: # We have no language configured for this domain, try to read it from session
if session.current.getLanguage():
self.language = session.current.getLanguage()
elif conf["viur.languageMethod"] == "url":
tmppath = urlparse.urlparse( path ).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
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():
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 )
评论列表
文章目录