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

msg-localize

v1.0.11

Published

Module for localizing messages. Can be used in bots, probably for websites.

Downloads

23

Readme

msg-localize

Module for localizing messages. Can be used in bots, probably for websites.

NPM version Dependency Status License Downloads

Installation

npm install msg-localize --save

Usage

Create a folder where your localization files will be stored. For example the folder ./languages. Within this folder, create subfolders to localize various scripts for your application. For example, to localize a simple telegram bot, I created the folders ./languages/start and./languages​/weather for the scripts of the commands /start and/weather, respectively. Localization files in the format *.json or *.hjson must be placed in these folders. In each of them for English and Russian, I put files ./languages​/start/en_US.json,./languages​​/start/ru_RU.json, ./languages​/weather/en_US.json and./languages/weather/ru_RU.json. For example, I will give 2 of them:

./languages/start/en_US.json

{
    "welcome": [
        "${name}, greetings",
        "${name} hello",
        "${name}, nice to see you"
    ],
    "info": {
        "simple": "Here you can check the current weather. For a detailed description of the features, enter the command /help",
        "help": [
            "To find out the weather, use the /weather {city} command\n",
            "where {city} is the name of the settlement you are interested in\n",
            "You can also list several localities\n",
            "Here are some examples:\n",
            "/weather London\n",
            "/weather Washington acapulco\n"
        ]
    }
}

./languages/start/ru_RU.json

{
    "welcome": [
        "${name}, приветствую вас",
        "${name}, здравствуйте",
        "${name}, рад вас видеть"
    ],
    "info":{
        "simple": "Здесь вы можете узнать текущую погоду. Для подробного описания возможностей введите команду /help",
        "help": [
            "Для того чтобы узнать погоду необходимо воспользоваться командой /weather {city}\n",
            "где {city} это название интересующего вас населенного пункта\n",
            "Так-же вы можете перечислить несколько населенных пунктов\n",
            "Вот несколько примеров:\n",
            "/weather London\n",
            "/weather Washington acapulco\n"
        ]
    }
}
  • /if you wish, you can make translations into all languages ​​of the world, but unfortunately I'm not a polyglot/

Everything is now ready to use. Let's get started:

connect the localization module

const Messages = require('msg-localize');

// load localization files for the start script (commands /start and /help)
// and set the default localization. This will download the localization 
// files all present in the folder `./languages/start`
const messages = new Messages("./languages/start","en_US");

we will receive a welcome message

const text = messages.getMessage("welcome.*", {name: "Alexander"});
console.log(text);
    // for example, I print messages to the console.
    // will display one of the following messages (the choice of message is random):
    //    "Alexander, greetings"
    //    "Alexander hello"
    //    "Alexander, nice to see you"

then I want to acquaint the user with a brief description of the bot's capabilities

const text = messages.getMessage("info.simple");
console.log(text);
    // will output the following:
    //    "Here you can check the current weather. For a detailed description of the features, enter the command /help"

if the user's language differs from the default language, then we can specify it explicitly, taking the user's language from his data. You can do it like this: messages.getMessage("info.help", "ru_RU"); or so: messages.getMessage("welcome.*", {name: "Alexander"}, "ru_RU");

const text = messages.getMessage("info.simple", "ru_RU"); 
console.log(text);
    // will output the following:
    //    "Здесь вы можете узнать текущую погоду. Для подробного описания возможностей введите команду /help"

when the user enters the command /help, you need to send him a full description of the bot's capabilities.

const text = messages.getMessage("info.help");
console.log(text);
    // will output the following text (broken line by line):
    //    "To find out the weather, use the /weather {city} command
    //     where {city} is the name of the settlement you are interested in
    //     You can also list several localities
    //     Here are some examples:
    //     /weather London
    //     /weather Washington acapulco"

Exstended samples

The capabilities of this module are somewhat wider than shown above, then you can familiarize yourself with them

for example, let's take such a localization file

{
    "like": {
        "pets": "I really love animals, I have ${pet1} and ${pet2} at home",
        "randomPets": "I really love animals, I have ${pets.*} and ${pets.*} at home",
        "met": "I like you ${options}",
        "but": "you are too poor for me",
        "but`s": [
            "you are too poor for me",
            "i prefer to have sex with girls",
            "i am a married woman"
        ],
    },
    "about": "My name is ${sname} ${name}, I am a ${profession}.",
    "reqursion": "${insert}"

}

examples of many additional features provided by the module

passing substitution parameters

const text = messages.getMessage("like.pets",{pet1:"a kitten", pet2:"a puppy"});
console.log(text);
    // will output the following text:
    //   "I really love animals, I have a kitten and a puppy at home"

passing parameters and their random substitution

const text = messages.getMessage("like.randomPets",{pets: [ "a kitten", "a puppy", "a duck", "a parrot", "a canary", "a guinea pig", "a hamster", "a raccoon", "a skunk"]});
console.log(text);
    // will output the following text (the choice of pets is random):
    //   "I really love animals, I have a kitten and a puppy at home"
    // or
    //   "I really love animals, I have a parrot and a raccoon at home"
    // or
    //   "I really love animals, I have a skunk and a kitten at home"
    // etc

recursive parameter substitution

const text = messages.getMessage("like.met",{options:"but ${like.but}"});
console.log(text);
    // will output the following text:
    //   "I like you but you are too poor for me"

recursive substitution of random parameters

const text = messages.getMessage("like.met",{options:"but ${like.but`s.*}"});
console.log(text);
    // will output the following text (the choice of "but" is random)::
    //   "I like you but you are too poor for me"
    // or
    //   "I like you but i prefer to have sex with girls"
    // or
    //   "I like you but i am a married woman"

multiple parameter substitution

const text = messages.getMessage("about",{name:"Nelson", sname:"Horatio", profession:"naval admiral"});
console.log(text);
    // will output the following text:
    //   "My name is Horatio Nelson, I am a naval admiral."

also this module automatically tracks closed recursion (when the message replaces itself)

const text = messages.getMessage("reqursion",{insert:"${reqursion}"});
console.log(text);
    // will output the following text:
    //   "    !!!! FIND RECURSION FOR DEEPKEY: reqursion!!!!    "

... documentation in processed

Participation in development

https://github.com/lastuniverse/msg-localize/issues

License

MIT

NPM