Skip to content

Commit

Permalink
Merge pull request #417 from dehilsterlexis/LOCALIZE_DELETE_ORPHANS
Browse files Browse the repository at this point in the history
feat: remove orphan localization strings
  • Loading branch information
GordonSmith committed Apr 19, 2024
2 parents 65cfee9 + d5cd2fc commit 30a4047
Showing 1 changed file with 64 additions and 15 deletions.
79 changes: 64 additions & 15 deletions util/nls/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ const prevWeeks = ["Last Week", "Two Weeks Ago", "Three Weeks Ago", "Four Weeks
const tmpDir = "./tmp/";

const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8"));
const en = JSON.parse(fs.readFileSync("./package.nls.json", "utf-8"));
const en = JSON.parse(fs.readFileSync("./package.nls.json", "utf-8"));
const all = {};

export function initLocales() {
export function addStringArrays(found: object) {
prevWeeks.forEach( (week) => {
if (en[week] === undefined) {
en[week] = week;
if (found[week] === undefined) {
found[week] = week;
}
});
}
Expand All @@ -30,16 +31,21 @@ export function googleTrans() {

function fix(item: any, what: string): boolean {
let key = item[what];
if (key === undefined) return false;
if (key === undefined || key == "") return false;
let retVal = false;
if (key[0] !== "%" || key[key.length - 1] !== "%") {
if (key[0] == "%" || key[key.length - 1] == "%") {
const k = key.substring(1, key.length - 1);
all[k] = "";
}
else if (key[0] !== "%" || key[key.length - 1] !== "%") {
key = `%${key}%`;
item[what] = key;
retVal = retVal || true;
}
key = key.substring(1, key.length - 1);
if (!en[key]) {
en[key] = key;
all[key] = "";
retVal = retVal || true;
}
return retVal;
Expand Down Expand Up @@ -92,10 +98,20 @@ export function fixPackage() {
touched = fix(item, "description") || touched;
}

const deleted = {};
for (const key in en) {
if (all[key] === undefined) {
deleted[key] = key;
}
}

if (touched) {
fs.writeFileSync("./package.json", JSON.stringify(pkg, undefined, 2), "utf-8");
sortAndWrite(tmpDir + "package.nls.missing.json", en);
}
if (touched || deleted) {
sortAndWrite(tmpDir + "package.nls.deleted.json", deleted);
}
}

const localizeRegex = /localize\("(.*?)"\)/gm;
Expand Down Expand Up @@ -146,14 +162,15 @@ async function runPythonScript(scriptPath: string): Promise<string> {
}

export function updateLocaleFiles() {
initLocales();
const found = {};

traverseDir("./src", found);
addStringArrays(found);

locales.forEach(l => {
const otherFile = `./package.nls.${l}.json`;
const otherFileMissing = `${tmpDir}package.nls.${l}.missing.json`;
const otherFileDeleted = `${tmpDir}package.nls.${l}.deleted.json`;

let other = {};
if (fs.existsSync(otherFile)) {
other = JSON.parse(fs.readFileSync(otherFile, "utf-8"));
Expand All @@ -163,7 +180,7 @@ export function updateLocaleFiles() {

const otherMissing = {};
let touched = false;
for (const key in en) {
for (const key in all) {
if (other[key] === undefined) {
otherMissing[key] = "";
touched = true;
Expand All @@ -180,26 +197,50 @@ export function updateLocaleFiles() {
if (touched) {
sortAndWrite(otherFileMissing, otherMissing);
}

const otherDeleted = {};
touched = false;
for (const key in other) {
if (all[key] === undefined && found[key] === undefined) {
otherDeleted[key] = "";
touched = true;
}
}

if (touched) {
sortAndWrite(otherFileDeleted, otherDeleted);
}
});
}

function mergeLocal(origFile: string, missingFile: string): void {
function mergeLocal(fileName: string, type: string): void {

const origFile = fileName.replace(`.${type}.`, ".");
const deleteFile = fileName.replace(`.${type}.`, ".deleted.");

if (!fs.existsSync(path.join("./", origFile))) {
console.log(`Original file not found: ${origFile}`);
return;
}

if (!fs.existsSync(path.join(tmpDir, missingFile))) {
console.log(`Translation file not found: ${missingFile}`);
if (!fs.existsSync(path.join(tmpDir, fileName))) {
console.log(`Translation file not found: ${fileName}`);
return;
}

let deletes = false;
if (fs.existsSync(path.join(tmpDir, fileName))) {
deletes = true;
}

let currentData: any;
let newData: any;
let deletedData: any;
try {
currentData = JSON.parse(fs.readFileSync(path.join("./", origFile), "utf-8"));
newData = JSON.parse(fs.readFileSync(path.join(tmpDir, missingFile), "utf-8"));
newData = JSON.parse(fs.readFileSync(path.join(tmpDir, fileName), "utf-8"));
if (deletes)
deletedData = JSON.parse(fs.readFileSync(path.join(tmpDir, deleteFile), "utf-8"));
} catch (err) {
console.error(`Error reading files: ${err}`);
return;
Expand All @@ -211,16 +252,24 @@ function mergeLocal(origFile: string, missingFile: string): void {
}
}

if (deletes) {
for (const fieldName in deletedData) {
if (fieldName in currentData) {
delete currentData[fieldName];
}
}
}

const mergedFile: string = path.join(tmpDir, origFile);
sortAndWrite(mergedFile, currentData);
}

export function mergeLocales() {
for (const fileName of fs.readdirSync(tmpDir)) {
if (fileName.startsWith("package.nls.") && fileName.includes(".trans.")) {
mergeLocal(fileName.replace(".trans.", "."), fileName);
mergeLocal(fileName, "trans");
} else if (fileName == "package.nls.missing.json") {
mergeLocal(fileName.replace(".missing.", "."), fileName);
mergeLocal(fileName, "missing");
}
}
}
Expand Down

0 comments on commit 30a4047

Please sign in to comment.