forked from espocrm/espocrm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.js
177 lines (134 loc) · 5.45 KB
/
lang.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
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
// node lang.js espocrm-nl_NL.po lastRelease nl_NL
if (process.argv.length < 2) {
throw new Error('No dir argument passed');
}
var path = require('path');
var fs = require('fs');
var nodePath = require('path');
var os = require('os');
var isWin = /^win/.test(os.platform());
var espoPath = path.dirname(fs.realpathSync(__filename)) + '';
var resLang = process.argv[2] || 'lang_LANG';
var poPath = process.argv[3] || espoPath + '/build/' + 'espocrm-' + resLang +'.po';
var deleteFolderRecursive = function (path) {
var files = [];
if( fs.existsSync(path) ) {
files = fs.readdirSync(path);
files.forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
function Lang (poPath, espoPath) {
this.poPath = poPath;
this.espoPath = espoPath;
if (this.espoPath.substr(-1) != '/') {
this.espoPath += '/';
}
this.currentPath = path.dirname(fs.realpathSync(__filename)) + '/';
this.moduleList = ['Crm'];
this.baseLanguage = 'en_US';
var dirNames = this.dirNames = {};
var resDirNames = this.resDirNames = {};
var coreDir = this.espoPath + 'application/Espo/Resources/i18n/' + this.baseLanguage + '/';
var dirs = [coreDir];
dirNames[coreDir] = 'application/Espo/Resources/i18n/' + resLang + '/';
this.moduleList.forEach(function (moduleName) {
var dir = this.espoPath + 'application/Espo/Modules/' + moduleName + '/Resources/i18n/' + this.baseLanguage + '/';
dirs.push(dir);
dirNames[dir] = 'application/Espo/Modules/' + moduleName + '/Resources/i18n/' + resLang + '/';
}, this);
var installDir = this.espoPath + 'install/core/i18n/' + this.baseLanguage + '/';
dirs.push(installDir);
dirNames[installDir] = 'install/core/i18n/' + resLang + '/';
this.dirs = dirs;
};
Lang.prototype.escape = function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
Lang.prototype.run = function () {
var translationHash = {};
var dirs = this.dirs;
var contents = fs.readFileSync(this.poPath, 'utf8');
var matches = contents.match(new RegExp('msgid (\"(.*)\".*\n)+.*msgstr (\"(.*)\"(\n)?)+', 'g'));
matches.forEach(function (part) {
//remove line break "\n"
part = part.replace(/"\n"/g, '');
var res = part.match(new RegExp('msgid \"(.*)\".*\n.*msgstr \"(.*)\"'));
translationHash[res[1]] = res[2];
}, this);
dirs.forEach(function (path) {
var resDirPath = this.dirNames[path];
var resPath = this.currentPath + 'build/' + resLang + '/' + resDirPath;
if (!fs.existsSync(resPath)) {
var d = '';
resPath.split('/').forEach(function (f) {
if (!f) {
return;
}
if (isWin) {
d = nodePath.join(d, f);
} else {
d += '/' + f;
}
if (!fs.existsSync(d)) {
fs.mkdirSync(d);
}
});
}
var list = fs.readdirSync(path);
list.forEach(function (fileName) {
var filePath = path + fileName;
var resFilePath = resPath + '/' + fileName;
var contents = fs.readFileSync(filePath, 'utf8');
for (var key in translationHash) {
if (!translationHash[key].trim()) {
continue;
}
var escapedKey = this.escape(key);
contents = contents.replace(new RegExp(': *\"(' + escapedKey + ')\"', 'g'), ': "' + translationHash[key] + '"');
}
for (var key in translationHash) {
if (key.substr(0, 2) == "\\\"") {
if (!translationHash[key].trim()) {
continue;
}
var escapedKey = this.escape(key);
contents = contents.replace(new RegExp('(' + escapedKey.replace(/\\"/g, '"') + ')', 'g'), '' + translationHash[key].replace(/\\"/g, '"') + '');
}
}
if (fs.existsSync(resFilePath)) {
fs.unlinkSync(resFilePath);
}
fs.writeFileSync(resFilePath , contents);
}, this);
}, this);
};
var lang = new Lang(poPath, espoPath);
lang.run();