gomoob / i18n Goto Github PK
View Code? Open in Web Editor NEWPolyglot.js extended to load JSON translation files and cache them.
License: MIT License
Polyglot.js extended to load JSON translation files and cache them.
License: MIT License
Their is an issue to manage PO files here airbnb/polyglot.js#30.
With the implementation of #4 we could be able to add a custom PO parser, the following implementations could be useful :
For now the load
method returns nothing, it could be great if this method returned an ES6 promise to allow subsequent calls.
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 !
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 objectLOCAL_STORAGE
: Cache translations using the Local StorageSESSION_STORAGE
: Cache translations using the Session StorageINDEXED_DB
: Cache translations using IndexedDbWEB_SQL
: Cache translations using Web SQLEach 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) {
...
}
}
);
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.
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 parseThe parser
method must return an object which corresponds to the Polyglot phrases.
Terminate the documentation
urlBuilder
optionforceReload
optionlocale
optiontype
optionversion
optionconstuctor(options)
methodcache(type, locale, version, phrases)
methodtype(newType)
methodt
method to templatesAdditional 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 instanceel
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 displayparser
option transmitted to the constructor
, this would allow to configure to read translations pulled from a DOM node or from AJAX HTTP requestscache
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.A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.