I know about standard way to localize java spring application with adding ?lang=en(fr, de, ch), but it is localize only one page, than after move to other page (press on any href="/newpage"), there again default locale, not choosen before. My question is: how i can ot determinate at login page (or index page) user locale and set it as default locale for the whole web application?
Update question 14.02.2014-16:32 Addition information.
Default spring implementation of localization it is adding "?lang=en" to request URI and takes new messages from bundle. So there is an example: default locale set to en uri: http://example.com/index - en locale. uri: http://example.com/index?lang=de - German Locale setted - page translated to German. But than, i click on the link, which got href="/profile". When i clicked: uri: http://example.com/profile - i got default locale - en.
What i want: uri: http://example.com/index - user press on German flag in the top of the site, than some bean get this action and set default locale - German. Than all pages will loading with default german locale. uri: http://example.com/profile - default locale - German.
In uri request example, i want something like that: example.com - detect user locale - set default locale, German for an example/ User press on English flag - default locale set to English for all pages.
Update question 14.02.2014-17:38 Addition information. Information about my mistake Everything is good. I marked right answer. Spring standard implementation works great and exactly i want. I got mistake in JSP pages with not full translate, and when i change locale, i though that not everything translated - but it was. Thanks!
The Spring Documentation is very well for this theme: 17.8 Using locales You need two things:
LocalResolver
- that is holds the local for an user and when the user send a request, it add the local to the request context (available via RequestContext.getLocale()
) - often the SessionLocalResolver
or CookieLocaleResolver
is used for a normal web page based application - it is important that the bean id/name is localeResolver
( DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME="localeResolver"
) - (if the name is different, then DispatcherServlet
will not find the locale resolver ! LocaleChangeInterceptor
that change the locale when the user add ?lang=de
to its request. example configuration
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
id="localeResolver"
name="localeResolver">
<property name="defaultLocale" value="en"/>
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="lang">
</bean>
...
</mvc:interceptors>
For your problem, it is likely that something with the LocalResolver
is wrong, maybe you just forget to set its id/name* to localeResolver
or you use a AcceptHeaderLocaleResolver
(that always "fall back" the the request header locale)
* see this answer for the difference between Id and Name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.