@hug/ngx-g11n
Version:
Angular helpers for internationalizing and localizing your application
138 lines • 6.69 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_OPTIONS = void 0;
const schematics_1 = require("@angular-devkit/schematics");
const ngx_schematics_utilities_1 = require("@hug/ngx-schematics-utilities");
const node_path_1 = require("node:path");
exports.DEFAULT_OPTIONS = {
defaultLanguage: 'fr-CH',
defaultCurrency: 'CHF',
useNavigatorLanguage: true,
loadLocaleExtra: false,
translationsPath: '/translations',
queryParamName: 'lang'
};
const customizeProject = ({ project, tree }, options, ngVersion) => {
const rules = [];
// tsconfig.json
if (Number(ngVersion.major) <= 14) {
rules.push((0, ngx_schematics_utilities_1.modifyJsonFile)('tsconfig.json', ['angularCompilerOptions', 'skipLibCheck'], true));
}
// angular.json
const extractOptionsPath = ['projects', project.name, 'architect', 'extract-i18n', 'options'];
if (project.assetsPath) {
rules.push((0, ngx_schematics_utilities_1.modifyJsonFile)('angular.json', [...extractOptionsPath, 'outputPath'], (0, node_path_1.join)(project.assetsPath, options.translationsPath)));
}
rules.push((0, ngx_schematics_utilities_1.modifyJsonFile)('angular.json', [...extractOptionsPath, 'outFile'], `${options.defaultLanguage}.json`));
rules.push((0, ngx_schematics_utilities_1.modifyJsonFile)('angular.json', [...extractOptionsPath, 'format'], 'json'));
rules.push((0, ngx_schematics_utilities_1.modifyJsonFile)('angular.json', ['projects', project.name, 'i18n', 'sourceLocale'], options.defaultLanguage));
// Provide library
let configFile;
if (project.isStandalone) {
configFile = project.mainConfigFilePath;
}
else if (tree.exists(project.pathFromSourceRoot('app/app-module.ts'))) {
configFile = project.pathFromSourceRoot('app/app-module.ts'); // for Angular 20+
}
else {
configFile = project.pathFromSourceRoot('app/app.module.ts'); // for Angular < 20
}
if (configFile && tree.exists(configFile)) {
const libName = Number(ngVersion.major) >= 15 ? '@hug/ngx-g11n' : '@hug/ngx-g11n/legacy';
let provider = project.isStandalone ? 'provideG11n(' : 'G11nModule.forRoot(';
// ---- locales
if (options.defaultLocales && options.material) {
rules.push((0, ngx_schematics_utilities_1.addImportToFile)(configFile, 'withDefaultLocales', '@hug/ngx-g11n/locales'));
provider += '\n withDefaultLocales()';
}
else {
rules.push((0, ngx_schematics_utilities_1.addImportToFile)(configFile, 'withLocales', libName));
const getLocale = (locale) => {
let str = `'${locale}': {`;
str += `\n base: () => import('@angular/common/locales/${locale}')`;
if (options.loadLocaleExtra) {
str += `,\n extra: () => import('@angular/common/locales/extra/${locale}')`;
}
if (options.material) {
str += `,\n datefns: () => import('date-fns/locale/${locale}')`;
}
str += '\n }';
return str;
};
provider += '\n withLocales({';
if (options.defaultLocales) {
provider += `\n ${getLocale('fr-CH')}`;
provider += `,\n ${getLocale('de-CH')}`;
}
else {
provider += `\n ${getLocale(options.defaultLanguage)}`;
}
provider += '\n })';
}
// ---- options
const opts = {};
Object.entries(options).forEach(([key, value]) => {
if (key in exports.DEFAULT_OPTIONS && exports.DEFAULT_OPTIONS[key] !== value) {
// @ts-expect-error error expected
opts[key] = value;
}
});
if (Object.keys(opts).length) {
rules.push((0, ngx_schematics_utilities_1.addImportToFile)(configFile, 'withOptions', libName));
provider += `,\n withOptions(${JSON.stringify(opts, null, 4)
.replace(/"([^"]+)":/g, '$1:')
.replace(/"/g, '\'')})`;
}
// ---- material
if (options.material) {
rules.push((0, ngx_schematics_utilities_1.addImportToFile)(configFile, 'withDateFnsMaterial', '@hug/ngx-g11n/material'));
provider += ',\n withDateFnsMaterial()';
}
// ---- interceptor
if (options.interceptor) {
rules.push((0, ngx_schematics_utilities_1.addImportToFile)(configFile, 'withInterceptor', libName));
provider += ',\n withInterceptor()';
}
provider += '\n)';
if (project.isStandalone) {
rules.push((0, ngx_schematics_utilities_1.addProviderToBootstrapApplication)(project.mainFilePath, provider, libName));
}
else {
rules.push((0, ngx_schematics_utilities_1.addImportToNgModule)(configFile, provider, libName));
}
}
else {
const error = `Could not find application ${project.isStandalone ? 'config' : 'module'} file (skipping)`;
rules.push((0, ngx_schematics_utilities_1.logError)(error));
}
return (0, schematics_1.chain)(rules);
};
exports.default = (options) => async () => {
const ngVersion = await (0, ngx_schematics_utilities_1.getAngularVersion)();
return (0, ngx_schematics_utilities_1.schematic)('@hug/ngx-g11n', [
(0, ngx_schematics_utilities_1.workspace)()
.spawn('ng', ['add', '@angular/localize', '--skip-confirmation'])
.rule(() => options.material
? (0, schematics_1.chain)([
(0, ngx_schematics_utilities_1.addPackageJsonDependencies)([
{
name: '@angular/material-date-fns-adapter',
version: `^${ngVersion.major}.0.0`
}
]),
(0, ngx_schematics_utilities_1.packageInstallTask)()
])
: (0, schematics_1.noop)())
.toRule(),
(0, ngx_schematics_utilities_1.application)(options.project)
.rule(({ project }) => {
if (!project.assetsPath) {
return (0, ngx_schematics_utilities_1.logError)('Deploying assets failed: no assets path found');
}
return (0, ngx_schematics_utilities_1.deployFiles)(options, './files', (0, node_path_1.join)(project.assetsPath, options.translationsPath));
})
.rule(context => customizeProject(context, options, ngVersion))
.toRule()
], options);
};
//# sourceMappingURL=index.js.map