java - Spring LocaleResolver 链接

标签 java spring spring-mvc localization internationalization

是否可以链接语言环境解析器?

我想从以下位置获取语言环境值:

  1. session 是否映射到我接受的语言

  2. cookie 是否与我接受的语言对应

  3. 如果两者都不包含语言环境信息,则从接受语言中提取它并与我接受的语言环境进行最佳匹配。

你会怎么做?

最佳答案

我觉得你需要自己写LocaleResolver包装了 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

或者如果您更喜欢来自两个链接的 View :

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>
 * &lt;bean id=&quot;localeResolver&quot; class=&quot;org.lds.stack.web.spring.i18n.ChainedLocaleResolver&quot;&gt;
 *      &lt;property name=&quot;localeResolvers&quot;&gt;
 *          &lt;list&gt;
 *              &lt;bean class=&quot;org.lds.stack.web.spring.i18n.UrlLocaleResolver&quot; /&gt;
 *              &lt;bean class=&quot;org.lds.stack.web.spring.i18n.NoDefaultSessionLocaleResolver&quot; /&gt;
 *              &lt;bean class=&quot;org.lds.stack.web.spring.i18n.NoDefaultCookieLocaleResolver&quot;&gt;
 *                  &lt;property name=&quot;cookieMaxAge&quot; value=&quot;31536000&quot;/&gt;
 *              &lt;/bean&gt;
 *              &lt;bean class=&quot;org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver&quot; /&gt;
 *              &lt;bean class=&quot;org.springframework.web.servlet.i18n.FixedLocaleResolver&quot; /&gt;
 *          &lt;/list&gt;
 *      &lt;/property&gt;
 *  &lt;/bean&gt;
 * </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>
 * &lt;stack-web:locale-resolver /&gt;
 * </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>
 *  &lt;mvc:interceptors&gt;
 *      &lt;bean id=&quot;localeChangeInterceptor&quot; class=&quot;org.springframework.web.servlet.i18n.LocaleChangeInterceptor&quot; /&gt;
 *  &lt;/mvc:interceptors&gt;
 * </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;
    }
}

关于java - Spring LocaleResolver 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12107317/

相关文章:

java - spring mvc 验证异常不由 ControllerAdvice 处理

java - 是否可以手动为我的应用设置语言(而不是让用户进行设置?)

java - Spring MVC 中的 @ImportResource 路径

java - 如何在特定包中获取带有某些注释的类

java - Spring Boot Web starter View 位置

java - 在单元测试 Controller 时模拟 Spring Validator

java - 使用改造 :2. 0.0-beta1 发送带有文件的多部分

java - 使用 jsoup 从 body 标签中提取 innerHtml

java - 使用 java6 从 Tomcat6 调用 JAX-WS RI 2.2 服务

java - ProGuard混淆变量命名,如何避免local和param前缀?