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

simplin.js

v1.1.0-n

Published

A simple, tiny Node.js module providing a basic multi-language system

Downloads

4

Readme

simplin.js

A simple, tiny Node.js module providing a basic multi-language system — dependency-free.

Aim of the project

A primary goal of this project is to provide bare-basic solution for multi-language feature (app localization), mainly for purposes within smaller Node.js projects. Conventional libraries might not be easy to learn and use, or they are just way too complex for a little hobby projects.

Installation

npm i simplin.js

How to start

  1. Prepare en-US.json, that will serve both as default source of strings for your app, and as a template for the translators of your project.

    {
     "MSG_HELLO_WORLD": "Hello, world!",
     "MSG_NEW_MESSAGE": {
                          "zero": "You have no new message.",
                          "one": "You have %i new message.",
                          "other": "You have {0} new messages."
                        }
    }

    NOTICE: %i, %s, %0, {0} are just placeholders to be filled-up by your app dynamically and can be combined freely. However, if you are intending to pass multiple variables to a single string, you should stick with form %0%9 or {0}{9}. Translator can move them freely within the sentence, e.g. {2} alpha beta {0} gamma {1} delta, i.e. they don't have to be in ascending order.

    ZERO: Zero-forms are not used in some languages. Input argument 0 normally returns other string, unless you explicitely define special 0-case, on top of standard.

  2. Where you are initializing your application, add

    const { StringProvider } = require("simplin.js");
    ...
    class MyApp {
     constructor() {
         this.locales = StringProvider
                        .load(path.resolve(__dirname, "../resources/locales")) //__dirname is pre-defined by your environment, similar to "pwd"
                        .setDefault("cs"); // you can omit setDefault(), in that case it's defaulted to 'en-US'
     }
    }
  3. Anywhere in your app simply

    const { _ } = require("simplin.js");
    ...
    console.log(_("MSG_HELLO_WORLD"));
    console.log(_("MSG_NEW_MESSAGE", 3)); // 3 new messages

    Or, alternatively (without require() by referencing the previously initialized object)

    console.log(MyApp.locales.get("MSG_HELLO_WORLD");
  4. Let translators to translate your project. Put all files to the same folder as en-US.json, they will be loaded automatically.

Getting a string in every language

You may need to get a string for each locale within a single object.

const { _A_ } = require("simplin.js");
...
const [localizedStrings, defaultLocaleString] = _A_("MSG_HELLO_WORLD");
////////////////////////////////////
console.log(localizedStrings)
=> {
     cs: "Ahoj, světe!",
     fr: "Bonjour le monde!"
   }
////////////////////////////////////
console.log(defaultLocaleString)
=> "Hello world!"

In case you'd need to concatenate the default language

localizedStrings[StringProvider.defaultLocale] = defaultLocaleString;