object internationalization{
def localeCalculator(request : Box[HTTPRequest]): Locale =
request.flatMap(r => {
def localeCookie(in: String): HTTPCookie =
HTTPCookie("internationalization",Full(in),
Full(S.hostName),Full(S.contextPath),Full(2629743),Empty,Empty)
def localeFromString(in: String): Locale = {
val x = in.split("_").toList;
new Locale(x.head,x.last)
}
def calcLocale: Box[Locale] = {
if (GMRConfig.sessionVars.selected_lang.get != "unknown"){
Full(localeFromString(GMRConfig.sessionVars.selected_lang.get))}
else {
S.findCookie("internationalization").map(
_.value.map(localeFromString)
).openOr(Full(LiftRules.defaultLocaleCalculator(request)))
}
}
S.param("locale") match {
case Full(null) => calcLocale
case f@Full(selectedLocale) =>
S.addCookie(localeCookie(selectedLocale))
tryo(localeFromString(selectedLocale))
case _ => calcLocale
}
}).openOr(Locale.getDefault())
}