使用XML来管理国际化数据

使用XML来管理国际化数据

1.问题提出
XML是Extensible Markup Language的简写,是一种扩展性标识语言。XML已经被应用到很多领域,并且会被用到
更多的领域。



应用程序中的国际化和本地化会涉及到资源文件的问题。Java中通常使用.properties文件来存储不同语言的资源,
例如res_en_US.properties和内容分别如下:
文件res_en_US.propertie
helloWorld=Hello World!
success=Success

文件res_zh_CN.properties

helloWorld=你好世界!
success=成功

还有一些数据会在数据库初始化时放到数据中,例如国家列表:
脚本country_zh.sql
insert into country (id,name) values(1,"中国");
insert into country (id,name) values(2,"美国");
insert into country (id,name) values(3,"印度");

脚本country_en.sql
insert into country (id,name) values(1,"China");
insert into country (id,name) values(2,"America");
insert into country (id,name) values(3,"India");


当这些数据非常多的时候,多个文件的同步维护非常繁琐。

2.解决方案
我们可以通过XML+XSLT+Ant来解决这个问题。
使用一个XML文件统一维护资源:
res.xml

<?xml version="1.0" encoding="UTF-8"?>
<properties>
  <property key="helloworld"
            value_zh="你好世界!"
            value_en="Hello World!"/>
  <property key="success"
            value_zh="成功"
            value_en="Success"/>
</properties>

定义XSLT转换文件:
res.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:param name="lang"/>
   
    <xsl:template match="/">   
        <xsl:for-each select="properties/property">           
            <xsl:value-of select="@key" />
            <xsl:text>=</xsl:text>
            <xsl:if test="$lang='zh_CN'">
                <xsl:value-of select="@value_zh"/>
            </xsl:if>
       
            <xsl:if test="$lang='en_US'">
              <xsl:value-of select="@value_en"/>
            </xsl:if>
          <xsl:text>&#xa;</xsl:text>      
        </xsl:for-each>                     
    </xsl:template>
</xsl:stylesheet>

使用XML文件维护国家列表:
country.xml
<?xml version="1.0" encoding="UTF-8"?>
<countryList>
  <country id="1"
           value_zh_CN="中国"
           value_en_US="China"/>
  <country id="2"
           value_zh_CN="美国"
           value_en_US="America"/>
  <country id="3"
           value_zh_CN="印度"
           value_en_US="India"/>
</countryList>

定义转换文件:
country.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:param name="lang"/>
   
    <xsl:template match="/">   
        <xsl:for-each select="countryList/country">   
          <xsl:text>insert into country (id,name) values(</xsl:text>       
            <xsl:value-of select="@id" />
            <xsl:text>,'</xsl:text>
            <xsl:if test="$lang='zh_CN'">               
                <xsl:value-of select="@value_zh_CN"/>
                <xsl:text>');</xsl:text>
            </xsl:if>
           
       
            <xsl:if test="$lang='en_US'">
              <xsl:value-of select="@value_en_US"/>
              <xsl:text>');</xsl:text>
            </xsl:if>
           
          <xsl:text>&#xa;</xsl:text>      
        </xsl:for-each>                     
    </xsl:template>
</xsl:stylesheet>

定义Ant文件:
<?xml version="1.0" encoding="UTF-8"?>

<project name="app" basedir="." default="all">
    <property name="basedir"   location="."/>
    <property name="build.hom" location="./build"/>
    <property name="res.file"  value="res.xml"/>
    <property name="res.style" value="res.xsl"/>
    <property name="" value=""/>
       
        <target name="clear" description="clear up">
            <delete file="*.properties"/>
        </target>
       
   
    <target name="buildres" depends="clear" description="creates resource files">  
      <style in="./res.xml"    style="./res.xsl"     out="./res_zh_CN.properties" force="yes">
        <param name="lang" expression="zh_CN"/>
          </style>
         
          <style in="./res.xml"    style="./res.xsl"     out="./res_en_US.properties" force="yes">
        <param name="lang" expression="en_US"/>
          </style>           
    
         
          <style in="${basedir}/country.xml"    style="./country.xsl"     out="./country_zh_CN.sql" force="yes">
        <param name="lang" expression="zh_CN"/>
          </style>
          <style in="${basedir}/country.xml"    style="./country.xsl"     out="./country_en_US.sql" force="yes">
        <param name="lang" expression="en_US"/>
          </style> 
    </target>
   

        <target name="all" depends="buildres" description="all"/>
</project>


3.总结
  通过这种方式,我们可以轻松统一维护国际化资源。上面的例子对于不同的资源定义了不同的XML文件和XSLT文件,更进一步,
  可以定义统一的XML文件和XSLT文件,还可以通过Ant的合并任务concat,达到字符集(Encoding)的转换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值