Giter Club home page Giter Club logo

i18n-js-formatter's Introduction

i18n JS formatter

A simple translation module with dynamic json storage which helps to format strings quickly and easily. It works with template litterals strings.

Formatter can be extended with your own formatters.

It can be used in any JavaScript application (web, worker, NodeJS, ...).

You can play with the API at: https://restimel.github.io/i18n-js-formatter/demo/index.html

Goal

i18n-js-formatter's mission is to provide to the end-user the best output (translated, formatted, secured) in an easy way to build it with JavaScript.

Documentation

Watch following links for more information:

Quick usage examples

configuration:

i18n.configuration({
	locales: ['en', 'fr', 'de'],
	dictionary: 'dictionary.json'
});

Changing locale:

i18n.setLocale('fr');

Using strings which have to be translated

i18n('hello'); //returns 'salut'
i18n('Hello %s!', 'Jim'); //returns 'Salut Jim!'
i18n`Hello ${'Kate'}%s!`; //returns 'Salut Kate!'

Format number depending on locale

i18n('%f sentences', 1234567.8);
	//returns '1,234,567.8 sentences' (en)
	//returns '1 234 567,8 phrases' (fr)
	//returns '1.234.567,8 Sätze' (de)

Contextualize your sentence to make difference from same sentence or to give more context.

i18n.context('computer key', 'Give the correct key'); // should be translated in French 'Donnez la bonne touche'
i18n.context('door key', 'Give the correct key'); // should be translated in French 'Donnez la bonne clef'

i18n.context('time unit: minute', 'min');
i18n.context('abbreviation: minimum', 'min');

To give plural translations (this should be done in version 0.5)

i18n.n('a cat', '%s cats', 1); // in German: 'eine Katze'
i18n.n('a cat', '%s cats', 3); // in German: '3 Katzen'

To escape string easily depending on context (mainly to avoid user entries to create unexpected issues)

i18n('quotes: %{esc:html}s', 'I <b>cannot</b> hack your site');
	// in English: 'quotes: I &lt;b&gt;cannot&lt;/b&gt; hack your site'
i18n('http://my_site.com/?search=%{esc:uric}s&limit=10', 'foo bar&baz=42');
	// returns 'http://my_site.com/?search=foo%20bar%26baz%3D42&limit=10'

You can create your own formatting rules.

i18n.addRule('*', function(data) {
	return data.value.replace(/./g, '*');
});
i18n.addRule('l', function(data) {
	return data.value.length;
});

i18n('password is %* (length: %(1)l)', 'secret'); // in French: 'le mot de passe est ****** (longueur: 6)'

Parsing

In order to parse all your strings from your code and translate them easily, you can use i18n-js-parser.

Licence

Creative Commons Attribution 3.0 Unported License: You are free to use, share, modify it while you keep credit to author and contributors (http://creativecommons.org/licenses/by/3.0/)

Download

Get the minified version of

the i18n API

the full API (i18n API + formatter)

Get source files:

i18n library

sprintf wrapper (to load it as formatter) note: you must also load sprintf

inner formatter (the one described below)

You don't need both sprintf wrapper and the inner formatter. Only one is enough ;)

Test

Jasmine tests are provided in folder spec. Run tests with mainTest.html (located in the root folder) to assert it works well: https://restimel.github.io/i18n-js-formatter/mainTest.html

Please note that some tests may fail depending of browsers. Mainly depending of support of locale string conversion (uppercase, lowercase) where some locale were not fully supported.

Version 0.4.0

If you want you can help me to improve it. Fork the project and pull request your change.

Keep tune for further update I am working on it.

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.