Load multiple locale specific properties files in internationalization with resource bundle in java

StackOverflow https://stackoverflow.com/questions/17294009

質問

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.

役に立ちましたか?

解決

You need to call ResourceBundle.getBundle(baseName, locale) each time in getLabel. ResourceBundle maintains an internal cache so it wont load all props files each time:

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; les Canadiens will be thankful. With Locale.setDefault(availableLocale) elect an available locale. The root locale properties, Application.properties, should also contain the language keys. 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. 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.

If you'll use it at the first you need to implement abstract class MultiplePropertiesResourceBundle as below:

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:

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. For more information just look inside github repo.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top