GithubHelp home page GithubHelp logo

gitter-badger / sz-i18n Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stefanyohansson/sz-i18n

0.0 1.0 0.0 39 KB

Internationalization library. ️ Easy and Fast. :zap:

Makefile 0.42% JavaScript 99.58%

sz-i18n's Introduction

sz-i18n

Build Status npm version

NPM

Internationalization library. ️ Easy and Fast. ⚡️

Highly inspired by: https://github.com/roddeh/i18njs

Install

npm install sz-i18n

Test

npm test

Base json

You need to write a json (base source) with values and contexts, this structure is wrote in your default system language.

// src-lang/en.json
  {
    "values": {
      "Cancel": "Cancel",
      "%n comments": [
        [0, 0, "%n comments"],
        [1, 1, "%n comment"],
        [2, null, "%n comments"]
      ]
    },
    "contexts": [
      {
        "matches": {
          "gender": "male"
        },
        "values": {
          "%{name} uploaded %n photos to their %{album} album": [
            [0, 0, "%{name} uploaded %n photos to his %{album} album"],
            [1, 1, "%{name} uploaded %n photo to his %{album} album"],
            [2, null, "%{name} uploaded %n photos to his %{album} album"]
          ]
        }
      }
    ]
  }

export

sz-i18n export sz-cli/test-files/src-lang/en.json -o sz-cli/test-files/lang/ -t pt,ja

This will export base json into multiple simple json (each supported language), so you can use a translator tool and import back.

// lang/pt/pt.json
{
  "%n comment": "%n comentário",
  "%n comments": "%n comentários",
  "%{name} uploaded %n photo to her %{album} album": "",
  "%{name} uploaded %n photo to his %{album} album": "",
  "%{name} uploaded %n photos to her %{album} album": "",
  "%{name} uploaded %n photos to his %{album} album": "",
  "%{name} uploaded %n photos to their %{album} album": "",
  "Cancel": "Cancelar",
  "Due -%n days ago": "",
  "Due Today": "",
  "Due Tomorrow": "",
  "Due Yesterday": "",
  "Due in %n days": ""
}

import

sz-i18n import sz-cli/test-files/lang/ -b sz-cli/test-files/src-lang/en.json -o sz-cli/test-files/src-lang/ -t pt,ja

It'll parse simple json key/values and replace in source base file creating new base lang files (each supported language).

// src-lang/pt.json
{
  "values": {
    "Cancel": "Cancelar",
    "%n comments": [
      [0, 0, "%n comentários"],
      [1, 1, "%n comentário"],
      [2, null, "%n comentários"]
    ]
  },
  "contexts": [
    {
      "matches": {
        "gender": "male"
      },
      "values": {
        "%{name} uploaded %n photos to their %{album} album": [
          [0, 0, "%{name} uploaded %n photos to his %{album} album"],
          [1, 1, "%{name} uploaded %n photo to his %{album} album"],
          [2, null, "%{name} uploaded %n photos to his %{album} album"]
        ]
      }
    }
  ]
}

How to use

Simple example

  var i = new i18n
  var ja = i.create('ja', {values: {'Cancel': 'キャンセル'}})
  i.translate('Cancel')
  // result: キャンセル

Two dictionaries

  var i = new i18n
  var ja = i.create('ja', {values: {'Cancel': 'キャンセル'}})
  var pt = i.create('pt', {values: {'Cancel': 'Cancelar'}})

  i.using('pt').translate('Cancel')
  // result: Cancelar
  // or
  pt.translate('Cancel')
  // result: Cancelar

  i.using('ja').translate('Cancel')
  // result: キャンセル
  // or
  ja.translate('Cancel')
  // result: キャンセル

Change dictionary values

  var i = new i18n
  var ja = i.create('ja', {values: {'Cancel': 'キャンセル'}})
  // result: キャンセル

  i.using('ja').translate('Cancel')

  i.getDictionary('ja').add({values: {
  "Hello": "こんにちは"
  }})

  i.using('ja').translate('Hello')
  // result: こんにちは

Pluralisation

  var i = new i18n
  var ja = i.create('ja', {
            values: {
              "Hello": "こんにちは",
              "Yes": "はい",
              "No": "いいえ",
              "Ok": "Ok",
              "Cancel": "キャンセル",
              "%n comments":[
              [0, null, "%n コメント"]
              ],
              "_monkeys":"猿も木から落ちる"
            }
          })

 i.translate("%n comments", 0)
 // result: 0 コメント
 i.translate("%n comments", 1)
 // result: 1 コメント
 i.translate("%n comments", 2)
 // result: 2 コメント

Interpolation

  var i = new i18n
  var pt = i.create('pt', {values: {'Welcome %{name}': 'Bem Vindo %{name}'}})

  i.translate('Welcome %{name}', { name:"John" })
  // result: Bem Vindo John

Contexts

  var i = new i18n
  var en = _i.create('en', {
      values: {},
      contexts:[
        {
          "matches": {
            "gender":"male"
          },
          "values": {
            "%{name} uploaded %n photos to their %{album} album":[
              [0, 0, "%{name} uploaded %n photos to his %{album} album"],
              [1, 1, "%{name} uploaded %n photo to his %{album} album"],
              [2, null, "%{name} uploaded %n photos to his %{album} album"]
            ]
          }
        },
        {
          "matches": {
            "gender":"female"
          },
          "values": {
            "%{name} uploaded %n photos to their %{album} album":[
              [0, 0, "%{name} uploaded %n photos to her %{album} album"],
              [1, 1, "%{name} uploaded %n photo to her %{album} album"],
              [2, null, "%{name} uploaded %n photos to her %{album} album"]
            ]
          }
        }
      ]
    })

  i.using('en').translate("%{name} uploaded %n photos to their %{album} album", 1,
    { name:"John", album:"Buck's Night" },
    { gender:"male" }
  )
  // result: John uploaded 1 photo to his Buck's Night album

sz-i18n's People

Contributors

ja0n avatar roddeh avatar stefanyohansson avatar

Watchers

 avatar

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.