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

i18n-central-storage

v2.0.4

Published

parse files to find words that should be translated by pattern and put them to elasticsearch storage

Downloads

65

Readme

i18n-central-storage

helps to agragate translations from several projects to one place(in our case elasticsearch only supported),

result

You can have your own i18-n or i18n-2 with followen configuration for example:

var locales = ['ru', 'en'];
var i18nConfig = {
    locales: locales,
    defaultLocale: 'ru',
    directory: './messages/'
};
var i18n = new (require('i18n-2'))(i18nConfig);

and you have your source files with messages that should be translated, so you can specify patterns for singular and plural:

// match any string in your code with gettext('any text')
var pattern = /gettext\('(.*?)'\)/gi;
// match any string in your code with gettextP('singular', 'plural', count)
var pluralPattern = /gettextP\('(.*?)', *'(.*?)', *(\d+)\)/gi;

and directories where your source code located:

var directories = [
    path.resolve(__dirname, '../templates/'),
    path.resolve(__dirname, '../assets/js')
];

and extentions(array of strings) that should be included in search:

var extentions = ['.js', '.ejs', '.jsx'];

list of other configuration options:

// setting extension of messages files - defaults to '.js'
extension: '.js',

// where to store files - required parameter
messagesDirectory: './mylocales',

// here you should specify host and port of your elasticsearch instance,
// index wich you are going to use and project reference to split several projects by
// that property
var elasticConfig = {
    host: '192.168.1.237:9200',
    index: 'localizations',
    project: 'projectReferenceOne'
};

as result all you translated messages will be fetched from elasticsearch and placed to your localization file.

usage example as a gulp task for one locale:


var I18nCentralStorage = require('i18n-central-storage');
var path = require('path');

var directories = [
    path.resolve(__dirname, '../templates/'),
    path.resolve(__dirname, '../assets/js')
];
var messagesDirectory = path.resolve(__dirname, '../messages/');
var extentions = ['.js', '.ejs', '.jsx'];
var pattern = /gettext\('(.*?)'\)/gi;

var elasticConfig = {
    host: '192.168.1.237:9200',
    index: 'localizations',
    project: 'projectReferenceOne'
};

var taskFunction = function() {

        var promise = new Promise(function(resolve, reject){
            var i18nCentralStorage = new I18nCentralStorage.default({
                directories,
                messagesDirectory,
                extentions,
                pattern,
                elasticConfig
            }, function() {

                var locale = 'ru';
                var result = i18nCentralStorage.analize(locale);

                i18nCentralStorage
                    .syncLocale(result, locale, { writeResultToFile: true })
                    .then(function (result) {

                        resolve(result);
                    })
                    .catch(function (error) {
                        reject(error);
                    });
            });
        });

        return promise;
    };


module.exports = taskFunction;

migration to version 2.*

since we should use only one _type for future versions of elastic (from 7.*) we should gave only documents with single type, see: https://www.elastic.co/blog/index-type-parent-child-join-now-future-in-elasticsearch

execute in kibana following:

POST _reindex
{
  "source": {
    "index": "localizations"
  },
  "dest": {
    "index": "localizations.v2"
  },
  "script": {
    "source": """
    ctx._id = ctx._type + "-" + ctx._id;
    ctx._source.locale = ctx._type;
    ctx._type = "doc";
"""
  }
}

POST localizations.v2/doc/_delete_by_query
{
  "query": {
    "match": {
      "locale": {
        "query": "messages"
      }
    }
  }
}