Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically generate a list of available languages in Spring MVC

I have set up i18n in Spring MVC 3, and it is working correctly. There are several files, each with its own language: messages_en.properties, messages_de.properties, etc.

In one of my JSPs, I need to show the users a combo with all available languages, and I would like this list to be dynamic i.e. generated on the fly from the existing language files in the server.

Is there any built-in method to generate this list? Or do I have to resort to check the folder where the language files reside and parse them?

Thanks!

Nacho

like image 696
npintos Avatar asked Nov 15 '22 01:11

npintos


1 Answers

How about putting this into something that has access to the ReloadableResourceBundleMessageSource?

ReloadableResourceBundleMessageSource rrbms = getMessageSource();   
final String defaultMessage = "NOT FOUND";
List<Locale> availableLocales = new ArrayList<Locale>();
for (Locale locale : Locale.getAvailableLocales()) {
    String msg = rrbms.getMessage("test.code", null, defaultMessage, locale);
    if (!defaultMessage.equals(msg)) {
       availableLocales.add(locale);
    }
}

Just make sure each supported language supplies a test.code value and you're done.

like image 132
millhouse Avatar answered May 17 '23 23:05

millhouse