@sentzunhat/zacatl
Version:
A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.
78 lines • 3.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeStaticCatalogs = exports.resolveBuiltInLocalesDir = exports.loadStaticCatalog = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const helpers_1 = require("./helpers");
const index_1 = require("../../error/index");
const loadStaticCatalog = (input) => {
const { localesDir, supportedLocales } = input;
const catalog = {};
for (const locale of supportedLocales) {
const filePath = path_1.default.join(localesDir, `${locale}.json`);
if (!fs_1.default.existsSync(filePath)) {
continue;
}
catalog[locale] = (0, helpers_1.readJsonFile)(filePath);
}
return catalog;
};
exports.loadStaticCatalog = loadStaticCatalog;
const getHere = () => {
if (typeof __dirname !== 'undefined')
return __dirname;
if (typeof process !== 'undefined' &&
Array.isArray(process.argv) &&
typeof process.argv[1] === 'string' &&
process.argv[1].length > 0) {
return path_1.default.dirname(process.argv[1]);
}
return process.cwd();
};
const resolveBuiltInLocalesDir = () => {
const here = getHere();
const candidates = [
path_1.default.resolve(here, '../locales'),
path_1.default.resolve(here, '../../../src/localization/locales'),
path_1.default.resolve(process.cwd(), 'src/localization/locales'),
];
const found = (0, helpers_1.findExistingDir)(candidates);
if (found == null) {
throw new index_1.NotFoundError({
message: `Unable to locate Zacatl built-in locales directory. Tried: ${candidates.join(', ')}`,
reason: 'Built-in locales directory not found in expected paths',
component: 'I18nNode',
operation: 'getBuiltInLocalesDir',
metadata: { candidates },
});
}
return found;
};
exports.resolveBuiltInLocalesDir = resolveBuiltInLocalesDir;
const mergeStaticCatalogs = (input) => {
const { base, additions, overrideBuiltIn } = input;
const out = {};
const locales = new Set([
...Object.keys(base),
...additions.flatMap((a) => Object.keys(a)),
]);
for (const locale of locales) {
const baseLocale = base[locale] ?? {};
const additionsLocale = additions.reduce((acc, addition) => {
const addLocale = addition[locale];
if (addLocale == null) {
return acc;
}
return (0, helpers_1.deepMerge)(acc, addLocale);
}, {});
out[locale] = overrideBuiltIn
? (0, helpers_1.deepMerge)(baseLocale, additionsLocale)
: (0, helpers_1.deepMerge)(additionsLocale, baseLocale);
}
return out;
};
exports.mergeStaticCatalogs = mergeStaticCatalogs;
//# sourceMappingURL=core.js.map