|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 5 | +LANG_DIR="${ROOT_DIR}/src/folderview.plus/usr/local/emhttp/plugins/folderview.plus/langs" |
| 6 | +# shellcheck source=scripts/lib.sh |
| 7 | +source "${ROOT_DIR}/scripts/lib.sh" |
| 8 | + |
| 9 | +fvplus::require_commands node |
| 10 | + |
| 11 | +if [[ ! -d "${LANG_DIR}" ]]; then |
| 12 | + fvplus::fail "Missing language directory: ${LANG_DIR}" |
| 13 | +fi |
| 14 | + |
| 15 | +node - "${LANG_DIR}" <<'NODE' |
| 16 | +const fs = require('fs'); |
| 17 | +const path = require('path'); |
| 18 | +
|
| 19 | +const langDir = process.argv[2]; |
| 20 | +const files = fs.readdirSync(langDir).filter((name) => name.endsWith('.json')).sort(); |
| 21 | +
|
| 22 | +if (!files.includes('en.json')) { |
| 23 | + console.error(`ERROR: Missing base locale file: ${path.join(langDir, 'en.json')}`); |
| 24 | + process.exit(1); |
| 25 | +} |
| 26 | +
|
| 27 | +const readLocale = (file) => { |
| 28 | + const fullPath = path.join(langDir, file); |
| 29 | + let parsed; |
| 30 | + try { |
| 31 | + parsed = JSON.parse(fs.readFileSync(fullPath, 'utf8')); |
| 32 | + } catch (error) { |
| 33 | + console.error(`ERROR: Invalid JSON in ${fullPath}: ${error.message}`); |
| 34 | + process.exit(1); |
| 35 | + } |
| 36 | + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { |
| 37 | + console.error(`ERROR: Locale file must contain a JSON object: ${fullPath}`); |
| 38 | + process.exit(1); |
| 39 | + } |
| 40 | + return parsed; |
| 41 | +}; |
| 42 | +
|
| 43 | +const base = readLocale('en.json'); |
| 44 | +const baseKeys = Object.keys(base).sort(); |
| 45 | +let failed = false; |
| 46 | +
|
| 47 | +for (const key of baseKeys) { |
| 48 | + if (key === '@metadata') { |
| 49 | + if (!base[key] || typeof base[key] !== 'object' || Array.isArray(base[key])) { |
| 50 | + console.error('ERROR: en.json key "@metadata" must map to an object value.'); |
| 51 | + failed = true; |
| 52 | + } |
| 53 | + continue; |
| 54 | + } |
| 55 | + if (typeof base[key] !== 'string') { |
| 56 | + console.error(`ERROR: en.json key "${key}" must map to a string value.`); |
| 57 | + failed = true; |
| 58 | + } |
| 59 | +} |
| 60 | +
|
| 61 | +for (const file of files) { |
| 62 | + const locale = readLocale(file); |
| 63 | + const localeKeys = Object.keys(locale).sort(); |
| 64 | + const missing = baseKeys.filter((key) => !Object.prototype.hasOwnProperty.call(locale, key)); |
| 65 | + const extra = localeKeys.filter((key) => !Object.prototype.hasOwnProperty.call(base, key)); |
| 66 | + if (missing.length > 0) { |
| 67 | + console.error(`ERROR: ${file} is missing ${missing.length} key(s): ${missing.slice(0, 12).join(', ')}`); |
| 68 | + failed = true; |
| 69 | + } |
| 70 | + if (extra.length > 0) { |
| 71 | + console.error(`ERROR: ${file} has ${extra.length} unexpected key(s): ${extra.slice(0, 12).join(', ')}`); |
| 72 | + failed = true; |
| 73 | + } |
| 74 | + for (const key of localeKeys) { |
| 75 | + if (key === '@metadata') { |
| 76 | + if (!locale[key] || typeof locale[key] !== 'object' || Array.isArray(locale[key])) { |
| 77 | + console.error(`ERROR: ${file} key "@metadata" must map to an object value.`); |
| 78 | + failed = true; |
| 79 | + } |
| 80 | + continue; |
| 81 | + } |
| 82 | + if (typeof locale[key] !== 'string') { |
| 83 | + console.error(`ERROR: ${file} key "${key}" must map to a string value.`); |
| 84 | + failed = true; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +
|
| 89 | +if (failed) { |
| 90 | + process.exit(1); |
| 91 | +} |
| 92 | +
|
| 93 | +console.log(`i18n guard passed: ${files.length} locale file(s) aligned with en.json (${baseKeys.length} keys).`); |
| 94 | +NODE |
0 commit comments