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

て烟熏妆下的殇ゞ 提交于 2019-12-30 03:44:05

问题


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());
      }
}

回答1:


I don't think there is an API for this because new valid locale objects can be created on the fly:

Locale locale = new Locale("abcd");

without the need to register it somewhere. And then you can use a resource bundle widget_abcd.properties without restrictions:

ResourceBundle resource= ResourceBundle.getBundle("widget", new Locale("abcd"));

From the java.util.Locale API docs:

Because a Locale object is just an identifier for a region, no validity check is performed when you construct a Locale. If you want to see whether particular resources are available for the Locale you construct, you must query those resources.

To solve the problem you can still iterate over all files called "widget_" in the resource directory and discover the new added resource bundles.

Note that Locale.getAvailableLocales() is not 100% sure for the above reason: you might some day define a non standard locale. But if you'll add only a few standard locales you can use this static method to iterate over the system locales and get the corresponding bundles.




回答2:


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.




回答3:


If you can make two basic assumptions:

1) You have a default resource bundle with no locale, or at least one locale that you know is there.

2) All your resources are in the same location (ie. the same path within a single jar file)

Then you can get the URL for a single resource:

URL url = SomeClass.class.getClassLoader().getResource("widget.properties");

once you have that, you should be able to parse the URL.

If you use commons-vfs, you should be able to convert the URL into a FileObject:

FileSystemManager manager = VFS.getManager();
FileObject resource = manager.resolveFile(url.toExternalForm());
FileObject parent = resource.getParent();
FileObject children[] = parent.getChildren();
// figure out which ones are bundles, and parse the names into a list of locales.

This will save you the trouble of dealing with the complexities of jar: style url's and such, since vfs will handle that for you.




回答4:


A solve it by listing files.

public class JavaApplication1 {

    public static final ArrayList<String> LOCALES = new ArrayList<>();

    static {
        try {
            File f = new File(JavaApplication1.class.getResource("/l10n").toURI());
            final String bundle = "widget_";// Bundle name prefix.
            for (String s : f.list(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.startsWith(bundle);
                }
            })) {
                LOCALES.add(s.substring(bundle.length(), s.indexOf('.')));
            }
        } catch (URISyntaxException x) {
            throw new RuntimeException(x);
        }
        LOCALES.trimToSize();
    }

    ...

}


来源:https://stackoverflow.com/questions/12072454/in-java-how-do-i-find-out-what-languages-i-have-available-my-resource-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!