Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 271
m4tx Avatar asked Aug 10 '26 11:08

m4tx


1 Answers

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à.

like image 111
JB Nizet Avatar answered Aug 13 '26 00:08

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!