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

后端 未结 4 2047
梦如初夏
梦如初夏 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 11:52

    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.

提交回复
热议问题