resources-tsk
Advanced tools
Comparing version 1.0.6 to 1.0.7
# Changelog | ||
## [1.0.0] - 2020-08-09 | ||
## [1.0.0] — 2020-08-09 | ||
- Release first version | ||
## [1.0.1] - 2020-08-09 | ||
## [1.0.1] — 2020-08-09 | ||
### Removed | ||
- Unnecessary files | ||
## [1.0.3] - 2020-08-09 | ||
## [1.0.3] — 2020-08-09 | ||
### Added | ||
- Types and root file. | ||
## [1.0.4] - 2020-08-10 | ||
## [1.0.4] — 2020-08-10 | ||
### Updated | ||
- Documentation. | ||
## [1.0.5] - 2020-08-11 | ||
## [1.0.5] — 2020-08-11 | ||
### Updated | ||
- Documentation. | ||
## [1.0.6] - 2020-08-13 | ||
## [1.0.6] — 2020-08-13 | ||
### Added | ||
- Runkit example file. | ||
## [1.0.7] — 2020-08-16 | ||
### Updated | ||
- Pure function concept was supported. |
export declare class Resources { | ||
private defaultLanguage; | ||
private language; | ||
private globalLanguage; | ||
private locals; | ||
@@ -16,6 +16,6 @@ resourceKeys: { | ||
Init(language: string): void; | ||
Get(resourceName: string): string; | ||
Get(resourceName: string, language?: string): string; | ||
GetWithParams(resourceName: string, params: { | ||
[key: string]: string; | ||
}): string; | ||
}, language?: string): string; | ||
} |
@@ -8,3 +8,3 @@ "use strict"; | ||
this.defaultLanguage = null; | ||
this.language = null; | ||
this.globalLanguage = null; | ||
this.locals = null; | ||
@@ -36,8 +36,13 @@ this.locals = locals; | ||
} | ||
this.language = language; | ||
this.globalLanguage = language; | ||
}; | ||
Resources.prototype.Get = function (resourceName) { | ||
if (this.locals[this.language] && this.locals[this.language][resourceName]) { | ||
return this.locals[this.language][resourceName]; | ||
Resources.prototype.Get = function (resourceName, language) { | ||
if (language === void 0) { language = null; } | ||
if (language && this.locals[language] && this.locals[language][resourceName]) { | ||
return this.locals[language][resourceName]; | ||
} | ||
if (this.locals[this.globalLanguage] && | ||
this.locals[this.globalLanguage][resourceName]) { | ||
return this.locals[this.globalLanguage][resourceName]; | ||
} | ||
if (this.locals[this.defaultLanguage] && | ||
@@ -49,7 +54,12 @@ this.locals[this.defaultLanguage][resourceName]) { | ||
}; | ||
Resources.prototype.GetWithParams = function (resourceName, params) { | ||
var resource; | ||
if (this.locals[this.language] && this.locals[this.language][resourceName]) { | ||
resource = this.locals[this.language][resourceName]; | ||
Resources.prototype.GetWithParams = function (resourceName, params, language) { | ||
if (language === void 0) { language = null; } | ||
var resource = null; | ||
if (language && this.locals[language] && this.locals[language][resourceName]) { | ||
resource = this.locals[language][resourceName]; | ||
} | ||
else if (this.locals[this.globalLanguage] && | ||
this.locals[this.globalLanguage][resourceName]) { | ||
resource = this.locals[this.globalLanguage][resourceName]; | ||
} | ||
else if (this.locals[this.defaultLanguage] && | ||
@@ -56,0 +66,0 @@ this.locals[this.defaultLanguage][resourceName]) { |
{ | ||
"name": "resources-tsk", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "tool for use with or without NodeTskeleton template project", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -106,3 +106,3 @@ # Resources tool 🧰 | ||
> Don't forget to perform the language initialization for your resource manager in the localization middleware: | ||
> Don't forget to perform the language initialization for your resource manager in the localization middleware as following: | ||
@@ -115,5 +115,16 @@ ```ts | ||
``` | ||
But if you prefer, applying the concept of `pure function`, you have the option to pass the `optional language parameter` in the functions to get the parameters as shown below: | ||
```ts | ||
const message = resources.Get(localKeys.SOMETHING_WENT_WRONG, user.language); | ||
// Or | ||
const enrichedMessage = resources.GetWithParams( | ||
localKeys.NOT_VALID_EMAIL, | ||
{ email: user.email }, | ||
user.language, | ||
); | ||
``` | ||
## Warning 💀 | ||
> Use this resource at your own risk. |
@@ -47,11 +47,40 @@ var res_tsk = require("resources-tsk"); | ||
// In any Use Case | ||
var Person = (function () { | ||
function Person(name, lastName, age, language) { | ||
this.name = name; | ||
this.lastName = lastName; | ||
this.age = age; | ||
this.language = language; | ||
} | ||
return Person; | ||
})(); | ||
var user = new Person("Carl", "Sagan", new Date().getFullYear() - 1934, "es"); | ||
const simpleMessage = resources.Get(resourceKeys.SOMETHING_WENT_WRONG); | ||
const enrichedMessage = resources.GetWithParams(resourceKeys.YOUR_OWN_NEED, { | ||
name: "Carl", | ||
lastName: "Sagan", | ||
age: new Date().getFullYear() - 1934, | ||
name: user.name, | ||
lastName: user.lastName, | ||
age: user.age, | ||
}); | ||
console.log("Simple: ", simpleMessage); | ||
console.log("Enriched: ", enrichedMessage); | ||
console.log("Simple with global language:", simpleMessage); | ||
console.log("Enriched with global language:", enrichedMessage); | ||
// Or with optional language param | ||
const simpleMessage2 = resources.Get(resourceKeys.SOMETHING_WENT_WRONG, user.language); | ||
const enrichedMessage2 = resources.GetWithParams( | ||
resourceKeys.YOUR_OWN_NEED, | ||
{ | ||
name: user.name, | ||
lastName: user.lastName, | ||
age: user.age, | ||
}, | ||
user.language, | ||
); | ||
console.log("Simple with language as param:", simpleMessage2); | ||
console.log("Enriched with language as param:", enrichedMessage2); |
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
12417
170
129