Giter VIP home page Giter VIP logo

grunt-pot's Introduction

grunt-pot

Scan files and creates a .pot file using xgettext.

Getting Started

This plugin requires Grunt >=0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-pot --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-pot');

The "pot" task

Overview

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

grunt.initConfig({
  pot: {
    options: {
      // Specify options
    },
    files: {
      // Specify files to scan
    },
  },
})

Options

text_domain

Type: String Default value: (Your package name)

This is the text domain of your project. Currently it is only used to generate the destination file name: [text-domain].pot.

dest

Type: String Default value: False

Either a path to a folder (with trailing slash) for the generated .pot to be placed or a file path. When using a folder, the filename is generated using the text domain.

overwrite

Type: Bool Default value: True

When false, append to pre-existing .pot file, rather than overwriting it.

encoding

Type String|Bool Default value: False.

Specifies the encoding of the input files. E.g. "ASCII" or "UTF-8". This option is needed only if some untranslated message strings or their corresponding comments contain non-ASCII characters. This option maps to xgettext's --from-code command line option. False (default value) does not specify an encoding, by default, xgettext will interpret input files as ASCII. Note that Tcl and Glade input files are always assumed to be in UTF-8, regardless of this option.

language

Type String|Bool Default value: False.

Specifies the language of the input files. The supported languages are C, C++, ObjectiveC, PO, Shell, Python, Lisp, EmacsLisp, librep, Scheme, Smalltalk, Java, JavaProperties, C#, awk, YCP, Tcl, Perl, PHP, GCC-source, NXStringTable, RST, Glade.

By default xgettext guesses the language based on the input file name extension.

keywords

Type: Array Default value: (none)

An array of strings ('keywords'). Each keyword specifies a gettext function to scan for:

keywords: ['gettext', '__'], //functions to look for

By default xgettext looks for strings in the first argument of each keyword. However you can specify a different argument with id:argnum. xgettext then uses the argnum-th argument. If keyword is of the form id:argnum1,argnum2, xgettext looks for strings in the argnum1-th and in the argnum2-th argument of the call, and treats them as singular/plural variants for a message with plural handling.

keywords: [ 'gettext', '__', 'dgettext:2', 'ngettext:1,2' ]

If keyword is of the form id:contextargnumc,argnum or id:argnum,contextargnumc, xgettext treats strings in the contextargnum-th argument as a context specifier.

keywords: [ 'gettext', '__', 'pgettext:1c,2']

package_name

Type: String Default value: (name specified in your package.json)

This is the name that appears in the header msgid.

package_version

Type: String Default value: (version specified in your package.json)

This is the version that appears in the header msgid

msgid_bugs_address

Type: String Default value: (none)

The email (to report bugs to) that appears in the header msgid

omit_header

Type: Bool Default value: false

Whether to omit the header. It is recommended to keep this false.

comment_tag

Type: String Default value: /

Comments immediately above a listed keyword which begin with this tag will be included as a comment in the generate .pot file. This is useful for providing hints or guidance for translators. For example, in your parsed file(s) you might have:

/// TRANSLATORS: This should be translated as a shorthand for YEAR-MONTH-DAY using 4, 2 and 2 digits.
echo gettext("yyyy-mm-dd");

msgmerge

Type: Bool|String Default value: false

After the .pot file has been generated, you can msgmerge it into existing .po files. This updates the .po files, preserving the translations (as long as they are still required), but updating extracted comments and file references to those given by the newly generated .pot file. Where an exact match cannot be found, fuzzy matching is used to produce better results. In effect this keeps extracted comments and references in the .po files 'up to date' with the .pot files, while ensuring any minor string changes do not loose their existing translation.

You can enable this by setting msgmerge to true, in which case the .po files are assumed to be in the same directory as the generated .pot file. If you wish to specify an alternative directory for the .po files you may set this option to that directory path (with trailing slash).

add_location

Type: Null|String Default value: null

Whether (and how) to include the translatable string's location(s). Accepts 'full' (file and line number), 'file' (file name only) or 'never' (no references). When not specified reverts to the default behaviour of 'full'.

Usage Examples

grunt.initConfig({
  pot: {
      options:{
	  text_domain: 'my-text-domain', //Your text domain. Produces my-text-domain.pot
	  dest: 'languages/', //directory to place the pot file
	  keywords: ['gettext', '__'], //functions to look for
	},
	files:{
	  src:  [ '**/*.php' ], //Parse all php files
	  expand: true,
       }
  },
})
grunt.initConfig({
  pot: {
      options:{
	text_domain: 'my-text-domain', //Your text domain. Produces my-text-domain.pot
	dest: 'languages/', //directory to place the pot file
	keywords: [ 'gettext', 'ngettext:1,2' ], //functions to look for
	msgmerge: true
	},
	files:{
	  src:  [ '**/*.php' ], //Parse all php files
	  expand: true,
       }
  },
})

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 0.3.0 - Added support for --add-location #16; Use fs for directory detection #13.
  • 0.2.1 - Fixes bug if directories are included in files. See #10
  • 0.2.0 - Add msmerge option.
  • 0.1.2 - Pass error messages from exec to Grunt.
  • 0.1.1 - Added language, encoding and overwrite option. Thanks to @robinnorth.
  • 0.1.0 - Initial release

grunt-pot's People

Contributors

robinnorth avatar stephenharris avatar timvevida avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

grunt-pot's Issues

A solution for running under Windows (CygWin)

Hi Stephen @stephenharris ,

I wasn't able to run the msgmerge from within the pot. Running it manually from the shell worked fine, but not from Grunt.

The patch below worked (with Node 0.12).

I am not sure, but it looked to me like async spawn wasn't working well under Windows, even with CygWin.

Check if you want to replace all execs with execSyncs. All works fast enough, so why do you need async?

Thank you!

            if (poFilePaths && options.msgmerge) {
                var execSync = require('child_process').execSync;

                poFilePaths.forEach(function (poFile) {
                    execSync('msgmerge -vU ' + poFile + ' ' + potFile);
                });

            }

Not recognizing package name and version header options

Runnig grunt-pot on Windows with gettext-0.14.4 installed. Xgettext does not recognize --package-version and --package-name options.

Running "pot:files" (pot) task
Destination: lang/roots.pot
Warning: Command failed: xgettext: unrecognized option --package-name=roots
Try (null) --help for more information.
Use --force to continue.

Aborted due to warnings.

If I comment corresponding lines in pot.js file, task runs without errors.

Make it true multitask

This task doesn't seem to be a multitask. I'm new to Grunt but it seems Grunt sends different parameters when you have multiple tasks setup. For it to work properly as a multitask, the code at pot.js:87 should be changed to something like this:

    this.files[0].src.forEach(function(file) {
        if( !grunt.file.isDir( file ) ){
            inputFiles +=  " " + file;
        }
    });

If this alteration is made, the setup should change to something similar to the following :

pot: {
    first_task: {
        options: {
            text_domain: text_domain,
            dest: 'build/languages/',
            keywords: ['gettext', '__', '_e']
        },
        files: {
            src: ['build/**'],
            expand: true
        }
    }
}

msgmerge not updating PO files

Hi @stephenharris!

great plugin! I'm just having trouble updating .PO files. I set msgmerge: true but no update is happening.

GNU gettext tools are in my system (Windows 8.1) and in PATH.

I added some log messages to your "pot.js" file so I can trace if execution goes through your code... and it does! But exec( 'msgmerge -U ' + poFile + ' ' + potFile, function(error, stdout, stderr) {} ); seems to do nothing.

No error is thrown and the process finishes with an exit code of 0.

If I manually execute the command msgmerge -U fileNme.po fileName.pot from Win command line... it works, .PO files get updated.

Any idea what I'm missing??

Thank you very much!

extract both msgid and msgstr from javascript

I realize this question is probably about xgettext, but just in case any body can help. Is it possible to extract both the msgid and msgstr from javascript?

It seems like a very obvious requirement for a grunt-generated pot file but I can't seem to figure out how to do it.

I am trying to avoid the situation where the key changes if you add e.g. a question mark to the string inside your javascript. t('Are you sure') becoming t('Are you sure?').

Folders aren't skipped when creating gettext command

If files contains a directory you'll get this error:

Warning: Command failed: xgettext: warning: file '' extension '' is unknown; will try C
xgettext: error while reading "": Is a directory
Use --force to continue.

Scanning Attributes

Hi,
I trying scanning gettext within attributes, but not working as well. Do need add some configuration to making is work?

Possible to search for strings with a specific textdomain?

I was wondering if it's possible to scan a folder for strings containing only a specific text domain? I'm asking because my theme also contains other text domains (BuddyPress ones) because I have to overwrite certain BuddyPress templates. When I used Grunt-Pot these strings are picked up again and I'd like to find a way around this.

Any help would be awesome and thanks a lot for this huge time saver :-)

Third parameter as context not supported?

I'm creating a grunt task for extracting strings from WordPress and it has been working fine so far, thanks so much! The issue I bumped into was the _nx() function in WordPress. The first parameter is the singular, the second is the plural, the third is the context.

My keywords are set up as below:

 keywords: ["_ex:1,2c", "_x:1,2c", "_nx:1,2,3c"],

In this case the functions are not parsed at all. When I use _nx:1,2 it works fine but doesn't parse the context. If it would be possible to implement this easily that would be awesome. Once I get some breathing room from the project I'll try and submit a pull request if it remains unresolved.

Thanks so much for the grunt task :)

Empty pot file produced

I am using grunt-pot to help with WordPress development. However I cannot seem to get a pot file created with any content in, it always produces an empty pot file.

My grunt task looks like this:

    pot: {
        options: {
            text_domain: 'test_site',
            dest: '<%= yeoman.dev %>/languages/',
            keywords: ['__', '_e']
        },

        files:{
            src:  [ '<%= yeoman.app %>/{,*/{,*/}}*.php' ],
            expand: true
        }
    }

I know the files>src property is fine as the same one works for the watch task.
Here is an example of what I want extracting into the pot file:

<?php _e( 'That page cannot be found.', 'test_site' ); ?>

Have I got an option set wrong?
Many thanks

I'm using grunt-pot 0.1.1

Add more options for header

Being able to customize these lines from the header of the .pot file through options would be nice:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.```
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.