-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
37 lines (35 loc) · 1.69 KB
/
index.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
const fs = require('fs');
const path = require('path');
const { withStringsXml, AndroidConfig } = require('@expo/config-plugins');
const xml2js = require('xml2js');
const builder = new xml2js.Builder({ headless: true });
function withAndroidLocalizedName(config) {
return withStringsXml(config,
async config => {
const projectRoot = config.modRequest.projectRoot;
const resPath = await AndroidConfig.Paths.getResourceFolderAsync(projectRoot);
for (const locale of Object.keys(config.locales ?? {})) {
const json = await fs.promises.readFile(config.locales[locale]);
const strings = JSON.parse(json);
const resources = [];
for (const key of Object.keys(strings)) {
// Skip values that are not marked for translation or simply do not exist
// because gradle does not like them
const untranslated = config.modResults.resources.string?.find(item =>
item.$.name === key && item.$.translatable !== false);
if (untranslated)
resources.push({ string: { $: { name: key }, _: strings[key] } });
}
if (resources.length) {
await fs.promises.mkdir(path.resolve(resPath, `values-${locale}`), { recursive: true });
await fs.promises.writeFile(
path.resolve(resPath, `values-${locale}`, 'strings.xml'),
builder.buildObject({ resources })
);
}
}
return config;
},
);
};
module.exports = withAndroidLocalizedName;