Resources
It is a internationalization tool that will allow you to manage and administer the local messages of your application, even with enriched messages.
How To Use
We should create a Resource class with your values:
const keys = {
SOMETHING_WAS_WRONG: "SOMETHING_WAS_WRONG",
MISSING_ATTRIBUTES: "MISSING_ATTRIBUTES",
ONLY_ENGLISH: "ONLY_ENGLISH"
}
enum LocaleType {
EN = "en",
PT_BR = "pt-br"
}
const locals = {
[LocaleType.EN]: {
[keys.SOMETHING_WAS_WRONG]: "Unknown Error",
[keys.MISSING_ATTRIBUTES]: "Missing Attributes: %$s",
[keys.ONLY_ENGLISH]: "This resource is not defined in another language"
},
[LocaleType.PT_BR]: {
[keys.SOMETHING_WAS_WRONG]: "Erro desconhecido",
[keys.MISSING_ATTRIBUTES]: "Atributos que falta: %$s. Feito por %$s"
}
}
const resources = new Resources(locals, Object.values(keys), LocaleType.EN)
For use it in any user case, you should do something like:
console.log(resources.get(keys.SOMETHING_WAS_WRONG)) // Unknown Error
console.log(resources.getWithParams(keys.MISSING_ATTRIBUTES, "name: string, age: number", "Igor Hakcolt")) // Missing Attributes: name: string, age: number
console.log(resources.get(keys.ONLY_ENGLISH)) // This resource is not defined in another language
resources.language = LocaleType.PT_BR
console.log(resources.get(keys.SOMETHING_WAS_WRONG)) // Erro desconhecido
console.log(resources.getWithParams(keys.MISSING_ATTRIBUTES, "name: string, age: number", "Igor Hakcolt")) // Atributos que faltam: name: string, age: number por Igor Hakcolt
console.log(resources.get(keys.ONLY_ENGLISH)) // This resource is not defined in another language