forked from addyosmani/devtools-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_i18n_content.js
127 lines (127 loc) · 5.55 KB
/
html_i18n_content.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
// html_i18n_content.js
// https://github.com/anaran/devtools-snippets
// Generate downloadable chrome.i18n messages file for location.href.
// See http://developer.chrome.com/extensions/i18n.html
// Messages are based on innerText or value attribute, keyed by class attribute.
// $Placeholders$ are added to messages.json as well.
// Generate downloadable HTML file with corresponding i18n-content attributes.
// Generate downloadable script to initialize localized messages on load.
try {
console.log(location.href);
var hsh = {};
var replacer = [];
replacer.push('message', 'description', 'placeholders', 'content', 'example');
var addKeyValuePlaceHolders = function(messages, key, value) {
messages[key] = {
'message': value,
'description': value
};
var placeHolders = value.match(/\$([^$]+)\$/g);
if (placeHolders) {
messages[key]['placeholders'] = {};
for (j = 0, jlen = placeHolders.length; j < jlen; j++) {
var placeHolderName = placeHolders[j].replace(/\$/g, '').toLowerCase();
messages[key]['placeholders'][placeHolderName] = {
'content': '$' + (j + 1),
'example': value
};
if (!replacer.some(function(value) {
return value === placeHolderName;
})) {
replacer.push(placeHolderName);
}
}
}
return messages;
};
var i18nForValueAttribute = function(select, messages) {
var nds = document.querySelectorAll(select);
for (i = 0, len = nds.length; i < len; i++) {
var value = nds[i].getAttribute('value');
if (value) {
var key = nds[i].className;
if (key && value) {
nds[i].setAttribute('i18n-content', key);
// Better keep value for round-tripping.
// nds[i].setAttribute('value', '');
messages = addKeyValuePlaceHolders(messages, key, value);
}
}
}
};
var i18nForInnerText = function(select, messages) {
var nds = document.querySelectorAll(select);
for (i = 0, len = nds.length; i < len; i++) {
var value = nds[i].innerText;
if (nds[i].childElementCount === 0 && value) {
var key = nds[i].className;
var value = value.replace(/\s+/g, ' ').trim();
if (key && value) {
nds[i].setAttribute('i18n-content', key);
// Better keep value for round-tripping.
// nds[i].innerText = '';
messages = addKeyValuePlaceHolders(messages, key, value);
}
}
}
};
i18nForInnerText('*', hsh);
i18nForValueAttribute('input[value]', hsh);
Object.getOwnPropertyNames(hsh).sort().forEach(function(value) {
replacer.push(value);
});
var messagesString = JSON.stringify(hsh, replacer, 4);
var htmlFileText = '<!DOCTYPE ' + document.doctype.name + '>\n' + document.documentElement.outerHTML;
var htmlFileName = location.pathname.split('/').pop();
var makeDownloadLink = function(data, filename, style) {
var blob = new window.Blob([data], {
'type': 'text/utf-8'
});
var a = document.createElement('a');
a.innerText = 'Download ' + filename;
a.href = URL.createObjectURL(blob);
a.download = filename;
document.body.appendChild(a);
a.setAttribute('style', style);
a.onclick = function() {
setTimeout(function() {
document.body.removeChild(a);
}, 500);
};
}
var applyChromeI18nMessages = function() {
// This generated function body is wrapped in (...)(); to execute on load.
// This will only install the onreadystatechange event handler
// to be run when the document load is complete.
// Load this file into the associated HTML file by including
// <script src="applyChromeI18nMessages.js"></script>
// in its head element.
try {
document.addEventListener('readystatechange', function(event) {
if (event.target.readyState !== 'complete') {
return;
}
(function() {
var nds = document.querySelectorAll('[i18n-content]');
for (i = 0, len = nds.length; i < len; i++) {
var value = nds[i].getAttribute('value');
var key = nds[i].getAttribute('i18n-content');
if (value === null) {
nds[i].innerText = chrome.i18n.getMessage(key);
} else {
nds[i].setAttribute('value', chrome.i18n.getMessage(key));
}
}
})();
}, false);
} catch (exception) {
window.alert('exception.stack: ' + exception.stack);
console.log((new Date()).toJSON(), 'exception.stack:', exception.stack);
}
};
makeDownloadLink(messagesString, 'messages.json', 'position:fixed;top:2em;left:50%;opacity:0.5');
makeDownloadLink(htmlFileText, htmlFileName, 'position:fixed;top:4em;left:50%;opacity:0.5');
makeDownloadLink('(' + applyChromeI18nMessages + ')();', 'applyChromeI18nMessages.js', 'position:fixed;top:6em;left:50%;opacity:0.5');
} catch (exception) {
console.log(exception.stack);
}