nestjs-i18n
Advanced tools
Comparing version 2.1.4 to 2.2.0
@@ -48,24 +48,28 @@ "use strict"; | ||
translate(lang, key, args = []) { | ||
try { | ||
let translation = this.translations[lang][key]; | ||
if (translation === undefined || translation === null) { | ||
const message = `translation "${key}" in "${lang}" doesn't exist.`; | ||
this.logger.error(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; | ||
} | ||
let translation = undefined; | ||
const keys = key.split('.'); | ||
if (this.translations[lang] !== undefined) { | ||
for (let i = 0; i < keys.length; i++) { | ||
translation = | ||
translation === undefined | ||
? this.translations[lang][keys[i]] | ||
: translation[keys[i]]; | ||
} | ||
if (args && args.length > 0) { | ||
translation = format(translation, ...(args || [])); | ||
} | ||
if (translation === undefined || translation === null) { | ||
const message = `translation "${key}" in "${lang}" doesn't exist.`; | ||
this.logger.error(message); | ||
if ((this.i18nOptions.fallbackLanguage !== null || | ||
this.i18nOptions.fallbackLanguage !== undefined) && | ||
lang !== this.i18nOptions.fallbackLanguage) { | ||
return this.translate(this.i18nOptions.fallbackLanguage, key, args); | ||
} | ||
return translation; | ||
else { | ||
return undefined; | ||
} | ||
} | ||
catch (e) { | ||
return e.message; | ||
if (args && args.length > 0) { | ||
translation = format(translation, ...(args || [])); | ||
} | ||
return translation; | ||
} | ||
@@ -72,0 +76,0 @@ }; |
{ | ||
"name": "nestjs-i18n", | ||
"version": "2.1.4", | ||
"version": "2.2.0", | ||
"description": "", | ||
@@ -21,6 +21,6 @@ "author": "Toon van Strijp", | ||
"devDependencies": { | ||
"@nestjs/common": "^6.2.4", | ||
"@nestjs/core": "^6.2.4", | ||
"@nestjs/common": "^6.3.2", | ||
"@nestjs/core": "^6.3.2", | ||
"@types/glob": "^7.1.1", | ||
"@types/node": "^12.0.4", | ||
"@types/node": "^12.0.10", | ||
"@types/string-format": "^2.0.0", | ||
@@ -30,2 +30,3 @@ "husky": "2.3.0", | ||
"prettier": "1.17.1", | ||
"reflect-metadata": "^0.1.13", | ||
"typescript": "3.5.1" | ||
@@ -32,0 +33,0 @@ }, |
@@ -32,3 +32,3 @@ ## Description | ||
### Translation file | ||
The format for the translation file should look like this: | ||
The format for the translation file could look like this: | ||
``` | ||
@@ -38,6 +38,14 @@ { | ||
"GOODBYE_MESSAGE": "Goodbye {username}", | ||
"USER_ADDED_PRODUCT": "{0.username} added {1.productName} to cart" | ||
"USER_ADDED_PRODUCT": "{0.username} added {1.productName} to cart", | ||
"SETUP": { | ||
"WELCOME": "Welcome {0.username}", | ||
"GOODBYE": "Goodbye {0.username}" | ||
} | ||
} | ||
``` | ||
String formatting is done by: [string-format](https://github.com/davidchambers/string-format) | ||
#### Constrains | ||
- Don't use `.` in your keys because you can use the `.` to target nested translations. | ||
### Translation module | ||
@@ -91,4 +99,5 @@ To use the translation service we first add the module. **The `I18nModule` has an `@Global()` attribute so you should only import it once**. | ||
this.i18n.translate('en', 'HELLO_MESSAGE', {id: 1, username: 'Toon'}); | ||
this.i18n.translate('en', 'SETUP.WELCOME', {id: 1, username: 'Toon'}); | ||
} | ||
} | ||
``` |
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
12462
209
101
10