GithubHelp home page GithubHelp logo

philperron / lang.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rmariuzzo/lang.js

0.0 2.0 0.0 70 KB

:performing_arts: Laravel Translator class in JavaScript!

License: MIT License

JavaScript 100.00%

lang.js's Introduction

Lang.js

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

Build Status Laravel 5.0 Laravel 5.1 Laravel 5.2 Laravel 5.3 NPM Montly Downloads NPM package Bower package GitHub license

Installation

Different installation methods:

Documentation

Initialization

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

To use Lang.js we need to specify at least the messages sources. This can be done during instantiation as shown in the previous code or later using the setMessages() method.

Messages source format

The messages source format looks like:

{
    "locale1.name": {
        "key1": "value1",
        "key2": "value2",
        // ... and more key-value pairs.
    },
    "locale2.name": {
        "key1": "value1",
        "key2": "value2",
        // ... and more key-value pairs.
    },
    // ... and more locales.
}

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

Methods

setMessages

Set messages source. Check messages source format.

var lang = new Lang();
lang.setMessages(source);

getLocale

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

var lang = new Lang();

lang.getLocale();
// > "en"

lang.setLocale('fr');
lang.getLocale();
// > "fr"

setLocale

Set the current locale.

var lang = new Lang();

lang.setLocale('ht');
lang.getLocale();
// > "ht"

getFallback

Get the fallback locale.

var lang = new Lang();

lang.getFallback();
// > undefined

lang.setFallback('de');
lang.getFallback();
// > "de"

setFallback

Set the fallback locale. When retrieving a message (using get() or has()) which is not defined in the specified locale, then it will try to find a message with the fallback locale (if set).

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. Return true if the key is defined on the messages source, otherwise false. This method will try to get a message for the specified locale, if not found, then it will return a message for the fallback locale, if not found, then false will be returned.

var lang = new Lang({
    messages: {
        'en.greetings': {
            'hi': 'Hi'
        },
        'es.greetings': {
            'hi': 'Hola'
        }
    }
});

lang.has('greetings.hi');
// > true

lang.has('greetings.hi', 'es');
// > true

lang.has('greetings.hello');
// > false

get

Get a translation message if found, otherwise return the given key. This method will try to get a message for the specified locale, if not found, then it will return a message for the fallback locale, if not found, then the given key will be returned.

var lang = new Lang({
    messages: {
        'en.greetings': {
            'hi': 'Hi'
        },
        'es.greetings': {
            'hi': 'Hola'
        }
    }
});

lang.get('greetings.hi');
// > "Hi"

lang.get('greetings.hi', {}, 'es');
// > "Hola"

lang.get('greetings.hello');
// > "greetings.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.

var lang = new Lang({
    messages: {
        'en.fruits': {
            'apple': 'apple|apples'
        },
        'es.greetings': {
            'apple': 'manzana|manzanas'
        }
    }
});

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

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

lang.choice('fruits.apple', 4, {}, 'es');
// > "manzanas"

transChoice

This method act as an alias of choice().

Development

  1. Fork this repository and clone it.
  2. Create a branch from develop: git checkout -b feature/xxxxxxx
  3. Submit a PR to be merge into develop branch.

Get help!

Testing

To run the tests use the following commands:

  • Single run: npm run test
  • Run on changes: npm run test:watch

lang.js's People

Contributors

rmariuzzo avatar nivla avatar fgrassals avatar gpopoteur avatar antonkomarev avatar esantelises avatar jasonvarga avatar jhuliano avatar lphilps avatar

Watchers

James Cloos avatar Phil Perron 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.