Skip to content

Commit

Permalink
feat(ci): update .lokalise.json after pulling from Lokalise
Browse files Browse the repository at this point in the history
  • Loading branch information
zyf722 committed Dec 11, 2024
1 parent ed6208b commit ba305c6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/i18n-update-pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ jobs:
mkdir -p $LOKALISE_TEMP && touch $LOKALISE_TEMP/locales.zip && npm run i18n-update-pull -- $PR_BRANCH && rm -rf $LOKALISE_TEMP
env:
LOKALISE_TEMP: lokalise_tmp

- name: Generate Lokalise JSON files
run: npm run i18n-lokalise-json all

- name: Linting and fixing
run: npm run fix
Expand Down
50 changes: 37 additions & 13 deletions scripts/i18n-lokalise-json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,42 @@ const parseObject = (node) => {
result[key] = parseValue(prop.value);
});
return result;
}
};

const parseValue = (node) => {
switch (node.type) {
case "ObjectExpression":
case 'ObjectExpression':
return parseObject(node);
case "ArrayExpression":
case 'ArrayExpression':
return node.elements.map(parseValue);
case "StringLiteral":
case "NumericLiteral":
case "BooleanLiteral":
case 'StringLiteral':
case 'NumericLiteral':
case 'BooleanLiteral':
return node.value;
case "NullLiteral":
case 'NullLiteral':
return null;
default:
throw new Error(`Unsupported node type: ${node.type}`);
}
}
};

const generateLokaliseJSON = async () => {
const lang = process.argv[2];
/** @param {string} lang */
const generateLokaliseJSON = async (lang) => {
const srcDir = path.resolve('src/livecodes/i18n/locales/' + lang);
if (!fs.existsSync(srcDir)) {
console.error(`Language ${srcDir} does not exist.`);
return;
} else if (lang === 'en') {
console.warn('This script is not intended to be run for English language.\nPlease use `npm run i18n-export` instead.');
return
}

const files = fs
.readdirSync(srcDir)
.filter((file) => file.endsWith('.ts'))
.map((file) => path.resolve(srcDir, file));

await Promise.all(
return Promise.all(
files.map(async (file) => {
try {
console.log(`Generating Lokalise JSON for ${file} in language ${lang}...`);
Expand All @@ -83,12 +86,33 @@ const generateLokaliseJSON = async () => {
}

const outFile = path.resolve(srcDir, file.replace('.ts', '.lokalise.json'));
await fs.promises.writeFile(outFile, sortedJSONify(result).replace(/<(\/?)(\d+)>/g, '<$1tag-$2>'));
await fs.promises.writeFile(
outFile,
sortedJSONify(result).replace(/<(\/?)(\d+)>/g, '<$1tag-$2>'),
);
} catch (err) {
console.error(err);
}
}),
);
};

generateLokaliseJSON();
const main = async () => {
const langs = new Set(process.argv.slice(2));

if (langs.has('all')) {
langs.delete('all');
const srcDir = path.resolve('src/livecodes/i18n/locales');
const files = fs
.readdirSync(srcDir)
.filter(
(file) =>
fs.statSync(path.resolve(srcDir, file)).isDirectory() && file !== 'en' && file !== 'tmp',
);
files.forEach((file) => langs.add(file));
}

await Promise.all([...langs].map(generateLokaliseJSON));
};

main();

0 comments on commit ba305c6

Please sign in to comment.