resources-tsk
Advanced tools
Comparing version 2.1.5 to 2.5.0
@@ -125,3 +125,3 @@ # Changelog | ||
### Updated | ||
### Update | ||
@@ -138,4 +138,10 @@ - Packages was updated | ||
### Update | ||
- Packages was updated | ||
## [2.5.0] — 2024-07-30 | ||
### Update | ||
- Packages was updated | ||
- Added strong type for better inference and avoid errors |
@@ -0,0 +0,0 @@ const res_tsk = require("resources-tsk"); |
@@ -1,14 +0,24 @@ | ||
export declare class Resources { | ||
export declare class Resources<LTE extends string, LKD extends string, LMD extends { | ||
[K in LKD]: string; | ||
}, LT extends { | ||
[K in LTE]: LMD; | ||
}> { | ||
private defaultLanguage; | ||
private globalLanguage; | ||
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>>, localKeys: Record<string, string>): void; | ||
get(resourceName: string, language?: string): string; | ||
getWithParams(resourceName: string, params: Record<string, string>, language?: string): string; | ||
private values; | ||
keys: { | ||
[K in LKD]: LKD; | ||
}; | ||
constructor(locals: LT, localKeys: { | ||
[K in LKD]: LKD; | ||
}, defaultLanguage?: LTE); | ||
setDefaultLanguage(defaultLanguage: LTE): void; | ||
init(language: LTE): void; | ||
updateLocals(locals: LT, localKeys: { | ||
[K in LKD]: LKD; | ||
}): void; | ||
get(resourceName: LKD, language?: LTE): string; | ||
getWithParams(resourceName: LKD, params: Record<string, string>, language?: LTE): string; | ||
static replaceParams(text: string, params: Record<string, string>): string; | ||
private static applyPattern; | ||
} |
@@ -8,3 +8,4 @@ "use strict"; | ||
this.globalLanguage = null; | ||
this.values = null; | ||
this.values = {}; | ||
this.keys = {}; | ||
this.values = locals; | ||
@@ -56,3 +57,4 @@ this.keys = localKeys; | ||
get(resourceName, language = null) { | ||
if (language && this.values[language] && this.values[language][resourceName]) { | ||
var _a, _b; | ||
if (language && ((_b = (_a = this.values) === null || _a === void 0 ? void 0 : _a[language]) === null || _b === void 0 ? void 0 : _b[resourceName])) { | ||
return this.values[language][resourceName]; | ||
@@ -71,4 +73,5 @@ } | ||
getWithParams(resourceName, params, language = null) { | ||
var _a, _b; | ||
let resource = null; | ||
if (language && this.values[language] && this.values[language][resourceName]) { | ||
if (language && ((_b = (_a = this.values) === null || _a === void 0 ? void 0 : _a[language]) === null || _b === void 0 ? void 0 : _b[resourceName])) { | ||
resource = this.values[language][resourceName]; | ||
@@ -75,0 +78,0 @@ } |
{ | ||
"name": "resources-tsk", | ||
"version": "2.1.5", | ||
"version": "2.5.0", | ||
"description": "resource tool to use with or without NodeTskeleton template project", | ||
@@ -17,3 +17,3 @@ "repository": { | ||
], | ||
"author": "Vickodev<harvic3@ingenieros.com>", | ||
"author": "Vickodev<harvic3@proton.me>", | ||
"license": "MIT", | ||
@@ -36,6 +36,6 @@ "homepage": "https://github.com/harvic3/nodetskeleton-tools/blob/master/src/resources-tsk/README.md", | ||
"devDependencies": { | ||
"@types/node": "^20.14.9", | ||
"typescript": "^5.5.2" | ||
"@types/node": "^20.14.13", | ||
"typescript": "^5.5.4" | ||
}, | ||
"dependencies": {} | ||
} |
103
README.md
@@ -18,17 +18,25 @@ # Resources tool 🧰 | ||
```js | ||
// ./locals/resources/en.local.json | ||
// Resource file for english. | ||
{ | ||
"SOMETHING_WENT_WRONG": "Oh sorry, something went wrong with current action!", | ||
"SOME_PARAMETERS_ARE_MISSING": "Some parameters are missing: {{missingParams}}.", | ||
"YOUR_OWN_NEED": "You are the user {{name}}, your last name is {{lastName}} and you are {{age}} years old." | ||
} | ||
// ./locals/resources/es.local.json | ||
// Resource file for spanish. | ||
{ | ||
"SOMETHING_WENT_WRONG": "Oh lo sentimos, algo salió mal con esta acción!", | ||
"SOME_PARAMETERS_ARE_MISSING": "Faltan algunos parámetros: {{missingParams}}.", | ||
"YOUR_OWN_NEED": "Usted es {{name}}, su apellido es {{lastName}} y su edad es {{age}} años." | ||
} | ||
/* others as you needed */ | ||
// ./locals/resources/en.local.ts | ||
// Resource file for English. | ||
import { LocalMessageDictionary } from "./keys"; | ||
const enMessages: LocalMessageDictionary = { | ||
SOMETHING_WENT_WRONG: "Oh sorry, something went wrong with current action!", | ||
SOME_PARAMETERS_ARE_MISSING: "Some parameters are missing: {{missingParams}}.", | ||
YOUR_OWN_NEED: "You are the user {{name}}, your last name is {{lastName}} and you are {{age}} years old." | ||
/* others as you needed */ | ||
}; | ||
export default enMessages; | ||
// ./locals/resources/es.local.ts | ||
// Resource file for Spanish. | ||
import { LocalMessageDictionary } from "./keys"; | ||
const esMessages: LocalMessageDictionary = { | ||
SOMETHING_WENT_WRONG: "Oh lo sentimos, algo salió mal con esta acción!", | ||
SOME_PARAMETERS_ARE_MISSING: "Faltan algunos parámetros: {{missingParams}}.", | ||
YOUR_OWN_NEED: "Usted es {{name}}, su apellido es {{lastName}} y su edad es {{age}} años." | ||
/* others as you needed */ | ||
}; | ||
export default esMessages; | ||
``` | ||
@@ -43,8 +51,10 @@ | ||
```js | ||
// ./locals/resources/keys.json | ||
{ | ||
"SOMETHING_WENT_WRONG": "SOMETHING_WENT_WRONG", | ||
"SOME_PARAMETERS_ARE_MISSING": "SOME_PARAMETERS_ARE_MISSING", | ||
"YOUR_OWN_NEED": "YOUR_OWN_NEED" | ||
} | ||
// ./locals/resources/keys.ts | ||
export enum KeysDictionaryEnum { | ||
SOMETHING_WENT_WRONG: "SOMETHING_WENT_WRONG", | ||
SOME_PARAMETERS_ARE_MISSING: "SOME_PARAMETERS_ARE_MISSING", | ||
YOUR_OWN_NEED: "YOUR_OWN_NEED" | ||
}; | ||
// This type name is according to your like | ||
export type LocalMessageDictionary = { [key in keyof typeof KeysDictionaryEnum]: string }; | ||
``` | ||
@@ -55,27 +65,26 @@ So now we can set up our index file which we will use to manage our internationalization resources: | ||
// ./locals/index.ts | ||
import { KeysDictionaryEnum, LocalMessageDictionary } from "./resources/keys"; | ||
import esLocal from "./resources/es.local.ts"; | ||
import enLocal from "./resources/en.local.ts"; | ||
import { Resources } from "resources-tsk"; | ||
import * as esLocal from "./resources/es.local.json"; | ||
import * as enLocal from "./resources/en.local.json"; | ||
/* others as you needed */ | ||
import * as localKeys from "./resources/keys.json"; | ||
export enum LocaleTypeEnum { | ||
ES = "es_ES", | ||
EN = "en_US", | ||
} | ||
const locals = { | ||
es: esLocal, | ||
en: enLocal, | ||
/* others as you needed */ | ||
type LocaleType = { | ||
[K in LocaleTypeEnum]: LocalMessageDictionary; | ||
}; | ||
const defaultLanguage = "en"; | ||
const locals: LocaleType = { | ||
[LocaleTypeEnum.ES]: esLocal, | ||
[LocaleTypeEnum.EN]: enLocal, | ||
}; | ||
const resourceKeys = localKeys; | ||
const defaultLanguage = LocaleTypeEnum.ES; | ||
// The types are for strict control to improve inference and avoid errors | ||
const resources = new Resources<LocaleTypeEnum, KeysDictionaryEnum, LocalMessageDictionary, LocaleType>(locals, KeysDictionaryEnum, defaultLanguage); | ||
const resources = new Resources(locals, localKeys, defaultLanguage); | ||
/* | ||
This line is recommended so that intellisence can suggest existing keys, however the keys will also be available from the same resources object through the resourceKeys member (resources.resourceKeys.KEY_NAME). | ||
*/ | ||
export { resourceKeys }; | ||
export default resources | ||
export default resources; | ||
``` | ||
@@ -86,7 +95,7 @@ | ||
```ts | ||
import resources, { resourceKeys } from "../locals/index"; | ||
import resources from "../locals/index"; | ||
const simpleMessage = resources.get(resourceKeys.ITEM_PRODUCT_DOES_NOT_EXIST); | ||
const simpleMessage = resources.get(resources.keys.ITEM_PRODUCT_DOES_NOT_EXIST); | ||
const enrichedMessage = resources.getWithParams(resourceKeys.SOME_PARAMETERS_ARE_MISSING, { | ||
const enrichedMessage = resources.getWithParams(resources.keys.SOME_PARAMETERS_ARE_MISSING, { | ||
missingParams: keysNotFound.join(", "), | ||
@@ -96,3 +105,3 @@ }); | ||
// You can add enriched messages according to your own needs, for example: | ||
const yourEnrichedMessage = resources.getWithParams(resourceKeys.YOUR_OWN_NEED, { | ||
const yourEnrichedMessage = resources.getWithParams(resources.keys.YOUR_OWN_NEED, { | ||
name: firstName, lastName, age: userAge | ||
@@ -102,3 +111,3 @@ }); | ||
Output: | ||
You are the user Jhon, your last name is Doe and you are 24 yeard old. | ||
You are the user John, your last name is Doe and you are 24 yeard old. | ||
*/ | ||
@@ -108,3 +117,3 @@ ``` | ||
> The resource files can be local files in JSON format or you can get them from an external service. | ||
> The resource files can be local or you can get them from an external service. | ||
@@ -127,3 +136,3 @@ ## Important | ||
const enrichedMessage = resources.getWithParams( | ||
localKeys.NOT_VALID_EMAIL, | ||
resources.keys.NOT_VALID_EMAIL, | ||
{ email: user.email }, | ||
@@ -136,3 +145,3 @@ user.language, | ||
> The library provides a static function to replace keys into text like this: | ||
> The library provides a static function to replace keys into text as follow: | ||
@@ -139,0 +148,0 @@ ```ts |
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
16967
197
160