**What** - Adds a test that validates that en.json and $schema.json matches, this will help us catch missing translations, and prevent us from forgetting to update the schema when we delete/add new keys. - Adds a script for validating translations against the translation schema. This can be used to validate that community PRs for i18n contain all the required keys. To use the script you can run `yarn i18n:validate <file name>` e.g. `yarn i18n:validate da.json` which will look for a da.json file in the translation folder, and validate it against the schema. We handle this with a script as we don't want to do so through a test. Doing it with a test would mean that if we update the schema, we would also have to update all translations files that we don't maintain ourselves. The purpose of the script is just to allow us to easily review community PRs and also as a tool for people opening PR's to check their translations agains the schema, as it will print missing/additional keys. - Also adds a script to generate a schema from the en.json file. After adding/deleting keys to en.json you should run `yarn i18n:generate`.
48 lines
1.4 KiB
JavaScript
48 lines
1.4 KiB
JavaScript
const Ajv = require("ajv")
|
|
const fs = require("fs")
|
|
const path = require("path")
|
|
const schema = require("../../src/i18n/translations/$schema.json")
|
|
|
|
const ajv = new Ajv({ allErrors: true })
|
|
const validate = ajv.compile(schema)
|
|
|
|
// Get file name from command line arguments
|
|
const fileName = process.argv[2]
|
|
|
|
if (!fileName) {
|
|
console.error("Please provide a file name (e.g., en.json) as an argument.")
|
|
process.exit(1)
|
|
}
|
|
|
|
const filePath = path.join(__dirname, "../../src/i18n/translations", fileName)
|
|
|
|
try {
|
|
const translations = JSON.parse(fs.readFileSync(filePath, "utf-8"))
|
|
|
|
if (!validate(translations)) {
|
|
console.error(`\nValidation failed for ${fileName}:`)
|
|
validate.errors?.forEach((error) => {
|
|
if (error.keyword === "required") {
|
|
const missingKeys = error.params.missingProperty
|
|
console.error(
|
|
` Missing required key: "${missingKeys}" at ${error.instancePath}`
|
|
)
|
|
} else if (error.keyword === "additionalProperties") {
|
|
const extraKey = error.params.additionalProperty
|
|
console.error(
|
|
` Unexpected key: "${extraKey}" at ${error.instancePath}`
|
|
)
|
|
} else {
|
|
console.error(` Error: ${error.message} at ${error.instancePath}`)
|
|
}
|
|
})
|
|
process.exit(1)
|
|
} else {
|
|
console.log(`${fileName} matches the schema.`)
|
|
process.exit(0)
|
|
}
|
|
} catch (error) {
|
|
console.error(`Error reading or parsing file: ${error.message}`)
|
|
process.exit(1)
|
|
}
|