dynamic-i18n

1.0.5 • Public • Published

Dynamic I18n

Dynamic I18n is a tool to export and import your i18n data.

Supported Providers

  • Google sheets

Getting started

npm install --save dynamic-i18n

Usage

Configuration

{
    logger: console.log
    title: 'Translate',
    provider: 'google-sheet',
    languages: ['fr', 'en'],
    'spreadsheet-key': '1FDE8DWKCQu6HWm3TqgEd-VUK7EuImAnbtanObrNzgZg',
    'credentials': "Your credential according to the provider that you are using"
}
key Required Default Description
languages Required [] Contain all the locales we want to import.
spreadsheet-key Required '' The identifier for the source of the data stored.
credentials Required {} Configuration for the provider.
logger Optional console.log Used to display log informations.
title Optional 'Translate' Title of the document.
outputFilePrefix Optional 'locale' Prefix of the output file like : {{outputFilePrefix}}-{{language}}.json.

Sample of credentials for google-sheet provider

{
    "type": "service_account",
    "project_id": " [...] ",
    "private_key_id": " [...] ",
    "private_key": "-----BEGIN PRIVATE KEY----- [...] -----END PRIVATE KEY-----\n",
    "client_email": " [...] @ [...] .gserviceaccount.com",
    "client_id": " [...] ",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/ [...] .gserviceaccount.com"
}
 

Import i18n files : provider --> locale.json

var dynamicI18n = require('dynamic-i18n');
 
dynamicI18n.importI18n(__dirname, configuration, function (err) {
    if (err) console.log("error", err);
});

Export i18n files : locale.json --> provider

var dynamicI18n = require('dynamic-i18n');
 
dynamicI18n.exportI18n([{
    path: 'locale-fr.json',
    key: 'fr'
}], configuration, function (err) {
    if (err) console.log("error ", err);
});

Example with Gulp

This task will import your locales from the provider to your project

gulp.task('extractI18n', function (next) {
    require('dynamic-i18n').importI18n(__dirname + '/app/locales/', { /* config */ }, next);
});

This task will initialize the locales to the provider based on your project locales.

gulp.task('exportOnlineI18n', function (next) {
    require('dynamic-i18n').exportI18n([{
        path: __dirname + '/app/locales/locale-fr.json',
        key: 'fr'
    }, {
        path: __dirname + '/app/locales/locale-en.json',
        key: 'en'
    }, {
        path: __dirname + '/app/locales/locale-es.json',
        key: 'es'
    }, {
        path: __dirname + '/app/locales/locale-de.json',
        key: 'de'
    }, {
        path: __dirname + '/app/locales/locale-it.json',
        key: 'it'
    }, {
        path: __dirname + '/app/locales/locale-sp.json',
        key: 'sp'
    }], { /* config */ }, next);
});

Example with Webpack

This plugin export locales to google-sheet :

const dynamicI18n = require('dynamic-i18n');
const fs = require('fs');
 
function TranslatePlugin(options) {
    this.options = options
}
 
TranslatePlugin.prototype.apply = function (compiler) {
    compiler.plugin('emit', (compilation, callback) => {
        console.log('\nTranslate START');
        const configuration = this.options['dynamic-i18n'].conf;
 
 
        // uncomment to export at the first run
        dynamicI18n.exportI18n([{
            path: __dirname + '/../app/translations/fr-FR.json',
          key: 'fr-FR'
        }, {
          path: __dirname + '/../app/translations/en-GB.json',
          key: 'en-GB'
         }], configuration, (err) => {
          if (err) {
          console.log(err);
          }
            console.log('Translate END');
            callback();
         });
    });
};
 
module.exports = TranslatePlugin;
 
 

This plugin import locales from google-sheet to translations in dist folder :

const dynamicI18n = require('dynamic-i18n');
const fs = require('fs');
 
function TranslatePlugin(options) {
    this.options = options
}
 
TranslatePlugin.prototype.apply = function (compiler) {
    compiler.plugin('emit', (compilation, callback) => {
        console.log('\nTranslate START');
        const configuration = this.options['dynamic-i18n'].conf;
 
        if (!fs.existsSync(compilation.outputOptions.path)) {
            fs.mkdirSync(compilation.outputOptions.path);
        }
 
        dynamicI18n.importI18n(compilation.outputOptions.path + '/translations', configuration, (err) => {
            if (err) {
                console.log(err);
                return;
            }
 
            console.log('Translate END');
            callback();
        });
 
    });
};
 
module.exports = TranslatePlugin;
 
 

Plugin usage sample :

    const TranslatePlugin = require('./translate-plugin');
 
...
 
 
    new TranslatePlugin({
        "dynamic-i18n": {
            conf: {
                logger: console.error,
                title: 'translate-dev',
                provider: 'google-sheet',
                languages: ['FR-FR', 'EN-GB'],
                'spreadsheet-key': '1bkUt23PspVVC0Q2BGQq4PWQFdP0LXT7H5ypzPnKizAY',
                credentials: {
                    "type": "service_account",
                [...]
                    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/admin-346%40alehos-m5.iam.gserviceaccount.com"
                }
            }
        }
    });
 

Package Sidebar

Install

npm i dynamic-i18n

Weekly Downloads

0

Version

1.0.5

License

ISC

Last publish

Collaborators

  • benjd90
  • gaetansenn