Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Spring's i18n mechanism?
    primarykey
    data
    text
    <p>I added localization to my Spring project and it appears to be working but I wonder how I change the language, if the language choice is done based on the browser setting, the HTTP header, a cookie or something else. Is there a way to be explicit as well e.g. taking the locale as a parameter in a way like e.g. <code>hl=de</code> on the HTTP query string? I also want to allow the user to set the language on a settings page, how can I do that? My implementation looks like this and writes messages in English:</p> <p><code>&lt;h4 class="title"&gt;&lt;fmt:message key="login.title"/&gt;&lt;/h4&gt;</code></p> <p>servlet.xml:</p> <pre><code>&lt;bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt; &lt;property name="basename" value="classpath:messages" /&gt; &lt;property name="defaultEncoding" value="UTF-8"/&gt; &lt;/bean&gt; &lt;bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt; &lt;property name="paramName" value="lang" /&gt; &lt;/bean&gt; &lt;bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"&gt; &lt;property name="defaultLocale" value="en"/&gt; &lt;/bean&gt; &lt;bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt; &lt;property name="interceptors"&gt; &lt;ref bean="localeChangeInterceptor" /&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>So how can I</p> <p>a) Make the locale choice explicit by enabling overriding the locale with a HTTP GET parameter such as <code>hl=de</code> for German and <code>hl=fr</code> for French?</p> <p>b) Let a user choose locale? </p> <h2>Update</h2> <p>The interceptor is not working. The XML is:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"&gt; &lt;bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt; &lt;property name="basename" value="classpath:messages" /&gt; &lt;property name="defaultEncoding" value="UTF-8"/&gt; &lt;/bean&gt; &lt;bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"&gt; &lt;property name="defaultLocale" value="sv" /&gt; &lt;/bean&gt; &lt;mvc:interceptors&gt; &lt;bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt; &lt;property name="paramName" value="language" /&gt; &lt;/bean&gt; &lt;/mvc:interceptors&gt; &lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt; &lt;!-- one of the properties available; the maximum file size in bytes --&gt; &lt;property name="maxUploadSize" value="100000"/&gt; &lt;/bean&gt; &lt;/beans&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.
    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