tjweir/liftbook — Issue #52: Calculating Locale Based on Cookies invalid characters (fix code)
Author: heralight
Created: 2012-03-07 15:50:11+00:00
State: open

In http://exploring.liftweb.net/master/index-D.html in the end of file part "Calculating Locale Based on Cookies and Parameters" code

wrong code character : replace ’-’, ’_’ with ’-’, ’_’ and remove invisible illegal characters. fixed code:

// Properly convert a language tag to a Locale
def computeLocale(tag : String) = tag.split(Array(’-’, ’_’)) match {
case Array(lang) => new Locale(lang)
case Array(lang, country) => new Locale(lang, country)
case Array(lang, country, variant) => new Locale(lang, country, variant)
}

// Define this to be whatever name you want
val LOCALE_COOKIE_NAME = "SelectedLocale"

LiftRules.localeCalculator = {
case fullReq @ Full(req) => {
// Check against a set cookie, or the locale sent in the request
def currentLocale : Locale =
S.findCookie(LOCALE_COOKIE_NAME).flatMap {
cookie => cookie.value.map(computeLocale)
} openOr LiftRules.defaultLocaleCalculator(fullReq)

// Check to see if the user explicitly requests a new locale
S.param("locale") match {
case Full(requestedLocale) if requestedLocale != null => {
val computedLocale = computeLocale(requestedLocale)
S.addCookie(HTTPCookie(LOCALE_COOKIE_NAME, requestedLocale))
computedLocale
}
case _ => currentLocale
}
}
case _ => Locale.getDefault
}