Skip to content

Commit

Permalink
[i18n] post-processing to remove unnecessary content from the build
Browse files Browse the repository at this point in the history
  • Loading branch information
userXinos committed Jan 14, 2024
1 parent 447cdb8 commit 3b3e3e6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
5 changes: 3 additions & 2 deletions i18n/externalLocales/en/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';

import usedLocKeys from './usedLocKeys.js';
import usedLocKeys from '../../../modules/usedLocKeys.js';
import serviceLines from './serviceLines.json' assert {type: 'json'};

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT_PATH = path.join(__dirname, `../../../../`);


const rawLocale = await import(path.join(ROOT_PATH, 'parser/dist/loc_strings/en.js')).then((m) => m.default);
const languageFilePath = path.join(ROOT_PATH, '/i18n/dist/en.json');
const data = {
...getObjByKeys(rawLocale, await usedLocKeys()),
...getObjByKeys(rawLocale, await usedLocKeys(languageFilePath)),
...serviceLines,
};

Expand Down
15 changes: 15 additions & 0 deletions i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import program from './modules/program.js';
import loadLocale from './modules/loadLocale.js';
import compileLocale from './modules/compileLocale.js';
import parser from './modules/parser.js';
import usedLocKeys from './modules/usedLocKeys.js';

program.parse();

const time = new Timer();
const defaultLocale = await loadLocale(CONFIG.defaultLang);
const externalLocales = readdirSync(CONFIG.externalLocales);
const languagesMap = await parser(`languages.csv`);
const usedKeys = await generateUsedKeys();
const files = readdirSync(CONFIG.additionalContent).map(handler);


Expand All @@ -36,6 +38,12 @@ async function handler(locale) {
languagesMap[config.languages.Name] = { ...config.languages };
} else {
parsedData = await parser(`loc_strings/loc_strings_${locale}.csv`);

Object.keys(parsedData).forEach((k) => {
if (!usedKeys.includes(k)) {
delete parsedData[k];
}
});
}
const data = await compileLocale(locale, parsedData, defaultLocale);
const path = join(CONFIG.savePath, `${locale}.json`);
Expand All @@ -51,3 +59,10 @@ async function handler(locale) {
return readFile(path, 'utf-8').then((f) => JSON.parse(f));
}
}
async function generateUsedKeys() {
const path = join(CONFIG.savePath, `tmp.json`);
const data = await parser(`/loc_strings/loc_strings_en.csv`);
await writeFile(path, JSON.stringify(data)); // vue-i18n-extract требует это виде файла

return usedLocKeys(path);
}
1 change: 0 additions & 1 deletion i18n/modules/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import runner from '../../parser/runners/LocStrings.js';
/**
* Загрузить и распарсить через ранер файл
* @param {String} file путь до файла относительно папки с сырыми данными
* @param {Function<Runner>} runner
* @return {Promise<Object>}
*/
export default async function(file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { fileURLToPath } from 'url';
import VueI18NExtract from 'vue-i18n-extract';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT_PATH = path.join(__dirname, `../../../../`);
const ROOT_PATH = path.join(__dirname, `../../`);

const PARSER_DIST_PATH = path.join(ROOT_PATH, '/parser/dist');
const REGULATION_LOC_PATH = path.join(ROOT_PATH, '/src/regulation/locKeys.mjs');
const RAW_EN_LOCALE_PATH = path.join(ROOT_PATH, '/parser/dist/loc_strings/en.js');


export default async function() {
export default async function(languageFilePath) {
const i18nReport = await VueI18NExtract.createI18NReport({
vueFiles: path.join(ROOT_PATH, `/src/**/*.?(js|ts|vue)`),
languageFiles: path.join(__dirname, `../../../dist/en.json`),
languageFiles: languageFilePath,
output: false,
});
const keysToRemove = i18nReport.unusedKeys.map((e) => e.path);
Expand Down

0 comments on commit 3b3e3e6

Please sign in to comment.