UNPKG

@jsverse/transloco-keys-manager

Version:

Extract translatable keys from projects that uses Transloco

69 lines 2.63 kB
import path, { sep } from 'node:path'; import { getConfig } from '../config.js'; import { isObject } from './validators.utils.js'; export function pathUnixFormat(path) { return path.split(sep).join('/'); } export function buildPath(obj) { return Object.keys(obj).reduce((acc, curr) => { const keys = isObject(obj[curr]) ? buildPath(obj[curr]).map((inner) => `${curr}.${inner}`) : [curr]; acc.push(...keys); return acc; }, []); } /** * /Users/username/www/folderName/src/assets/i18n/admin/es.json => { scope: admin, lang: es } * /Users/username/www/folderName/src/assets/i18n/es.json => { scope: undefined, lang: es } */ export function getScopeAndLangFromPath({ filePath, translationsPath, fileFormat, }) { filePath = pathUnixFormat(filePath); translationsPath = pathUnixFormat(translationsPath); if (!translationsPath.endsWith('/')) { translationsPath = `${translationsPath}/`; } const [_, pathWithScope] = filePath.split(translationsPath); const scopePath = pathWithScope.split('/'); const removeExtension = (str) => str.replace(`.${fileFormat}`, ''); let scope, lang; if (scopePath.length > 1) { lang = removeExtension(scopePath.pop()); scope = scopePath.join('/'); } else { lang = removeExtension(scopePath[0]); } return { scope, lang }; } function resolvePath(configPath = '') { return path.resolve(process.cwd(), configPath); } export function resolveConfigPaths(config) { const interpolate = interpolatePathFactory(config.__sourceRoot); config.input = config.input.map(interpolate).map(resolvePath); config.output = resolvePath(interpolate(config.output)); config.translationsPath = resolvePath(interpolate(config.translationsPath)); } export function buildScopeFilePaths({ aliasToScope, output, langs, fileFormat, }) { const { scopePathMap = {}, __sourceRoot = '' } = getConfig(); const interpolate = interpolatePathFactory(__sourceRoot); return Object.values(aliasToScope).reduce((files, scope) => { langs.forEach((lang) => { let bastPath = scopePathMap[scope] ? interpolate(scopePathMap[scope]) : `${output}/${scope}`; files.push({ path: `${bastPath}/${lang}.${fileFormat}`, scope, }); }); return files; }, []); } function interpolatePathFactory(sourceRoot) { return sourceRoot ? (pathStr) => pathStr.replace(/\$\{sourceRoot}/g, sourceRoot) : (v) => v; } //# sourceMappingURL=path.utils.js.map