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

后端 未结 4 2033
梦如初夏
梦如初夏 2021-01-01 11:26

I have some resource bundles packaged in my main jar

widget_en.properties
widget_de.properties

I retrieve a resource bundle based on my de

4条回答
  •  死守一世寂寞
    2021-01-01 12:06

    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.

提交回复
热议问题