{hero}

i18n()

起始版本:DataTables 1.10.7

国际化符号查找。

描述

此方法专用于将数据创建于 DataTables 基础上的插件和扩展的开发者,其中软件将语言字符串呈现给最终用户。此方法支持将 language 配置对象用作语言字符串的单一配置点,随后可由此对象查找值。对于开发者尚未提供其自身字符串的情况,应提供默认值。

i18n() 方法还对奇数、复数、双数等形式提供基本支持,在考虑国际化时必须考虑到这些因素。这是通过提供包含一个对象所用的形式键及其默认值来实现的。

例如,考虑下列对象

1
2
3
4
5
{
    _: "%d rows selected",
    0: "Click a row to select",
    1: "1 row selected"
}

在将 0 作为数字值(第三个参数)传入时,将使用字符串 “Click a row to select”。针对 1,将使用字符串 “1 row selected”。对于所有其他值,都将使用默认 _ 参数值,其中数字值替换 %d。对于使用双数形式的语言,请添加一个 2 参数,依此类推。

应注意,国际化(简称 i18n)/本地化(简称 l10n很困难。此方法为 DataTables 中的基本国际化及其组件提供了良好的支持,但并非完全支持。完全支持目前超出了 DataTables 库的范围,并且是一个完整的项目本身!

类型

函数 i18n(token, def [, numeric ] )

描述

查找在 DataTables language 初始化对象中定义的语言符号。

参数
返回

得到国际化的字符串

示例

没有使用定义字符串(即使用默认值)的简单字符串查找

1
2
3
4
var table = new DataTable('#myTable');
 
// Will show "Copy to clipboard"
alert(table.i18n('buttons.copy', 'Copy to clipboard'));

同上,但使用开发人员定义的值

1
2
3
4
5
6
7
8
9
10
var table = $('#myTable').DataTable({
    language: {
        buttons: {
            copy: 'Click to copy'
        }
    }
});
 
// Will show "Click to copy"
alert(table.i18n('buttons.copy', 'Copy to clipboard'));

没有开发人员定义选项的复数形式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var table = new DataTable('#myTable');
 
// Will show "0 rows selected"
alert(
    table.i18n(
        'select.rows',
        {
            _: '%d rows selected',
            1: '1 row selected'
        },
        0
    )
);
 
// Will show "1 row selected"
alert(
    table.i18n(
        'select.rows',
        {
            _: '%d rows selected',
            1: '1 row selected'
        },
        1
    )
);
 
// Will show "4 rows selected"
alert(
    table.i18n(
        'select.rows',
        {
            _: '%d rows selected',
            1: '1 row selected'
        },
        4
    )
);

具有开发人员定义选项的复数形式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var table = $('#myTable').DataTable({
    language: {
        select: {
            rows: {
                _: '%d rows selected',
                0: 'Click a row to select',
                1: 'Just one row selected'
            }
        }
    }
});
 
// Will show "Click a row to select"
alert(
    table.i18n(
        'select.rows',
        {
            _: '%d rows selected',
            1: '1 row selected'
        },
        0
    )
);
 
// Will show "Just one row selected"
alert(
    table.i18n(
        'select.rows',
        {
            _: '%d rows selected',
            1: '1 row selected'
        },
        1
    )
);
 
// Will show "4 rows selected"
alert(
    table.i18n(
        'select.rows',
        {
            _: '%d rows selected',
            1: '1 row selected'
        },
        4
    )
);

相关

以下选项直接相关,在您的应用程序开发中也可能会有用。

Comments (0)

No comments posted for this page yet. Be the first to contribute!

Post new comment

Contributions in the form of tips, code snippets and suggestions for the above material are very welcome. To post a comment, please use the form below. Text is formatted by Markdown.

To post comments, please sign in to your DataTables account, or register:

Any questions posted here will be deleted without being published.
Please post questions in the Forums. Comments are moderated.