SpringMVC国际化

133 阅读2分钟

i18n internationalization 国际化

各国缩写: www.cnblogs.com/exmyth/p/11…

1、通过浏览器语言设置国际化

2、通过超级链接来切换国际化

3、国际化类型转换和验证失败的信息

4、国际化代码中的内容


1、添加国际化语言的资源文件(中文 英文等) 2、 告诉spirngmvc 3、动态调用资源文件

基于浏览器设置的国际化语言切换

1、新建jsp对应的国际化属性资源文件

image.png image.png

2、配置spring xml文件 ,将国际化文件都注入到springmvc中

<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">

    <property name="basenames">
        <array>
            <value>i18n/login</value>
            <value>i18n/default</value>
        </array>
    </property>
</bean>

3、jsp文件配置taglib 及调用 <spring:message code="txt.password"></spring:message>

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<div class="form-label-group">
    <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
    <label for="inputPassword"><spring:message code="txt.password"></spring:message> </label>
</div>

<div class="checkbox mb-3">
    <label>
        <input type="checkbox" value="remember-me"> <spring:message code="txt.remeber"></spring:message>
    </label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit"><spring:message code="txt.login"></spring:message> </button>
<p class="mt-5 mb-3 text-muted text-center">&copy; 2017-2020</p>

基于URL链接实现国际化方式1

1、在基于浏览器基础上添加springxml bean配置


<!--    使用SessionLocaleReslover保持Local的状态    会存session中获取Locale对象-->
    <bean class="org.springframework.web.servlet.i18n.SessionLocaleResolver" id="localeResolver">

    </bean>
    
    

2、在JSP文件中添加 切换链接

<div class="form-label-group">
    <a class="col-md-6" href="${basepath}/i18n/en_US">English</a>
    <a  class="col-md-6"  href="${basepath}/i18n/zh_CN">中文</a>
</div>

3、在控制器中实现切换方法

@RequestMapping("/i18n/{language}_{locale}")
public  String changelocale(@PathVariable String locale,@PathVariable String language, @Autowired SessionLocaleResolver localeResolver, HttpServletRequest req, HttpServletResponse resp){
    Locale local = new Locale(language,locale);

    localeResolver.setLocale(req,resp,local);
    return "login";
} 

基于URL链接实现国际化方式2

使用Spring 提供的拦截器
1、在Spring xml文件中添加拦截器

<!--使用-->
<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>

2、URL访问

<div class="form-label-group">
    <a class="col-md-6" href="?locale=en_US">English_interceptor</a>
        <a  class="col-md-6"  href="?locale=zh_CN">中文_拦截器</a>
</div>

在编辑器中使用 properties i18n的配置文件

1、自动注入MessageSource

@Autowired
MessageSource messageSource;

2、调用messageSource.getMessage方法

model.addAttribute("errMsg",messageSource.getMessage("txt.login_error",null,locale));

备注:方法入口要传入 locale 参数

public String login(@Valid @ModelAttribute  User user, BindingResult bindingResult, Model model, Locale locale){

带参数

txt.login_error=用户名或密码错误,不存在{0}的账号,{1}密码不正确

调用

model.addAttribute("errMsg",messageSource.getMessage("txt.login_error",new Object[]{user.getEmail(),user.getPassword()},locale));

验证信息使用i18n

1、去掉model里面的message

@NotEmpty(message = "邮箱地址不能为空")
@Email(message = "请输入正确的邮箱")
private String email;

改为

@NotEmpty
@Email
private String email;

2、在properties里面输入要提示的内容 注解.对象.属性名

@NotEmpty
@Email
private String email;

自定义日期格式转换失败

typeMismatch.user.date=日期格式不正确