[英]Load multiple locale specific properties files in internationalization with resource bundle in java
I have four properties files 我有四个属性文件
So now I need internationalization in multiple programs, so now I need to load multiple properties files and get the key-value pair of values from properties files specific to a locale. 所以现在我需要在多个程序中进行国际化,所以现在我需要加载多个属性文件并从特定于语言环境的属性文件中获取键值对。 For that I have a ResourceBundleService.java 为此,我有一个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??
}
}
Hope the question is clear. 希望问题清楚。
You need to call ResourceBundle.getBundle(baseName, locale)
each time in getLabel
. 您需要每次在getLabel
调用ResourceBundle.getBundle(baseName, locale)
。 ResourceBundle maintains an internal cache so it wont load all props files each time: ResourceBundle维护内部缓存,因此不会每次都加载所有props文件:
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);
}
For the time being use Application_fr.properties; 暂时使用Application_fr.properties; les Canadiens will be thankful. les Canadiens非常感谢。 With Locale.setDefault(availableLocale)
elect an available locale. 使用Locale.setDefault(availableLocale)
选择一个可用的语言环境。 The root locale properties, Application.properties, should also contain the language keys. 根区域设置属性Application.properties也应包含语言键。 You could copy the French ones. 您可以复制法语的。 In that case you need not set the default locale. 在这种情况下,您无需设置默认语言环境。
Lets check this implementation on github it works really nice. 让我们在github上检查此实现,它确实很好用。 It requires the following function naming convention: 它需要以下函数命名约定:
MultiplePropertiesResourceBundle is an abstract base implementation to allow to combine a ResourceBundle from multiple properties files whereas these properties files must end with the same name - the base-name for these combined ResourceBundle. MultiplePropertiesResourceBundle是一个抽象的基本实现,允许组合来自多个属性文件的ResourceBundle,而这些属性文件必须以相同的名称结尾-这些组合的ResourceBundle的基本名称。
If you'll use it at the first you need to implement abstract class MultiplePropertiesResourceBundle
as below: 如果首先使用它,则需要实现抽象类MultiplePropertiesResourceBundle
,如下所示:
import ch.dueni.util.MultiplePropertiesResourceBundle;
public class CombinedResources extends MultiplePropertiesResourceBundle {
public CombinedResources() {
super("package_with_bundles");
}
}
then you should implement empty class which extends out CombinedResources
: 那么您应该实现扩展了CombinedResources
空类:
public class CombinedResources_en extends CombinedResources {}
and so on for other languages. 其他语言也是如此。 After that you can use your bundle as below: 之后,您可以按以下方式使用捆绑包:
ResourceBundle bundle = ResourceBundle.getBundle("CombinedResources");
This bundle will be use all properties files inside package_with_bundles
. 该捆绑软件将使用package_with_bundles
所有属性文件。 For more information just look inside github repo. 有关更多信息,请查看github repo。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.