Grails 国际化

来源:互联网 发布:jquery.dialog.js 编辑:程序博客网 时间:2024/06/03 20:25

1国际化资源文件

message.properties

#Customlang=zh_CNok=Okcancel=Cancelsystem.name=Jollychic WMSlogin.username=Usernamelogin.password=Passwordlogin.password.valid.text=Your password must between 6-20 characters.login.button.text=Loginlogin.miss.username.text=Please enter your username.


message_zh_CN.properties

#Customlang=zh_CNok=确定cancel=取消system.name=执御WMS管理系统login.username=用户名login.password=密码login.password.valid.text=密码必须是 6-20 个字符。login.button.text=登录login.miss.username.text=请输入用户名login.change.language.text=语种index.current.user=当前用户

2页面国际化切换

<span class="layout_north_user_language">  <g:message code="login.change.language.text"/>:  </span>    <select name="lang" style="vertical-align: top; display: inline-block;" class="combobox"  onChange="changeLanguage(this);">        <g:each in="${commons.ConstantValue.Languages}">            <option value="${it.key}" <g:if test="${params.lang ==  it.key}">selected="selected"</g:if>>${it.value}</option>        </g:each>    </select>
3session里放置国际化属性

def index() {        session.setAttribute(ConstantValue.SESSION_KEY_LANGUAGE, params.lang)        def user = UserUtils.currentUser        List<Long> ids = new ArrayList<Long>()        Application application = Application.findByAppKey(APPLICAITON_KEY);        if (user.roles.size() > 0) {            user.roles.each { role ->                role.menus.each { model ->                    if (model.applicationId.intValue() == application.id.intValue()) {                        ids.add(model.id)                    }                }            }        }        def menus = Menu.findAllByParentIdAndIdInListAndType(0, ids, ConstantValue.MENU_TYPE_MODEL, [sort: "displayOrder", order: "asc"])        for (Menu menu : menus) {            menu.internationalMenuName = message(code: menu.getInternationalCodeByName())        }        render(view: 'index', model: [menus: menus])    }
4数据库字段内容国际化,比如菜单,根据语言选择菜单中英文名称

<div  data-options="region:'west',split:true,headerCls:'panel-header-west',onCollapse:collapseWest" title="<g:message code="index.title.navigate"/>" style="width:200px;">    <div class="easyui-accordion" data-options="fit:true,border:false">        <g:each in="${menus}" >                <div title="<span class='menu-title-icon menu-title-icon${it.id}'></span> ${it.internationalMenuName}">                    <ul class="easyui-tree" data-options="url:'getMenuData?parentId=${it.id}&lang=${params.lang}',                    iconCls:'test',                    onClick:function(node){                                if(node.attributes){                                    Open(node.text,node.attributes.url);                                }                            }                    "></ul>                </div>        </g:each>    </div></div>
5Controller层国际化转化

class BaseController {    protected proPageParams(params, page) {        Integer num = Integer.parseInt(params.rows)        page = page as Integer        params.max = Math.min(num, 200)        params.offset = page == 1 ? 0 : (page - 1) * num    }    protected String convertFileName(HttpServletRequest request, String fileName) {        String userAgent = request.getHeader("USER-AGENT");        String finalFileName = "";        try {            if (userAgent.contains("MSIE")) {//IE浏览器                finalFileName = URLEncoder.encode(fileName, "UTF8");            } else if (userAgent.contains("Mozilla")) {//google,火狐浏览器                finalFileName = new String(fileName.getBytes(), "ISO8859-1");            } else {                finalFileName = URLEncoder.encode(fileName, "UTF8");//其他浏览器            }        } catch (UnsupportedEncodingException e) {            log.error(e.getMessage());        }        return finalFileName;    }    public String message(String code, Object... args) {        return message(code: code, args: args) as String    }}

继承BaseController,要转化的内容通过message方法转化

6Service类内容国际化

class BaseService {    private ApplicationTagLib taglib = new ApplicationTagLib()    public String message(String code, Object... args) {        return taglib.message(code: code, args: args) as String    }}
继承BaseService要转化的内容通过message方法转化

7GSP页面国际化

例如:

<g:message code="index.title.navigate"/>

8报表国际化

我们用的是jasper report,把国际化后的变量通过参数传给报表就可以了




原创粉丝点击