This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
/
translations-android.js
60 lines (49 loc) · 1.64 KB
/
translations-android.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
const fs = require('fs').promises;
const path = require('path');
const contentDirectory = 'src/locale/translations';
const outputFilename = 'strings.xml';
// add strings here i.e. ['AppName', 'NoExposureDetected.RegionCovered.Title']
const stringPaths = ['AppName'];
const resolveObjectPath = (stringPath, obj) => {
return stringPath.split('.').reduce((prev, curr) => {
return prev ? prev[curr] : '';
}, obj);
};
const camelCaseToUnderScore = str => {
return str
.split(/(?=[A-Z])/g)
.join('_')
.split('.')
.join('')
.toLowerCase();
};
const getLocaleContent = async lang => {
const filePath = path.join(__dirname, contentDirectory, `${lang}.json`);
const data = await fs.readFile(filePath, 'utf8');
return JSON.parse(data);
};
const writeFile = options => {
const {content, filePath} = options;
let xml = '<resources>\n';
stringPaths.forEach(strPath => {
const str = resolveObjectPath(`Home.${strPath}`, content);
const name = camelCaseToUnderScore(strPath);
xml += `\t<string name="${name}">${str}</string>\n`;
});
xml += '</resources>';
fs.writeFile(filePath, xml, err => {
if (err) return console.log('error writing file', err); // eslint-disable-line no-console
});
};
const writeXml = async (lang = '') => {
const basePath = 'android/app/src/main/res/values';
const outputDirectory = lang === '' ? `${basePath}/` : `${basePath}-${lang}/`;
const content = await getLocaleContent(lang || 'en');
const filePath = path.join(__dirname, outputDirectory, outputFilename);
writeFile({content, filePath});
};
(async () => {
// defaults to en
await writeXml();
await writeXml('fr');
})();