Skip to content

Commit

Permalink
add script to reorg intl translations
Browse files Browse the repository at this point in the history
  • Loading branch information
pettinarip committed Feb 4, 2023
1 parent 344fa22 commit 40e1bf9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"markdown-checker": "node src/scripts/markdown-checker.js",
"generate-heading-ids": "ts-node -O '{ \"module\": \"commonjs\" }' src/scripts/generateHeadingIds.ts",
"unused-translations": "ts-node -O '{ \"module\": \"commonjs\" }' src/scripts/unusedTranslations.ts",
"reorg-intl": "ts-node -O '{ \"module\": \"commonjs\" }' src/scripts/reorgIntlFiles.ts",
"start": "gatsby develop",
"start:lambda": "netlify-lambda serve src/lambda",
"start:static": "gatsby build && gatsby serve",
Expand Down
91 changes: 91 additions & 0 deletions src/scripts/reorgIntlFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import fs from "fs"
import path from "path"
import walkdir from "walkdir"
import _ from "lodash"

import { defaultLanguage, supportedLanguages } from "../utils/languages"

const sourcePath = "src/intl"

async function reorgIntl() {
const intl: Record<string, Record<string, string>> = {}

supportedLanguages.forEach((language) => {
intl[language] = {}

walkdir.sync(path.join(sourcePath, language), (filepath) => {
const namespace = path.basename(filepath)

// load english content per ns in memory in order to compare it with the
// other languages
const data = fs.readFileSync(filepath, { encoding: "utf8" })
const translations = JSON.parse(data)

intl[language][namespace] = translations
})
})

supportedLanguages.forEach((language) => {
walkdir.sync(path.join(sourcePath, language), (filepath) => {
const ns = path.basename(filepath)

console.log(`Analyzing ${ns}`)

const data = fs.readFileSync(filepath, { encoding: "utf8" })
const translations = JSON.parse(data)

// check differences and fill the missing translations strings in english
const defaultLangContent = intl[defaultLanguage][ns]

const langKeys = Object.keys(translations)
const defaultLangKeys = Object.keys(defaultLangContent)

// translations keys that the defaultLang has but the lang not
const missingKeys = _.difference(defaultLangKeys, langKeys)

// search for the missing translation in the lang
const missingTranslations = missingKeys.reduce<Record<string, string>>(
(translations, key) => {
// look for string in lang
const namespace = findNamespace(intl[language], key)

if (namespace) {
return {
...translations,
[key]: namespace[key],
}
}

return translations
},
{}
)

// add the missing translations on the lang file
const completeSet = { ...translations, ...missingTranslations }

const leftoverKeys = _.difference(
Object.keys(completeSet),
defaultLangKeys
)

const final = _.omit(completeSet, leftoverKeys)

fs.writeFileSync(
path.join(sourcePath, language, ns),
JSON.stringify(final, null, 2)
)
})
})
}

function findNamespace(
data: Record<string, string>,
key: string
): string | undefined {
return _.find(data, (ns) => {
return ns.hasOwnProperty(key)
})
}

reorgIntl()

0 comments on commit 40e1bf9

Please sign in to comment.