From 0ee1caa7bb60874f34e26484bcd54b40734e5d7e Mon Sep 17 00:00:00 2001 From: rolux Date: Fri, 10 May 2013 09:38:18 +0200 Subject: [PATCH] add tools/locale (string extraction) --- tools/locale/strings.js | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tools/locale/strings.js diff --git a/tools/locale/strings.js b/tools/locale/strings.js new file mode 100644 index 00000000..9f145e39 --- /dev/null +++ b/tools/locale/strings.js @@ -0,0 +1,67 @@ +var path = Ox.PATH.replace('/build/', '/dev/'), + files = [ + path + 'Ox/json/Ox.json', + path + 'Ox.UI/json/Ox.UI.json' + ], + strings = { + 'inside Ox._()': {}, + 'outside Ox._()': {} + }, + lines = ['\n']; + +Ox.getJSON(files, function(data) { + files = []; + Ox.forEach(data, function(data, file) { + files = files.concat(Ox.flatten(data.files).filter(function(file) { + return Ox.endsWith(file, '.js'); + })); + }); + Ox.getAsync(files.map(function(file) { + return Ox.PATH + file; + }), Ox.get, function(data) { + Ox.forEach(data, function(data, file) { + var inside = false, + level = 0, + tokens = Ox.tokenize(data); + tokens.forEach(function(token, i) { + var key, string; + if (i >= 3 + && tokens[i - 3].value + tokens[i - 2].value + + tokens[i - 1].value + tokens[i].value == 'Ox._(' + ) { + inside = true; + level = 0; + } else if (inside) { + if (token.value == '(') { + level++; + } else if (token.value == ')') { + level--; + inside = level > 0; + } + } + if (token.type == 'string' && token.value.length > 2) { + key = inside ? 'inside Ox._()' : 'outside Ox._()'; + string = token.value.slice(1, -1) + .replace(/\\'/g, '\'') + .replace(/([^\\]")/, '\\"') + if (!Ox.isArray(strings[key][string])) { + strings[key][string] = []; + } + strings[key][string].push( + file.replace(path, '') + ':' + token.line + ); + } + }); + }); + Ox.forEach(strings, function(values, key) { + lines.push(key); + Ox.sort(Object.keys(values)).forEach(function(string) { + lines.push(Ox.repeat(' ', 4) + '"' + string + '"'); + Ox.sort(strings[key][string]).forEach(function(file) { + lines.push(Ox.repeat(' ', 8) + file); + }); + }); + }); + Ox.print(lines.join('\n')); + }); +});