npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dynamic-i18n

v1.0.5

Published

Import and Export i18n files to provide dynamic update"

Downloads

8

Readme

#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"
				}
			}
		}
	});