UNPKG

@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.

69 lines 2.42 kB
import fs from 'fs'; import path from 'path'; import { readJsonFile, deepMerge, findExistingDir } from './helpers.js'; import { NotFoundError } from '../../error/index.js'; export const loadStaticCatalog = (input) => { const { localesDir, supportedLocales } = input; const catalog = {}; for (const locale of supportedLocales) { const filePath = path.join(localesDir, `${locale}.json`); if (!fs.existsSync(filePath)) { continue; } catalog[locale] = readJsonFile(filePath); } return catalog; }; 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.dirname(process.argv[1]); } return process.cwd(); }; export const resolveBuiltInLocalesDir = () => { const here = getHere(); const candidates = [ path.resolve(here, '../locales'), path.resolve(here, '../../../src/localization/locales'), path.resolve(process.cwd(), 'src/localization/locales'), ]; const found = findExistingDir(candidates); if (found == null) { throw new 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; }; export 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 deepMerge(acc, addLocale); }, {}); out[locale] = overrideBuiltIn ? deepMerge(baseLocale, additionsLocale) : deepMerge(additionsLocale, baseLocale); } return out; }; //# sourceMappingURL=core.js.map