next-intl-split
Version:
A loader for next-intl to split translation files properly.
1 lines • 4.02 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","sources":["../src/src/load/index.ts"],"sourcesContent":["import { existsSync, readdirSync, readFileSync } from 'fs';\nimport { AbstractIntlMessages } from 'next-intl';\nimport path from 'path';\n\nimport { writeMessages } from '../write';\n\nconst FILE_NAME = 'index.json';\n\nconst doesPathExist = (pathToCheck: string) => existsSync(pathToCheck);\n\nconst isAJSONFilePath = (filePath: string): boolean =>\n filePath.includes(FILE_NAME) && filePath.includes(path.sep);\n\nconst createNestedObject = (obj: { [key: string]: any }, keys: string[]) => {\n let current = obj;\n\n for (const key of keys) {\n if (!current[key]) {\n current[key] = {};\n }\n current = current[key];\n }\n\n return { obj, lastKey: current };\n};\n\nexport const loadI18nTranslations = (\n dictionariesPath: string,\n locale: string,\n enableTypeCheck?: boolean\n) => {\n const allMessages = loadMessages(dictionariesPath);\n\n const localeMessages = Object.entries(allMessages).filter(\n (localeMessage) => localeMessage[0] === locale\n )[0][1];\n\n if (enableTypeCheck) {\n const absolutePath = path.resolve(process.cwd(), dictionariesPath);\n\n const pathExist = doesPathExist(absolutePath);\n\n if (pathExist) {\n writeMessages(absolutePath, allMessages, { muteLogs: true });\n }\n }\n\n return localeMessages as AbstractIntlMessages;\n};\n\nexport const loadMessages = (dictionariesPath: string) => {\n let messages: { [key: string]: any } = {};\n\n try {\n const files = readdirSync(dictionariesPath, { recursive: true });\n\n for (let filePathIndex = 0; filePathIndex < files.length; filePathIndex++) {\n const file = files[filePathIndex];\n\n if (typeof file === 'string' && isAJSONFilePath(file)) {\n const fileParents = file\n .split(path.sep)\n .filter((parent) => parent !== FILE_NAME);\n\n const pathToFile = path.resolve(process.cwd(), dictionariesPath, file);\n const fileMessages = JSON.parse(readFileSync(pathToFile, 'utf-8'));\n\n messages = {\n ...messages,\n };\n\n const { lastKey } = createNestedObject(messages, fileParents);\n\n Object.assign(lastKey, fileMessages);\n }\n }\n } catch (error) {\n console.error(\n 'The following error occured in loader in next-intl-split.',\n error\n );\n }\n\n return messages;\n};\n"],"names":["FILE_NAME","createNestedObject","obj","keys","current","key","lastKey","loadMessages","dictionariesPath","messages","files","readdirSync","recursive","filePathIndex","length","file","filePath","includes","path","sep","fileParents","split","filter","parent","pathToFile","resolve","process","cwd","fileMessages","JSON","parse","readFileSync","Object","assign","error","console","locale","enableTypeCheck","allMessages","localeMessages","entries","localeMessage","absolutePath","pathToCheck","existsSync","writeMessages","muteLogs"],"mappings":"6NAMA,MAAMA,EAAY,aAOZC,EAAqB,CAACC,EAA6BC,KACvD,IAAIC,EAAUF,EAEd,IAAK,MAAMG,KAAOF,EACXC,EAAQC,KACXD,EAAQC,GAAO,IAEjBD,EAAUA,EAAQC,GAGpB,MAAO,CAAEH,MAAKI,QAASF,EAAS,EA2BrBG,EAAgBC,IAC3B,IAAIC,EAAmC,CAAA,EAEvC,IACE,MAAMC,EAAQC,EAAAA,YAAYH,EAAkB,CAAEI,WAAW,IAEzD,IAAK,IAAIC,EAAgB,EAAGA,EAAgBH,EAAMI,OAAQD,IAAiB,CACzE,MAAME,EAAOL,EAAMG,GAEnB,GAAoB,iBAATE,KAjDQC,EAiD6BD,GAhD3CE,SAASjB,IAAcgB,EAASC,SAASC,UAAKC,MAgDI,CACrD,MAAMC,EAAcL,EACjBM,MAAMH,EAAI,QAACC,KACXG,QAAQC,GAAWA,IAAWvB,IAE3BwB,EAAaN,EAAI,QAACO,QAAQC,QAAQC,MAAOnB,EAAkBO,GAC3Da,EAAeC,KAAKC,MAAMC,EAAYA,aAACP,EAAY,UAEzDf,EAAW,IACNA,GAGL,MAAMH,QAAEA,GAAYL,EAAmBQ,EAAUW,GAEjDY,OAAOC,OAAO3B,EAASsB,EACxB,CACF,CACF,CAAC,MAAOM,GACPC,QAAQD,MACN,4DACAA,EAEH,CAvEqB,IAAClB,EAyEvB,OAAOP,CAAQ,+BAzDmB,CAClCD,EACA4B,EACAC,KAEA,MAAMC,EAAc/B,EAAaC,GAE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