spring国际化locale的代码原理以及拓展配置

Posted wdw123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring国际化locale的代码原理以及拓展配置相关的知识,希望对你有一定的参考价值。

原理

两种不同的LocaleResolve

LocaleResolver

public interface LocaleResolver {

  	//解析Lcoale
   Locale resolveLocale(HttpServletRequest request);

    //设置Locale
   void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, 
                  @Nullable Locale locale);

LocaleContextResolver多了解析时区的功能

/**
 * Extension of {@link LocaleResolver}, adding support for a rich locale context
 * (potentially including locale and time zone information).
 */
public interface LocaleContextResolver extends LocaleResolver {

   LocaleContext resolveLocaleContext(HttpServletRequest request);

   void setLocaleContext(HttpServletRequest request, 
                         @Nullable HttpServletResponse response,
        				 @Nullable LocaleContext localeContext);

LocaleContext

public interface LocaleContext {
    @Nullable
    Locale getLocale();
}

在DispatchServlet配置新的LocaleContext

//每次请求都会解析配置新的LocaleContext
@Override
protected LocaleContext buildLocaleContext(final HttpServletRequest request) {
    LocaleResolver lr = this.localeResolver;
    //根据locale不同等级进行调用
    if (lr instanceof LocaleContextResolver) {
        return ((LocaleContextResolver) lr).resolveLocaleContext(request);
    }
    else {
        //lumbda表达式返回一个类(重写了LcoaleContext的getLocale方法)
        return () -> (lr != null ? lr.resolveLocale(request) : request.getLocale());
    }
}

通过rend方法,使用response渲染

protected void render(ModelAndView mv, HttpServletRequest request,
                      HttpServletResponse response) throws Exception {
    // Determine locale for request and apply it to the response.
    Locale locale =
        (this.localeResolver != null ?
         this.localeResolver.resolveLocale(request) : request.getLocale());
    response.setLocale(locale);//在response方法写出Locale

实现

/**
 *自定义的LocaleReslover和Controller(可以根据URL设置Locale并且放在Session当中)
 */
public class ParamLocaleResolver implements LocaleResolver {
	//Session域的键的名称
    public static final String LOCALE_SESSION_ATTRIBUTE_NAME 
        = ParamLocaleResolver.class.getName() + ".LOCALE";
    
    private String localeAttributeName = LOCALE_SESSION_ATTRIBUTE_NAME;
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //每次从Session当中获取,不需要重新创建。
        Locale locale 
            = ((Locale) WebUtils.getSessionAttribute(request, localeAttributeName));
        if (locale == null) {
            //第一次将浏览器默认的locale写入Session
            locale = request.getLocale();
            WebUtils.setSessionAttribute(request, localeAttributeName, locale);
        }
        return locale;
    }
    @Override
    public void setLocale(HttpServletRequest request, 
                          HttpServletResponse response, Locale locale) {
        if (locale != null) {
            //重新写入Session当中
            WebUtils.setSessionAttribute(request, localeAttributeName, locale);
        }
    }
}
@Controller
public class LocaleController {

    @RequestMapping("/setLocal")
    public String setLocale(HttpServletRequest request,
                            @Autowired ParamLocaleResolver resolver) {
        //根据参数获取Locale
        String param = request.getParameter("locale");

        if (!StringUtils.isEmpty(param)) {
            String[] split = param.split("_");
            Locale locale = new Locale(split[0], split[1]);
            //locale解析成功设置新的locale
            resolver.setLocale(request, null, locale);
        }
        return "login";
    }
}

配置

(

applications.properties

# 默认放在类路径下的 message.properties
#eg:classpath:/message/login.properties basename写为message.login (会将‘.’解析为 ‘/’)
spring.messages.basename=message

以上是关于spring国际化locale的代码原理以及拓展配置的主要内容,如果未能解决你的问题,请参考以下文章

Java常用类库——国际化程序(Locale,ResourceBundle以及MessageFormat处理动态文本)

Spring学习系列——Spring中的国际化

#yyds干货盘点#Spring专题「实战系列」Spring Security原理以及实战认证分析开发指南

Android 国际化 设置Locale.ENGLISH不生效以及createConfigurationContext()问题

深入浅出Spring原理及实战「开发实战系列」SpringSecurity原理以及实战认证分析开发指南

Spring--Spring国际化