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

gulp-transformer

v1.0.2

Published

Translate files by locale's dictionary translations and some other transforms.

Downloads

5

Readme

Build Status Coverage Status

Gulp Transformer

Make your files for many languages (locales) with it. You can use YAML or JSON dictionaries format. Also you can use it without translation.

Usage

First, install gulp-transformer as a development dependency:

npm install --save-dev gulp-transformer

Then, add it to your gulpfile.js:

var translate = require('gulp-transformer');

gulp.task('translate', function() {
  var langs = ['en', 'ru', 'de'];

  langs.forEach(function(lang){
    gulp.src('src/file/path')
      .pipe(translate (options))
      .pipe(gulp.dest('dest/file/path/' + lang));
  });
});

or better, handle errors:

gulp.task('translate', function() {
  var langs = ['en', 'ru', 'de'];

  langs.forEach(function(lang){
    gulp.src('src/file/path')
      .pipe(
        translate(options)
        .on('error', console.error.bind(console, lang))
      )
      .pipe(gulp.dest('dest/file/path/' + lang));
  });
});

Options

options in translate function can be:

  • String - Path to locale file.
  • Object:
    • .path - String. Optional. Path to locale file. Or you can use .lang, .localeDirectory.
    • .locale - String. Optional. Target language.
    • .directory - String. Optional. Directory with locale files. Default: __dirname+'/'.
    • .ext - String. Optional. Extension of file. Default: ''.
    • .pattern - RegExp. Optional. Pattern to find strings to replace. You can specify your own pattern. To transform strings without translate. Default: /\{{2}([\w\.\s\"\']+\s?\|\s?translate[\w\s\|]*)\}{2}/g
    • .transformSeparator - String. Optional. Something to split parts of transform. Default: '|'.
    • .transform - Object. Optional. Every field is you transform function. First argument is an content to transform it. Function should return transformed string or Error object with some message.
    • .keyPartSeparator - String. Optional. Some symbol to separate key parts (key like a.b.c has '.' as separator). Default: '.'.
    • .strictDictionary - Boolean. Optional. If it is true will not use default dictionary as fallback if can not find kay in this dictionary. To search some key in default - set it to false. Default: true.
    • .defaultDictionary - Object. Optional. Can be a String or an Object. If String - it should Be path to default locale path. If Object contents fields like options:
      • .path - like options.path.

      • .locale - like options.locale.

      • .directory - like options.directory.

      • .ext - like options.ext.

If no .path specified, try construct it from .directory + .locale + .ext.

If you specify your own pattern:

  • create it with g flag (/pattern/g). Example we want to transform something between '*~' and '~*'. Then it should be like /\*~\s?(\w\.\s?|\s?translate[\w\s\|]*)\s?~\*/g. Also - you can remove translate. But I doesn't know why do you want it.
  • make some string to check. To test it fast, check, that 'some string were your *~word|translate~* <-pattern is'.match(pattern) returns [ 'word|translate' ] without '*~' and '~*'.

Be careful! If you function is named like base transform, you will newer spawn this base transform function, and will use yours.

Specifying user transforms example:

options = {
  path: 'some/path/to/locale',
  transform: {
    upperLowerRandom: function(string){
      string.split('').reduce(function(result, letter){
        result += Math.floor(Math.random()*2) ? letter.toUpperCase() : letter.toLowerCase();
        return result;
      }, '');
    }
  }
}

How to use in files

By default it using angular-like syntax. Expressions in {{}} with | translate transformer will be translated.

Following examples assume that "some" in locales equals "My bIg StRing tO trAnsfOrm. Let's Do "

Example:

'{{ some | translate }}' transforms to "My bIg StRing tO trAnsfOrm. Let's Do "

You can use another base transforms. Or if you want - specify yours.

Base transforms are:

  • translate: '{{ some | translate }}' transforms to "My bIg StRing tO trAnsfOrm. Let's Do "
  • uppercase: '{{ some | translate | uppercase }}' transforms to "MY BIG STRING TO TRANSFORM. LET'S DO "
  • lowercase: '{{ some | translate | lowercase }}' transforms to "my big string to transform. let's do "
  • capitalize: '{{ some | translate | capitalize }}' transforms to "My big string to transform. let's do "
  • capitalizeEvery: '{{ some | translate | capitalizeEvery}}' transforms to "My Big String To Transform. Let'S Do "
  • capitalizeDot: '{{ some | translate | capitalizeDot}}' transforms to "My big string to transform. Let's do "
  • trim: '{{ some | translate | trim}}' transforms to "My big string to transform. Let's Do"
  • reverse: '{{ some | translate | reverse }}' transforms to " oD s'teL .mrOfsnArt Ot gniRtS gIb yM"

Test

Run tests

npm test

To check code coverage:

sudo npm install -g istanbul
  • Run npm script
npm run coverage
  • Look into path/to/project/coverage/lcov-report/index.html

LICENSE - "MIT License"

Copyright (c) 2015 Konstantine Petryaev

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.