forked from 3liz/lizmap-javascript-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translation.js
226 lines (214 loc) · 14 KB
/
translation.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*This javascript script translate layers and groups names, the title of the project,
the print layout name and the aliases or fileds names according to the language of the browser and using the json files created
with the python script get_translatable_string.py
If a translated string is provided in the json file the original string is translated, otherwise the original string is shown.
The json file must be saved in the media folder.*/
var translated_string = {};
var langId = ''
var check_lang = 0
// get browser language
var userLang = navigator.language || navigator.userLanguage;
// to be changed with the desired language ID code
if (userLang === 'fr' || userLang === 'fr-FR'){
langId = 'fr'
check_lang++;
}
else if (userLang === 'en' || userLang === 'en-EN'){
langId = 'en'
check_lang++;
}
var json_url = OpenLayers.Util.urlAppend(
lizUrls.media,
OpenLayers.Util.getParameterString({
"repository": lizUrls.params.repository,
"project": lizUrls.params.project,
"path": "media/translation_"+langId+".json"
})
);
//This function is executed when the uicreated event is triggered
lizMap.events.on({
uicreated: function(e) {
if (check_lang === 1){
// get the json file with transalted strings
// the path to the json file must be provided as relative path, using the web url the browser can return a security error
fetch(json_url).then(function(response) {
if (response.ok)
return response.json();
throw new Error('Network response was not ok.');
}).then(function(translation) {
// put the json content in to the dictionary
translated_string = translation;
//console.log(translated_string);
// iterate over dictionary key
Object.keys(translated_string).forEach((inputLang) => {
//check if a translation is provided for each string
if (translated_string[inputLang] != ""){
// translate layers and groups name in the layer tree
$("div#content div#switcher.tab-pane div#switcher-layers-container.switcher div.menu-content div#switcher-layers div.without-blocks.no-group table.tree.treeTable tbody tr td span.label").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
// translate layers name in the baselayer menu
$("div#content div#switcher.tab-pane div#switcher-baselayer.baselayer div.menu-content div.baselayer-select select option").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
// translate layers name in the editing tool menu
$("div#content div#edition.tab-pane div.edition div.menu-content div select option").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
// translate layers name in the selection tool menu
$("div#content div#map-content div#mini-dock div.tabbable.tabs-below div#mini-dock-content.tab-content div#selectiontool.tab-pane div.selectiontool div.menu-content table tbody tr td select#selectiontool-layer-list option").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
// translate layouts name in the print tool menu
$("div#content div#mini-dock div#print.tab-pane div.print div.menu-content table tbody tr td select#print-template option").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
// translate layers and groups name in the attribut layer tool menu
$("div#content div#bottom-dock div#attributeLayers.tab-pane div.tab-content div#attribute-layer-list table tbody tr td").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
// translate the title of the project
$("div#header div#title h1").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
}
else {
$("div#content div#switcher.tab-pane div#switcher-layers-container.switcher div.menu-content div#switcher-layers div.without-blocks.no-group table.tree.treeTable tbody tr td span.label").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
$("div#content div#switcher.tab-pane div#switcher-baselayer.baselayer div.menu-content div.baselayer-select select option").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
$("div#content div#edition.tab-pane div.edition div.menu-content div select option").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
$("div#content div#map-content div#mini-dock div.tabbable.tabs-below div#mini-dock-content.tab-content div#selectiontool.tab-pane div.selectiontool div.menu-content table tbody tr td select#selectiontool-layer-list option").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
$("div#content div#mini-dock div#print.tab-pane div.print div.menu-content table tbody tr td select#print-template option").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
$("div#content div#bottom-dock div#attributeLayers.tab-pane div.tab-content div#attribute-layer-list table tbody tr td").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
$("div#header div#title h1").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
}
});
});
}
}
});
//This function is executed when the lizmapeditionformdisplayed event is triggered
lizMap.events.on({
lizmapeditionformdisplayed: function(e) {
if (check_lang === 1){
//console.log(translated_string);
// iterate over dictionary key
Object.keys(translated_string).forEach((inputLang) => {
//check if a translation is provided for each string
if (translated_string[inputLang] != ""){
// translate layers name in the edition form
$("div#content div#edition.tab-pane div.edition div.menu-content div#edition-form-container div h3").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
// translate aliases or fields name in the edition form
$("div#content div#edition.tab-pane div.edition div.menu-content div#edition-form-container form#jforms_view_edition.form-horizontal div.control-group label").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
}
else {
$("div#content div#edition.tab-pane div.edition div.menu-content div#edition-form-container div h3").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
$("div#content div#edition.tab-pane div.edition div.menu-content div#edition-form-container form#jforms_view_edition.form-horizontal div.control-group label").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
}
});
}
}
});
//This function is executed when the lizmappopupdisplayed event is triggered
lizMap.events.on({
lizmappopupdisplayed: function(e) {
if (check_lang === 1){
// iterate over dictionary key
Object.keys(translated_string).forEach((inputLang) => {
//check if a translation is provided for each string
if (translated_string[inputLang] != ""){
// translate layers name in the popup
$("div#content div#map-content div#map.olMap div#OpenLayers_Map_377_OpenLayers_ViewPort div#OpenLayers_Map_377_OpenLayers_Container div#liz_layer_popup.olPopup.lizmapPopup div#liz_layer_popup_GroupDiv div#liz_layer_popup_contentDiv.olPopupContent.lizmapPopupContent h4").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
// translate aliases or fields name in the popup
$("div#content div#map-content div#map.olMap div#OpenLayers_Map_377_OpenLayers_ViewPort div#OpenLayers_Map_377_OpenLayers_Container div#liz_layer_popup.olPopup.lizmapPopup div#liz_layer_popup_GroupDiv div#liz_layer_popup_contentDiv.olPopupContent.lizmapPopupContent div.lizmapPopupDiv table.lizmapPopupTable tbody tr th").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
}
else {
$("div#content div#map-content div#map.olMap div#OpenLayers_Map_377_OpenLayers_ViewPort div#OpenLayers_Map_377_OpenLayers_Container div#liz_layer_popup.olPopup.lizmapPopup div#liz_layer_popup_GroupDiv div#liz_layer_popup_contentDiv.olPopupContent.lizmapPopupContent h4").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
$("div#content div#map-content div#map.olMap div#OpenLayers_Map_377_OpenLayers_ViewPort div#OpenLayers_Map_377_OpenLayers_Container div#liz_layer_popup.olPopup.lizmapPopup div#liz_layer_popup_GroupDiv div#liz_layer_popup_contentDiv.olPopupContent.lizmapPopupContent div.lizmapPopupDiv table.lizmapPopupTable tbody tr th").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
}
});
}
}
});
//This function is executed when the minidockopened event is triggered (it works only if the location tool is opened clicking on the related button of the toolbar)
lizMap.events.on({
minidockopened: function(){
if (check_lang === 1){
// translate the layer name shown in the location tool
$("div#content div#map-content div#mini-dock div.tabbable.tabs-below div#mini-dock-content.tab-content div#locate.tab-pane.active div.locate div.menu-content div.locate-layer span.custom-combobox input[placeholder='Comuni Roia']").attr("placeholder", "Vive la revolucion");
}
}
});
//This function is executed when the lizmapswitcheritemselected event is triggered
lizMap.events.on({
lizmapswitcheritemselected: function(){
if (check_lang === 1){
//console.log(translated_string);
// iterate over dictionary key
Object.keys(translated_string).forEach((inputLang) => {
//check if a translation is provided for each string
if (translated_string[inputLang] != ""){
// translate the content of the layer information sub-dock
$("div#content div#sub-dock div.sub-metadata div.menu-content dl dd").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
}
else{
$("div#content div#sub-dock div.sub-metadata div.menu-content dl dd").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
}
});
}
}
});
// Work in progress: translate the layer tooltip that is shown when the mouse pointer pass over the layer name in the layer tree (see issue https://github.com/3liz/lizmap-web-client/issues/428)
/*lizMap.events.on({
mouseover: function(){
console.log('Ciao!')
if (check_lang === 1){
// iterate over dictionary key
Object.keys(translated_string).forEach((inputLang) => {
if (translated_string[inputLang] != ""){
$("div#content div#switcher.tab-pane div#switcher-layers-container.switcher div.menu-content div#switcher-layers div.without-blocks.no-group table.tree.treeTable tbody tr td div.tooltip div.tooltip-inner").filter(function(){
return $(this).text() === inputLang
}).html(translated_string[inputLang]);
}
else{
$("div#content div#switcher.tab-pane div#switcher-layers-container.switcher div.menu-content div#switcher-layers div.without-blocks.no-group table.tree.treeTable tbody tr td div.tooltip div.tooltip-inner").filter(function(){
return $(this).text() === inputLang
}).html(inputLang);
}
});
}
}
});*/