All Projects โ†’ rmariuzzo โ†’ Lang.js

rmariuzzo / Lang.js

Licence: mit
๐ŸŽญ Laravel Translator class in JavaScript!

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Lang.js

Laravel Js Localization
๐ŸŒ Convert your Laravel messages and consume them in the front-end!
Stars: โœญ 451 (+94.4%)
Mutual labels:  i18n, laravel, localization, internationalization
Angular I18next
angular v2.0+ integration with i18next v8.4+
Stars: โœญ 69 (-70.26%)
Mutual labels:  i18n, localization, internationalization
Node Gettext
A JavaScript implementation of gettext, a localization framework.
Stars: โœญ 175 (-24.57%)
Mutual labels:  i18n, localization, internationalization
Keys Translations Manager
KTM, a locale management web app built on MERN stack, lets you manage and control locales in one place. It's particularly useful for someone who needs to manage multiple internationalization/localization projects.
Stars: โœญ 81 (-65.09%)
Mutual labels:  i18n, localization, internationalization
Laravel Lang
๐ŸŒ 75 languages support for Laravel application.
Stars: โœญ 1,134 (+388.79%)
Mutual labels:  i18n, laravel, localization
Locale2
๐Ÿ’ช Try as hard as possible to detect the client's language tag ("locale") in node or the browser. Browserify and Webpack friendly!
Stars: โœญ 65 (-71.98%)
Mutual labels:  i18n, localization, internationalization
Linguist
Easy multilingual urls and redirection support for the Laravel framework
Stars: โœญ 188 (-18.97%)
Mutual labels:  i18n, laravel, localization
Goloc
A flexible tool for application localization using Google Sheets.
Stars: โœญ 42 (-81.9%)
Mutual labels:  i18n, localization, internationalization
Traduora
Everยฎ Traduora - Open-Source Translation Management Platform
Stars: โœญ 1,580 (+581.03%)
Mutual labels:  localization, internationalization, i18n
Phabricator zh hans
Phabricator zh-Hans Translation & Tools.
Stars: โœญ 113 (-51.29%)
Mutual labels:  i18n, localization, internationalization
Weblate
Web based localization tool with tight version control integration.
Stars: โœญ 2,719 (+1071.98%)
Mutual labels:  i18n, localization, internationalization
Formatjs
The monorepo home to all of the FormatJS related libraries, most notably react-intl.
Stars: โœญ 12,869 (+5446.98%)
Mutual labels:  i18n, localization, internationalization
Redux React I18n
An i18n solution for React/Redux and React Native projects
Stars: โœญ 64 (-72.41%)
Mutual labels:  i18n, localization, internationalization
React Translated
A dead simple way to add complex translations (i18n) in a React (DOM/Native) project ๐ŸŒŽ๐ŸŒ๐ŸŒ
Stars: โœญ 176 (-24.14%)
Mutual labels:  i18n, localization, internationalization
React Intl Hooks
React hooks for internationalization without the hassle โš›๏ธ๐ŸŒ
Stars: โœญ 64 (-72.41%)
Mutual labels:  i18n, localization, internationalization
Gatsby Starter Prismic I18n
Based on gatsby-starter-prismic with Internationalization (i18n) support
Stars: โœญ 77 (-66.81%)
Mutual labels:  i18n, localization, internationalization
Serge
Continuous localization platform
Stars: โœญ 212 (-8.62%)
Mutual labels:  i18n, localization, internationalization
Frenchkiss.js
The blazing fast lightweight internationalization (i18n) module for javascript
Stars: โœญ 776 (+234.48%)
Mutual labels:  i18n, localization, internationalization
Texterify
The localization management system.
Stars: โœญ 37 (-84.05%)
Mutual labels:  i18n, localization, internationalization
Pseudo Localization
Dynamic pseudo-localization in the browser and nodejs
Stars: โœญ 109 (-53.02%)
Mutual labels:  i18n, localization, internationalization

Lang.js โ€“ Localization library written in JavaScript highly inspired on Laravel's Lang.

Build Status Laravel 5.5 Laravel 5.0 NPM Montly Downloads GitHub license




Installation

Different installation methods:




Are you interested in the next version?




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"

Get a translation file from a nested directory

Lang.get('forum/thread.hello');
// > "Hello"

Lang.get('forum/thread.hello', {}, 'es');
// > "Hola"

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.fruits': {
            'apple': 'manzana|manzanas'
        }
    }
});

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

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

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

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 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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].