Giter Site home page Giter Site logo

opdev1004 / op-i18n Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 56 KB

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

Home Page: https://www.npmjs.com/package/op-i18n

License: MIT License

JavaScript 100.00%
i18n locale localization localisation internationalization internationalisation language translation localize translate

op-i18n's Introduction

✨ OP i18n (op-i18n) ✨

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

📢 Notice

This repository is no longer maintained. Because I am going to work on totally new internationalization module with tot file format. I found JSON is not good when we handle a huge data. And we cannot obtain partial data as well. I found many delays from my softwares. I would not remove module so you can still use it however there won't be any update as I will create something better.

▶️ 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.

op-i18n's People

Contributors

opdev1004 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

op-i18n's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.