-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathLang.js
More file actions
121 lines (113 loc) · 3.14 KB
/
Lang.js
File metadata and controls
121 lines (113 loc) · 3.14 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import { en } from './locales/en-US';
import { zh } from './locales/zh-CN';
/**
* @name Lang
* @namespace
* @category BaseTypes Internationalization
* @description 国际化的命名空间,包含多种语言和方法库来设置和获取当前的语言。
* @usage
* ```
* // 浏览器
* <script type="text/javascript" src="{cdn}"></script>
* <script>
* const result = {namespace}.Lang.getCode();
*
* // 弃用的写法
* const result = SuperMap.Lang.getCode();
*
* </script>
*
* // ES6 Import
* import { Lang } from '{npm}';
*
* const result = Lang.getCode();
*
* ```
*/
let Lang = {
'en-US': en,
"zh-CN": zh,
/**
* @member {string} Lang.code
* @description 当前所使用的语言类型。
*/
code: null,
/**
* @member {string} [Lang.defaultCode='en-US']
* @description 默认使用的语言类型。
*/
defaultCode: "en-US",
/**
* @function Lang.getCode
* @description 获取当前的语言代码。
* @returns {string} 当前的语言代码。
*/
getCode: function () {
if (!Lang.code) {
Lang.setCode();
}
return Lang.code;
},
/**
* @function Lang.setCode
* @description 设置语言代码。
* @param {string} code - 此参数遵循IETF规范。
*/
setCode: function () {
var lang = this.getLanguageFromCookie();
if (lang) {
Lang.code = lang;
return;
}
lang = Lang.defaultCode;
if (navigator.appName === 'Netscape') {
lang = navigator.language;
} else {
lang = navigator.browserLanguage;
}
if (lang.indexOf('zh') === 0) {
lang = 'zh-CN';
}
if (lang.indexOf('en') === 0) {
lang = 'en-US';
}
Lang.code = lang;
},
/**
* @function Lang.getLanguageFromCookie
* @description 从 cookie 中获取语言类型。
*/
getLanguageFromCookie() {
var name = 'language=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1)
}
if (c.indexOf(name) !== -1) {
return c.substring(name.length, c.length)
}
}
return "";
},
/**
* @function Lang.i18n
* @description 从当前语言字符串的字典查找 key。
* @param {string} key - 字典中 i18n 字符串值的关键字。
* @returns {string} 国际化的字符串。
*/
i18n: function (key) {
var dictionary = Lang[Lang.getCode()];
var message = dictionary && dictionary[key];
if (!message) {
// Message not found, fall back to message key
message = key;
}
return message;
}
};
export { Lang };