tapestry5.1本地化
来源:互联网 发布:为什么会用反射java 编辑:程序博客网 时间:2024/05/16 10:55
在如下程序中设置使应用程序值接受那种语言
/**
* CopyRright (c)2003-2010
* 项目名称: bblTapestry5
* 文件名称: AppModule.java
* 描述:
* 使用的JDK版本: JDK1.6
* 包名: com.org.services
* 创 建 人: 白正阳
* 生成日期: May 21, 2010
* 修改人:
* 修改日期:
* 修改原因描述
* 版本号: 1.1
*/
package com.org.services;
import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.ioc.MappedConfiguration;
/**
* 创 建 人: 白正阳
* 生成日期 : May 21, 2010
*/
public class AppModule
{
public static void contributeApplicationDefaults(MappedConfiguration<String, String> configuration)
{
configuration.add(SymbolConstants.PRODUCTION_MODE, "false");
//can only accept english and Chinese
configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en,zh_CN");
}
}
详细信息请参考java.util.Locale. en and zh are the language code defined by ISO-639. CN is the country /area code.
A Message Catalog is a group of properties files. the file format of Properties file ise java.util.ResourceBundle file format. each page/component can have their own Message Catalog.
For Start page, it can have a main properties page called Start.properties. and can also have properties files for specific language, e.g. Start_en.properties is specific for english, Start_zh.pproperties is specific for Chinese. Furthermore, Start page can also specify properties file in the form of a language of a country, Start_zh_CN.properties is specified to mainland China. These properties file and the Start page class need to be stored in the package.
There is also another type of Message Catalog called app.properties. which has the scope of the whole application. app is the name defined in the filter of web.xml. Similiarly, there are also app_zh.properties, app_zh_CN.properties. These files are placed in the WEB-INF file.
If we have a Start.xml
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
<div>${message:heading}
</div>
<t:form t:id="regForm"> ...
</t:form>
</html>
In case a page request come from "zh_CN", Tapestry will search for Start_zh_CN.properties files firstly, and usethe "heading" in that file to replace the ${message:heading} in the Start.tml file.
if above file does not exists or there is no heading parameter in that file, Tapestry will continue search for Start_zh.properties file. If failed again, then try to find the Start.properties file.
if the Start page Message Catalog does not contain "heading", then Tapestry will search the application message Catalog in the sequence of app_zh_CN.properties, app_zh.properties, app.properties.
If the heading parameter still cannot be found, then use the “[[missing key: heading]] instead.
Start_en.properties:
heading=Register
Start_zh_CN.properties:
heading=注册新帐号
<t:textfield t:id="userName" value="user.name" label="用户名" validate="required,minlength=6,maxlength=20,regexp" />
set the parameter of the "label" to "“message:userName" and add userName=用户名, In another word, we do not need the "label" property, the TextField will get its property value from "message:userName-label" by default
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"> <div>${message:heading}</div> <t:form t:id="regForm"> <t:errors /> <div> <t:label for="userName" /> <t:textfield t:id="userName" value="user.name" validate="required,minlength=6,maxlength=20,regexp" /> </div> <div> <t:label for="password" /> <t:passwordfield t:id="password" value="user.password" validate="required,minlength=6,maxlength=20" /> </div> <div> <t:label for="password2" /> <t:passwordfield t:id="password2" value="password2" validate="required,minlength=6,maxlength=20" /> </div>
................
And we have the Start_en.properties
heading=Register
userName-label=User Name
password-label=Password
password2-label=Repeat Password
email-label=Email
age-label=Age
gender-label=Gender
male-label=Male
female-label=Female
country-label=Country
subscribe-label=Subscribe newsletter
register=Register
registerAndLogin=Register & Login
and the Chinese version of the Start_zh_CN.properties
heading=注册新帐号
userName-label=用户名
password-label=密码
password2-label=重复密码
email-label=电子邮件
age-label=年龄
gender-label=性别
male-label=男
female-label=女
country-label=国家
subscribe-label=预订邮件
register=注册
registerAndLogin=注册并登录
Create an app_en.properties file in WEB-INF forlder
Country.CHINA = China
Country.USA=USA
restart the server
userName-regexp-message=%2$s may only contain letters, numbers and underscore
add above line to Start_en.properties file
userName-regexp-message=%2$s只能由字母、数字和下划线组成。
add above line to Start_zh_CN.properties file
comment out the userName-regexp-message from the Start.properties
#userName-regexp-message=%2$s只能由字母、数字和下划线组成。
userName-regexp=^[a-zA-Z0-9_]*$
The verification of whether two passwords are same is done in the onValidateForm() method in Start page class
public class Start
{
... void onValidateForm()
{
if (password2 != null && !password2.equals(user.getPassword()))
{ regForm.recordError(password2Field, messages .get("differ-passwords"));
} }
void onSuccess()
{ System.out.println(user);
System.out.println("password2:" + password2);
System.out.println("login:" + login);
} }
we use the Messages to retrieve localize information from properties file by .get() method.
we can use the Messages.format() method is the information have some parameters with it.
append the line below into the Start_en.properties file
differ-passwords=Two versions of password do not match.
and
differ-passwords=两个密码不一样。
to Start_zh_CN.properties
LOCALIZE THE COMPLETE TML
provide three files
one page class file , e.g.
public class Localization
{
}
and two tml files
Localization_en.tml
and
localization_zh_CN.tml
So that we do not need to provide tml for different users.
LOCALIZE ASSET
Asset are the files that require downloading except the normal files download bgy explorer, e.g. css file, img and javascript file.
first of all, we need to create a package called "com.org.img", and put two images in it. Then modify the Start.tml file
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
<div>
<img src="${asset:../img/register.gif}" />
</div>
<div>
${message:heading}
</div>
...
</html>
The asset prefix get the asset from classpath by default, we can put the asset in the web context, and get the asset from context, e.g. “${asset:context:img/register.gif}”OR“${context:img/register.gif}
create a css file in WEBContent, and create two css files
main_en.css
@CHARSET "UTF-8";
div label:first-child
{ width: 140px; float: left; text-align: right; }
div label:first-child:after
{ content: ":"; }
main_zh_CN.css
@CHARSET "UTF-8";
div
label:first-child
{ width: 140px; float: left; text-align: right; }
div label:first-child:after
{ content: ":"; }
NOTICE THAT THE SEMICOLON IN CHINESE CSS FILE IS THE CHINESE SEMICOLON AND THE
ONE IN ENGLISH CSS FILE IS THE ENGLISH SEMI COLON.
SWITCH LOCALE
display a link to switch Locale.
we defined the tapestry.supported-locales(“en,zh_CN”) in the appModule class, now
we will read this parameter in the LocaleBar class
public class LocaleBar {
@Inject
@Service("ApplicationDefaults")
private SymbolProvider symbolProvider;
public String[] getLocales()
{
String symbol = symbolProvider .valueForSymbol(
SymbolConstants.SUPPORTED_LOCALES);
return symbol.split(",");
}
}
and create LocaleBar.tml in com.org.components package
<t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
<t:loop source="locales" value="var:locale"> >
<t:actionlink t:id="switch" context="var:locale">${var:locale}
</t:actionlink>
</t:loop>
</t:container>
and use this component in the Start.tml
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
<div>
<t:localebar />
</div>
<div>
<img src="${asset:../img/register.gif}" />
</div> ... </html>
the text of the link should corresponds to the language environment of the user.
Modify LocaleBar class
public class LocaleBar {
@Property
private String locale;
@Inject
@Service("ApplicationDefaults")
private SymbolProvider symbolProvider;
public String[] getLocales() {
String symbol = symbolProvider .valueForSymbol(
SymbolConstants.SUPPORTED_LOCALES);
return symbol.split(",");
}
public String getLocaleName()
{ Locale l = toLocale(locale);
return l.getDisplayName(l);
}
private Locale toLocale(String shortName)
{
String[] result = shortName.split("_");
if (result.length == 1)
{ return new Locale(result[0]); }
else
{ return new Locale(result[0], result[1]); }
}
}
and modify the LocaleBar.tml
<t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
<t:loop source="locales" value="locale"> >
<t:actionlink t:id="switch" context="locale">${localeName}
</t:actionlink>
</t:loop>
</t:container>
- tapestry5.1本地化
- Tapestry5.1 form submit
- Tapestry5.1 select
- tapestry5.1 最简单工程
- Tapestry5.1中的一些"规约"
- 解读Tapestry5.1——页面渲染
- 解读Tapestry5.1——Form
- 解读Tapestry5.1——页面渲染
- 解读Tapestry5.1——页面渲染
- Tapestry5.1在myeclipse6.5导入tomcat6时报找不到org.apache.tapestry5.spring.TapestrySpringFilter
- 解读Tapestry5.1——请求调用链
- 解读Tapestry5.1——请求调用链
- tapestry5.1使用 t:if时应注意一点
- 解读Tapestry5.1——请求调用链
- Tapestry5杂谈
- Tapestry5 grid
- Tapestry5杂谈
- balst-2.7.1+ 本地化
- 奇怪的校正结果
- MTK Memory dump
- sis中lucene 的应用
- 自定义控件不能显示: ThemeInfoAttribute
- FCKEditor控件的配置使用
- tapestry5.1本地化
- JavaScript弹出页面
- row_number()over函数的使用(转)
- 使用js代码在aspx页面动态显示当前日期和时间
- 在窗体加载时通过下面的代码为什么结束不了explorer进程???
- windows远程访问linux
- 文件下载代码,可以用迅雷进行下载(因为之前用过一段下载文件的代码,但是不能用迅雷下载,超级汗!!!)
- mtk知识小结
- sql语句学习笔记(6)-常用字符串操作函数