UNPKG

@jsverse/transloco-keys-manager

Version:

Extract translatable keys from projects that uses Transloco

62 lines 2.26 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) { config.input = config.input.map(resolvePath); config.output = resolvePath(config.output); config.translationsPath = resolvePath(config.translationsPath); } export function buildScopeFilePaths({ aliasToScope, output, langs, fileFormat, }) { const { scopePathMap = {} } = getConfig(); return Object.values(aliasToScope).reduce((files, scope) => { langs.forEach((lang) => { let bastPath = scopePathMap[scope] ? scopePathMap[scope] : `${output}/${scope}`; files.push({ path: `${bastPath}/${lang}.${fileFormat}`, scope, }); }); return files; }, []); } //# sourceMappingURL=path.utils.js.map