All Downloads are FREE. Search and download functionalities are using the official Maven repository.

moreswing.util.ResourceBundleUtf8.scala Maven / Gradle / Ivy

The newest version!
package moreswing.util

import java.io.UnsupportedEncodingException
import java.util._

/** It's [[java.util.ResourceBundle]] with UTF-8 support.
 *
 * @author myst3r10n
 */
object ResourceBundleUtf8 {

  class PropertyResourceBundleUtf8(
    b: PropertyResourceBundle)
    extends ResourceBundle {

    /* It's java.util.ResourceBundle#getKeys. */
    def getKeys = bundle.getKeys


    /* It's java.util.ResourceBundle#handleGetObject(java.lang.String). */
    protected def handleGetObject(key: String): Object = {

      val value = bundle.handleGetObject(key).asInstanceOf[String]

      if(value == null)
        return null

      try { new String(value.getBytes("ISO-8859-1"), "UTF-8") }
      catch { case e: UnsupportedEncodingException => null }

    } 

    private var bundle = b

  }

  /** Gets a resource bundle using the specified base name, the default locale, and the caller's class loader. Calling this method is equivalent to calling
   * {{{ bundle(baseName, locale, this.getClass.getClassLoader) }}},
   * except that getClassLoader is run with the security privileges of [[scala.util.ResourceBundleUtf8]]. See bundle for a complete description of the search and instantiation strategy.
   *
   * @param baseName The base name of the resource bundle, a fully qualified class name.
   */
  def bundle(baseName: String): ResourceBundle =
    createUtf8PropertyResourceBundle(ResourceBundle.getBundle(baseName))

  /** Gets a resource bundle using the specified base name and locale, and the caller's class loader. Calling this method is equivalent to calling
   * {{{ bundle(baseName, locale, this.getClass.getClassLoader }}},
   * except that getClassLoader is run with the security privileges of [[scala.util.ResourceBundleUtf8]]. See bundle for a complete description of the search and instantiation strategy.
   *
   * @param baseName The base name of the resource bundle, a fully qualified class name.
   * @param locale The locale for which a resource bundle is desired.
   */
  def bundle(baseName: String, locale: Locale): ResourceBundle =
    createUtf8PropertyResourceBundle(ResourceBundle.getBundle(baseName, locale))

  /** Gets a resource bundle using the specified base name, locale, and class loader.
   *
   * @param baseName The base name of the resource bundle, a fully qualified class name.
   * @param locale The locale for which a resource bundle is desired.
   * @param loader The class loader from which to load the resource bundle.
   */
  def bundle(baseName: String, locale: Locale, loader: ClassLoader) =
    createUtf8PropertyResourceBundle(ResourceBundle.getBundle(baseName, locale, loader))


  private def createUtf8PropertyResourceBundle(
    bundle: ResourceBundle): ResourceBundle = {

    if(!bundle.isInstanceOf[PropertyResourceBundle])
      return bundle

    new PropertyResourceBundleUtf8(bundle.asInstanceOf[PropertyResourceBundle])

  }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy