zoukankan      html  css  js  c++  java
  • 复习Spring第四课---Spring对国际化的支持

    其实国际化这东西很少使用,之前也就是粗略的学了下,趁今天有空,拿出来稍微写写。以前学android开发的时候,类似于多语言的版本。差别就是一个是手机打开,一个是浏览器打开,本质是一样的。

    在Spring配置文件中的配置如下:

     1 <?xml version="1.0" encoding="UTF-8"?>  
     2 <beans xmlns="http://www.springframework.org/schema/beans"  
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
     4     xmlns:tx="http://www.springframework.org/schema/tx"  
     5     xsi:schemaLocation="  
     6             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
     7             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
     8             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
     9 <!--  
    10  Spring 通过ApplicationContext 容器的MessageSource 接口实现Sping 对国际化的支持。  
    11 这里的Bean id 必须是“messageSource”,因为Spring 在装配系统Bean 时会根据这个名字进行查找。  
    12        这样,我们便可以在程序中通过ApplicationContext 对象调用getMessage()方法获取资源文件的信息。  
    13 -->  
    14  <bean id="messageSource"  
    15   class="org.springframework.context.support.ResourceBundleMessageSource"  
    16   abstract="false" lazy-init="default"  
    17   autowire="default" dependency-check="default">  
    18   <property name="basenames">  
    19   <list>  
    20    <value>config/properties</value>  
    21   </list>  
    22   </property>  
    23  </bean>  
    24 </beans>

    两个资源文件

    properties_en_US.properties

    message1=Hello {0},GoodBye{1}

    properties_zh_CN.properties

    message1=u4f60u597d{0},u518du89c1{1}

    至于u4f60u597d和u518du89c1其实就是‘你好’和‘再见’的ASCII码值,在国际化资源文件中,中文汉字必须要配置成ASCII形式的,具体怎么得到这些值的呢

    win+r-->运行窗口 输入native2ascii (前提是装了JDK了)

    测试代码:

    package com.lsd.test;  
      
    import java.util.Date;  
    import java.util.Locale;  
      
    import org.junit.Test;  
    import org.springframework.context.ApplicationContext;  
    import org.springframework.context.support.FileSystemXmlApplicationContext;  
      
    public class CommonTest {  
      
        @Test  
        public void test1() {  
            String path = "WebRoot/WEB-INF/applicationContext.xml";  
            ApplicationContext context = new FileSystemXmlApplicationContext(path);  
            Object[] objs = new Object[] { "lsd",  
                    new Date().toLocaleString() };  
            // message1为资源文件的key值,objs为资源文件value值所需要的参数,Local.CHINA为国际化为什么语言  
            String str = context.getMessage("message1", objs, Locale.CHINA);  
            System.out.println(str);  
        }  
    }  
    </span>  

    在WEB页面上怎么实现呢,首先添加个sping.tld到WEB-INF/lib下,还需要spring-webmvc.jar

    在这之后需在Spring配置文件中添上

    1 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>

    在jsp页面上:

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
     2 <%@ taglib prefix="spring" uri="WEB-INF/lib/spring.tld"%>  
     3 <html>  
     4   <head>  
     5     <title>Spring国际化</title>  
     6   </head>  
     7   <body>  
     8      <spring:message code="message1" /><br>  
     9     <input type="button" value="<spring:message code="message1" />"/><br>  
    10   </body>  
    11 </html>

    浏览器默认都是中文的,所以显示的结果肯定是中文的,显示英文只要更换下浏览器语言即可

  • 相关阅读:
    Python-HTML基础
    异常处理
    反射hasattr; getattr; setattr; delattr
    Python 属性方法、类方法、静态方法、 特殊属性__doc__ (内建属性)
    Python3 day6面向对象
    re模块计算器作业
    re正则表达式:import re ;re.search()
    hashlib模块学习:hmac
    ConfigParser模块,主要应用于对php.ini等格式的配置文件内容读取和生成。删改较少用
    ymal文档格式 处理
  • 原文地址:https://www.cnblogs.com/timePasser-leoli/p/7591656.html
Copyright © 2011-2022 走看看