以下是我想要实现的目标。我们有一个作为servlet在IBM Domino服务器上运行的应用程序。该应用程序使用资源包来根据浏览器语言获取翻译后的消息和标签。
我们希望使客户能够覆盖其中的一些值。我们不能在运行时修改.jar中的bundle_lang.properties文件。因此,我们的想法是随.jar一起提供额外的bundleCustom_lang.properties文件
这个包可以在运行时使用
private static void addToClassPath(String s) throws Exception {
File file = new File(s);
URLClassLoader cl = (URLClassLoader) ClassLoader.getSystemClassLoader();
java.lang.reflect.Method m = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
m.setAccessible(true);
m.invoke(cl, new Object[] { file.toURI().toURL() });
}到目前为止,一切顺利,这在Eclipse中可以正常工作。在这里,我将数据文件放在工作区外的一个目录中(/ bundleCustom /DATA/Temp/)
一旦添加的ResourceBundle可用,我们首先检查这个包中的密钥。如果它返回一个值,则该值将用于转换。如果没有返回值,或者文件不存在,则使用.jar内捆绑包中的值。
我的完整代码在这里
public class BundleTest2 {
static final String CUSTOM_BUNDLE_PATH = "/volumes/DATA/Temp/";
static final String CUSTOM_BUNDLE_MODIFIER = "Custom";
public static void main(String[] args) {
try {
addToClassPath(CUSTOM_BUNDLE_PATH);
System.out.println(_getTranslation("LabelBundle", "OutlineUsersAllVIP"));
} catch (Exception e) {
}
}
private static String _getTranslation(String bundle, String translation) {
return _getTranslation0(bundle, new Locale("de"), translation);
}
private static String _getTranslation0(String bundle, Locale locale, String key) {
String s = null;
try {
try {
ResourceBundle custom = ResourceBundle.getBundle(bundle + CUSTOM_BUNDLE_MODIFIER, locale);
if (custom.containsKey(key)) {
s = custom.getString(key);
}
} catch (MissingResourceException re) {
System.out.println("CANNOT FIND CUSTOM RESOURCE BUNDLE: " + bundle + CUSTOM_BUNDLE_MODIFIER);
}
if (null == s || "".equals(s)) {
s = ResourceBundle.getBundle(bundle, locale).getString(key);
}
} catch (Exception e) {
}
return s;
}
private static void addToClassPath(String s) throws Exception {
File file = new File(s);
URLClassLoader cl = (URLClassLoader) ClassLoader.getSystemClassLoader();
java.lang.reflect.Method m = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
m.setAccessible(true);
m.invoke(cl, new Object[] { file.toURI().toURL() });
}
}当我在servlet中尝试同样的操作时,我得到了一个MissingResourceException。
我还尝试将.properties文件放入customization.jar中,并提供完整路径(包括调用addToClassPath()时的.jar )。显然,customization.jar已加载(它被锁定在文件系统中),但我仍然获得MissingResourceException。
我们已经在addToClassPath中使用了相同的代码来加载Db2驱动程序,并且工作正常。
我遗漏了什么?
发布于 2017-08-02 16:18:02
为什么不使用Database来存储被覆盖的翻译?在应用程序的本地部署中保留客户端创建的内容通常不是一个好主意,如果重新部署应用程序会发生什么情况,这些资源会被删除吗?如果您必须运行应用程序的另一个节点,您将如何复制自定义属性文件?
https://stackoverflow.com/questions/45454246
复制相似问题