forked from EddyVerbruggen/nativescript-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
before-prepare.js
87 lines (79 loc) · 2.86 KB
/
before-prepare.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
var fs = require('fs-extra');
var path = require('path');
var parser = require('xmldom').DOMParser;
module.exports = function(logger, platformsData, projectData, hookArgs) {
var platform = hookArgs.platform.toLowerCase();
var appResourcesDirectoryPath = projectData.appResourcesDirectoryPath;
var i18nFolderPath = path.join(projectData.projectDir, 'app', 'i18n');
var pjson = require(path.join(projectData.projectDir, 'package.json'));
var defaultlang = 'en';
if (pjson["nativescript-i18n"]) {
if (pjson['nativescript-i18n'].customLangPath) {
var customLangPath = path.join(projectData.projectDir, pjson['nativescript-i18n'].customLangPath);
if (fs.existsSync(customLangPath)) {
i18nFolderPath = customLangPath;
}
}
if (pjson["nativescript-i18n"].defaultLang) {
defaultLang = pjson["nativescript-i18n"].defaultLang;
}
}
return new Promise(function(resolve, reject) {
if (fs.existsSync(i18nFolderPath)) {
if (platform === 'android') {
fs.readdirSync(i18nFolderPath).forEach(function(lang) {
var outputFile;
if (lang === defaultlang) {
outputFile = path.join(appResourcesDirectoryPath, 'Android', 'values', 'strings.xml');
} else {
outputFile = path.join(appResourcesDirectoryPath, 'Android', 'values-' + lang, 'strings.xml');
}
fs.copy(path.join(i18nFolderPath, lang, 'strings.xml'), outputFile, {
clobber: true
},
function(err) {
if (err) {
console.error(err);
reject();
}
});
});
} else if (platform == 'ios') {
fs.readdirSync(i18nFolderPath).forEach(function(lang) {
var outputFile = path.join(appResourcesDirectoryPath, 'iOS', lang + '.lproj', 'Localizable.strings');
var fileData = fs.readFileSync(path.join(i18nFolderPath, lang, 'strings.xml'), 'utf8');
var doc = new parser().parseFromString(fileData);
var defs = doc.getElementsByTagName('string');
var result = '';
for (var i = 0; i < defs.length; i++) {
var def = defs[i];
result += '"' + def.getAttribute('name') + '"="' + unescape(def.textContent) + '";\n';
if (def.getAttribute('name') == 'app_name') {
var outputPlistFile = path.join(appResourcesDirectoryPath, 'iOS', lang + '.lproj', 'InfoPlist.strings');
var infoPlistData='"CFBundleDisplayName"="'+unescape(def.textContent)+'";\n';
fs.ensureFile(outputPlistFile, function(err) {
if (!err) {
fs.writeFile(outputPlistFile, infoPlistData);
} else {
console.error(err);
reject();
}
});
}
}
fs.ensureFile(outputFile, function(err) {
if (!err) {
fs.writeFile(outputFile, result);
} else {
console.error(err);
reject();
}
});
});
} else if (platform == 'windows') {
//TODO PRs gladly accepted :P
}
}
resolve();
});
};