JSPM

  • Created
  • Published
  • Downloads 162763
  • Score
    100M100P100Q168150F
  • License MIT

A text scanner for i18next

Package Exports

  • i18next-scanner

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (i18next-scanner) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

i18next-scanner build status

NPM

i18next-scanner is available as both gulp and grunt plugins that can scan your code, extracts translation keys/values, and merges them into i18n resource files.

Features

Installation

npm install i18next-scanner

Gulp Usage

gulp.task('i18next-scanner', function() {
    var i18next = require('i18next-scanner');

    return gulp.src(['src/**/*.{js,html}'], {base: 'src'})
        .pipe(i18next({
            // Provides a list of supported languages by setting the lngs option.
            lngs: ['en', 'de'],
            // Sorts the keys in ascending order.
            sort: true, // default: false
            // Provides a default value if a value is not specified.
            defaultValue: '', // default: ''
            // The resGetPath is your source i18n path, it is relative to current working directory.
            resGetPath: 'assets/i18n/__lng__/__ns__.json', // default: 'i18n/__lng__/__ns__.json'
            // The resSetPath is your target i18n path, it is relative to your gulp.dest path.
            resSetPath: 'i18n/__lng__/__ns__.json', // default: 'i18n/__lng__/__ns__.json'
            // Default namespace is 'translation'.
            ns: 'translation'
        })
        .pipe(gulp.dest('assets'));
});

Grunt Usage

Usage with i18next-text

Parses the i18n._() method

gulp.task('i18next-scanner', function() {
    var i18next = require('i18next-scanner');
    var hash = require('i18next-text').hash['sha1'];
    var options = {
        lngs: ['en', 'de'],
        defaultValue: '__STRING_NOT_TRANSLATED__',
        resGetPath: 'assets/i18n/__lng__/__ns__.json',
        resSetPath: 'i18n/__lng__/__ns__.json',
        ns: 'translation'
    };

    var customTransform = function(file, enc, done) {
        var parser = this.parser;
        var extname = path.extname(file.path);
        var content = fs.readFileSync(file.path, enc);

        /*
         * Supports i18next-text's _() method for i18next:
         *
         * i18n._('This is text value');
         * i18n._("text"); // result matched
         * i18n._('text'); // result matched
         * i18n._("text", { count: 1 }); // result matched
         * i18n._("text" + str); // skip run-time variables
         */
        (function() {
            var results = content.match(/i18n\._\(("[^"]*"|'[^']*')\s*[\,\)]/igm) || '';
            _.each(results, function(result) {
                var key, value;
                var r = result.match(/i18n\._\(("[^"]*"|'[^']*')/);

                if (r) {
                    value = _.trim(r[1], '\'"');
                    key = hash(value); // returns a SHA-1 hash value as default key
                    parser.parseValue(value, key);
                }
            });
        }());

        done();
    };

    return gulp.src(['src/**/*.js'], {base: 'src'})
        .pipe(i18next(options))
        .pipe(gulp.dest('assets'));
});

Handlebars i18n helper with block expressions

gulp.task('i18next-scanner', function() {
    var i18next = require('i18next-scanner');
    var hash = require('i18next-text').hash['sha1'];
    var options = {
        lngs: ['en', 'de'],
        defaultValue: '__STRING_NOT_TRANSLATED__',
        resGetPath: 'assets/i18n/__lng__/__ns__.json',
        resSetPath: 'i18n/__lng__/__ns__.json',
        ns: 'translation'
    };

    var customTransform = function(file, enc, done) {
        var parser = this.parser;
        var extname = path.extname(file.path);
        var content = fs.readFileSync(file.path, enc);

        /*
         * Supports Handlebars i18n helper
         *
         * {{i18n 'bar'}}
         * {{i18n 'bar' defaultKey='foo'}}
         * {{i18n 'baz' defaultKey='locale:foo'}}
         * {{i18n defaultKey='noval'}}
         */
        (function() {
            var results = content.match(/{{i18n\s+("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')?([^}]*)}}/gm) || [];
            _.each(results, function(result) {
                var key, value;
                var r = result.match(/{{i18n\s+("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')?([^}]*)}}/m) || [];

                if ( ! _.isUndefined(r[1])) {
                    value = _.trim(r[1], '\'"');
                }

                var params = parser.parseHashArguments(r[2]);
                if (_.has(params, 'defaultKey')) {
                    key = params['defaultKey'];
                }
                
                if (_.isUndefined(key) && _.isUndefined(value)) {
                    return;
                }

                if (_.isUndefined(key)) {
                    key = hash(value); // returns a SHA-1 hash value as default key
                    parser.parseValue(value, key);
                    return;
                }
                
                parser.parseKey(key, value);
            });
        }());

        /*
         * Supports Handlebars i18n helper with block expressions
         *
         * {{#i18n}}Some text{{/i18n}}
         * {{#i18n this}}Description: {{description}}{{/i18n}}
         * {{#i18n this last-name=lastname}}{{firstname}} ${last-name}{{/i18n}}
         *
         * http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-wordo
         */
        (function() {
            var results = content.match(/{{#i18n\s*([^}]*)}}((?:(?!{{\/i18n}})(?:.|\n))*){{\/i18n}}/gm) || [];
            _.each(results, function(result) {
                var key, value;
                var r = result.match(/{{#i18n\s*([^}]*)}}((?:(?!{{\/i18n}})(?:.|\n))*){{\/i18n}}/m) || [];

                if ( ! _.isUndefined(r[2])) {
                    value = _.trim(r[2], '\'"');
                }

                if (_.isUndefined(value)) {
                    return;
                }

                key = hash(value); // returns a SHA-1 hash value as default key
                parser.parseValue(value, key);
            });
        }());

        done();
    };

    return gulp.src(['src/**/*.hbs'], {base: 'src'})
        .pipe(i18next(options))
        .pipe(gulp.dest('assets'));
});

Options

License

Copyright (c) 2015 Cheton Wu

Licensed under the MIT License.