Skip to content

rmariuzzo/Lang.js

 
 

Repository files navigation

Lang.js – Localization library written in JavaScript highly inspired on Laravel's Lang.

Laravel 5.5 Laravel 5.0 NPM Montly Downloads GitHub license




Installation

npm install lang.js@next

Documentation

Initialization

var lang = new Lang({
  messages: source, // required
  locale: 'fr', // optional
  fallback: 'zn' // optional
})

Messages source format

The messages source format looks like this:

{
    "locale-1.mesages-name": {
        "key-1": "value-1",
        "key-2": "value-2",
        // ... and more key-value pairs.
    },
    "locale-2.mesages-name": {
        "key-1": "value-1",
        "key-2": "value-2",
        // ... and more key-value pairs.
    },
    // ... and more locales.
}

See the sample used in tests located at: test/fixture/messages.json.


API

setMessages

Set messages source. Check messages source format.

lang.setMessages(source)

getLocale

Get the current locale, if none set, the default locale will be returned (en).

lang.getLocale()
// > "en"

setLocale

Set the current locale.

lang.setLocale('ht')

getFallback

Get the fallback locale.

lang.getFallback()
// > de

setFallback

Set the fallback locale for messages not found using the default locale.

var lang = new Lang({
  messages: {
    'en.greetings': {
      hi: 'Hi',
      hello: 'Hello'
    },
    'it.greetings': {
      hi: 'Salve'
    }
  }
})

lang.setLocale('it')
lang.get('greetings.hello')
// > "greetings.hello"

lang.setFallback('en')
lang.get('greetings.hello')
// > "Hello"

has

Indicate if a given key is defined on the messages source.

lang.has('greetings.hi')

get

Get a translation message.

lang.get('greetings.hi')
lang.get('forum/thread.hello')

trans

This method act as an alias of get().

choice

Get the plural or singular form of the message specified based on an integer value.

lang.choice('fruits.apple', 1)
// > "apple"

lang.choice('fruits.apple', 4)
// > "apples"

You may even create more complex pluralization rules which specify translation strings for multiple number ranges:

var lang = new Lang({
  messages: {
    'en.fruits': {
      apple: '{0} There are none|[1,19] There are some|[20,*] There are many'
    }
  }
})

lang.choice('fruits.apple', 0)
// > "There are none"

lang.choice('fruits.apple', 1)
// > "There are some"

lang.choice('fruits.apple', 3)
// > "There are some"

lang.choice('fruits.apple', 20)
// > "There are many"

lang.choice('fruits.apple', 22)
// > "There are many

transChoice

This method act as an alias of choice().


Development

  1. Fork this repository and clone it.
  2. Create a branch from next branch.
  3. Submit a PR to be merge into next branch.

Testing

To run the tests use the following commands:

  • Single run: npm run test

Deployment

We do deployment using np:

npm run release:next
npm run release:stable

About

🎭 Laravel Translator class in JavaScript!

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

 
 
 

Contributors