使用SpringMVC实现国际化之根据浏览器默认语言切换语言环境
  • 编写国际化资源文件

    • login_en_US.properties

      welcomeinfo=welcome to keyi.world
      username=USERNAME
      password=PASSWORD
      loginbtn=LOGIN
    • login_zh_CN.properties

      welcomeinfo=欢迎来到keyi.world
      username=用户名
      password=密码
      loginbtn=登陆
  • 在SpringMVC配置文件中配置ResourceBundleMessageSource,管理国际化资源文件

    <bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource">               
        <property name="basename" value="country/login"></property>
    </bean>
    其中,basename属性的value值,写的是resources资源目录下的国际化资源文件的开头,login
  • 在页面中取资源文件的value值

    加入fmt标签:<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    使用时:<fmt:message key="welcomeinfo"/>
    key的值表示从资源文件中取value值
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <html>
        <head>
            <title>国际化登录页面</title>
        </head>
        <body>
            <fmt:message key="welcomeinfo"/>
            <form action="" method="post">
                <fmt:message key="username"/><input type="text" name="username"><br>
                <fmt:message key="password"/><input type="text" name="password"><br>
                <input type="submit" value="<fmt:message key="loginbtn"/>">
            </form>
        </body>
    </html>
  • 实现的效果:根据浏览器的语言设置,访问login页面时,切换相应的语言。

使用SpringMVC实现国际化之通过点击链接切换语言环境(自定义区域信息解析器实现)
  • 前端页面

    <fmt:message key="welcomeinfo"/>
    <form action="" method="post">
        <fmt:message key="username"/><input type="text" name="username"><br>
        <fmt:message key="password"/><input type="text" name="password"><br>
        <input type="submit" value="<fmt:message key="loginbtn"/>">
    </form>
    切换<a href="goLogin?locale=zh_CN">中文</a>|<a href="goLogin?locale=en_US">英文</a>
  • 自定义区域信息解析器,实现LocaleResolver

    public class MyLocaleResolver implements LocaleResolver {
        /*解析区域信息*/
        @Override
        public Locale resolveLocale(HttpServletRequest httpServletRequest) {
            Locale locale=null;
            String localeInfo = httpServletRequest.getParameter("locale");
            if (localeInfo!=null&&!localeInfo.equals("")){
                System.out.println(localeInfo);
                locale = new Locale(localeInfo.split("_")[0],
                    localeInfo.split("_")[1]);
            }else {
                locale=httpServletRequest.getLocale();
            }
            return locale;
        }
    
        /*修改区域信息,设置为不修改*/
        @Override
        public void setLocale(HttpServletRequest httpServletRequest, 
            HttpServletResponse httpServletResponse, Locale locale) {
                throw new UnsupportedOperationException
                ("Cannot change HTTP accept header - use a different locale 
                    resolution strategy");
        }
    }
  • SpringMVC配置文件中,指定区域信息解析器为我们自定义的解析器,而不是SpringMVC默认的
    AcceptHeaderLocaleResolver

    <!--自定义区域信息解析器-->
    <bean id="localeResolver" class="world.keyi.localeResolver.MyLocaleResolver"></bean>
  • 实现的效果:通过点击链接中文/英文,切换页面的语言环境

此外,还可以通过SpringMVC中自带的SessionLocaleResolver或者CookieLocaleResolver实现上述效果
通过SessionLocaleResolver或者CookieLocaleResolver实现时,需要配置LocaleChangeInterceptor拦截器