Skip to content

Instantly share code, notes, and snippets.

@arashi01
Forked from karma4u101/Boot.scala
Created October 10, 2012 10:24
Show Gist options
  • Save arashi01/3864601 to your computer and use it in GitHub Desktop.
Save arashi01/3864601 to your computer and use it in GitHub Desktop.
Lift - Locale via user action (clicking flag img)
<?xml version="1.0"?>
<resources> <!-- A small example showing one way to do internationalization in Lift (_resources_index.html)-->
:
<res name="about.link.text" lang="en"default="true">About</res>
<res name="about.link.text" lang="sv">Om</res>
:
</resources>
<!--In template-->
<!--One way to get localized string from resource file -->
... <span data-lift="Loc?locid=about.link.text">About</span>
<!-- Let the user switch lang by clicking flags -->
<li ><a class="flag-se" href="?hl=sv_SE"></a> </li>
<li><a class="flag-gb" href="?hl=en_GB"></a></li>
package bootstrap.liftweb
import java.util.Locale
:
//object holding the chosen locale during the session, can be updated by user at any time.
object localeOverride extends SessionVar[Box[Locale]](Empty)
class Boot extends Loggable {
def boot {
:
// Build SiteMap using Menu.i to get localized menu names from resource files
val entries = List(
Menu.i("Home") / "index",
:
:
//Localization block
val swedishLocale = new Locale("sv", "SE")
object localeMemo extends RequestMemoize[Int, Locale] {
override protected def __nameSalt = randomString(20)
}
LiftRules.localeCalculator = (request: Box[HTTPRequest]) =>
localeMemo(request.hashCode, (for {
r <- request
p <- tryo(r.param("hl").head.split(Array('_', '-')))
} yield p match {
case Array(lang) => {
if (!lang.equals("hl")) {
logger.debug("setting local to "+lang)
localeOverride.set(Full(new Locale(lang)))
}
localeOverride.open_!
}
case Array(lang, country) => {
logger.debug("setting local to "+lang+"_"+country)
localeOverride.set(Full(new Locale(lang, country)))
localeOverride.open_!
}
}).openOr(localeOverride.openOr(swedishLocale)))
//end loxalization block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment