Spring-mvc – Dynamically generate a list of available languages in Spring MVC

dynamicinternationalizationlistspring-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

Best Answer

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.