1.0.17 • Published 11 months ago

vvlad1973-strings-resources v1.0.17

Weekly downloads
-
License
MIT with Commerci...
Repository
-
Last release
11 months ago

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 vvlad1973@gmail.com

1.0.17

11 months ago

1.0.16

11 months ago

1.0.15

11 months ago

1.0.14

11 months ago

1.0.13

11 months ago

1.0.12

11 months ago

1.0.11

11 months ago

1.0.10

11 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago