GithubHelp home page GithubHelp logo

i18n's People

Contributors

bgaillard avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

i18n's Issues

Add an el option

Add an el option transmitted to the constructor or the load method.

This would allow to pull translations from a DOM node, this technic is useful to speeup first page display.

If the el option is transmitted to the constructor then the locale, phrases, type and version options must not be provided.

The el option takes a CSS selector to select one to node which must have the following parameters :

  • data-locales
  • data-phrases
  • data-type
  • data-version (optional)

HTML sample

<div id="i18n" 
     data-locale="en" 
     data-phrases='{"hello" : "Hello you !", "please-login" : "Please login with your project X account."}' 
     data-type="public"></div>

Polyglot instance

var polyglot = new Polyglot(
    el : '#i18n',
    urlBuilder : function(type, locale, version) {
        ...
    }
);

polyglot.t('hello'); // Hello you !

Add a cache option to the constructor.

For now the library cache translations using a plain Javascript object (i.e in memory), it works but it can be problematic with large translation files.

We could add a cache option transmitted to the Polyglot constructor. If this cache option is a string it allows to use a pre-defined cacheManager, if this cache option is a function it defined a custom cacheManager.

Pre-defined cache managers could be :

  • MEMORY : Cache translations using a plain Javascript object
  • LOCAL_STORAGE : Cache translations using the Local Storage
  • SESSION_STORAGE : Cache translations using the Session Storage
  • INDEXED_DB : Cache translations using IndexedDb
  • WEB_SQL : Cache translations using Web SQL

Each pre-defined cache manager could also be configured using a Javascript object, for example.

var polyglot = new Polyglot(
    {
        cache : {
            type : 'LOCAL_STORAGE', 
            params : {
                key : 'myproject.i18n'
            }
        }
    }
);

Sample custom cache manager

var polyglot = new Polyglot(
    {
        cache : function(type, locale, version, data) {
            ...
        }
    }
);

Add a reconfigure option to the load method.

For now the load method automatically reconfigures the Polyglot instance with the retrieved translations.

In some cases we do not want to do this and only want to pull the translations to put them into the translations cache.

We could add a reconfigure option with a default value equal to false to do this. For example at application starting this could be useful to preload several translation files.

Add a parser option

For now the library only support JSON string to specify translations, for example :

{
    "key1" : "Translation for key 1",
    "key2" : "Translation for key 2"
}

It could be great if translation specified in other formats could be loaded, to do this the library could provided an additionnal parser option.

For example if translations are stored using a Base 64 encoded JSON string we could define the following custom parser.

Previous translations encoded using Base 64.

ew0KICAgICJrZXkxIiA6ICJUcmFuc2xhdGlvbiBmb3Iga2V5IDEiLA0KICAgICJrZXkyIiA6ICJUcmFuc2xhdGlvbiBmb3Iga2V5IDIiDQp9

Parsing this Base 64 string.

var polyglot = new Polyglot(
    {
        parser : function(from, data) {
            return JSON.parse(atob(data));
        }
    }
);

The parser method takes 2 parameters :

  • from : Parameter which can take 2 values for the moment DOM_NODE or HTTP, it indicates if the translation string comes from a DOM node or an HTTP request. This can be useful if we want to use different formats for translations stored in DOM nodes and pulled from HTTP.
  • data : The data to parse

The parser method must return an object which corresponds to the Polyglot phrases.

Terminate the documentation

Terminate the documentation

  • urlBuilder option
  • forceReload option
  • locale option
  • type option
  • version option
  • constuctor(options) method
  • cache(type, locale, version, phrases) method
  • type(newType) method
  • Best practices - Use a Polyglot singleton
  • Best practices - Speedup first time display
  • Best practices - Translation files versionning
  • Best practices - Caching large translation files
  • Best practices - Using common translation files in several projects
  • Best practices - Create JSON translation files with Gulp and Browserify
  • Best practices - Embed translation in a DOM node with Gulp and Preprocess
  • Best practices - Expose the t method to templates

Additional feature not implemented for now :

  • reconfigure option transmitted to the load method, this would allow to pull and cache translations without re-configuring the Polyglot instance
  • el option transmitted to the constructor or the load method, this would allow to pull translation from a DOM node, this technic is useful to speeup first page display
  • parser option transmitted to the constructor, this would allow to configure to read translations pulled from a DOM node or from AJAX HTTP requests
  • cache option transmitted to the constructor, this option could take pre-defined strings to use pre-configured cache managers. If the option is a method its considered to be a custom cache manager.

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.