Last active
February 15, 2023 21:58
-
-
Save carlesbarreda/7504de323556b439d948f2e695ac0c55 to your computer and use it in GitHub Desktop.
Elk: Extract valencians strings locales
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { resolve } from 'pathe' | |
import fs from 'fs-extra' | |
async function readI18nFile(file: string) { | |
return JSON.parse(Buffer.from( | |
await fs.readFile(resolve(`./locales/${file}`), 'utf-8'), | |
).toString()) | |
} | |
/** | |
* Simple object check. | |
* @param item | |
* @returns {boolean} | |
*/ | |
export function isObject(item) { | |
return (item && typeof item === 'object' && !Array.isArray(item)) | |
} | |
export function extractValencian(target, base, source) { | |
if (isObject(target) && isObject(base) && isObject(source)) { | |
for (const key in base) { | |
if (isObject(base[key])) { | |
if (!source[key]) | |
Object.assign(source, { [key]: {} }) | |
if (!target[key]) | |
Object.assign(target, { [key]: {} }) | |
extractValencian(target[key], base[key], source[key]) | |
} | |
else { | |
if (source[key] && source[key].toLowerCase() !== base[key].toLowerCase()) | |
Object.assign(target, { [key]: source[key] }) | |
} | |
} | |
} | |
return target | |
} | |
export function mergeLocales(target, base, source) { | |
if (isObject(target) && isObject(base) && isObject(source)) { | |
for (const key in base) { | |
if (isObject(base[key])) { | |
if (!source[key]) | |
Object.assign(source, { [key]: {} }) | |
if (!target[key]) | |
Object.assign(target, { [key]: {} }) | |
mergeLocales(target[key], base[key], source[key]) | |
} | |
else { | |
if (!source[key]) | |
Object.assign(target, { [key]: base[key] }) | |
else | |
Object.assign(target, { [key]: source[key] }) | |
} | |
} | |
} | |
return target | |
} | |
const en = await readI18nFile('en.json') | |
// Extract valencians strings from ca-1577.json | |
const ca = await readI18nFile('ca-1577.json') | |
const newCa = extractValencian({}, en, ca) | |
await fs.writeJSON('locales/ca-1577-NEW.json', newCa, { spaces: 2, EOL: '\n' }) | |
// Extract valencians strings from ca-ES-Valencian-1577.json | |
const caVal = await readI18nFile('ca-ES-Valencian-1577.json') | |
const newCaVal = extractValencian({}, en, caVal) | |
await fs.writeJSON('locales/ca-ES-Valencian-1577-NEW.json', newCaVal, { spaces: 2, EOL: '\n' }) | |
// Merge valencians strings | |
const newCaValMerged = mergeLocales({}, newCa, newCaVal) | |
await fs.writeJSON('locales/ca-ES-Valencian-1577-Merged-NEW.json', newCaValMerged, { spaces: 2, EOL: '\n' }) | |
// Extract the Valencian strings that differ from my ca.json strings | |
const myCa = await readI18nFile('ca.json') | |
const newCaValFinal = extractValencian({}, myCa, newCaValMerged) | |
await fs.writeJSON('locales/ca-ES-Valencian-1577-Final-NEW.json', newCaValFinal, { spaces: 2, EOL: '\n' }) | |
// Add next line without comment to scripts section in package.json | |
// "extractValencian": "esno scripts/extractValencian.ts", | |
// $ pnpm extractValencian |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { resolve } from 'pathe' | |
import fs from 'fs-extra' | |
async function readI18nFile(file: string) { | |
return JSON.parse(Buffer.from( | |
await fs.readFile(resolve(`./locales/${file}`), 'utf-8'), | |
).toString()) | |
} | |
/** | |
* Simple object check. | |
* @param item | |
* @returns {boolean} | |
*/ | |
export function isObject(item) { | |
return (item && typeof item === 'object' && !Array.isArray(item)) | |
} | |
export function extractValencian(target, base, source) { | |
if (isObject(target) && isObject(base) && isObject(source)) { | |
for (const key in base) { | |
if (isObject(base[key])) { | |
if (!source[key]) | |
Object.assign(source, { [key]: {} }) | |
if (!target[key]) | |
Object.assign(target, { [key]: {} }) | |
extractValencian(target[key], base[key], source[key]) | |
} | |
else { | |
if (source[key] && source[key].toLowerCase() !== base[key].toLowerCase()) | |
Object.assign(target, { [key]: source[key] }) | |
} | |
} | |
} | |
return target | |
} | |
export function mergeLocales(target, base, source) { | |
if (isObject(target) && isObject(base) && isObject(source)) { | |
for (const key in base) { | |
if (isObject(base[key])) { | |
if (!source[key]) | |
Object.assign(source, { [key]: {} }) | |
if (!target[key]) | |
Object.assign(target, { [key]: {} }) | |
mergeLocales(target[key], base[key], source[key]) | |
} | |
else { | |
if (!source[key]) | |
Object.assign(target, { [key]: base[key] }) | |
else | |
Object.assign(target, { [key]: source[key] }) | |
} | |
} | |
} | |
return target | |
} | |
const ca = await readI18nFile('ca.json') | |
const caVal = await readI18nFile('ca-ES-Valencia.json') | |
const caValFinal = extractValencian({}, ca, caVal) | |
await fs.writeJSON('locales/ca-ES-Valencia-NEW.json', caValFinal, { spaces: 2, EOL: '\n' }) | |
// Add next line without comment to scripts section in package.json | |
// "extractValencian": "esno scripts/extractValencian.ts", | |
// $ pnpm extractValencian |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Script used to merge locales in repo Elk | |
https://github.com/elk-zone/elk/pull/1634#issuecomment-1417946632 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment