Skip to content

Instantly share code, notes, and snippets.

Created November 23, 2011 15:11
Show Gist options
  • Save alaz/1388917 to your computer and use it in GitHub Desktop.
Save alaz/1388917 to your computer and use it in GitHub Desktop.
ResourceBundle.Control implementation based on Liftweb resources XML format
// How to use
def getBundle(locale: Locale): ResourceBundle =
// this will try to load resource validationMessages.xml
ResourceBundle.getBundle("validationMessages", locale, LiftResourceBundleControl)
package com.osinka.i18n
import java.util.{Locale,ResourceBundle}
import collection.JavaConversions._
import net.liftweb.common.Box._
import net.liftweb.util.{BundleBuilder,PCDataXmlParser}
* ResourceBundle.Control implementation for Liftweb resources
* Useful if there is a need to use Liftweb localization resources
* from outside of "S".
* @author Alexander Azarov <>
* @see java.util.ResourceBundle
* @see java.util.ResourceBundle.Control
object LiftResourceBundleControl extends ResourceBundle.Control {
val LiftBundleFormat = "liftweb.xml"
val Suffix = "xml"
override def getFormats(baseName: String): java.util.List[String] = LiftBundleFormat :: Nil
override def newBundle(baseName: String, locale: Locale, fmt: String, loader: ClassLoader, reload: Boolean): ResourceBundle = {
def resourceName =
* We have no need to calculate bundle name (via toBundleName) from baseName and locale, since
* Lifweb resources file contains all locales at once.
* Otherwise, the line below would look like
* toResourceName(toBundleName(baseName, locale), Suffix)
toResourceName(baseName, Suffix)
def stream =
reload match {
case true =>
for {url <- Option(loader getResource resourceName)
connection <- Option(url.openConnection)}
yield {
case false =>
Option(loader getResourceAsStream resourceName)
(for {fmt <- Option(fmt) if fmt == LiftBundleFormat
is <- stream
xml <- PCDataXmlParser(is)
b <- BundleBuilder.convert(xml, locale)}
yield b).orNull
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment