UNPKG

@ts-intl/dictionary

Version:

I18n dictionary/messages generator

26 lines (25 loc) 1.44 kB
import { buildTrieByNSPath, dictionaryResolverFs, readJsonFile, } from '@ts-intl/shared'; import { extractDictionary } from './extractDictionary'; export const extractDictionaryFs = ({ localePath, locale, basicLocale, include, exclude, reader = readJsonFile, }) => { const includeTrie = buildTrieByNSPath(include); const excludeTrie = (exclude === null || exclude === void 0 ? void 0 : exclude.length) ? buildTrieByNSPath(exclude) : undefined; const [lngDict, fallbackDict] = [locale, basicLocale].map((locale) => initDictionaryFs(localePath, reader, includeTrie, excludeTrie, locale)); return extractDictionary(lngDict, fallbackDict, includeTrie, excludeTrie); }; const initDictionaryFs = (localePath, reader, includeTrie, excludeTrie, locale) => { if (!locale) return {}; const include = []; includeTrie.childrenNameMap.forEach((v) => { var _a; if (!v.name || ((_a = excludeTrie === null || excludeTrie === void 0 ? void 0 : excludeTrie.get(v.name)) === null || _a === void 0 ? void 0 : _a.isLeaf)) return; include.push(v.name); }); const { dictionary } = dictionaryResolverFs(localePath, locale, reader, include); return include.reduce((res, namespace) => { var _a; res[namespace] = (_a = dictionary === null || dictionary === void 0 ? void 0 : dictionary[namespace]) !== null && _a !== void 0 ? _a : {}; return res; }, {}); };