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

grunt-translate-extract

v0.0.3

Published

Automatically extracts translatable strings from source code.

Downloads

34

Readme

grunt-translate-extract

grunt-translate-extract helps developers with the internationalization off their code. Searchs for translatable string in the source code and generate a json file for each language.

code generation

Features

  • Wordpress: support all i10n functions, including context and pluralization.
  • PHP gettext: support pluralization but not context.
  • Angular Translate: support only translation filters ie: <ANY>{{ TRANSLATION_ID | translate }}</ANY>
  • You can edit the generated files, modified values (translations) will be preserved after run the task multiple times.
  • Parsers are regular expression based, that means the functionality is very limited. Ideally it should be implemented with a grammar parser, maybe in a future.

Getting Started

This plugin requires Grunt ~0.4.5

npm install grunt-translate-extract --save-dev

Enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-translate-extract');

Overview

In your project's Gruntfile, add a section named translate_extract to the data object passed into grunt.initConfig().

var path = require("path");

grunt.initConfig({
  translate_extract: {
    options: {
      output: [ 'en.json', 'es.json', 'fr.json', 'de.json'],
      outputDir: "./output",
      basePath: optional //Just in case the basePath is not the root of the project.
      builtInParser: "wordpress",
      customParser: null,
      errorOnDuplicatedKeys: true
    },
    files: {
      src: ["src/**/*.php"]
    }
  }
});

Options

options.output

Type: String Array Default value: [ "en", "es" , "fr", "de"]

Name of the generated files. *FileName = basePath + outputDir + output[n]

options.outputDir

Type: String Default value: ./output

Directory where the locale files will be stored.
files.dest is ignored by this task and can be omitted.

options.basePath

Type: String Default value: ./output

Optional parameter. Use just in case the basePath is not the root of the project.

options.errorOnDuplicatedKeys

Type: bolean Default value: true

If true the system will throw and error if finds two translatable string with the same msgid, if false the two string will be considered the same an treated as a single entry .

options.builtInParser

Type: String Default value: wordpress

The parser used to match the translation strings. List of built in parser . gettextPHP => matches php gettext functions like _("text to translate") or $object->_("text to translate") . wordpress => matches php wordpress functions like __("text to translate") or _e("text to translate") . angularTranslate => matches angular translate declarations using the translate filter like {{ msgid | translate}}

options.customParser

Type: Object Default value: null

If there is no Built In Parser that match the language or template that you are using it is posible to define a custom parser that matchs and extracts your translatable strings.
DEPRECATION NOTICE: Custom Parsers may be removed in future versions.. In order to introduce support for plurals and context. The complexity of the parser has increased a lot to support plurals and content, so built custom parsers is not an easy task anymore, therefore it makes no sense to support custom parsers.
AngularTranslate Parser Example: matchs {{ TRANSLATION_ID | translate }}

var utils = require("../Utils");
var AngularTranslateParser = (function () {
    function AngularTranslateParser() {
    }
    AngularTranslateParser.prototype.getRegexpList = function () {
        return [/\{\{\s*([a-zA-Z_]+[a-zA-Z0-9_]*)\s*\|\s*translate\s*\}\}/g];
    };
    AngularTranslateParser.prototype.parseMatch = function (match, regExp) {
        var text = utils.escapeLiteral(match[1]);
        if (text === false)
            text = match[1];
        return { msgid: text, msgstr: text, msgid_plural: null, msgctxt: null, line: null };
    };
    AngularTranslateParser._asd = "";
    return AngularTranslateParser;
})();

Release History

0.0.3 Add support for plurals, context, improve documentation. BREAKIN CHANGES INTRODUCED
0.0.2 print number of files parsed and found translation entries. add a new gettextPHP_KV parser.
0.0.1 first beta released with support for php gettext , wordpress and angular-translate. no hard test yet.