forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
130 lines (115 loc) · 4.3 KB
/
common.js
File metadata and controls
130 lines (115 loc) · 4.3 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
121
122
123
124
125
126
127
128
129
130
$(document).ready(function () {
initI18N();
bindEvents();
function initI18N() {
var path = getCommonScriptPath();
var lan = utils.getLanguage();
i18next
.use(i18nextXHRBackend)
.init({
lng: lan,
whitelist: ["zh-CN", "en-US"],//语言列表,跟locales下的目录名对应
ns: "resources",//locales下的json文件名称
defaultNS: "resources",//locales下的json文件名称
fallbackLng: "zh-CN",//默认语言
backend: {
loadPath: path + './locales/{{lng}}/{{ns}}.json'
}
}, function (err, t) {
if (window.isSite) {
localize();
}
jqueryI18next.init(i18next, $);
$('html').attr("lang", lan);
$(".nav").localize();//翻译nav下所有的文档
$(".icl-footer").localize();//翻译footer
onLoadCallBack(); //设置标题栏当前语言
});
}
function onLoadCallBack() {
var lan = utils.getLanguage();
var lang_text = $("[data-lang=" + lan + "]").html() || "中文";
$('#lang').html(lang_text);
setCurrentVersion();
resetCurrentVersionLink();
}
//设置头部版本号
function setCurrentVersion() {
if (window.isLocal) {
return;
}
var version = getVersion();
var versionText = version ? "" + version : "";
$('#version').html(versionText);
}
function getVersion() {
var pathname = window.location.pathname.replace("/en/", "/");
var match = pathname.match(/^\/(dev|(?:\d+\.)+\d)\/.*/);//匹配版本:dev|9.0.0
return match && match[1] ? match[1] : null;
}
//重置当前版本链接,不带版本号
function resetCurrentVersionLink() {
if (!window.version) {
return;
}
var version = window.version;
version = version.toString();
$(".icl-nav-version").each(function (key, item) {
if (item.href) {
var reg = new RegExp("(.*)\/(" + version + ")(\/.*)");
var match = item.href.match(reg);
if (match && match[1] && match[3]) {
item.href = match[1] + match[3];
}
}
});
}
function bindEvents() {
$('.icl-header').on('click', '.lang-option', function () {
var value = $(this).data('lang');
utils.setLanguage(value);
$('#lang').html($(this).html());
i18next.changeLanguage(value);
if (window.isSite) {
localize();
return;
}
window.location.reload();
});
}
function localize() {
var lang = utils.getLanguage();
var pathname = window.location.pathname.replace("/en/", "/");
var href = window.location.origin + pathname;
if (lang === "en-US") {
if (getVersion()) {
href = window.location.origin + pathname.replace(/([^\/]*\/){1}([^\/]*)/, '$1$2/en');
//href = window.location.origin + pathname.replace(/([^\/]*\/){2}([^\/]*)/, '/$1$2/en');
} else if (window.isLocal) {
href = window.location.origin + pathname.replace(/(([^\/]*\/){3})([^\/]*)/, '$1$3/en')
} else {
href = window.location.origin + pathname.replace(/([^\/]*\/){1}([^\/]*)/, '/en/$2');
//href = window.location.origin + pathname.replace(/([^\/]*\/){1}([^\/]*)/, '/$2/en');
}
}
if ((window.location.origin + window.location.pathname) === href) {
return;
}
window.location = href;
}
function getCommonScriptPath() {
var r = new RegExp("(^|(.*?\\/))(common\.js)(\\?|$)"),
s = document.getElementsByTagName('script'), relativePath;
for (var i = 0; i < s.length; i++) {
var src = s[i].getAttribute('src');
if (src) {
var m = src.match(r);
if (m) {
relativePath = m[1] ? m[1].replace("js/", "") : "./";
break;
}
}
}
return relativePath;
}
});