Dynamic JQGrid Internationalization

痴心易碎 提交于 2019-12-23 03:27:11

问题


I am using JQGrid in Ruby on Rails that pulls data from a controller and that controller return data in json format. Below is my JQuery code

<script type="text/javascript">
var list = null;
$(document).ready(function(){
    grid = $("#list");
    grid.jqGrid({
        datatype: "json",
        url:'/applications?'+new Date().getTime(),
        height: <%= JQGRID_HEIGHT %>,
        rowNum:<%= ROW_NUM %>, 
        rowList:<%= ROW_LIST %>,
        viewrecords: true,
        autowidth:true,
        gridComplete: function() {
            initLytebox();
            common.jqGridComplete($(this));
            initLytebox();

        },
        afterInsertRow: function(rowId) {           
            var action = grid.jqGrid('getRowData',rowId).action;
            var cellValue = '<a href="/applications/'+rowId+'/edit"><img alt="Edit" src="/assets/edit.png" title="Edit"></a>&nbsp;'
            cellValue += '<a href="javascript:void(0);" onclick="deleteApplication('+rowId+')" class = "delete_egress_group" id = "'+rowId+'">';            
            cellValue += '<img alt="Delete" src="/assets/delete.png" title="Delete"></a>';
            grid.jqGrid('setRowData',rowId,{action:cellValue});
        },
        colNames:['Application Name','Description','Action'],       
        colModel:[  
                    {name:'name',index:'name', width:"20%"},
                    {name:'description',index:'description', width:"70%"},
                    {name:'action',index:'action', width:"10%",search:false,sortable:false}
                 ],              
        multiselect: false,
        pager: '#pager',
        toolbar: [true,"top"],
        caption: "Applications" }
    );  
    grid.jqGrid('navGrid', '#pager', {search: false, add: false, edit: false, del: false },{}, {}, {},
     { multipleSearch: true, overlay: false});
    grid.jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true, defaultSearch: 'cn' });
    grid.jqGrid('navButtonAdd', '#pager',
        {
            caption: "Add",title: "Add New",buttonicon: 'ui-icon-plus',
            onClickButton: function () {window.location = "/applications/new";}             
        }
    );
}); 
</script>
<select id="languages">
    <option value="en">English (US)</option>
    <option value="en-GB">English (GB)</option>
    <option value="en-IN">English (IN)</option>
    <option value="de">German</option>
    <option value="ru">Russian</option>
</select>
<div style="width:100%">
    <table id="list"></table>
    <div id="pager"></div>
</div>

I have downloaded multi-lingual js files from below locations and placed under JQGrid folder of my application

http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-3.8.2/src/i18n/grid.locale-en-fixed.js
http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-3.8.2/src/i18n/grid.locale-ru-fixed.js
http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-3.8.2/src/i18n/grid.locale-de-fixed.js

Now i simply want to convert the column header and Pagination strings of my JQGrid as per the selected language.


回答1:


You use locale files from very old version of jqGrid (3.8.2) described in the answer. I verified and the same idea can be still used in jqGrid.

Here is modified version of the old demo which works. If you compare the original localization files (for example grid.locale-en.js) with the modified version (for example grid.locale-en-dyn.js) you will see that the modification which I done very easy. The original file grid.locale-en.js which looks like

(function($){
....
$.jgrid = $.jgrid || {};
$.extend($.jgrid,{
    defaults : {
    ....
});
})(jQuery);

I modified to

(function($){
....
$.jgrid = $.jgrid || {};
if (!$.jgrid.hasOwnProperty("regional")) {
    $.jgrid.regional = {};
}
$.jgrid.regional.en = $.jgrid.regional["en-US"] = {
    defaults : {
    ....
};
$.extend($.jgrid,$.jgrid.regional.en);
})(jQuery);

Such modification allows to save the language specific information not only in the common part like $.jgrid.defaults.recordtext, but additionally in the language specific parts like $.jgrid.regional.en.defaults.recordtext and $.jgrid.regional["en-US"].defaults.recordtext. It allows to include multiple language specific settings which can be copied to the standard $.jgrid.defaults part used by jqGrid.



来源:https://stackoverflow.com/questions/14582891/dynamic-jqgrid-internationalization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!