Skip to content
/ i18n Public

Tree shakable dictionary for eCommerce JS apps

License

Notifications You must be signed in to change notification settings

ecomplus/i18n

Repository files navigation

i18n

Publish npm version License MIT

🇧🇷 🇺🇸

Tree shakable dictionary focused on eCommerce JS applications.

i18n ~ i19 ~ Internationalization

Getting started

npm i --save @ecomplus/i18n

Usage

import { i19hello, i19visitor } from '@ecomplus/i18n'
console.log(`${i19hello.en_us} ${i19visitor.pt_br}`)
// Hello Visitor
console.log(`${i19hello.pt_br} ${i19visitor.pt_br}`)
// Olá Visitante

We recommend using it with ecomUtils.i18n:

import { i18n } from '@ecomplus/utils'
import { i19hello, i19visitor } from '@ecomplus/i18n'
console.log(`${i18n(i19hello)} ${i18n(i19visitor)}`)
// Hello Visitor

Change current language with ecomUtils._config:

import { _config, i18n } from '@ecomplus/utils'
import { i19hello, i19visitor } from '@ecomplus/i18n'
_config.set('lang', 'pt_br')
console.log(`${i18n(i19hello)} ${i18n(i19visitor)}`)
// Olá Visitante

Import entire dictionary object

It'll output large size bundle, not good for frontend apps.

import dictionary from '@ecomplus/i18n'
console.log(`${dictionary.i19hello.en_us} ${dictionary.i19visitor.en_us}`)
// Hello Visitor

Webpack alias

You can import only one language variation using Webpack resolve.alias as following:

// webpack.config.js
module.exports = {
  //...
  resolve: {
    alias: {
      '@ecomplus/i18n$': `@ecomplus/i18n/src/${lang}/`
    }
  }
}

By this way you'll import only strings instead of objects:

import { i19hello, i19visitor } from '@ecomplus/i18n'
console.log(`${i19hello} ${i19visitor}`)
// Hello Visitor

You can still use ecomUtils.i18n the same way:

import { i18n } from '@ecomplus/utils'
import { i19hello, i19visitor } from '@ecomplus/i18n'
console.log(`${i18n(i19hello)} ${i18n(i19visitor)}`)
// Hello Visitor

Conventions

  1. String values always with uppercased first letter (eg.: 'Hello');
  2. Variable (const) names always in English;
  3. Prefix i19 for all variable names;
  4. String variables must be camelCased (eg.: i19helloWorld);
  5. Object (enums) variables must be PascalCased (eg.: i19OrderStatus);
  6. All language options must have same variables;
  7. For long messages: variable name should be suffixed with Msg;
  8. For questions: variable name should be suffixed with Qn;

Code style

  1. Exported constants must be alphabetically ordered;
  2. Additional line break before objects (not for strings);