Created
November 23, 2011 15:11
-
-
Save alaz/1388917 to your computer and use it in GitHub Desktop.
ResourceBundle.Control implementation based on Liftweb resources XML format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// How to use | |
def getBundle(locale: Locale): ResourceBundle = | |
// this will try to load resource validationMessages.xml | |
ResourceBundle.getBundle("validationMessages", locale, LiftResourceBundleControl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <azarov@osinka.ru> | |
* @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 { | |
connection.setUseCaches(false) | |
connection.getInputStream | |
} | |
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