UNPKG

1.77 kBJavaScriptView Raw
1var FS = require("fs");
2var Template = require("./template");
3
4function splitIfNeeded(txt) {
5 // Transformer tous les "\n" en sauts de lignes.
6 var parts = txt.split("\\");
7 if (parts.length > 0) {
8 parts.forEach(
9 function(itm, idx) {
10 if (idx > 0) {
11 var c = itm.substr(0, 1);
12 if (c == 'n') {
13 txt += "\n";
14 } else {
15 txt += "\\" + c;
16 }
17 txt += itm.substr(1);
18 } else {
19 txt = itm;
20 }
21 }
22 );
23 }
24 return txt;
25}
26
27exports.parse = function(file) {
28 var content = FS.readFileSync(file).toString();
29 var dic = {};
30 var currentLang = null;
31 content.split("\n").forEach(
32 function(line, idx) {
33 line = line.trim();
34 if (line == '') return;
35 var c = line.charAt(0);
36 if (c == '#' || c == '/') return;
37 var pos, lang, key, text;
38 if (c == '[') {
39 pos = line.indexOf(']');
40 lang = line.substr(1, pos - 1).toLowerCase();
41 currentLang = dic[lang];
42 if (!currentLang) {
43 dic[lang] = {};
44 currentLang = dic[lang];
45 }
46 return;
47 }
48 if (c > '32' && c != ':') {
49 pos = line.indexOf(':');
50 key = line.substr(0, pos).trim();
51 text = line.substr(pos + 1).trim();
52 currentLang[key] = splitIfNeeded(text);
53 }
54 }
55 );
56
57 var params = {dico: JSON.stringify(dic)};
58 return Template.file("intl.js", params).out;
59};