package org.lds.stack.web.spring.i18n;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.i18n.FixedLocaleResolver;
/**
* This locale resolver provides the ability to define a list of resolvers from which to determine
* the locale. This allows us to give preference to certain locale resolution schemes by putting
* them earlier in the list.
* <p/>
*
* The order of resolvers from which to find the given locale (or set a specified locale) could be
* defined in the spring context file with something like:
*
* <pre>
* <bean id="localeResolver" class="org.lds.stack.web.spring.i18n.ChainedLocaleResolver">
* <property name="localeResolvers">
* <list>
* <bean class="org.lds.stack.web.spring.i18n.UrlLocaleResolver" />
* <bean class="org.lds.stack.web.spring.i18n.NoDefaultSessionLocaleResolver" />
* <bean class="org.lds.stack.web.spring.i18n.NoDefaultCookieLocaleResolver">
* <property name="cookieMaxAge" value="31536000"/>
* </bean>
* <bean class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver" />
* <bean class="org.springframework.web.servlet.i18n.FixedLocaleResolver" />
* </list>
* </property>
* </bean>
* </pre>
*
* This allows you to remove, or re-order the locale resolution schemes to meet your needs.
* Also note that the id of localeResolver is significant. The Spring Framework knows to use this
* resolver as the LocaleResolver by virtue of the id being "localeResolver".
* <p/>
*
* NOTE: If the default resolver order, shown above, will work for your application, then you can
* skip this verbose definition by utilizing the stack-web namespace handler, providing any exposed
* attribute values for minor customizations. The namespace handler is defined as follows:
*
* <pre>
* <stack-web:locale-resolver />
* </pre>
*
* Additionally, in order to change the locale based on a url parameter, you can configure a
* LocaleChangeInterceptor, which will call the set method of all of the locales in the chained
* resolver, so that they can be found when resolveLocale(...) is called on them.
* <p/>
*
* The interceptor configuration might look as follows:
* <pre>
* <mvc:interceptors>
* <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
* </mvc:interceptors>
* </pre>
*/
public class ChainedLocaleResolver implements LocaleResolver {
private List<LocaleResolver> localeResolvers;
public ChainedLocaleResolver() {
//if anything other than this default order or set is desired, the list of resolvers
//to be chained should be set up in the bean definition as shown above
localeResolvers = new ArrayList<LocaleResolver>();
//TODO: Is the Url resolver necessary if we have an interceptor that changes the locale from the url?
localeResolvers.add(new UrlLocaleResolver());
localeResolvers.add(new NoDefaultSessionLocaleResolver());
NoDefaultCookieLocaleResolver cookieLocaleResolver = new NoDefaultCookieLocaleResolver();
cookieLocaleResolver.setCookieMaxAge(31536000);
localeResolvers.add(cookieLocaleResolver);
//TODO: may need to create a NoDefault, but that seems difficult as the implementation is provided
//by the javax.servlet, ..., Also, maybe we could just remove the fixedLocaleResolver, as this one
//gets the default, and then if people did not use this one, they could add the fixed one back in.
localeResolvers.add(new AcceptHeaderLocaleResolver());
localeResolvers.add(new FixedLocaleResolver());
}
@Override
public Locale resolveLocale(HttpServletRequest request) {
Locale locale = null;
for (LocaleResolver resolver : getLocaleResolvers()) {
locale = resolver.resolveLocale(request);
if (locale != null) {
return locale;
}
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
for (LocaleResolver resolver : getLocaleResolvers()) {
try {
resolver.setLocale(request, response, locale);
} catch (UnsupportedOperationException uoe) {}
}
}
public List<LocaleResolver> getLocaleResolvers() {
return localeResolvers;
}
public void setLocaleResolvers(List<LocaleResolver> localeResolvers) {
this.localeResolvers = localeResolvers;
}
}
2条答案
按热度按时间yhqotfr81#
我认为你需要编写你自己的LocaleResolver来 Package Spring的区域解析器列表。你一个接一个地调用它们,直到
Locale
被解析。如果列表没有产生Locale
,你在LocaleResolver
中提供一个默认行为。以下是一些您可能会觉得有用的链接:
http://code.lds.org/maven-sites/stack/modules/web-spring/3.0.8-SNAPSHOT/apidocs/org/lds/stack/web/spring/i18n/ChainedLocaleResolver.html
https://mvnrepository.com/artifact/org.lds.stack.web/stack-web-spring/3.0.8
或者,如果您更喜欢两个链接的视图:
eagi6jfj2#
这是有用的,也是一个好的开始。2从我的Angular 来看,一个理想的链式解析器应该按照这个优先顺序
1.如果LocaleIterceptor找到“lang”请求参数,请使用该区域设置并设置Cookie
1.否则,如果有cookie,则将其用作区域设置
1.否则,返回浏览器的区域设置。
这使得用户和测试人员只需添加一个'?lang={locale}查询参数就可以快速更改区域设置。这将为将来的请求设置一个cookie。否则页面将只使用浏览器发送的区域设置。