1 引用本地化语言包
在 js/i18n 文件夹中,提供了大量预定义的语言包。它包括为所有字符串定义的,包括消息,标题,分页信息,搜索/添加/删除 的对话框 文本等。
在jQuery库文件后,在jqGrid 脚本文件前 引用语言包。
1
<
head
id
="Head1"
runat
="server"
>
2
<!--
The jQuery UI theme that will be used by the grid
-->
>
3
<
link
rel
="stylesheet"
type
="text/css"
media
="screen"
href
="/themes/redmond/jquery-ui-1.7.1.custom.css"
/>
4
<!--
The jQuery UI theme extension jqGrid needs
-->
>
5
<
link
rel
="stylesheet"
type
="text/css"
media
="screen"
href
="/themes/ui.jqgrid.css"
/>
6
<!--
jQuery runtime minified
-->
>
7
<
script
src
="/js/jquery-1.3.2.min.js"
type
="text/javascript"
></
script
>
8
<!--
The localization file we need, English in this case
-->
>
9
<
script
src
="/js/i18n/grid.locale-en.js"
type
="text/javascript"
></
script
>
10
<!--
The jqGrid client-side javascript
-->
>
11
<
script
src
="/js/jquery.jqGrid.min.js"
type
="text/javascript"
></
script
>
12
</
head
>
2 修改创建语言包
1
$.jgrid = {
2
defaults : {
3
recordtext: "View {0} - {1} of {2}",
4
emptyrecords: "No records to view",
5
loadtext: "Loading...",
6
pgtext : "Page {0} of {1}"
7
},
8
search : {
9
caption: "Search...",
10
Find: "Find",
11
Reset: "Reset",
12
odata : ['equal', 'not equal', 'less', 'less or equal','greater','greater or equal', 'begins with',
13
'does not begin with','is in','is not in','ends with',
14
'does not end with','contains','does not contain'],
15
groupOps: [ { op: "AND", text: "all" }, { op: "OR", text: "any" } ],
16
matchText: " match",
17
rulesText: " rules"
18
},
19
...
1
formatter : {
2
integer : {thousandsSeparator: " ", defaultValue: '0'},
3
number : {decimalSeparator:".", thousandsSeparator: " ", decimalPlaces: 2, defaultValue: '0.00'},
4
currency : {decimalSeparator:".", thousandsSeparator: " ", decimalPlaces: 2, prefix: "", suffix:"", defaultValue: '0.00'},
5
date : {
6
dayNames: [
7
"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat",
8
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
9
],
10
monthNames: [
11
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
12
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
13
],
14
...
通过修改 这些文本,可以达到自定义的目的。
3 以代码的方式,重写语言默认的文本
可以在 Model 和 Controller 中以代码的方式,改变 edit dialog 的文本。
改变之前,默认为:
1
edit : {
2
editCaption: "Edit Record",
3
bSubmit: "Submit",
4
bCancel: "Cancel"
5
}
在 Controller 中改变
JQGridModel.EditDialogSettings.Caption = "My New Caption";
JQGridModel.EditDialogSettings.SubmitText = "My Submit Text";
JQGridModel.EditDialogSettings.CancelText = "My Cancel Text";
4 以代码的方式,改变语言包
在这种情况下,与其直接将文件添加懂啊header,不如使用 viewData 替代。
1
<
head
id
="Head1"
runat
="server"
>
2
...
3
<
script
type
="text/javascript"
src
="http://www.trirand.net/aspnetmvc/Scripts/jquery-1.3.2.min.js"
></
script
>
4
<
script
type
="text/javascript"
src
='<%=
ViewData["localeScript"] %
>
'
></script>
5
<script type="text/javascript" src="http://www.trirand.net/aspnetmvc/Scripts/jqgrid/jquery.jqGrid.min.js"></script>
6
...
7
</head>
这会默认使用 英语。我们可以在服务端代码中修改。如Controller:
1
//
locale can be passed as a GET parameter from a dropdownlist for example
2
public
ActionResult FunctionalityLocalization(
string
locale)
3
{
4
string
jsLocale
=
locale
??
"
grid.locale-en.js
"
;
5
6
ViewData[
"
localesList
"
]
=
GetLocales(jsLocale);
7
ViewData[
"
localeScript
"
]
=
"
http://www.trirand.net/aspnetmvc/Scripts/jqgrid/i18n/
"
+
jsLocale;
8
9
//
Pass the custmomized grid model to the View
10
return
View(GetGridModel());
11
}
12
13
public
SelectList GetLocales(
string
selectedValue)
14
{
15
List
<
SelectListItem
>
countries
=
new
List
<
SelectListItem
>
();
16
countries.Add(
new
SelectListItem { Text
=
"
English
"
, Value
=
"
grid.locale-en.js
"
});
17
countries.Add(
new
SelectListItem { Text
=
"
German
"
, Value
=
"
grid.locale-de.js
"
});
18
countries.Add(
new
SelectListItem { Text
=
"
French
"
, Value
=
"
grid.locale-fr.js
"
});
19
return
new
SelectList(countries,
"
Value
"
,
"
Text
"
, selectedValue);
20
}