Koala平台的i18n组件是基于Maven的项目模块,最方便的集成是项目也使用Maven,war项目集成会稍显麻烦
Maven项目集成
添加仓库
<repositories>
<repository>
<id>koala-releases</id>
<url>http://nexus.openkoala.org/content/repositories/public-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>koala-snapshots</id>
<url>http://nexus.openkoala.org/content/repositories/public-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
添加依赖
<dependency>
<groupId>org.openkoala.koalacommons</groupId>
<artifactId>koala-commons-i18n</artifactId>
<version>3.0.0</version>
</dependency>
创建i18n.tld文件
在WEB-INF目录下创建i18n.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.1</tlib-version>
<short-name>i18n</short-name>
<uri>http://www.openkoala.org/i18n</uri>
<tag>
<name>i18n</name>
<tag-class>org.openkoala.framework.i18n.tag.I18nTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>key</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>locale</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
配置web.xml
<jsp-config>
<taglib>
<taglib-uri>http://www.openkoala.org/i18n</taglib-uri>
<taglib-location>/WEB-INF/i18n.tld</taglib-location>
</taglib>
</jsp-config>
创建资源文件
在类路径下创建i18n目录,资源文件名称必须是xxxx_语言_国家(大写).properties,内容是key-value键值对
如:test_zh_CN.properties
test_en_US.properties
i18n目录下可以按业务模块创建子目录,这样可以使资源文件内容分散到各个模块
应用JSP页面
创建i18n.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.openkoala.org/i18n" prefix="koala" %>
<!DOCTYPE html>
<html>
<head>
<title>I18N</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
I18N:<koala:i18n key="test" />
</body>
</html>
i18n组件是根据浏览器语言进行切换,切换英文如下
War项目集成
添加下图所示的依赖jar,到http://nexus.openkoala.org/下载
把jar包拷贝到lib目录下,其它步骤与上面一致