我有四个属性文件
所以现在我需要在多个程序中进行国际化,所以现在我需要加载多个属性文件,并从特定于区域设置的属性文件中获取键值对值。为此,我有一个ResourceBundleService.java
public class ResourceBundleService {
private static String language;
private static String country;
private static Locale currentLocale;
static ResourceBundle labels;
static {
labels = ResourceBundle
.getBundle("uday.properties.Application");
labels = append(Database.properties");
//** how to append existing resource bundle with new properties file?
}
public static String getLabel(String resourceIndex, Locale locale) {
return labels.getString(resourceIndex);
//How to get locale specific messages??
}
}希望问题是清楚的。
发布于 2013-06-25 10:11:39
每次在ResourceBundle.getBundle(baseName, locale)中都需要调用getLabel。ResourceBundle维护一个内部缓存,因此它不会每次加载所有道具文件:
public static String getLabel(String resourceIndex, Locale locale) {
ResourceBundle b1 = ResourceBundle.getBundle("uday.properties.Application", locale);
if (b1.contains(resourceIndex)) {
return b1.getString(resourceIndex);
}
ResourceBundle b2 = ResourceBundle.getBundle("uday.properties.Database", locale);
return b2.getString(resourceIndex);
}发布于 2013-06-25 09:55:05
就目前而言,使用Application_fr.properties;les将感谢。使用Locale.setDefault(availableLocale)选择可用的区域设置。根区域设置属性Application.properties也应该包含语言键。你可以模仿法国的。在这种情况下,不需要设置默认的区域设置。
发布于 2014-10-08 09:03:22
让我们在github上检查一下这个实现,它非常好用。它需要以下函数命名约定:
MultiplePropertiesResourceBundle是一个抽象的基本实现,它允许组合来自多个属性文件的ResourceBundle,而这些属性文件必须以相同的名称--这些组合的ResourceBundle的基名结束。
如果您首先使用它,您需要实现抽象类MultiplePropertiesResourceBundle,如下所示:
import ch.dueni.util.MultiplePropertiesResourceBundle;
public class CombinedResources extends MultiplePropertiesResourceBundle {
public CombinedResources() {
super("package_with_bundles");
}
}然后,您应该实现空类,它扩展了CombinedResources
public class CombinedResources_en extends CombinedResources {}其他语言也是如此。之后,您可以如下所示使用您的包:
ResourceBundle bundle = ResourceBundle.getBundle("CombinedResources");这个包将使用package_with_bundles中的所有属性文件。要了解更多信息,只需查看github回购内部。
https://stackoverflow.com/questions/17294009
复制相似问题