nestjs-i18n
Advanced tools
Comparing version 9.0.5 to 9.0.6
@@ -22,4 +22,5 @@ export * from './i18n.module'; | ||
export * from './loaders/i18n.json.loader'; | ||
export * from './loaders/i18n.yaml.loader'; | ||
export * from './interceptors/i18n-language.interceptor'; | ||
export * from './filters/i18n-validation-exception.filter'; | ||
export { getI18nContextFromRequest, getI18nServiceFromGraphQLContext, getI18nServiceFromRpcContext, getI18nContextFromArgumentsHost, i18nValidationErrorFactory, i18nValidationMessage, } from './utils/util'; |
@@ -45,2 +45,3 @@ "use strict"; | ||
__exportStar(require("./loaders/i18n.json.loader"), exports); | ||
__exportStar(require("./loaders/i18n.yaml.loader"), exports); | ||
__exportStar(require("./interceptors/i18n-language.interceptor"), exports); | ||
@@ -47,0 +48,0 @@ __exportStar(require("./filters/i18n-validation-exception.filter"), exports); |
@@ -1,21 +0,5 @@ | ||
import { I18nLoader } from './i18n.loader'; | ||
import { OnModuleDestroy } from '@nestjs/common'; | ||
import { I18nTranslation } from '../interfaces/i18n-translation.interface'; | ||
import { Observable } from 'rxjs'; | ||
export interface I18nJsonLoaderOptions { | ||
path: string; | ||
filePattern?: string; | ||
watch?: boolean; | ||
import { I18nAbstractLoader, I18nAbstractLoaderOptions } from './i18n.abstract.loader'; | ||
export declare class I18nJsonLoader extends I18nAbstractLoader { | ||
getDefaultOptions(): Partial<I18nAbstractLoaderOptions>; | ||
formatData(data: any): any; | ||
} | ||
export declare class I18nJsonLoader extends I18nLoader implements OnModuleDestroy { | ||
private options; | ||
private watcher?; | ||
private events; | ||
constructor(options: I18nJsonLoaderOptions); | ||
onModuleDestroy(): Promise<void>; | ||
languages(): Promise<string[] | Observable<string[]>>; | ||
load(): Promise<I18nTranslation | Observable<I18nTranslation>>; | ||
private parseTranslations; | ||
private parseLanguages; | ||
private sanitizeOptions; | ||
} |
"use strict"; | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
var __param = (this && this.__param) || function (paramIndex, decorator) { | ||
return function (target, key) { decorator(target, key, paramIndex); } | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.I18nJsonLoader = void 0; | ||
const i18n_loader_1 = require("./i18n.loader"); | ||
const i18n_constants_1 = require("../i18n.constants"); | ||
const common_1 = require("@nestjs/common"); | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const file_1 = require("../utils/file"); | ||
const util_1 = require("util"); | ||
const rxjs_1 = require("rxjs"); | ||
const chokidar = require("chokidar"); | ||
const operators_1 = require("rxjs/operators"); | ||
const readFile = (0, util_1.promisify)(fs.readFile); | ||
const exists = (0, util_1.promisify)(fs.exists); | ||
const defaultOptions = { | ||
filePattern: '*.json', | ||
watch: false, | ||
}; | ||
let I18nJsonLoader = class I18nJsonLoader extends i18n_loader_1.I18nLoader { | ||
constructor(options) { | ||
super(); | ||
this.options = options; | ||
this.events = new rxjs_1.Subject(); | ||
this.options = this.sanitizeOptions(options); | ||
if (this.options.watch) { | ||
this.watcher = chokidar | ||
.watch(this.options.path, { ignoreInitial: true }) | ||
.on('all', (event) => { | ||
this.events.next(event); | ||
}); | ||
} | ||
const i18n_abstract_loader_1 = require("./i18n.abstract.loader"); | ||
class I18nJsonLoader extends i18n_abstract_loader_1.I18nAbstractLoader { | ||
getDefaultOptions() { | ||
return { | ||
filePattern: '*.json', | ||
watch: false, | ||
}; | ||
} | ||
async onModuleDestroy() { | ||
if (this.watcher) { | ||
await this.watcher.close(); | ||
} | ||
formatData(data) { | ||
return JSON.parse(data); | ||
} | ||
async languages() { | ||
if (this.options.watch) { | ||
return (0, rxjs_1.merge)((0, rxjs_1.from)(this.parseLanguages()), this.events.pipe((0, operators_1.switchMap)(() => this.parseLanguages()))); | ||
} | ||
return this.parseLanguages(); | ||
} | ||
async load() { | ||
if (this.options.watch) { | ||
return (0, rxjs_1.merge)((0, rxjs_1.from)(this.parseTranslations()), this.events.pipe((0, operators_1.switchMap)(() => this.parseTranslations()))); | ||
} | ||
return this.parseTranslations(); | ||
} | ||
async parseTranslations() { | ||
const i18nPath = path.normalize(this.options.path + path.sep); | ||
const translations = {}; | ||
if (!(await exists(i18nPath))) { | ||
throw new Error(`i18n path (${i18nPath}) cannot be found`); | ||
} | ||
if (!this.options.filePattern.match(/\*\.[A-z]+/)) { | ||
throw new Error(`filePattern should be formatted like: *.json, *.txt, *.custom etc`); | ||
} | ||
const languages = await this.parseLanguages(); | ||
const pattern = new RegExp('.' + this.options.filePattern.replace('.', '.')); | ||
const files = await [ | ||
...languages.map((l) => path.join(i18nPath, l)), | ||
i18nPath, | ||
].reduce(async (f, p) => { | ||
(await f).push(...(await (0, file_1.getFiles)(p, pattern))); | ||
return f; | ||
}, Promise.resolve([])); | ||
for (const file of files) { | ||
let global = false; | ||
const key = path | ||
.dirname(path.relative(i18nPath, file)) | ||
.split(path.sep)[0]; | ||
if (key === '.') { | ||
global = true; | ||
} | ||
const data = JSON.parse(await readFile(file, 'utf8')); | ||
const prefix = path.basename(file).split('.')[0]; | ||
for (const property of Object.keys(data)) { | ||
[...(global ? languages : [key])].forEach((lang) => { | ||
translations[lang] = translations[lang] ? translations[lang] : {}; | ||
if (global) { | ||
translations[lang][property] = data[property]; | ||
} | ||
else { | ||
translations[lang][prefix] = translations[lang][prefix] | ||
? translations[lang][prefix] | ||
: {}; | ||
translations[lang][prefix][property] = data[property]; | ||
} | ||
}); | ||
} | ||
} | ||
return translations; | ||
} | ||
async parseLanguages() { | ||
const i18nPath = path.normalize(this.options.path + path.sep); | ||
return (await (0, file_1.getDirectories)(i18nPath)).map((dir) => path.relative(i18nPath, dir)); | ||
} | ||
sanitizeOptions(options) { | ||
options = Object.assign(Object.assign({}, defaultOptions), options); | ||
options.path = path.normalize(options.path + path.sep); | ||
if (!options.filePattern.startsWith('*.')) { | ||
options.filePattern = '*.' + options.filePattern; | ||
} | ||
return options; | ||
} | ||
}; | ||
I18nJsonLoader = __decorate([ | ||
__param(0, (0, common_1.Inject)(i18n_constants_1.I18N_LOADER_OPTIONS)), | ||
__metadata("design:paramtypes", [Object]) | ||
], I18nJsonLoader); | ||
} | ||
exports.I18nJsonLoader = I18nJsonLoader; | ||
//# sourceMappingURL=i18n.json.loader.js.map |
@@ -89,3 +89,3 @@ "use strict"; | ||
translateObject(key, translations, options, rootTranslations) { | ||
var _a, _b; | ||
var _a, _b, _c; | ||
const keys = key.split('.'); | ||
@@ -129,3 +129,3 @@ const [firstKey] = keys; | ||
for (const nestedTranslation of nestedTranslations) { | ||
const result = this.translateObject(nestedTranslation.key, rootTranslations, Object.assign(Object.assign({}, options), { args: Object.assign({ parent: options.args }, nestedTranslation.args) })); | ||
const result = (_c = this.translateObject(nestedTranslation.key, rootTranslations, Object.assign(Object.assign({}, options), { args: Object.assign({ parent: options.args }, nestedTranslation.args) }))) !== null && _c !== void 0 ? _c : ''; | ||
translation = | ||
@@ -132,0 +132,0 @@ translation.substring(0, nestedTranslation.index - offset) + |
{ | ||
"name": "nestjs-i18n", | ||
"version": "9.0.5", | ||
"version": "9.0.6", | ||
"homepage": "https://nestjs-i18n.com", | ||
@@ -91,2 +91,3 @@ "description": "The i18n module for Nest.", | ||
"cookie": "^0.4.1", | ||
"js-yaml": "^4.1.0", | ||
"string-format": "^2.0.0" | ||
@@ -93,0 +94,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
641218
200
5498
9
+ Addedjs-yaml@^4.1.0
+ Addedargparse@2.0.1(transitive)
+ Addedjs-yaml@4.1.0(transitive)