Giter VIP home page Giter VIP logo

pocket-i18n's Introduction

Build Status

Pocket i18n

Pocket is a blazing fast internationalization library that can fit in your pocket (< 1KB)

✅ Fast translated string lookups (allows nesting!)

✅ String template interpolation

✅ Compatible with ALL browsers (yes, even IE 11)

✅ Can load translation files from URL

✅ Absolutely no dependencies

Getting started

npm i pocket-i18n

Import Pocket

You can use ES6 imports or (CommonJS require).

import Pocket from 'pocket-i18n';

Or, just use a script tag:

<script src="node_modules/pocket-i18n/pocket-i18n.js"></script>

Load translations

Both Pocket.load and Pocket.loadFromUrl return an intl object with a t method for translation lookups.

From an object

const translations = {
    homePage: {
        footer: {
            copyright: 'Copyright {companyName} 2019'
        }
    }
};

const intl = Pocket.load(translations);

intl.t('homePage.footer.copyright', {companyName: 'My Company'});
// Copyright My Company 2019

From URL

const intl = Pocket.loadFromUrl('translations/norwegian.json', function (intl) {
    intl.t('homePage.footer.copyright', {companyName: 'My Company'});
    // Opphavsrett My Company 2019
});

With async/await and promises (need polyfill for IE)

const intl = await new Promise(resolve => Pocket.loadFromUrl('translations/spanish.json', resolve));
intl.t('homePage.footer.copyright', {companyName: 'My Company'});
// Derechos de autor My Company 2019

Using t(lookupStr: string, fillers?: Object)

lookupStr

The translation lookup. Let's say translations object looks like this:

{
    homePage: {
        footer: {
            copyright: 'Copyright My Company 2019'
        }
    }
}

To look up the copyright translation, we would pass 'homePage.footer.copyright'.

fillers (optional)

The set of interpolation values. Let's say translations object looks like this:

{
    homePage: {
        footer: {
            copyright: 'Copyright {companyName} 2019'
        }
    }
}

We would pass the fillers object to t like so:

intl.t('homePage.footer.copyright', {companyName: 'My company'});

Advanced example

const translations = {
    homePage: {
        footer: {
            copyright: 'Copyright {companyName} 2019',
            companyName: 'Translated Company Name'
        }
    }
};

const intl = Pocket.load(translations);

intl.t('homePage.footer.copyright', {companyName: intl.t('homePage.footer.companyName')});
// Copyright Translated Company Name 2019

pocket-i18n's People

Contributors

drewdaemon avatar

Stargazers

Gavin Nitta 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.