使用java中的资源包在国际化中加载多个特定于语言环境的属性文件

Uda*_*ddy 1 java locale properties resourcebundle

我有四个属性文件

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

所以现在我需要在多个程序中进行国际化,所以现在我需要加载多个属性文件,并从特定于语言环境的属性文件中获取键值对值.为此,我有一个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??
    }
}
Run Code Online (Sandbox Code Playgroud)

希望问题很清楚.

Evg*_*eev 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);
}
Run Code Online (Sandbox Code Playgroud)