Skip to content

Commit

Permalink
improve android build to export locales list to android xml array
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBeastLT committed Dec 22, 2024
1 parent 3a8e303 commit a40b378
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions scripts/build-android.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
} = require('./sanitize');

const rootPath = 'android/src/androidMain/res'
const files = fs.readdirSync(process.cwd())
const files = fs.readdirSync(process.cwd()).filter(file => /[a-z]{2}-[A-Z]{2}\.json$/g.test(file));

fs.mkdirSync(path.join(process.cwd(), rootPath), { recursive: true });

Expand All @@ -36,25 +36,40 @@ const escapeXmlString = str => {
return str;
}

files
.filter(file => /[a-z]{2}-[A-Z]{2}\.json$/g.test(file))
.forEach(file => {
const langTag = file.replace(/(\w\w-)(\w\w)\.json/,'$1r$2');
const translations = JSON.parse(fs.readFileSync(path.join(process.cwd(), file)));
let langFolder = 'values';
if (langTag !== defaultLang) langFolder += `-${langTag}`;
let xml = '<?xml version="1.0" encoding="utf-8"?>\n';
xml += '<resources>\n';
const dedup = deduplicate(translations);
Object.keys(dedup).forEach(key => {
xml += ` <string name="${key}">${escapeXmlString(dedup[key])}</string>\n`;
});
xml += '</resources>';
fs.mkdirSync(path.join(process.cwd(), rootPath, langFolder), { recursive: true });
fs.writeFileSync(path.join(process.cwd(), rootPath, langFolder, 'strings.xml'), xml);
if (duplicatedLanguages[langTag]) {
langFolder = `values-${duplicatedLanguages[langTag]}`
function writeLocales() {
let resource = '<?xml version="1.0" encoding="utf-8"?>\n';
resource +='<resources>\n'
resource += ' <string-array name="interface_locales" translatable="false">\n'
files.forEach(file => resource += ` <item>${file.split('.').shift()}</item>\n`);
resource += ' </string-array>\n';
resource += '</resources>';
fs.mkdirSync(path.join(process.cwd(), rootPath, 'values'));
fs.writeFileSync(path.join(process.cwd(), rootPath, 'values', 'array.xml'), resource);
}

function writeTranslations() {
files
.forEach(file => {
const langTag = file.replace(/(\w\w-)(\w\w)\.json/, '$1r$2');
const translations = JSON.parse(fs.readFileSync(path.join(process.cwd(), file)));
let langFolder = 'values';
if (langTag !== defaultLang) langFolder += `-${langTag}`;
let xml = '<?xml version="1.0" encoding="utf-8"?>\n';
xml += '<resources>\n';
const dedup = deduplicate(translations);
Object.keys(dedup).forEach(key => {
xml += ` <string name="${key}">${escapeXmlString(dedup[key])}</string>\n`;
});
xml += '</resources>';
fs.mkdirSync(path.join(process.cwd(), rootPath, langFolder), { recursive: true });
fs.writeFileSync(path.join(process.cwd(), rootPath, langFolder, 'strings.xml'), xml);
}
});
if (duplicatedLanguages[langTag]) {
langFolder = `values-${duplicatedLanguages[langTag]}`
fs.mkdirSync(path.join(process.cwd(), rootPath, langFolder), { recursive: true });
fs.writeFileSync(path.join(process.cwd(), rootPath, langFolder, 'strings.xml'), xml);
}
})
}

writeLocales()
writeTranslations()

0 comments on commit a40b378

Please sign in to comment.