Git Product home page Git Product logo

i18next's Introduction

Introduction

Project goal is to provide an easy way to translate a website on clientside:

  • fetch resources from server
  • fetch each resource file individually (static) or all once via dynamicRoute
  • apply translation to HTML tags with the data-i18n attribute
  • post missing key-value pairs to server (for easy development -> just translate the new keys)
  • search for key en-US first, then in en, then in fallback language (or de-DE, de , fallback)

Check out the documentation

Usage

Assuming we loaded en-US, en and dev resources for two namespaces ('ns.special' and 'ns.common'):

// given loaded and merged dictionaries in i18next.js to:
{
    "en_US": {
        "ns.special": {
            "app": {
                "name": "i18n",
                "insert": "you are __youAre__",
                "child": "__count__ child",
                "child_plural": "__count__ children"
            }
        },
        "ns.common": {}
    },
    "en": {
        "ns.special": {
            "app": {
                "area": "Area 51"
            }
        },
        "ns.common": {}
    },
    "dev": {
        "ns.common": {
            "app": {
                "company": {
                    "name": "my company"
                }
            },
            "add": "add"
        },
        "ns.special": {
            "nav": {
                "1": "link1",
                "2": "link2",
                "home": "home"
            }
        }
    }
}

you can translate using $.t(key, [options])

$.i18n.init({
    lng: 'en-US',
    ns: { namespaces: ['ns.common', 'ns.special'], defaultNs: 'ns.special'}
}, function() {
    $.t('app.name'); // -> i18n (from en-US resourcefile)
    $.t('app.area'); // -> Area 51 (from en resourcefile)
    $.t('ns.common:app.company.name'); // -> my company (from dev resourcefile)
    $.t('ns.common:add'); // -> add (from dev resourcefile)
});

insert values into your translation

$.t('app.insert', {youAre: 'great'}) // -> you are great

support for plurals

$.t('app.child', {count: 1}) // -> 1 child
$.t('app.child', {count: 3}) // -> 3 children

support for key priority when you need to try multiple keys, using the first one that exists.

$.t(['app.missingKey', 'app.existingKey'], {greeting: "hello"}) // -> I am the existing key: hello

or you can just $('.mySelector').i18n() assuming you have added the data-i18n="key" attribute to your elements

// given
<ul class="nav">
	<li class="active"><a href="#" data-i18n="nav.home">home</a></li>
	<li><a href="#" data-i18n="nav.1">link1</a></li>
	<li><a href="#" data-i18n="nav.2">link2</a></li>
</ul>

// Run the following javascript to translate all elements having the _data-i18n_ attribute:
$.i18n.init({
    lng: 'en-US',
    ns: { namespaces: ['ns.common', 'ns.special'], defaultNs: 'ns.special'}
}, function() {
    $('.nav').i18n();
});

For missing keys (if the option 'addMissing' is set to true) will be send to server with actual text as defaultValue.

Sample usage

In the folder you find one static sample.

serverside integrations

i18next-node is bringing i18next to node.js

Inspiration

Building

To build your own copy of i18next, check out the repository and:

git clone https://github.com/jamuhl/i18next.git
cd i18next
npm install -g grunt-cli
npm install
grunt

The grunt command will build i18next into the bin/ and release/ folders.

License (MIT)

Copyright (c) 2011 Jan Mühlemann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

i18next's People

Contributors

alaa-eddine avatar anderslemke avatar ashkyd avatar binarykitchen avatar brettz9 avatar bugwelle avatar chacal avatar dpehrson avatar fabriziofortino avatar franck34 avatar hugojosefson avatar jamuhl avatar jessehouchins avatar jriecken avatar kevinold avatar loonkwil avatar lorismaz avatar marcbachmann avatar mdocter avatar mhaltner avatar mimecuvalo avatar mou avatar nburoojy avatar orloffv avatar qraynaud avatar stephenldecker avatar vkarampinis 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.