Resources tool 🧰
Resources tool (Locals) y part of the NodeTskeleton
template project.
NodeTskeleton
is a Clean Arquitecture
based template project
for NodeJs
using TypeScript
to implement with any web server framework
or even any user interface.
Go to NodeTskeleton
Resources
It is a basic internationalization
tool that will allow you to manage and administer the local messages of your application, even with enriched messages, for example:
Using Resources
The first thing to note is that your resource files must be in json or js format as shown in an example below:
{
"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 your age is {{age}}."
}
As a second step you must have the file that corresponds to the mapping of the keys containing your resource files as shown below:
{
"SOMETHING_WENT_WRONG": "SOMETHING_WENT_WRONG",
"SOME_PARAMETERS_ARE_MISSING": "SOME_PARAMETERS_ARE_MISSING",
"YOUR_OWN_NEED": "YOUR_OWN_NEED"
}
So now we can set up our index file which we will use to manage our internationalization resources:
import { Resources } from "@tskeleton/resources-tsk";
import * as esLocal from "./resources/es.local.json";
import * as enLocal from "./resources/en.local.json";
import * as localKeys from "./resources/keys.json";
const locals = {
es: esLocal,
en: enLocal,
};
const defaultLanguage = "en";
const resourceKeys = localKeys;
const resources = new Resources(locals, localKeys, defaultLanguage);
export { resourceKeys, Resources };
export default resources
Okay, so now you can use your resources where you need them, an example would be this:
import resources, { resourceKeys } from "../locals/index";
const simpleMessage = resources.Get(resourceKeys.ITEM_PRODUCT_DOES_NOT_EXIST);
const enrichedMessage = resources.GetWithParams(resourceKeys.SOME_PARAMETERS_ARE_MISSING, {
missingParams: keysNotFound.join(", "),
});
const yourEnrichedMessage = resources.GetWithParams(resourceKeys.YOUR_OWN_NEED, {
name: firstName, lastName, age: userAge
});
And you can add all the parameters you need with as many messages in your application as required.
Important
Don't forget to perform the language initialization for your resource manager in the localization middleware:
resources.Init(req.headers["accept-language"] || defaultLang);
Code of Conduct 👌
The Contributor Covenant Code of Conduct for this project is based on Covenant Contributor which you can find at the following link:
Warning 💀
Use this resource at your own risk.
-You are welcome to contribute to this project, dare to do so.
-If you are interested you can contact me by this means.