Using with polyglot

Using locize with polyglot

You can use locize in combination with polyglot. Polyglot is a internationalization framework provided by airbnb. Runs on the browser and node.js.

To integrate locize into your polyglot you will need to load the translations from our CDN via our API and needs a little more coding as using the locizify or i18next scripts.

Eg. Browser using the our API client locizer:

The script enables easy translation loading and provides on top functions to detect users language and to load the available languages on your project.

1
2
3
4
5
6
7
8
9
10
11
// <script src="https://unpkg.com/locizer/locizer.min.js"></script>
locizer
.init({
fallbackLng: 'en',
referenceLng: 'en',
projectId: '[your project id]'
})
.load('translation', function(err, translations, lng) {
const polyglot = new Polyglot({ phrases: translations, locale: lng });
console.log(polyglot.t('some key'));
});

Complete locizer documentation

Eg. Browser using the window.fetch:

1
2
3
4
5
6
7
8
window.fetch('https://api.locize.io/[YOUR_PROJECT_ID]/latest/[LNG]/[NAMESPACE]', {
mode: 'cors'
}).then(response => response.json())
.then(translations => {
const polyglot = new Polyglot({ phrases: translations, locale: '[LNG]' });
console.log(polyglot.t('some key'));
})
.catch(e => console.warn(e));

(You can find your projectId and API Key in your projects settings under the API Tab.)

Eg. NodeJS using the request module:

1
2
3
4
5
6
7
8
var request = require('request');
var polyglot = require('node-polyglot');
request('https://api.locize.io/[YOUR_PROJECT_ID]/latest/[LNG]/[NAMESPACE]', function(err, res, translations) {
var polyglot = new Polyglot({ phrases: translations, locale: '[LNG]' });
console.log(polyglot.t('some key'));
});
}

For more details you might read our post "the freedom of choice".


Have questions? Contact us!

Share