@ngneat/transloco
Version:
The internationalization (i18n) library for Angular
93 lines • 3.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const transloco_utils_1 = require("@ngneat/transloco-utils");
const schematics_consts_1 = require("../schematics.consts");
const array_1 = require("./array");
const projects_1 = require("./projects");
const p = require('path');
let config;
function getConfig() {
if (config)
return config;
config = transloco_utils_1.getConfig();
return config;
}
exports.getConfig = getConfig;
function createConfig(host, langs, rootTranslationsPath = 'src/assets/i18n/') {
if (!host.get(schematics_consts_1.CONFIG_FILE)) {
const config = `module.exports = {
rootTranslationsPath: '${rootTranslationsPath}',
langs: [${array_1.stringifyList(langs)}],
keysManager: {}
};`;
host.create(schematics_consts_1.CONFIG_FILE, config);
}
}
exports.createConfig = createConfig;
function updateConfig(host, config) {
const originalConfig = getConfig();
if (!originalConfig || Object.keys(originalConfig).length === 0) {
return createConfig(host, config.langs, config.rootTranslationsPath);
}
const stringifyConfig = JSON.stringify(Object.assign({}, config, originalConfig), null, 2);
const content = `module.exports = ${stringifyConfig};`;
host.overwrite(schematics_consts_1.CONFIG_FILE, content);
}
exports.updateConfig = updateConfig;
function getJsonFileContent(fileName, dir, parser = JSON.parse) {
return parser(dir.file(fileName).content.toString('utf-8'));
}
exports.getJsonFileContent = getJsonFileContent;
function setFileContent(host, dirPath, fileName, content) {
return host.overwrite(p.join(dirPath, fileName), JSON.stringify(content, null, 2));
}
exports.setFileContent = setFileContent;
function hasSubdirs(dir) {
return dir.subdirs && dir.subdirs.length;
}
exports.hasSubdirs = hasSubdirs;
function hasFiles(dir) {
return dir.subfiles && dir.subfiles.length;
}
exports.hasFiles = hasFiles;
function getTranslationKey(prefix = '', key) {
return prefix ? `${prefix}.${key}` : key;
}
exports.getTranslationKey = getTranslationKey;
function getTranslationsRoot(host, options) {
const translocoConfig = getConfig();
if (options.translationPath) {
return options.translationPath;
}
else if (translocoConfig && translocoConfig.rootTranslationsPath) {
return translocoConfig.rootTranslationsPath;
}
else {
const project = projects_1.getProject(host, options.project);
const rootPath = (project && project.sourceRoot) || 'src';
return p.join(rootPath, 'assets', 'i18n');
}
}
exports.getTranslationsRoot = getTranslationsRoot;
function getTranslationFiles(host, root, parser) {
const rootDir = host.getDir(root);
return rootDir.subfiles.map(fileName => ({
lang: fileName.split('.')[0],
translation: getJsonFileContent(fileName, rootDir, parser)
}));
}
exports.getTranslationFiles = getTranslationFiles;
function getTranslationEntryPaths(host, rootDirPath) {
const translocoConfig = 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) }));
}
exports.getTranslationEntryPaths = getTranslationEntryPaths;
function getDefaultLang(options) {
return options.defaultLang || getConfig().defaultLang;
}
exports.getDefaultLang = getDefaultLang;
//# sourceMappingURL=transloco.js.map