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

op-i18n

v1.0.11

Published

Easy Node.js i18n with json locale file storage. No fancy grammar.

Downloads

5

Readme

✨ OP i18n (op-i18n) ✨

Easy Node.js i18n with json locale file storage. No fancy grammar.

▶️ install

npm i op-i18n

👩‍🎓 Tutorial

Most of Case

  • en_US.json
{
    "name": "Victor"
}
  • ko_KR.json
{
    "name": "빅터"
}
  • index.js
const path = require('path');
const I18n = require('op-i18n');

// Note that '_' is used instead of '-' for key in options below.
// eg. ko-KR:{} (X), ko_KR:{} (O)
const options =
{
  locales:
  {
      ko_KR:
      {
        name: '한국어',
        iso: 'ko-KR',
        file: 'ko_KR.json'
      },
      en_US:
      {
        name: 'English',
        iso: 'en-US',
        file: 'en_US.json'
      }
  },
  directory: path.resolve(__dirname, 'locales/'),
  defaultLocale: 'en_US',
}

const i18n = new I18n(options);

// Change locale to Korean
i18n.updateDefaultLocale("ko_KR");
console.log(i18n.$t());
// output: { name: '빅터' }
console.log(i18n.$t("name"));
// output: 빅터
console.log(i18n.$t("name", "en_US"));
// output: Victor

Options

Requires locales, directory, defaultLocale in options. Note that _ is used instead of - for key in options.

js key | O/X -------|----- ko_KR | O 😇 ko-KR | X ☠️

const path = require('path');
const I18n = require('op-i18n');

// Note that '_' is used instead of '-' for key in options below.
// eg. ko-KR:{} (X), ko_KR:{} (O)
const options =
{
  locales:
  {
      ko_KR:
      {
        name: '한국어',
        iso: 'ko-KR',
        file: 'ko_KR.json'
      },
      en_US:
      {
        name: 'English',
        iso: 'en-US',
        file: 'en_US.json'
      }
  },
  directory: path.resolve(__dirname, 'locales/'),
  defaultLocale: 'en_US',
}

const i18n = new I18n(options);

Advanced $t()

Regular

You can use . notation just like how you get value out of js object:

  • ko_KR.json
{
    "name":
    {
        "first": "빅터"
        "last": "박"
    }
}
  • en_US.json
{
    "name":
    {
        "first": "Victor"
        "last": "Park"
    }
}
  • index.js
const path = require('path');
const I18n = require('op-i18n');

// Note that '_' is used instead of '-' for key in options below.
// eg. ko-KR:{} (X), ko_KR:{} (O)
const options =
{
  locales:
  {
      ko_KR:
      {
        name: '한국어',
        iso: 'ko-KR',
        file: 'ko_KR.json'
      },
      en_US:
      {
        name: 'English',
        iso: 'en-US',
        file: 'en_US.json'
      }
  },
  directory: path.resolve(__dirname, 'locales/'),
  defaultLocale: 'en_US',
}

const i18n = new I18n(options);

console.log(i18n.$t("name.first"));
//output: Victor

For getting value from specified locale:

console.log(i18n.$t("name.first", "ko_KR"));
//output: 빅터

You can get whole js object with empty key "".

console.log(i18n.$t(""));
//output: { name: { first: "Victor", last: "Park" } }
console.log(i18n.$t("", "ko_KR"));
//output: { name: { first: "빅터", last: "박" } }

Using Object

You can use object to get translation as well. { locale: "", key: "" }.

console.log(i18n.$t( { key:"name.first" } ));
//output: Victor
console.log(i18n.$t( { locale: "ko_KR", key:"name.first" } ));
//output: 빅터

📖 op-i18n Document

.$t()

return default translation in js object. Uses .getDefaultTranslation()

.$t(string:key)

return value according to key from default translation in js object

.$t(object:{ key: string:key, locale: string:locale })

return value according to key and locale in object.

.$t(string:key, string:locale key)

return value according to key from translation of specified locale in js object

.updateDefaultLocale(string:locale key)

update default locale and default translation according to specified locale

.updateDefaultLocaleByName(string:name)

update default locale and default translation according to specified name

.getDefaultTranslation()

return default translation in js object

.getNames()

return list of locale name

.getISOs()

return list of locale ISO

.getDefaultName()

return name of default locale in js object. eg) 한국어

.getDefaultISO()

return ISO of default locale in js object. eg) ko-KR

👨‍💻 Author

Victor Chanil Park

💯 License

MIT, See LICENSE.