forked from jsmitka/Nette-Translation-Panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslationPanel.panel.phtml
503 lines (423 loc) · 13.1 KB
/
TranslationPanel.panel.phtml
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
/*
* Copyright (c) 2010 Jan Smitka <[email protected]>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
/**
* Template for TranslationPanel
*
* @author Jan Smitka <[email protected]>
*/
/* @var $this TranslationPanel */
/* @var $translator IEditableTranslator */
?>
<style type="text/css">
#TranslationPanel {
min-width: 600px;
}
#TranslationPanel .nette-inner {
max-width: 800px !important;
height: <?php echo $this->height . 'px'; ?>;
padding: 1px;
}
#TranslationPanel-sidebar {
width: <?php if ($this->layout == self::LAYOUT_VERTICAL) echo '40%'; else echo '30%'; ?>;
height: 100%;
overflow: hidden;
float: left;
margin: -1px 0 -1px -1px;
}
#TranslationPanel-strings {
height: <?php echo ($this->height - 30) . 'px'; ?>;
overflow: auto;
border: 1px solid #AAA;
}
#TranslationPanel-filter {
padding: 3px 3px 3px 21px;
border: 1px solid #AAA;
margin-bottom: 2px;
background: #FFF url("<?php echo $this->getIconSrc('magnifier.png'); ?>") no-repeat 3px 50%;
}
#TranslationPanel-filterBox {
width: 80%;
}
#TranslationPanel-filterReset {
float: right;
width: 16px;
height: 16px;
margin: 1px 0;
background: transparent url("<?php echo $this->getIconSrc('cancel.png'); ?>") no-repeat 50% 50%;
}
#TranslationPanel div.TranslationPanel-string {
border-bottom: 1px solid #DDD;
padding: 0.3ex 1ex;
background-color: #E6EFC2;
}
#TranslationPanel div.TranslationPanel-string.untranslated {
background-color: #FFF6BF;
}
#TranslationPanel div.TranslationPanel-string a {
color: #264409;
text-decoration: underline;
cursor: pointer;
}
#TranslationPanel div.TranslationPanel-string a:hover {
background: none;
}
#TranslationPanel div.TranslationPanel-string.untranslated a {
color: #514721;
}
#TranslationPanel div.TranslationPanel-string.modified {
background-image: url("<?php echo $this->getIconSrc('bullet_star.png'); ?>");
background-position: 99% 50%;
background-repeat: no-repeat;
}
#TranslationPanel-editor {
width: <?php if ($this->layout == self::LAYOUT_VERTICAL) echo '58%'; else echo '68%'; ?>;
height: 100%;
margin: -1px -1px -1px 0;
float: right;
position: relative;
}
<?php if ($this->layout == self::LAYOUT_HORIZONTAL) { ?>
#TranslationPanel div.TranslationPanel-stringVariant {
width: 50%;
float: left;
}
<?php } ?>
#TranslationPanel div.TranslationPanel-stringVariant sup {
font-size: 60%;
}
#TranslationPanel label {
display: block;
font-weight: bold;
}
#TranslationPanel textarea {
width: 95%;
height: 70%;
background-color: #fff;
border: 1px solid #bbb;
margin: 0.5em 0;
border: 1px solid #888;
}
#TranslationPanel-tools {
position: absolute;
bottom: -1px;
right: 0px;
width: 98%;
background-color: #EDEAE0;
padding: 1ex;
text-align: right;
border-radius: 0.7ex;
-moz-border-radius: 0.7ex;
-webkit-border-radius: 0.7ex;
}
#TranslationPanel-tools input.save {
padding: 3px 3px 3px 22px;
background: #DFDDD8 url("<?php echo $this->getIconSrc('tick.png'); ?>") scroll no-repeat 3px 50%;
border: 1px solid #CCC;
border-radius: 0.7ex;
-moz-border-radius: 0.7ex;
-webkit-border-radius: 0.7ex;
}
#TranslationPanel-tools input.save:hover, #TranslationPanel-tools input.save:focus {
border-color: #AAA;
}
#TranslationPanel-tools input.save.disabled {
background-color: transparent;
background-image: url("<?php echo $this->getIconSrc('tick_gray.png'); ?>");
border: 1px solid #DDD;
}
#TranslationPanel-status {
float: left;
display: block;
padding: 4px 0;
}
#TranslationPanel-notification {
display: block;
position: absolute;
bottom: 0px;
font-size: 75%;
border: 1px solid #600;
background: #A00;
color: #FFF;
font-weight: bold;
border-radius: 3px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
line-height: 100%;
}
</style>
<script type="text/javascript">
/* <![CDATA[ */
var JSON = JSON || {};
JSON.stringify = JSON.stringify || function (obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
// simple data type
if (t == "string") obj = '"'+obj+'"';
return String(obj);
} else {
var n, v, json = [], arr = (obj && obj.constructor == Array);
for (n in obj) {
v = obj[n]; t = typeof(v);
if (t == "string") v = '"'+v+'"';
else if (t == "object" && v !== null) v = JSON.stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
};
/* ]]> */
</script>
<script type="text/javascript">
/* <![CDATA[ */
var translationPanel = {
strings: <?php echo json_encode($strings); ?>,
variants: <?php echo json_encode($translator->getVariantsCount()); ?>,
updatedStrings: [],
currentString: false,
filter: '',
listElements: [],
icon: null,
iconNormal: '',
iconNotification: <?php echo json_encode($this->getIconSrc('flag_red.png')); ?>,
init: function() {
this.icon = document.getElementById('TranslationPanel-icon');
this.iconNormal = document.getElementById('TranslationPanel-icon').src;
this.drawList();
for (var i = 0; i < this.variants; i++) {
var textarea = document.getElementById('TranslationPanel-stringVariant_' + i);
textarea.onchange = function () {
translationPanel.save();
};
}
var button = document.getElementById('TranslationPanel-save');
button.disabled = true;
button.className += ' disabled';
button.onclick = function () {
button.disabled = true;
button.className += ' disabled';
translationPanel.commit();
};
var filterBox = document.getElementById('TranslationPanel-filterBox');
var filterReset = document.getElementById('TranslationPanel-filterReset');
filterReset.style.visibility = 'hidden';
var onChange = function () {
translationPanel.filter = filterBox.value;
filterReset.style.visibility = (translationPanel.filter ? 'visible' : 'hidden');
translationPanel.drawList();
};
filterBox.onkeyup = filterBox.onchange = onChange;
filterReset.onclick = function () {
translationPanel.filter = filterBox.value = '';
filterReset.style.visibility = 'hidden';
translationPanel.drawList();
};
translationPanel.setStatus();
},
drawList: function () {
var list = document.getElementById('TranslationPanel-strings');
list.innerHTML = '';
this.listElements = [];
var strings = [{}, {}];
var untranslated = 0;
for (var i in this.strings) {
var translated = Boolean(this.strings[i]);
if (translated && (typeof(this.strings[i]) == 'object' && this.strings[i] instanceof Array) && this.strings[i].length > 1) {
for (var plural = 0; plural < this.variants; plural++) {
if (!this.strings[i][plural]) {
translated = false;
break;
}
}
}
if (translated)
strings[1][i] = this.strings[i];
else {
strings[0][i] = this.strings[i];
untranslated++;
}
}
var notification = document.getElementById('TranslationPanel-notification');
if (notification)
notification.parentNode.removeChild(notification);
if (untranslated) {
this.icon.src = this.iconNotification;
notification = document.createElement('span');
notification.id = 'TranslationPanel-notification';
notification.textContent = untranslated;
this.icon.parentNode.style.position = 'relative';
this.icon.parentNode.appendChild(notification);
} else
this.icon.src = this.iconNormal;
for (var translated in strings) {
for (var i in strings[translated]) {
if (!this.matchesFilter(i, this.strings[i]))
continue;
var div = document.createElement('div');
div.className = 'TranslationPanel-string';
var label;
var stringData = this.strings[i];
if (translated == 0) {
div.className += ' untranslated';
}
if (!stringData) {
label = i;
} else {
if (typeof(stringData) != 'object' || !(stringData instanceof Array))
stringData = [stringData];
label = stringData[0];
}
var a = document.createElement('a');
a.title = i;
a.innerHTML = (label.length <= 30 ? this.escape(label) : this.escape(label).substr(0, 30) + '…');
a.onclick = function () {
translationPanel.edit(this);
};
div.appendChild(a);
list.appendChild(div);
this.listElements[i] = div;
}
}
},
matchesFilter: function (string, stringData) {
if (!this.filter)
return true;
var filter = new RegExp(this.filter, 'i');
if (string.match(filter))
return true;
if (typeof(stringData) != 'object' || !(stringData instanceof Array))
stringData = [stringData];
for (var i in stringData) {
if (typeof(stringData[i]) == 'string' && stringData[i].match(filter))
return true;
}
return false;
},
edit: function (anchor) {
var string = anchor.title;
this.fillIn(this.strings[string]);
this.currentString = string;
document.getElementById('TranslationPanel-stringVariant_0').focus();
this.setStatus(string.length <= 50 ? this.escape(string) : this.escape(string).substr(0, 50) + '…');
},
fillIn: function (stringData) {
if (typeof(stringData) != 'object' || !(stringData instanceof Array))
stringData = [stringData];
for (var i = 0; i < this.variants; i++) {
var textarea = document.getElementById('TranslationPanel-stringVariant_' + i);
if (stringData[i])
textarea.value = stringData[i];
else
textarea.value = '';
}
},
save: function () {
if (this.currentString) {
var stringData = [];
for (var i = 0; i < this.variants; i++) {
var textarea = document.getElementById('TranslationPanel-stringVariant_' + i);
if (textarea.value)
stringData[i] = textarea.value;
}
if (stringData.length == 1)
stringData = stringData.shift();
else if (stringData.length == 0)
stringData = false;
this.strings[this.currentString] = stringData;
this.updatedStrings.push(this.currentString);
if (this.listElements[this.currentString].className.search('modified') == -1)
this.listElements[this.currentString].className += ' modified';
document.getElementById('TranslationPanel-save').disabled = false;
document.getElementById('TranslationPanel-save').className = 'save';
}
},
commit: function () {
var data = {};
for (var i in this.updatedStrings) {
data[this.updatedStrings[i]] = this.strings[this.updatedStrings[i]];
}
var xmlHttp = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
translationPanel.updatedStrings = [];
translationPanel.currentString = false;
translationPanel.setStatus('Sucessfuly saved.');
translationPanel.drawList();
} else {
translationPanel.setStatus('Unexpected error.');
}
}
}
xmlHttp.open("POST", <?php echo json_encode(Environment::getApplication()->getPresenter()->link('this')); ?>, true);
xmlHttp.setRequestHeader('X-Translation-Client', 'devel');
xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xmlHttp.send(JSON.stringify(data));
},
setStatus: function (text) {
if (text)
document.getElementById('TranslationPanel-status').innerHTML = text;
else
document.getElementById('TranslationPanel-status').innerHTML = '';
},
escape: function (string) {
return string
.replace('&', '&')
.replace('"', '"')
.replace('<', '<')
.replace('>', '>');
}
};
/* ]]> */
</script>
<div id="TranslationPanel">
<h1>Translations</h1>
<div class="nette-inner">
<div id="TranslationPanel-sidebar">
<div id="TranslationPanel-filter">
<input type="text" class="text" id="TranslationPanel-filterBox" />
<div id="TranslationPanel-filterReset"></div>
</div>
<div id="TranslationPanel-strings">
</div>
</div>
<div id="TranslationPanel-editor">
<?php
for ($i = 0; $i < $translator->getVariantsCount(); ++$i) {
?>
<div class="TranslationPanel-stringVariant">
<label for="TranslationPanel-stringVariant_<?php echo $i; ?>"><?php echo $i > 0 ? $i . '<sup>' . $this->ordinalSuffix($i) . '</sup> plural': 'Singular'; ?>:</label>
<textarea id="TranslationPanel-stringVariant_<?php echo $i; ?>" rows="4" cols="40"></textarea>
</div>
<?php
}
?>
<div id="TranslationPanel-tools">
<span id="TranslationPanel-status">Initializing…</span>
<input type="button" value="Save" class="save" id="TranslationPanel-save" />
</div>
</div>
</div>
</div>