zoukankan      html  css  js  c++  java
  • spring 国际化i18n配置

    i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称。在资讯领域,国际化(i18n)指让产品(出版物,软件,硬件等)无需做大的改变就能够适应不同的语言和地区的需要。对程序来说,在不修改内部代码的情况下,能根据不同语言及地区显示相应的界面。 在全球化的时代,国际化尤为重要,因为产品的潜在用户可能来自世界的各个角落。通常与i18n相关的还有L10n(“本地化”的简称)。<摘自百度百科http://baike.baidu.com/view/372835.htm?fr=aladdin >

    代码下载

    http://pan.baidu.com/s/1sjNQmfF

    Maven依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <properties>
            <springframework>4.0.5.RELEASE</springframework>
        </properties>
        <dependencies>
            <!-- Spring web mvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${springframework}</version>
            </dependency>
        </dependencies>

    项目截图

    在Spring应用中,国际化的配置比较简单,下面四步完成国际化的快速配置

    第一步,配置messageSource和localeResolver

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <!-- 配置国际化资源文件路径 -->
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename">
                <!-- 定义消息资源文件的相对路径 -->
                <value>messages/message</value>
            </property>
        </bean>
        <!-- 基于Cookie的本地化解析器 -->
         <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
           <property name="cookieMaxAge" value="604800"/>
           <property name="defaultLocale" value="zh_CN"/>
           <property name="cookieName" value="Language"></property>
         </bean>
        <!-- 基于Session的本地化解析器 -->
        <!--<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />-->

    第二步,编写message_*.properties

    message_en.properties

    1
    2
    3
    4
    5
    6
    7
    hi=hello
    something=The People's Republic of China
    Chinese=Chinese
    English=English
    index=Index
    welcome=Welcome
    OtherPage=Other Page

    message_zh_CN.properties(汉字已转成unicode码)

    1
    2
    3
    4
    5
    6
    7
    hi=u4F60u597D
    something=u4E2Du534Eu4EBAu6C11u5171u548Cu56FD
    Chinese=u4E2Du6587
    English=u82F1u6587
    OtherPage=u5176u4ED6u9875u9762
    index=u9996u9875
    welcome=u6B22u8FCE

    第三步,页面引入spring标签库

    引入

    1
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

    使用

    1
    <spring:message code="welcome"></spring:message>

    第四步,切换语言

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    @Autowired CookieLocaleResolver resolver;
         
        //@Autowired SessionLocaleResolver resolver;
         
        /**
         * 语言切换
         */
        @RequestMapping("language")
        public ModelAndView language(HttpServletRequest request,HttpServletResponse response,String language){
             
            language=language.toLowerCase();
            if(language==null||language.equals("")){
                return new ModelAndView("redirect:/");
            }else{
                if(language.equals("zh_cn")){
                    resolver.setLocale(request, response, Locale.CHINA );
                }else if(language.equals("en")){
                    resolver.setLocale(request, response, Locale.ENGLISH );
                }else{
                    resolver.setLocale(request, response, Locale.CHINA );
                }
            }
             
            return new ModelAndView("redirect:/");
        }

    已完成国际化的配置,其中请注意SessionLocaleResolver和CookieLocaleResolver的区别,很显然,通过Session只能对当前的会话有效,Cookie则对Cookie有效期内的会话都有效。在使用Cookie的时候,需要设置Cookie的过期时间,否则关闭浏览器之后,Cookie即失效了,没有达到目的。当然,也可以保存用户的语言设置信息到数据库,用户登录之后即可将语言改变成用户设置的语言。

     

    运行效果截图:

  • 相关阅读:
    C# Linq Enumerable 技巧
    Winform 踩坑
    BootStrap Table
    java8+junit5实现并发测试(多线程)
    Junit5+REST-assured 做接口测试
    log4j的使用
    ASP.NET项目启用SSL
    hyper-v虚拟机内存占用过高
    C#使用qq邮箱的smtp服务发邮件
    CALayer设置圆角
  • 原文地址:https://www.cnblogs.com/shaohz2014/p/7729028.html
Copyright © 2011-2022 走看看