Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, two solutions found. For both, assume they are being executed inside a Spring MVC <code>@Controller</code>-annotated class. Each will produce a HashMap (<code>languages</code>) in which the key is the 2-letter ISO language code, and the value the language name (in the current Locale, which in these examples is a static variable called <code>HSConstants.currentLocale</code>)</p> <p>1.- The one submitted by @millhouse (see above/below), which works after a bit of tweaking:</p> <pre><code> HashMap languages = new HashMap(); final String defaultMessage = "NOT FOUND"; HashMap availableLocales = new HashMap(); for (Locale locale : Locale.getAvailableLocales()) { String msg = rrbms.getMessage("currentLanguage", null, defaultMessage, locale); if (!defaultMessage.equals(msg) && !availableLocales.containsKey(locale.getLanguage())){ availableLocales.put(locale.getLanguage(), locale); } } for (String c : availableLocales.keySet()){ languages.put(c, availableLocales.get(c).getDisplayLanguage(HSConstants.currentLocale)); } model.addAttribute("languages", languages); </code></pre> <p>This solution requires that, in each of your language .properties files, you set an entry with the language (in the example above, it would be 'currentLanguage'). For ecample, in messages_it.properties, there must be an entry like this: currentLanguage=Italiano</p> <p>2.- Raw method, i.e. accesing the folder/files directly: assuming the files languages are in /WEB-INF/languages, and have a basename of fr-messages:</p> <pre><code> HashMap languages = new HashMap(); String languagesFolderPath = request.getSession().getServletContext().getRealPath("/WEB-INF/languages"); File folder = new File(languagesFolderPath); File[] listOfFiles = folder.listFiles(); for (int i = 0; i &lt; listOfFiles.length; i++){ String fileName = listOfFiles[i].getName(); if (fileName.startsWith("fr-messages_") && fileName.endsWith(".properties")){ // Extract the language code, which is between the underscore and the .properties extension String language = fileName.substring(12, fileName.indexOf(".properties")); Locale l = new Locale(language); languages.put(language, l.getDisplayLanguage(HSConstants.currentLocale)); } } model.addAttribute("languages", languages); </code></pre> <p>And then, in your JSP, render the select box using the <code>languages</code> map:</p> <pre><code>&lt;select name="language"&gt; &lt;c:forEach items="${languages}" var="language"&gt; &lt;c:choose&gt; &lt;c:when test="${platform.language == language.key}"&gt; &lt;option value="${language.key}" selected="SELECTED"&gt;${language.value}&lt;/option&gt; &lt;/c:when&gt; &lt;c:otherwise&gt; &lt;option value="${language.key}"&gt;${language.value}&lt;/option&gt; &lt;/c:otherwise&gt; &lt;/c:choose&gt; &lt;/c:forEach&gt; &lt;/select&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload