1.0.5 • Published 8 years ago

dynamic-i18n v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
8 years ago

#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"
}
keyRequiredDefaultDescription
languagesRequired[]Contain all the locales we want to import.
spreadsheet-keyRequired''The identifier for the source of the data stored.
credentialsRequired{}Configuration for the provider.
loggerOptionalconsole.logUsed to display log informations.
titleOptional'Translate'Title of the document.
outputFilePrefixOptional'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"
				}
			}
		}
	});
1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago