JavaWeb-25-SpringMVC国际化

目录

一:简单国际化

0:导包,防止jstl和fmt不能使用

1:写好国际化资源文件--一定放在资源目录下

2:让Spring的ResourceBundleMessageSource管理国际化资源文件

3、直接去页面取值

4、现象:是按照浏览器带来语言信息决定;

二:点击链接进行资源国际化

1:自定义区域语言解析器-LocalResolver

2:配置自定义解析器

三:点击链接使用SessionLocaleResolver

1:在请求的时候设置在session中

2:使用系统的区域信息拦截器进行设置(LocaleChangeInterceptor)


一:简单国际化

0:导包,防止jstl和fmt不能使用

<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard</artifactId>
      <version>1.2.5</version>
    </dependency>

1:写好国际化资源文件--一定放在资源目录下

2:让Spring的ResourceBundleMessageSource管理国际化资源文件

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="message"></property>
</bean>

3、直接去页面取值

4、现象:是按照浏览器带来语言信息决定;

 Locale locale = request.getLocale();//获取到浏览器的区域信息

系统默认是这样获取系统语言信息;

二:点击链接进行资源国际化

1:自定义区域语言解析器-LocalResolver

package com.wkl;

import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * Description:自定义区域解析器
 * Date:       2020/8/18 - 下午 1:56
 * author:     wangkanglu
 * version:    V1.0
 */
public class MyLocalResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String lang = request.getParameter("lang");
        Locale l = null;
        if(!"".equals(lang) && lang!=null){
            l = new Locale(lang.split("_")[0],lang.split("_")[1]);
        }else {
            l = request.getLocale();
        }
        return l;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}

2:配置自定义解析器

在初始化语言解析器时,会从容器中找id为localeResolver的解析器;没有采用默认的;所以我们自定义的id就设置成localeResolver

<!--自定义区域解析器-->
<bean id="localeResolver" class="com.wkl.MyLocalResolver"></bean>

三:点击链接使用SessionLocaleResolver

1:在请求的时候设置在session中

package com.wkl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.http.HttpSession;
import java.util.Locale;

/**
 * Description:
 * Date:       2020/8/18 - 上午 10:18
 * author:     wangkanglu
 * version:    V1.0
 */
@Controller
public class LoginPageController {

    @Autowired
    private MessageSource messageSource;

    @RequestMapping("tologinpage")
    public String toLoginPage(String lang, Locale locale, HttpSession session){
        Locale l = null;
        if(!"".equals(lang) && lang!=null){
            l = new Locale(lang.split("_")[0],lang.split("_")[1]);
        }else {
            l = locale;
        }
        session.setAttribute(SessionLocaleResolver.class.getName() + ".LOCALE",l);
        return "login";
    }
}

2:使用系统的区域信息拦截器进行设置(LocaleChangeInterceptor)

所以:

<a href="/tologinpage?locale=zh_CN">中文</a>
<a href="/tologinpage?locale=en_US">英文</a>

在dispatcher-servlet.xml中配置

<!--区域信息从session中拿-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>

<!--配置区域信息拦截器-->
<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
</mvc:interceptors>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苍煜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值