Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java how do I find out what languages I have available my Resource Bundle

Tags:

I have some resource bundles packaged in my main jar

widget_en.properties widget_de.properties 

I retrieve a resource bundle based on my default locale as folows

ResourceBundle.getBundle("widget", Locale.getDefault()); 

But I want to present the user with a list of available languages supported so that can select a language that may be different to their computers default

But I can't find a method in ResourceBundle that would list available locales, I don't want to hardcode a list as I may forget to update it when another resource bundle is added.

EDIT

As I only resource bundles for different language (I dont have country refinements) so I have got generated a list by iterating through all the known language codes and check for each one as a resource.

String[]langs = Locale.getISOLanguages(); for(String lang:langs) {       URL rb = ClassLoader.getSystemResource("widget_"+lang+".properties");       if(rb!=null)       {             System.out.println("Found:"+rb.toString());       } } 
like image 627
Paul Taylor Avatar asked Aug 22 '12 11:08

Paul Taylor


People also ask

How do I read a ResourceBundle in Java?

First you need a Locale instance. Then you pass that Locale instance to the ResourceBundle. getBundle() method along with the name of the resource bundle to load. Finally you can access the localized values in the ResourceBundle via its different getString() and getObject() etc.

Where does ResourceBundle properties file go?

ResourceBundle property files contain locale-specific objects for use by Java classes. The ResourceBundle property file must be placed somewhere in the CLASSPATH . Typically this is best accomplished by placing the ResourceBundle properties file in the same directory as the gear message class that it maps to.

How do you add a ResourceBundle in Java?

To create the ResourceBundle , invoke the getBundle method, specifying the base name and Locale : ResourceBundle labels = ResourceBundle. getBundle("LabelsBundle", currentLocale); The getBundle method first looks for a class file that matches the base name and the Locale .

What is ResourceBundle locale?

Each resource bundle has a different locale. The locale indicates the specific human language of the strings in the resource bundle. The locale is indicated by the end of the file name, before the . properties extension. Locales consist of one to three identifiers.


1 Answers

If you really package the resource files inside your JAR, then I would do it like this:

public static void main(String[] args) {   Set<ResourceBundle> resourceBundles = getResourceBundles(A.class.getName());   if (resourceBundles.isEmpty())     // ... }  public static Set<ResourceBundle> getResourceBundles(String baseName) {   Set<ResourceBundle> resourceBundles = new HashSet<>();    for (Locale locale : Locale.getAvailableLocales()) {     try {       resourceBundles.add(ResourceBundle.getBundle(baseName, locale));     } catch (MissingResourceException ex) {       // ...     }   }    return Collections.unmodifiableSet(resourceBundles); } 

If you care about your JARs then you would at least get a set containing the default resource for a given baseName.

If you have only resources with names like baseName_<country> this method works perfectly, because only those ResourceBundles will be added to the set which are present in your JAR. It'll work even if you decide you need separate baseName_en_US and baseName_en_UK resources, which is not unheard of.

Shameless self-plug: I wrote a ResourceBundle.Control which takes a Charset as its argument, you might be interested in it if you want to load UTF-8 encoded resources files. It's available at GitHub.

like image 77
Kohányi Róbert Avatar answered Nov 04 '22 20:11

Kohányi Róbert