resources-tsk
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -110,1 +110,7 @@ # Changelog | ||
- Function for replace key params into text was added. | ||
## [2.0.1] — 2022-01-02 | ||
### Updated | ||
- Access control for resource was changed and some params was renamed |
export declare class Resources { | ||
private defaultLanguage; | ||
private globalLanguage; | ||
private locals; | ||
resourceKeys: Record<string, string>; | ||
values: Record<string, Record<string, string>>; | ||
keys: Record<string, string>; | ||
constructor(locals: Record<string, Record<string, string>>, localKeys: Record<string, string>, defaultLanguage?: string); | ||
setDefaultLanguage(defaultLanguage: string): void; | ||
init(language: string): void; | ||
updateLocals(locals: Record<string, Record<string, string>>): void; | ||
updateLocals(locals: Record<string, Record<string, string>>, localKeys: Record<string, string>): void; | ||
get(resourceName: string, language?: string): string; | ||
@@ -11,0 +11,0 @@ getWithParams(resourceName: string, params: Record<string, string>, language?: string): string; |
@@ -8,10 +8,10 @@ "use strict"; | ||
this.globalLanguage = null; | ||
this.locals = null; | ||
this.locals = locals; | ||
this.resourceKeys = localKeys; | ||
if (defaultLanguage && !this.locals[defaultLanguage]) { | ||
this.values = null; | ||
this.values = locals; | ||
this.keys = localKeys; | ||
if (defaultLanguage && !this.values[defaultLanguage]) { | ||
throw new Error(`Default language ${defaultLanguage} not found in local resources.`); | ||
} | ||
this.defaultLanguage = defaultLanguage; | ||
const keysToCheck = Object.keys(this.resourceKeys); | ||
const keysToCheck = Object.keys(this.keys); | ||
const langToCheck = Object.keys(locals); | ||
@@ -21,3 +21,3 @@ const resourcesNotFound = []; | ||
langToCheck.forEach((lang) => { | ||
if (!this.locals[lang][key]) { | ||
if (!this.values[lang][key]) { | ||
resourcesNotFound.push(`${lang}: ${key}`); | ||
@@ -33,3 +33,3 @@ } | ||
setDefaultLanguage(defaultLanguage) { | ||
if (!this.locals[defaultLanguage]) { | ||
if (!this.values[defaultLanguage]) { | ||
throw new Error(`Default language ${defaultLanguage} not found in local resources.`); | ||
@@ -44,3 +44,3 @@ } | ||
} | ||
if (!this.locals[language]) { | ||
if (!this.values[language]) { | ||
console.log(`Accept-Language "${language}" not found in locals resource.`); | ||
@@ -52,18 +52,19 @@ return; | ||
/* Update the current locals at any time at runtime */ | ||
updateLocals(locals) { | ||
updateLocals(locals, localKeys) { | ||
if (locals) { | ||
this.locals = locals; | ||
this.values = locals; | ||
this.keys = localKeys; | ||
} | ||
} | ||
get(resourceName, language = null) { | ||
if (language && this.locals[language] && this.locals[language][resourceName]) { | ||
return this.locals[language][resourceName]; | ||
if (language && this.values[language] && this.values[language][resourceName]) { | ||
return this.values[language][resourceName]; | ||
} | ||
if (this.locals[this.globalLanguage] && | ||
this.locals[this.globalLanguage][resourceName]) { | ||
return this.locals[this.globalLanguage][resourceName]; | ||
if (this.values[this.globalLanguage] && | ||
this.values[this.globalLanguage][resourceName]) { | ||
return this.values[this.globalLanguage][resourceName]; | ||
} | ||
if (this.locals[this.defaultLanguage] && | ||
this.locals[this.defaultLanguage][resourceName]) { | ||
return this.locals[this.defaultLanguage][resourceName]; | ||
if (this.values[this.defaultLanguage] && | ||
this.values[this.defaultLanguage][resourceName]) { | ||
return this.values[this.defaultLanguage][resourceName]; | ||
} | ||
@@ -74,12 +75,12 @@ throw new Error(`Resource ${resourceName} not found in any local resource.`); | ||
let resource = null; | ||
if (language && this.locals[language] && this.locals[language][resourceName]) { | ||
resource = this.locals[language][resourceName]; | ||
if (language && this.values[language] && this.values[language][resourceName]) { | ||
resource = this.values[language][resourceName]; | ||
} | ||
else if (this.locals[this.globalLanguage] && | ||
this.locals[this.globalLanguage][resourceName]) { | ||
resource = this.locals[this.globalLanguage][resourceName]; | ||
else if (this.values[this.globalLanguage] && | ||
this.values[this.globalLanguage][resourceName]) { | ||
resource = this.values[this.globalLanguage][resourceName]; | ||
} | ||
else if (this.locals[this.defaultLanguage] && | ||
this.locals[this.defaultLanguage][resourceName]) { | ||
resource = this.locals[this.defaultLanguage][resourceName]; | ||
else if (this.values[this.defaultLanguage] && | ||
this.values[this.defaultLanguage][resourceName]) { | ||
resource = this.values[this.defaultLanguage][resourceName]; | ||
} | ||
@@ -97,4 +98,4 @@ if (!resource) { | ||
static applyPattern(text, params) { | ||
const keys = Object.keys(params); | ||
keys.forEach((key) => { | ||
const objectKeys = Object.keys(params); | ||
objectKeys.forEach((key) => { | ||
const pattern = `({{)${key}(}})`; | ||
@@ -101,0 +102,0 @@ const regex = RegExp(pattern); |
{ | ||
"name": "resources-tsk", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "resource tool for use with or without NodeTskeleton template project", | ||
@@ -36,5 +36,5 @@ "repository": { | ||
"@types/node": "^14.14.31", | ||
"typescript": "^4.2.2" | ||
"typescript": "^4.6.3" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -126,2 +126,20 @@ # Resources tool 🧰 | ||
## Replace function | ||
> The library provides a static function to replace keys into text like this: | ||
```ts | ||
import { Resources } from "resources-tsk"; | ||
const text = "This is a text with {{name}} and {{lastName}}."; | ||
const params = { | ||
name: "Carl", | ||
lastName: "Sagan", | ||
}; | ||
const textReady = Resources.replaceParams(text, params); | ||
console.log(textReady); | ||
// This is a text with Carl and Sagan. | ||
``` | ||
## RunKit demo | ||
@@ -128,0 +146,0 @@ |
16157
184
151