Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Short answer</h2> <p>Have you checked the <a href="http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html" rel="nofollow">official documentation</a> (chapter <em>17.8 Using locales</em>)? You need to configure <code>LocaleResolver</code> and possibly a <code>LocaleChangeInterceptor</code> (or write your own).</p> <h2>Longer description about how Spring works</h2> <p><strong>Note, that resolving client's locale is different task from getting a correct resource bundle.</strong></p> <ul> <li>Spring uses <code>LocaleResolver</code> to <strong>get or set</strong> the current locale. There are several implementations for different strategies to <code>LocaleResolver</code>: <ul> <li><code>FixedLocaleResolver</code> - will always resolve locale to predefined value (not capable of setting different locale)</li> <li><code>SessionLocaleResolver</code> - stores and resolves locale to value store on session under special key</li> <li><code>AcceptHeaderLocaleResolver</code> - this is the resolver which actually tries to get locale from the browser (not capable of setting different locale)</li> <li><code>CookieLocaleResolver</code> - stores and resolves locale to value stored in a browser cooke</li> </ul></li> </ul> <p><code>LocaleResolver</code> is used to populate <code>LocaleContextHolder</code> (btw. that is the class you should be getting locale from).</p> <p>There is a second mechanism <code>LocaleChangeInterceptor</code>, which is able to set locale via your selected <code>LocaleResolver</code> based on user request parameter.</p> <p>Now this infrastructure is unrelated to your resource bundles (messages.properties, messages_en.properties, ...) and the mechanism used to resolve your messages. The following examples will show why.</p> <h2>Example scenarios</h2> <ul> <li>Lets assume your resource bundles are: <ul> <li><code>messages.properties</code> - with <code>ru</code> messages (default messages)</li> <li><code>messages_ko.properties</code> - with <code>ko</code> messages</li> </ul></li> <li>Lets assume you have configured <code>SessionLocaleResolver</code> with default locale <code>ru</code></li> <li>And lets assume you have configured <code>LocaleChangeInterceptor</code></li> </ul> <p><strong>SCENARIO I</strong> - First requets:</p> <ol> <li>User makes first request to the application</li> <li>As soon as the request reaches Spring's <code>DispatcherServlet</code> it queries <code>LocaleResolver</code> to get locale for the request</li> <li>No locale is set on the session, so the locale is resolved to <code>ru</code> (default)</li> <li>...<em>handler stuff</em>...</li> <li>Now you are rendering the webpage and you want to use <code>&lt;spring:message&gt;</code> tag...</li> <li>The tag tries to resolve translation code using preconfigured <code>MessageSource</code> (<code>ResourceBundleMessageSource</code>) with <strong>request locale</strong> (this is the one resolved by your resolver).</li> <li>Message source tries to load translation code from <code>messages_ru.properties</code> which does not exist, so it moves to more general file <code>messages.properties</code> (which "by accident" contains your default language - <code>ru</code>)</li> <li>User get his page in russian language</li> </ol> <p><strong>SCENARIO II</strong> - User clicks link to change his language to <code>ko</code>:</p> <ol> <li>Second request is made with query parameter <code>locale=ko</code></li> <li><code>DispatcherServlet</code> resolves request locale to <code>ru</code> (this is what your locale resolver returns)</li> <li>Before the request is handed over to your handler it goes through the <code>LocaleChangeInterceptor</code> handler interceptor.</li> <li><code>LocaleChangeInterceptor</code> detects <code>locale</code> query parameter and calls <code>setLocale</code> method on your <code>LocaleResolver</code>, which leads to changing request locale and storing new locale on the session for future requests.</li> <li>...<em>handler stuff</em>...</li> <li>...<em>view stuff</em>...</li> <li>Now <code>&lt;spring:message&gt;</code> is calling <code>MessageSource</code> with <code>ko</code> locale.</li> <li>Message source tries to load <code>messages_ko.properties</code> and succeeds.</li> <li>User get his page in korean language</li> </ol> <p><strong>SCENARIO III</strong> - User tries to change to <em>invalid</em> locale:</p> <ol> <li>User makes request with query parameter <code>locale=en</code>.</li> <li>...<em>dispatcher stuff</em>... (locale is resolved to <code>ko</code> from the session)</li> <li>Handler interceptor changes the locale to <code>en</code> (this will be stored on the session as well)</li> <li>...<em>handler stuff</em>...</li> <li>...<em>view stuff</em>...</li> <li>Now <code>&lt;spring:message&gt;</code> is calling <code>MessageSource</code> with <code>en</code> locale.</li> <li>Message source tries to load <code>messages_en.properties</code> which does not exist so it moves to a more general file <code>messages.properties</code> and messages are translated to <code>ru</code>, even thou the request locale is set to <code>en</code>.</li> <li>User get his page in rusian language</li> </ol> <h2>Summary</h2> <p>Now the last example is probably what bothers you - there is no check whether the locale user selects is supported or not. If you don't want to allow user to switch to unsupported locale, then you need to either subclass some <code>LocaleResolver</code> or write your own <code>LocaleChangeInterceptor</code>.</p>
 

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