Created
August 17, 2010 18:41
-
-
Save ussy/531371 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.io._ | |
import java.util._ | |
import java.util.ResourceBundle.Control | |
import java.net._ | |
import java.security._ | |
class CharsetSupportedControl(charset: String = "utf-8") extends Control { | |
override def newBundle(baseName: String, locale: Locale, format: String, loader: ClassLoader, reload: Boolean) : ResourceBundle= { | |
if ("java.class" == format) { | |
super.newBundle(baseName, locale, format, loader, reload) | |
} else if ("java.properties" == format) { | |
val bundleName = toBundleName(baseName, locale) | |
val resourceName = toResourceName(bundleName, "properties") | |
val stream = getStream(resourceName, loader, reload) | |
if (stream != null) { | |
try { | |
val br = new BufferedReader(new InputStreamReader(stream, charset)) | |
try { | |
return new PropertyResourceBundle(br) | |
} finally { | |
br.close() | |
} | |
} finally { | |
stream.close() | |
} | |
} | |
null | |
} else { | |
throw new IllegalArgumentException("unknown format: " + format) | |
} | |
} | |
def getStream(resourceName: String, loader: ClassLoader, reload: Boolean) : InputStream = { | |
try { | |
AccessController.doPrivileged( | |
new PrivilegedExceptionAction[InputStream]() { | |
override def run() : InputStream = { | |
if (reload) { | |
val url = loader.getResource(resourceName) | |
if (url != null) { | |
val connection = url.openConnection() | |
if (connection != null) { | |
connection.setUseCaches(false) | |
return connection.getInputStream() | |
} | |
} | |
} else { | |
return loader.getResourceAsStream(resourceName) | |
} | |
return null | |
} | |
}) | |
} catch { | |
case e: PrivilegedActionException => throw e.getException() | |
} | |
} | |
} | |
object Main extends Application { | |
// message.properties をクラスパス直下に | |
val bundle = ResourceBundle.getBundle("messages", new CharsetSupportedControl()) | |
println(bundle.getString("message")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment