tjweir / liftbook

Book for liftweb: http://www.apress.com/book/view/1430224215

Home Page:http://groups.google.com/group/the-lift-book

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calculating Locale Based on Cookies invalid characters (fix code)

heralight opened this issue · comments

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
}