java request locale_SpringBoot 国际化配置,SpringBoot Locale 国际化

SpringBoot 国际化配置,SpringBoot Locale 国际化

================================

©Copyright 蕃薯耀 2018年3月27日

http://www.cnblogs.com/fanshuyao/

附件下载(源码下载)见:http://fanshuyao.iteye.com/blog/2414640

一、效果所下:

0a66e38abe851db9540465c8dad7f41d.png

f6398da3e0df6a20c0b8b1ee1595f53a.png

f578eef98c91648c6fe92a2ba731f55e.png

二、SpringBoot 国际化配置

1、创建国际化配置文件(3个):

mess.properties

Java代码  f1385d7018e48e7edefb17714e625f1b.png

mess.user.name=用户名

mess.user.password=密码

mess.user.btn=登录

mess_zh_CN.properties

Java代码  f1385d7018e48e7edefb17714e625f1b.png

mess.user.name=用户名

mess.user.password=密码

mess.user.btn=登录

mess_en_US.properties

Java代码  f1385d7018e48e7edefb17714e625f1b.png

mess.user.name=UserName

mess.user.password=Password

mess.user.btn=Sign In

SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.messages.basename:

Java代码  f1385d7018e48e7edefb17714e625f1b.png

#表示放在classpath的i18n文件夹,文件前缀为mess

spring.messages.basename=i18n.mess

2、自定义国际化语言解析器

Java代码  f1385d7018e48e7edefb17714e625f1b.png

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.LocaleResolver;

import org.thymeleaf.util.StringUtils;

/**

* 自定义国际化语言解析器

*

*/

public class MyLocaleResolver implements LocaleResolver{

private static final String I18N_LANGUAGE = "i18n_language";

private static final String I18N_LANGUAGE_SESSION = "i18n_language_session";

@Override

public Locale resolveLocale(HttpServletRequest req) {

String i18n_language = req.getParameter(I18N_LANGUAGE);

Locale locale = Locale.getDefault();

if(!StringUtils.isEmpty(i18n_language)) {

String[] language = i18n_language.split("_");

locale = new Locale(language[0], language[1]);

//将国际化语言保存到session

HttpSession session = req.getSession();

session.setAttribute(I18N_LANGUAGE_SESSION, locale);

}else {

//如果没有带国际化参数,则判断session有没有保存,有保存,则使用保存的,也就是之前设置的,避免之后的请求不带国际化参数造成语言显示不对

HttpSession session = req.getSession();

Locale localeInSession = (Locale) session.getAttribute(I18N_LANGUAGE_SESSION);

if(localeInSession != null) {

locale = localeInSession;

}

}

return locale;

}

@Override

public void setLocale(HttpServletRequest req, HttpServletResponse res, Locale locale) {

}

}

3、把国际化语言解析器放到Spring容器中:

这里创建了一个自定义的配置类:CustomMvcConfig ,继承WebMvcConfigurerAdapter,可以扩展SpringMvc的功能,包括拦截器,转换器等

Java代码  f1385d7018e48e7edefb17714e625f1b.png

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.LocaleResolver;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.lqy.springboot.message.locale.MyLocaleResolver;

//使用WebMvcConfigurerAdapter可以扩展SpringMvc的功能,包括拦截器,转换器等

//@EnableWebMvc //设置@EnableWebMvc为完全接管SpringMvc,但一般不要设置完全接管SpringMvc

@Configuration

public class CustomMvcConfig extends WebMvcConfigurerAdapter {

/**

* 配置自己的国际化语言解析器

* @return

*/

@Bean

public LocaleResolver localeResolver() {

return new MyLocaleResolver();

}

/**

* 配置自己的拦截器

*/

@Override

public void addInterceptors(InterceptorRegistry registry) {

//super.addInterceptors(registry);

}

}

4、页面显示及切换国际化操作:

Html代码  f1385d7018e48e7edefb17714e625f1b.png

html>

Insert title here

.ib{

display: inline-block;

}

.ml20{

margin-left: 20px;

}

.mt20{

margin-top: 20px;

}

[[#{mess.user.name}]]:
[[#{mess.user.password}]]:
[[#{mess.user.btn}]]

中文

英文

(如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!)

================================

©Copyright 蕃薯耀 2018年3月27日

http://www.cnblogs.com/fanshuyao/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值