-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
308 lines (289 loc) · 8.85 KB
/
javascript.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
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
var saved_json = null;
var en_json = null;
var your_json = null;
var string_counter = 0;
var needs_translation = 0;
function update_footer(){
function loaded(){
if (your_json) {
return "both files";
}
if (en_json) {
return "en.json only";
}
return "Nothing";
}
document.getElementById("status").innerHTML = "Loaded: "+loaded();
document.getElementById("strings").innerHTML = "Strings: "+string_counter;
document.getElementById("to_translate").innerHTML = "Not translated: "+needs_translation;
document.getElementById("percent").innerHTML = "Completed: "+(Math.ceil((string_counter-needs_translation)/string_counter*10000)/100 || 0)+"%";
}
function download(){
if ( navigator.msSaveBlob ) {
var blob = new Blob( [ JSON.stringify(saved_json, null, "\t") ], { type: "text/json"} );
navigator.msSaveBlob( blob, "your_translation.json" );
} else {
var dataStr = "data:text/json;charset=utf-8,"+ encodeURIComponent(
JSON.stringify(saved_json, null, "\t")
);
var dlAnchorElem = document.getElementById('actual_download');
dlAnchorElem.setAttribute("href", dataStr);
dlAnchorElem.click();
}
}
function merge(first_json, second_json){
var result = {};
for(var key in first_json) {
result[key] = first_json[key];
}
for(var key in second_json) {
if(result[key]){
if(typeof second_json[key] === 'string'){
result[key] = second_json[key];
}
else{
result[key] = merge(first_json[key], second_json[key]);
}
}
}
return result;
}
function update_json_file(this_input){
function update_json_key(old_json, path){
let current_level = path.shift();
if(typeof old_json[current_level] === 'string'){
old_json[current_level] = this_input.value;
}else{
update_json_key(old_json[current_level], path);
}
}
let path = this_input.previousSibling.id;
update_json_key(saved_json, path.split("."));
}
var last_active_input = null;
function changed_input_text(this_input){
last_active_input = this_input;
update_json_file(this_input);
if ( this_input.value == this_input.previousSibling.textContent
&& this_input.value != "" && this_input.value != " " ){
if( !this_input.classList.contains("needs_translation") ){
needs_translation++;
this_input.classList.add("needs_translation");
let ul_element= this_input.parentNode;
while (ul_element.tagName != "MAIN") {
if (ul_element.tagName == "UL"){
ul_element.classList.add("needs_translation");
}
ul_element = ul_element.parentNode;
}
}
}else{
if( this_input.classList.contains("needs_translation") ){
needs_translation--;
this_input.classList.remove("needs_translation");
let ul_element= this_input.parentNode;
while (ul_element.tagName != "MAIN") {
if (ul_element.tagName == "UL"){
let child_needs_translation = false
for(i = 0; i < ul_element.children.length; i++) {
let li = ul_element.children[i];
if( li.children[2] ){
if( li.children[2].classList.contains("needs_translation") ){
child_needs_translation = true;
break;
}
}else{
if( li.children[1].classList.contains("needs_translation") ){
child_needs_translation = true;
break;
}
}
}
if( !child_needs_translation ){
ul_element.classList.remove("needs_translation");
}
}
ul_element = ul_element.parentNode;
}
}
}
update_footer();
}
function changed_all_input_text(){
let uls = document.querySelectorAll('ul');
for(i = 0; i < uls.length; i++) {
uls[i].classList.remove("needs_translation");
}
let textInputs = document.querySelectorAll('textarea');
needs_translation = 0;
for(i = 0; i < textInputs.length; i++) {
if ( textInputs[i].value == textInputs[i].previousSibling.textContent
&& textInputs[i].value != "" && textInputs[i].value != " " ){
needs_translation++;
// ident
textInputs[i].classList.add("needs_translation");
let ul_element= textInputs[i].parentNode;
while (ul_element.tagName != "MAIN") {
if (ul_element.tagName == "UL"){
ul_element.classList.add("needs_translation");
}
ul_element = ul_element.parentNode;
}
}else{
textInputs[i].classList.remove("needs_translation");
}
}
}
function collapse(arrow){
arrow.nextSibling.classList.toggle("collapsed");
arrow.classList.toggle("change_arrow");
}
function reset_input_file(input_file_element){
input_file_element.value = "";
}
function isVisible(e) {
return !!( e.offsetWidth || e.offsetHeight || e.getClientRects().length );
}
function collapse_all_translated(){
var uls = document.querySelectorAll('ul');
for(i = 0; i < uls.length; i++) {
if ( isVisible(uls[i]) && !uls[i].classList.contains('needs_translation') ){
uls[i].classList.add("collapsed");
uls[i].previousSibling.classList.toggle("change_arrow");
}
}
}
function goto_next_unstranslated(){
let all_text_inputs = document.querySelectorAll("textarea");
if (!last_active_input){
last_active_input = all_text_inputs[all_text_inputs.length-1];
}
let visible_text_inputs = [];
for(var i = 0; i < all_text_inputs.length; i++){
if ( ( isVisible(all_text_inputs[i]) && all_text_inputs[i].classList.contains('needs_translation') ) || all_text_inputs[i] == last_active_input){
visible_text_inputs.push(all_text_inputs[i]);
}
}
for(var i = 0; i < visible_text_inputs.length; i++){
if(visible_text_inputs[i] == last_active_input){
let next = visible_text_inputs[i + 1];
if (next){
next.focus();
break;
}else{
visible_text_inputs[0].focus();
}
}
}
}
function google_translation(){
//querySelectorAll is not an array, and changes in real time, so lets make an array
let all_text_inputs = document.querySelectorAll("textarea");
let textInputs = [];
for(let i = 0; i < all_text_inputs.length; i++){
if(all_text_inputs[i].classList.contains('needs_translation')){
textInputs.push(all_text_inputs[i]);
}
}
for (let i = 0; i < textInputs.length; i++) {
textInputs[i].value = textInputs[i].nextSibling.textContent;
update_json_file(textInputs[i]);
}
changed_all_input_text();
update_footer();
}
function load_en_file() {
let en_input, en_file, fileReader;
en_input = document.getElementById('en_fileinput_element');
if (!en_input.files[0]) {
alert("Please select a file before clicking 'Load'");
}
else {
en_file = en_input.files[0];
fileReader = new FileReader();
fileReader.onload = receivedText;
fileReader.readAsText(en_file);
}
string_counter = 0;
function populateHTML(json_table, json_key_in_input_id) {
var txt = "<ul>";
for (key in json_table) {
var current_key = json_key_in_input_id+"."+key;
if(typeof json_table[key] === 'string'){
string_counter++;
txt+="<li class='key_value'>";
txt += "<span class='key'>"+key+": </span>";
txt += "<span class='value' id='"+current_key.replace('.','')+"'>"+
json_table[key].replace(/</g,'<')+"</span>";
txt += "<textarea onblur='changed_input_text(this)'>";
txt += json_table[key].replace(/'/g,"'")+"</textarea>";
//div for the google translated text
txt += "<div class='translate'>";
txt += json_table[key].replace(/</g,'<')+"</div>";
txt+="</li>";
}
else{
txt += "<li><span class='collapse_button' onclick='collapse(this)'>"+key+"</span>";
txt += populateHTML(json_table[key], current_key);
txt += "</li>";
}
}
return txt + "</ul>";
}
function receivedText(e) {
let lines = e.target.result;
try{
en_json = JSON.parse(lines);
}catch (error){
alert("The json file contain an error. \n" + error );
}
saved_json = en_json;
let new_html = populateHTML(en_json, "");
document.getElementsByTagName("main")[0].innerHTML = new_html;
changed_all_input_text();
update_footer();
reset_input_file(en_input);
}
}
function load_your_file() {
let your_input, your_file, fileReader;
your_input = document.getElementById('your_fileinput_element');
if (!your_input.files[0]) {
alert("Please select a file before clicking 'Load'");
}else if(saved_json == null){
alert("Please select the original file first");
}
else {
your_file = your_input.files[0];
fileReader = new FileReader();
fileReader.onload = receivedText;
fileReader.readAsText(your_file);
}
function populateHTML_inputs(json_table, json_key_in_input_id) {
for (key in json_table) {
var current_key = json_key_in_input_id+"."+key;
if(typeof json_table[key] === 'string'){
document.getElementById(current_key.replace('.',''))
.nextSibling.value = json_table[key];
}
else{
populateHTML_inputs(json_table[key], current_key);
}
}
}
function receivedText(e) {
let lines = e.target.result;
try{
your_json = JSON.parse(lines);
}catch (error){
alert("The json file contain an error. \n" + error );
}
saved_json = merge(saved_json, your_json)
needs_translation = 0;
populateHTML_inputs(saved_json, "");
document.getElementById("status").innerHTML = "Loaded: both files";
changed_all_input_text();
update_footer();
reset_input_file(your_input);
}
}