---
title: "Griffon 国际化"
author: "morry"
date: "2010-11-09T23:14:57Z"
tags: ["MVC", "Web"]
categories: ["非技术"]
slug: "griffon"
draft: false
lastmod: "2010-11-09T23:24:50Z"
id: 807116
url: "https://morry.iteye.com/blog/807116"
---

**更多内容请到我的博客:[http://www.mikameng.com](http://mikameng.com/)**

 

上一篇博客是 [**griffon桌面开发之hello world**](/blog/804768) , 界面上的显示是英文的， 现在咱们来做一下汉化。

Griffon是基于MVC架构， 在griffon-app\\i18n下是我们放多语言资源文件的地方， 就像web开发一样， 都有多语言资源文件。

**1. 安装多语言插件 i18n**

 

   默认是不支持多语言的，只有安装了此插件才启用。在项目根目录输入 griffon install-plugin i18n 即可安装多语言支持。 请参照 [http://griffon.codehaus.org/I18n+Plugin](http://griffon.codehaus.org/I18n+Plugin)

**2. 添加中文资源文件**

    在griffon-app\\i18n 新建文件messages\_zh\_CN.properties，此文件里存放和项目有关的中文资源。注意资源文件里的中文要转为Unicode编码。

转码可以用java自带的native2ascii命令:  **native2ascii  源文件 目标文件** ； 先将需要转的内容放到源文件里，执行命令后目标文件里就是转换后的编码。 将此内容复制到messages\_zh\_CN.properties文件里。

 

转码处理前：

 

app.title=Hello Word入门  
userName=用户名  
email=邮箱  
phone=电话  
close=关闭  
save=保存

 

Unicode编码 处理后：

app.title=Hello Word\\u5165\\u95e8  
userName=\\u7528\\u6237\\u540d  
email=\\u90ae\\u7bb1  
phone=\\u7535\\u8bdd  
close=\\u5173\\u95ed  
save=\\u4fdd\\u5b58

 

将处理后的内容放到中文资源messages\_zh\_CN.properties里。

 

**3.  更改界面View， 根据需要自动加载资源文件**

 

      i18n plugin会自动创建messageSource实例， 我们通过messageSource实例去获得多语言内容，从而将内容显示在界面上。 使用方式如下： ${messageSource.getMessage('key')}

 

更改后界面如下：

package helloword

 import griffon.util.GriffonNameUtils as GNU   
//application(title: 'HelloWord',  
application(title: **"${messageSource.getMessage('app.title')}"** ,,  
  pack: true,  
  locationByPlatform:true,  
  iconImage: imageIcon('/griffon-icon-48x48.png').image,  
  iconImages: [imageIcon('/griffon-icon-48x48.png').image,  
               imageIcon('/griffon-icon-32x32.png').image,  
               imageIcon('/griffon-icon-16x16.png').image]) {  
    panel(border:emptyBorder(10)) {

            borderLayout()  
             
            panel(constraints: CENTER){  
                    migLayout()  
                                      
            label (**"${messageSource.getMessage('userName')}"** , constraints: 'left')  
            textField(columns: 20, constraints: 'growx, wrap', text: bind('userName', target:model, mutual: true))

            label (**"${messageSource.getMessage('email')}"** , constraints: 'left')  
            textField(columns: 20, constraints: 'growx, wrap', text: bind('email', target:model, mutual: true))

                label (**"${messageSource.getMessage('phone')}"** , constraints: 'left')  
            textField(columns: 20, constraints: 'growx, wrap', text: bind('phone', target:model, mutual: true))  
        }  
             
            panel(constraints: SOUTH){  
                    migLayout()                     
         
                    button( **"${messageSource.getMessage('save')}"** , actionPerformed: controller.save,  constraints: 'left')  
                    button( **"${messageSource.getMessage('close')}"** , actionPerformed: controller.close, constraints: 'growx,wrap')  
            }

}  
             
}

 

4\. 运行程序，看下效果。 命令： griffon run-app

 

![](http://dl.iteye.com/upload/attachment/345589/e286fc3e-028b-39e6-bbac-efa3d24b407c.gif)

 

**更多内容请到我的博客:[http://www.mikameng.com](http://mikameng.com/)**

**版权声明: 原创作品，允许转载，转载时请务必以超链接形式标明文章出处 、作者信息和本声明。否则将追究法律责任。**