vvlad1973-strings-resources
v1.1.3
Published
A class that ensures storage and ensuring access to string resources in applications, to solve the problems of internalization, with the support of the categorization of text elements in languages and roles
Downloads
312
Maintainers
Readme
vvlad1973-strings
A class that ensures storage and ensuring access to string resources in applications, to solve the problems of internalization, with the support of the categorization of text elements in languages and roles.
Installation
You can install the package via npm:
npm install vvlad1973-strings
Usage
Importing the Library
import Strings from 'vvlad1973-strings';
Creating instance of class and loading strings
You can load resources to the internal storage of the class by transferring the method .Loadresources() a link to a variable with an object containing resources, either an object itself or a file to a file in JSON format.
Создайте экземпляр класса:
const strings = new Strings({
/* You can set additional parameters, for example, a role in a default, etc. */
});
Then define your string resources. It can be an array of elements...
const resources = [
{ language: 'en', role: 'admin', key: 'HELLO', value: 'Hello admin', },
{ language: 'en', role: 'admin', key: 'GOODBYE', value: 'Hola admin', },
{ language: 'en', role: 'guest', key: 'HELLO', value: 'Hello guest', },
{ language: 'en', role: 'guest', key: 'GOODBYE', value: 'Hola guest', },
{ language: 'fr', role: 'admin', key: 'HELLO', value: 'Bonjour admin', }
{ language: 'fr', role: 'admin', key: 'GOODBYE', value: 'Au revoir admin', },
{ language: 'fr', role: 'guest', key: 'HELLO', value: 'Bonjour guest', },
{ language: 'fr', role: 'guest', key: 'GOODBYE', value: 'Au revoir guest', },
// Language and role may not be defined
{ language: '', role: '', key: 'HELLO', value: 'Hello!', },
{ language: '', role: '', key: 'GOODBYE', value: 'Bye!', },
];
...or array of structured objects...
const resources = [
{
en: [
{
role: 'admin',
strings: {
HELLO: 'Hello admin',
GOODBYE: 'Goodbye admin',
},
},
{
role: 'guest',
strings: [
{ key: 'HELLO', value: 'Hello guest' },
{ key: 'GOODBYE', value: 'Goodbye guest' },
],
},
],
},
{
// This is default values when language and role are not defined
'': [
{
HELLO: 'Hello!',
GOODBYE: 'Bye!',
},
],
},
];
...or it may be file in JSON format:
/* This is a content of the file some-file-with-resources.json:
[
{ language: 'en', role: 'admin', key: 'HELLO', value: 'Hello admin', },
{ language: 'en', role: 'admin', key: 'GOODBYE', value: 'Hola admin', },
{ language: 'en', role: 'guest', key: 'HELLO', value: 'Hello guest', },
{ language: 'en', role: 'guest', key: 'GOODBYE', value: 'Hola guest', },
{ language: 'fr', role: 'admin', key: 'HELLO', value: 'Bonjour admin', }
{ language: 'fr', role: 'admin', key: 'GOODBYE', value: 'Au revoir admin', },
{ language: 'fr', role: 'guest', key: 'HELLO', value: 'Bonjour guest', },
{ language: 'fr', role: 'guest', key: 'GOODBYE', value: 'Au revoir guest', },
// Language and role may not be defined
{ language: '', role: '', key: 'HELLO', value: 'Hello!', },
{ language: '', role: '', key: 'GOODBYE', value: 'Bye!', },
]
*/
const resources = './some-file-with-resources.json';
Now you can to load it in the object:
await strings.loadResources(resources);
Getting resources from the internal storage of the object
You can use .getItem() method:
const helloInEnglishForAdmin = strings.getItem('en', 'admin').HELLO; // helloInEnglishForAdmin = 'Hello admin'
Or you can set a default language and role, and it is easier to do it:
const strings = new Strings({defaultLanguage: 'en', defaultRole: 'admin'});
await strings.loadResources(resources);
const helloInEnglishForAdmin = strings.HELLO; // helloInEnglishForAdmin = 'Hello admin'
Definition of different contexts for one instance of the object
You can define different contexts for the object for легкого доступа к ресурсам разных ролей на разном языке
const strings = new Strings({defaultLanguage: 'en', defaultRole: 'admin'});
await strings.loadResources(resources);
const franceForGuest = strings.withDefaults('fr', 'guest');
console.log(strings.HELLO); // It will get out: 'Hello admin'
console.log(franceForGuest.HELLO); // It will get out: 'Bonjour guest'
Documentation
Full information about the package is available in the ./docs directory
License
This project is licensed under the MIT License with Commercial Use.
Author
Vladislav Vnukovskiy [email protected]