blogjava's web log

blogjava's web log
...

jsf学习二(国际化)

国际化(I18N)就是设计软件应用,在不改变它们程序逻辑的前提下支持各种语言和区域
本地化(L10N)就是设计软件应用支持特定地区
由于数据经过本地化,因此同一应用程序能在全世界使用
当增加一种新的语言时,应用不需要重新编译
在显示和文化相关的数据(例如日期或货币)时,格式应用遵循用户的语言和区域
文本内容(如状态消息和组件标签)不是硬编码到应用程序中,而通常是从文本文件中动态提取的

国际化的步骤

创建一个包含应用程序默认语言的键/值对的文件
 文件命名格式:
Filename_languagecode_CountryCode.properties
例如:
ApplicationResources_zh_CN.properties



JSF 提供 <f:loadBundle> 标签以加载资源包
此标签具有两个属性:basename 和 var
basename 指定要加载的 properties 文件的名称
var属性用来为该properties文件起一个别名





< f:view >
    
< f:loadBundle  basename ="message"  vars ="msg"   />
….
    
< h:outputLabel  value ="#{msg.first}"   />
</ f:view >


资源文件编译

native2ascii –encoding gb2312 messagesCN.properties  messages_zh_CN.properties




一个登陆实现国际化

后台检查登陆是否正确
  public String Login(string userName,string passWord) {
      
if ( UserName=="blog"{
          
return "success";
      }
 else {
     FacesContext context 
= FacesContext.getCurrentInstance();
     ResourceBundle bundle 
= ResourceBundle.getBundle("messages", context.getViewRoot().getLocale());
      String msg 
= "";
     
if ( username=="" || passWord=="")
         msg 
= bundle.getString("username_isnull");
  
    context.addMessage (
null, new FacesMessage(msg));
    
return "shibai"

 }

  }


前台
<f:loadBundle basename="message" var="msg"/>
<html>
 
<f:view>
     
<h:form>
      
<h:outputText value="#{msg.login}"/> <br/>
      
<h:messages style="color: blue"/><br/>
      
<h:inputText id="username" value="#{UserBean.userName}"
                           required
="true">
           
<f:validateLongRange minimum="0" maximum="20"/>
      
</h:inputText><br/><br/>


    
<h:outputText value="#{msg.password}"/> <br/>
      
<h:messages style="color: blue"/><br/>
      
<h:inputText id="pssword" value="#{UserBean.passWord}"
                           required
="true">
           
<f:validateLongRange minimum="0" maximum="16"/>
      
</h:inputText><br/><br/>


     
<h:commandButton value="#{msg.button_title}" 
                                      action
="#{userBean.Login}"/>
   
</h:form>
 
</f:view>
</html>



posted on 2006-08-20 10:43 record java and net 阅读(2353) 评论(1)  编辑  收藏 所属分类: jsf学习

评论

# re: jsf学习二(国际化) 2007-08-24 14:37 牛人

JSF不能实现完全国际化!  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论