Skip to content

Commit

Permalink
feat(i18n): add an overwrite flag for i18n-export
Browse files Browse the repository at this point in the history
  • Loading branch information
zyf722 committed Jun 23, 2024
1 parent 9d15b76 commit eb9ba81
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions scripts/i18n-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const babel = require('@babel/core');
const parser = require('@babel/parser');

const outDir = path.resolve('src/livecodes/i18n/locales/tmp');
const enOutDir = path.resolve('src/livecodes/i18n/locales/en');
const srcBaseDir = path.resolve('src/livecodes');

const prettierConfig = {
Expand Down Expand Up @@ -39,7 +40,7 @@ const sortedJSONify = (obj, space = 2) =>
space,
);

const writeTranslation = async (namespace) => {
const writeTranslation = async (namespace, overwriteMode) => {
const name = namespace === 'translation' ? 'translation' : 'languageInfo';
const type = namespace === 'translation' ? 'I18nTranslation' : 'I18nLangInfoTranslation';
const code = `import { type I18nTranslationTemplate } from '../models';
Expand Down Expand Up @@ -72,6 +73,14 @@ const writeTranslation = async (namespace) => {
]);

console.log(`Generated namespace ${namespace} in ${outDir}.`);

if (overwriteMode) {
await fs.promises.copyFile(
path.resolve(outDir, namespace + '.ts'),
path.resolve(enOutDir, namespace + '.ts'),
);
console.log(`Copied to ${enOutDir}.`);
}
};

const addTranslation = (nsKey, value, desc, props) => {
Expand Down Expand Up @@ -278,12 +287,14 @@ const processTS = async (files) => {
};

/**
* Generate .ts and .lokalise.json files from HTML files.
* Generate .ts and .lokalise.json files from .html and .ts files.
*
* If no files are provided, it will process all relevant files in the base directory.
*/
const generateTranslation = async () => {
const files = process.argv.slice(2);
const files = process.argv.slice(2).filter((file) => !file.startsWith('-'));
const overwriteMode = process.argv.includes('--overwrite');

const HTMLFiles = [],
TSFiles = [];

Expand All @@ -307,9 +318,9 @@ const generateTranslation = async () => {
await processHTML(HTMLFiles);
await processTS(TSFiles);

writeTranslation('translation');
writeTranslation('translation', overwriteMode);
if (Object.keys(trans['language-info']).length > 0) {
writeTranslation('language-info');
writeTranslation('language-info', overwriteMode);
}
};

Expand Down

0 comments on commit eb9ba81

Please sign in to comment.