2

I have four properties files

  1. Application.properties
  2. Application_fr_FR.properties
  3. Database.properties
  4. Database_fr_FR.properties

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

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.

4

3 回答 3

5

您需要在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-25T10:11:39.670 回答
0

暂时使用Application_fr.properties;les Canadiens将不胜感激。Locale.setDefault(availableLocale)选择一个可用的语言环境。根区域设置属性 Application.properties 还应包含语言键。你可以复制法国的。在这种情况下,您无需设置默认语言环境。

于 2013-06-25T09:55:05.847 回答
0

让我们在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 repo。

于 2014-10-08T09:03:22.460 回答