UNPKG

@jsverse/transloco

Version:

The internationalization (i18n) library for Angular

88 lines 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createConfig = createConfig; exports.updateConfig = updateConfig; exports.getJsonFileContent = getJsonFileContent; exports.setFileContent = setFileContent; exports.hasSubdirs = hasSubdirs; exports.hasFiles = hasFiles; exports.getTranslationKey = getTranslationKey; exports.getTranslationsRoot = getTranslationsRoot; exports.getTranslationFiles = getTranslationFiles; exports.getTranslationEntryPaths = getTranslationEntryPaths; exports.getDefaultLang = getDefaultLang; const p = require("node:path"); const schematics_utils_1 = require("../schematics.utils"); const projects_1 = require("./projects"); const config_1 = require("./config"); function createConfig(host, langs, rootTranslationsPath = 'assets/i18n/') { if (!host.get(schematics_utils_1.CONFIG_FILE)) { host.create(schematics_utils_1.CONFIG_FILE, (0, schematics_utils_1.generateConfigFile)({ rootTranslationsPath: rootTranslationsPath, langs, keysManager: {}, })); } } function updateConfig(host, config) { const originalConfig = (0, config_1.getConfig)(); if (!originalConfig || Object.keys(originalConfig).length === 0) { return createConfig(host, config.langs, config.rootTranslationsPath); } host.overwrite(schematics_utils_1.CONFIG_FILE, (0, schematics_utils_1.generateConfigFile)({ ...config, ...originalConfig })); } function getJsonFileContent(fileName, dir, parser = JSON.parse) { return parser(dir.file(fileName).content.toString('utf-8')); } function setFileContent(host, dirPath, fileName, content) { return host.overwrite(p.join(dirPath, fileName), JSON.stringify(content, null, 2)); } function hasSubdirs(dir) { return dir.subdirs && dir.subdirs.length; } function hasFiles(dir) { return dir.subfiles && dir.subfiles.length; } function getTranslationKey(prefix = '', key) { return prefix ? `${prefix}.${key}` : key; } function getTranslationsRoot(host, options) { const translocoConfig = (0, config_1.getConfig)(); if (options.translationPath) { return options.translationPath; } else if (translocoConfig && translocoConfig.rootTranslationsPath) { return translocoConfig.rootTranslationsPath; } else { const project = (0, projects_1.getProject)(host, options.project); const rootPath = (project && project.sourceRoot) || 'src'; return p.join(rootPath, 'assets', 'i18n'); } } function getTranslationFiles(host, root, parser) { const rootDir = host.getDir(root); return rootDir.subfiles.map((fileName) => ({ lang: fileName.split('.')[0], translation: getJsonFileContent(fileName, rootDir, parser), })); } function getTranslationEntryPaths(host, rootDirPath) { const translocoConfig = (0, config_1.getConfig)(); if (translocoConfig.scopePathMap && Object.keys(translocoConfig.scopePathMap).length) { return Object.entries(translocoConfig.scopePathMap).map(([scope, path]) => ({ scope, path, })); } const rootDir = host.getDir(rootDirPath); return rootDir.subdirs.map((subDir) => ({ scope: subDir, path: p.join(rootDirPath, subDir), })); } function getDefaultLang(options) { return options.defaultLang || (0, config_1.getConfig)().defaultLang; } //# sourceMappingURL=transloco.js.map