-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.js
140 lines (122 loc) · 3.69 KB
/
core.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
const path = require('path');
const jsonFormatParser = require('./json-format-parser');
const propertiesFormatParser = require('./properties-format-parser');
const LANG_ISO_PLACEHOLDER = '%LANG_ISO%';
let _context;
let _lokalise;
module.exports = async (context, { LokaliseApi, fs }) => {
_context = context;
_lokalise = new LokaliseApi({ apiKey: context.apiKey });
_fs = fs;
const remoteKeys = await getRemoteKeys();
console.log(`${remoteKeys.length} remote keys.`);
const localKeys = await getLocalKeys();
const keysToCreate = getKeysToCreate(localKeys, remoteKeys);
const createRequest = buildLokaliseCreateKeysRequest(keysToCreate);
if (createRequest.length > 0) {
console.log(`Pushing ${createRequest.length} new keys to Lokalise`);
await _lokalise.keys.create(createRequest, { project_id: _context.projectId });
console.log('Push done!');
}
}
function buildLokaliseCreateKeysRequest (toCreate) {
console.log('Keys to push:');
const uploadKeys = [];
for (const key in toCreate) {
console.log(' ' + key);
const lokaliseKey = {
key_name: key,
platforms: [_context.platform],
translations: [],
filenames: {
[_context.platform]: _context.filename
}
};
for (const lang in toCreate[key]) {
console.log(` ${lang}: ${toCreate[key][lang]}`);
lokaliseKey.translations.push({
language_iso: lang,
translation: toCreate[key][lang]
});
}
uploadKeys.push(lokaliseKey);
}
return uploadKeys;
}
function getKeysToCreate (localKeys, remoteKeys) {
const toCreate = {};
for (const lang in localKeys) {
localKeys[lang].forEach(({ key, value }) => {
const keyExists = remoteKeys.some(x => x.key_name[_context.platform] === key);
if (!keyExists) {
if (!(key in toCreate)) {
toCreate[key] = {};
}
toCreate[key][lang] = value;
}
})
}
return toCreate;
}
async function getLocalKeys () {
const languageCodes = await getLanguageISOCodes();
console.log('Project language codes', languageCodes);
const languageKeys = {};
const readFilePromises = languageCodes.map(async (lang) => {
try {
const data = await readLanguageFile(lang);
let pairs;
switch (_context.format) {
case 'json':
pairs = jsonFormatParser(data);
break;
case 'properties':
pairs = propertiesFormatParser(data);
break;
default:
throw new Error('No parser found for format');
}
console.log(`Found ${pairs.length} keys in languge file for '${lang}'`);
languageKeys[lang] = pairs;
} catch (error) {
console.error(`Error reading language file ${lang}: ${error.message}`)
}
})
await Promise.all(readFilePromises);
return languageKeys;
}
async function getRemoteKeys () {
const {
projectId,
platform,
} = _context;
return await _lokalise
.keys
.list({
project_id: projectId,
filter_platforms: platform,
limit: 5000 // TODO: Implement pagination if more than 5000 keys
});
}
function buildLanguageFilePath (languageCode) {
return path.join(_context.directory, _context.filename.replace(LANG_ISO_PLACEHOLDER, languageCode))
}
async function getLanguageISOCodes () {
const languages = await _lokalise.languages.list({
project_id: _context.projectId
});
return languages.map(x => x.lang_iso);
}
function readLanguageFile (lang) {
const path = buildLanguageFilePath(lang);
return new Promise((resolve, reject) => {
_fs.readFile(path, 'utf-8', (err, data) => {
if (err) {
reject(err);
return;
}
console.log('Read language file ' + path);
resolve(data);
});
})
}