一、多國語言資源屬性文件
1、定義多國語言資源文件
??? 為了讓系統中支持多國語言,我們需要把各國具體的文字信息存放到資源文件中,EasyJWeb的多國語言資源文件統一存的位置在\WEB-INF\applicationSource目錄,文件名為類似下面的內容xxxx_zh.properties,xxxx_en.properties,其中xxx代表資源區域標識、zh及en代表語言。文件中的內容是標準的屬性文件,包括一系列成對的名稱與值定義的屬性。如下面是兩個多國語言文件內容:
hello_zh.properties-中文屬性信息
#Created by JInto -
#Thu Mar 15 12:43:36 CST 2007
msg=\u5582\uFF0C\u60A8\u597D\uFF0C\u6211\u662FEasyJWeb\uFF0C\u8BF7\u652F\u6301\u56FD\u4EA7\u5F00\u6E90\u9879\u76EE\uFF01
nowtime=\u73B0\u5728\u7684\u65F6\u95F4\u662F
title=\u6211\u7684\u7B2C\u4E00\u4E2AEasyjweb\u5E94\u7528
hello_en.properties-英文屬性信息
title=my first EasyJWeb application for US
nowtime=now time is
msg=hello,this is EasyJWeb openSource,Thanks for your support!
2、模板頁面中的使用多國語言標簽
??? 有了多國語言資源文件,即可在模板頁面中通過多個語言標簽來支持多國語言內容,主要有兩個步驟;
??? (1)、先使用$!i18n.init("hello")來加載資源文件,也可以在easyjweb配置文件中設置自動加載資源文件。
??? (2)、然后使用標簽$i18n.ml(“name”)來在頁面中調用加載多國語言信息。
下面是一個簡單的模板示例中的內容:
<
html
>
<
head
>
$!i18n.init("hello")
<
title
>
My?First?EasyJWeb?application
</
title
>
<
meta?
http-equiv
="Content-Type"
?content
="text/html;?charset=utf-8"
>
</
head
>
<
body
>
$!i18n.m("title")
</
body
>
</
html
>
二、自動多國語言模板支持
????? 在一些網站應用中,由于各國文化習慣及喜好的不同,各國語言的頁面版面及顯示內容也要單獨設計,此時可以為每一國語言設計一個單獨的模板文,并存放在指定的目錄下,這樣當這個國家的訪問這個頁面的時候,EasyJWeb會自動使用相應的模板來展現用戶內容。
??? 假如我們用一個名為hello.html的模板文件來顯示內容,則EasyJWeb將會自動根據下面的規則選擇特定的模板文件來顯示具體國的版面。
??? 默認模板的位置:easyjweb/ hello.html
?? 中文模板頁面:easyjweb/CN/hello.html
??英文模板頁面:easyjweb/US/hello.html
三、在Java程序中使用多國語言內容
????? EasyJWeb中通過util里面的I18n工具來處理多國語言信息,假如我們要在后臺的Action等程序中使用支持多國語言的內容,則需要直接在代碼中使用這個工具類,使用的方法如下:
??? 1、初始化I18n.init(moduleName)-這一操作將在第一次執行的時候把相應moduleName里面的屬性信息加載
??? 2、使用I18n.m(propertyName)-這一操作來得到多國語言信息。
如下面的示例:
public
?
class
?helloAction?
implements
?IWebAction?
...
{

?
public
?Page?execute(WebForm?form,?Module?module)?
throws
?Exception?
...
{
??I18n.init(
"
hello
"
);
??I18n.m(
"
msg
"
);
??String?msg
=
I18n.getInstance().m(
"
msg
"
);
??form.addResult(
"
msg
"
,msg);
//
?設置VO對象msg的值為String
??
??form.addResult(
"
time
"
,?
new
?Date());
//
?設置VO對象time的值為當前時間
??
return
?
new
?Page(
"
hello
"
,
"
/hello.html
"
,
"
template
"
);?
?}
}
四、EasyJWeb中多個語言標簽處理類I18n的代碼
?
?
package
?com.easyjf.util;
import
?java.util.Locale;
import
?java.util.Properties;

public
?
class
?I18n?
...
{
????
private
?
static
?I18n?singleton?
=
?
new
?I18n();

????
public
?
static
?I18n?getInstance()?
...
{
????????
return
?singleton;
????}
????
public
?
static
?
void
?init(String?moduleName)?
...
{????????
????????????FrameworkEngine.get(moduleName,?
null
);
????}
????
public
?
static
?
void
?init(String?language,?String?moduleName)?
...
{????????
????????????FrameworkEngine.get(moduleName,?language);????
????}
????
public
?
static
?String?m(String?propertyName)?
...
{????
????????????Properties?rb?
=
?FrameworkEngine.get(
null
,?
null
);
????????????
return
?rb.getProperty(propertyName);
????}
????
public
?
static
?String?m(String?propertyName,?String?language)?
...
{????????
????????????Properties?rb?
=
?FrameworkEngine.get(
null
,?language);
????????????
return
?rb.getProperty(propertyName);????????
????}
}
?
?小結:
????? EasyJWeb中提供了多種方式來滿足Web應用中的多國語言支持,可以根據實際應用程序的需求選擇使用適合的方式.
posted on 2007-03-16 22:10
簡易java框架 閱讀(1694)
評論(0) 編輯 收藏