UNPKG

@intlayer/chokidar

Version:

Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.

81 lines 2.21 kB
import { getConfiguration } from "@intlayer/config"; import { NodeType } from "@intlayer/core"; import { convertPluralsValues } from "./convertPluralsValues.mjs"; const isReactNode = (node) => typeof node?.key !== "undefined" && typeof node?.props !== "undefined" && typeof node?.type !== "undefined"; const buildDictionary = (content, locale) => { if ( // Translation node content && content.nodeType === NodeType.Translation ) { const contentState = content[NodeType.Translation]; const result = contentState[locale]; return buildDictionary(result, locale); } else if ( // Translation node content && content.nodeType === NodeType.Enumeration ) { const plurals = {}; Object.keys( content[NodeType.Enumeration] ).forEach((quantity) => { const letterNumber = convertPluralsValues(quantity); const value = content[quantity]; plurals[`${letterNumber}_${letterNumber}`] = buildDictionary( value, locale ); }); return plurals; } else if ( // React element node isReactNode(content) ) { return JSON.stringify(content); } else if ( // Nested object typeof content === "object" && Array.isArray(content) ) { const result = []; Object.keys(content).forEach((dictionaryValue) => { result.push( buildDictionary( content[dictionaryValue], locale ) ); }); return result; } else if ( // Nested object typeof content === "object" ) { const result = {}; Object.keys(content).forEach( (dictionaryValue) => { result[dictionaryValue] = buildDictionary( content?.[dictionaryValue], locale ); } ); return result; } return content; }; const createI18nextDictionaries = (content, configuration = getConfiguration()) => { const { locales } = configuration.internationalization; const result = locales.reduce( (acc, locale) => ({ ...acc, [locale]: buildDictionary(content, locale) }), {} ); return result; }; export { createI18nextDictionaries }; //# sourceMappingURL=convertContentDeclarationInto18nDictionaries.mjs.map