npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vvlad1973-strings-resources

v1.0.17

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

78

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]