📄 resloader.java
字号:
package studio.beansoft.util;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Map;
/**
* A ResLoader is a tool that allow you load a resouce bundle
* and return the formated message from given argument.
*
* Samle usage:
* // There should be a file named com/beansoft/resources.properties
* ResLoader res = ResLoader.getResource("com.beansoft.resources");
* System.out.println(res.getText("frame title"));
* System.out.println(res.getText("can't open {0}", "file.txt"));
*/
public class ResLoader
{
private ResourceBundle messageRB;
/**
* Holds a cached list of loaded resouces.
*/
private static Map resources = new java.util.HashMap();
public ResLoader() {
}
public ResLoader(String resourceName) {
loadResource(resourceName);
}
public void loadResource(String resourceName)
{
try
{
messageRB = ResourceBundle.getBundle(resourceName);
}
catch(MissingResourceException missingresourceexception)
{
throw new Error("Fatal: Resource " + resourceName + " is missing");
}
}
/**
* Returs a resource bundle of this class.
* @param resourceName
* @return a ResLoader fetched from the cache
*/
public static ResLoader getResource(String resourceName) {
if(resources.get(resourceName) == null) {
resources.put(resourceName, new ResLoader(resourceName));
}
return (ResLoader)resources.get(resourceName);
}
/**
* Read text from the res bundle.
* @param key
* @return string
*/
public String getText(String key)
{
return getText(key, (String)null);
}
public String getText(String key, String argument1)
{
return getText(key, argument1, null);
}
public String getText(String key, String argument1, String argument2)
{
return getText(key, argument1, argument2, null);
}
public String getText(String key, String argument1, String argument2, String argument3)
{
String as[] = new String[3];
as[0] = argument1;
as[1] = argument2;
as[2] = argument3;
return getText(key, as);
}
// Support any numbers arguments
public String getText(String key, String[] arguments)
{
if(messageRB == null)
return null;
try
{
String message = messageRB.getString(key);
return MessageFormat.format(message, arguments);
}
catch(MissingResourceException missingresourceexception)
{
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -