nestjs-i18n
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -1,1 +0,1 @@ | ||
export declare const I18N_PATH = "I18nPath"; | ||
export declare const I18N_OPTIONS = "I18nOptions"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.I18N_PATH = 'I18nPath'; | ||
exports.I18N_OPTIONS = 'I18nOptions'; |
import { DynamicModule } from '@nestjs/common'; | ||
export interface I18nOptions { | ||
path: string; | ||
fallbackLanguage?: string; | ||
} | ||
export declare class I18nModule { | ||
static forRoot(i18nPath: string): DynamicModule; | ||
static forRoot(options: I18nOptions): DynamicModule; | ||
} |
@@ -14,6 +14,6 @@ "use strict"; | ||
let I18nModule = I18nModule_1 = class I18nModule { | ||
static forRoot(i18nPath) { | ||
const i18nPathOption = { | ||
provide: i18n_constants_1.I18N_PATH, | ||
useValue: i18nPath, | ||
static forRoot(options) { | ||
const i18nOptions = { | ||
provide: i18n_constants_1.I18N_OPTIONS, | ||
useValue: options, | ||
}; | ||
@@ -23,4 +23,4 @@ return { | ||
providers: [ | ||
i18nPathOption, | ||
{ provide: i18n_service_1.I18nService, useValue: new i18n_service_1.I18nService(i18nPath) }, | ||
i18nOptions, | ||
i18n_service_1.I18nService, | ||
], | ||
@@ -27,0 +27,0 @@ exports: [i18n_service_1.I18nService], |
@@ -0,3 +1,4 @@ | ||
import { I18nOptions } from '../i18n.module'; | ||
export declare class I18nService { | ||
private readonly i18nPath; | ||
private readonly i18nOptions; | ||
translations: { | ||
@@ -9,3 +10,3 @@ [key: string]: { | ||
private logger; | ||
constructor(i18nPath: string); | ||
constructor(i18nOptions: I18nOptions); | ||
translate(lang: string, key: string, args?: Array<{ | ||
@@ -12,0 +13,0 @@ [k: string]: any; |
@@ -22,10 +22,10 @@ "use strict"; | ||
let I18nService = class I18nService { | ||
constructor(i18nPath) { | ||
this.i18nPath = i18nPath; | ||
constructor(i18nOptions) { | ||
this.i18nOptions = i18nOptions; | ||
this.translations = {}; | ||
this.logger = new common_1.Logger('I18nService'); | ||
if (!fs.existsSync(i18nPath)) { | ||
if (!fs.existsSync(this.i18nOptions.path)) { | ||
this.logger.error('i18n path does not exists'); | ||
} | ||
glob(i18nPath + '**/*.json', (err, files) => { | ||
glob(this.i18nOptions.path + '**/*.json', (err, files) => { | ||
files.map(file => { | ||
@@ -54,3 +54,8 @@ const key = path.parse(file).name; | ||
this.logger.error(message); | ||
return message; | ||
if (this.i18nOptions.fallbackLanguage !== null || this.i18nOptions.fallbackLanguage !== undefined && lang !== this.i18nOptions.fallbackLanguage) { | ||
return this.translate(this.i18nOptions.fallbackLanguage, key, args); | ||
} | ||
else { | ||
return message; | ||
} | ||
} | ||
@@ -69,5 +74,5 @@ if (args && args.length > 0) { | ||
common_1.Injectable(), | ||
__param(0, common_1.Inject(i18n_constants_1.I18N_PATH)), | ||
__metadata("design:paramtypes", [String]) | ||
__param(0, common_1.Inject(i18n_constants_1.I18N_OPTIONS)), | ||
__metadata("design:paramtypes", [Object]) | ||
], I18nService); | ||
exports.I18nService = I18nService; |
{ | ||
"name": "nestjs-i18n", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "", | ||
@@ -10,2 +10,4 @@ "author": "Toon van Strijp", | ||
}, | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"scripts": { | ||
@@ -12,0 +14,0 @@ "build": "rm -rf dist && tsc -p tsconfig.json", |
Sorry, the diff of this file is not supported yet
29683
155