Search code examples
javaresourcesinternationalizationresourcebundle

Internationalization - listing and adding languages


I'm making an application in Java. This application can be internationalized using ResourceBundles. I want to allow user to choose the language that he want to the application have. And there's a question: how to list available languages for the program? I don't want to use Locale.getAvailableLocales(), because I don't know if the app's got a .properties file for the chosen language. There's also the second question: can I add an ability to add additional language files outside the .jar file? And the last question: is there any better internationalization solution?

Regards


Solution

  • You could just store a fixed list of supported locales in some constant of your application. If you need to make this dynamic, I see two solutions:

    • use a properties file listing the supported locales, and load them from this properties file at startup.
    • iterate through the available locales (or languages), try to load a corresponding bundle properties file (using Class.getResourceAsStream()), and consider that the locale is supported if you get an InputStream, and not supported if you get null.

    You could let the user add a bundle by putting some directory in the classpath, in addition to your jar. Drop the properties file in this directory (respecting the package hierarchy), et voilà.