cedaro / grunt-wp-i18n

Internationalize WordPress themes and plugins with Grunt.
MIT License
160 stars 25 forks source link

Makepot output errors, and renaming pot file to include theme name #44

Closed dgwyer closed 9 years ago

dgwyer commented 9 years ago

I'm just starting to use Grunt and the grunt-wp-i18n plugin and am experiencing a couple of issues.

First off I'm running Grunt on Windows 8.1, with the latest versions of Grunt and grunt-wp-i18n. Here are the Grunt files I'm using:

Gruntfile.js:

'use strict';
module.exports = function(grunt) {

 // When this Gruntfile.js runs it:
 //
 // - Does an initial build of style.css.
 // - Builds the theme pot file.
 // - Watches the theme sass folder for any changes to *.scss files and rebuilds style.css when any changes are detected.

 // Project configuration.
 grunt.initConfig({
   pkg: grunt.file.readJSON('package.json'),

   // Config

    // watch for file changes
    watch: {
        compass: {
            files: ['sass/**/*.{scss,sass}'],
            tasks: ['compass']
        }
    },

    // compass and scss
    compass: {
        dist: {
            options: {
                config: 'config.rb',
                force: true
            }
        }
    },

    // compile theme pot file
    makepot: {
        target: {
            options: {
                domainPath: '/languages',
                potFilename: 'wpgo-theme.pot',
                processPot: function( pot ) {
                    var translation,
                        excluded_meta = [
                            'Theme Name of the plugin/theme',
                            'Theme URI of the plugin/theme',
                            'Author of the plugin/theme',
                            'Author URI of the plugin/theme'
                        ];

                    for ( translation in pot.translations[''] ) {
                        if ( 'undefined' !== typeof pot.translations[''][ translation ].comments.extracted ) {
                            if ( excluded_meta.indexOf( pot.translations[''][ translation ].comments.extracted ) >= 0 ) {
                                console.log( 'Excluded meta: ' + pot.translations[''][ translation ].comments.extracted );
                                delete pot.translations[''][ translation ];
                            }
                        }
                    }

                    return pot;
                },
                type: 'wp-theme'
            }
        }
    }

 });

 // Dependent plugins
 grunt.loadNpmTasks( 'grunt-contrib-watch' );
 grunt.loadNpmTasks( 'grunt-contrib-compass' );
 //grunt.loadNpmTasks( 'grunt-contrib-uglify' );
 grunt.loadNpmTasks( 'grunt-wp-i18n' );

 // Default task(s).
 grunt.registerTask('default', ['compass', 'makepot', 'watch']);

};

package.json:

{
  "name": "wpgo-themes-grunt",
  "version": "1.0.0",
  "devDependencies": {
    "grunt": "^0.4.5",
    "grunt-autoprefixer": "^2.0.0",
    "grunt-contrib-compass": "^1.0.1",
    "grunt-contrib-uglify": "^0.6.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-cssjanus": "^0.2.2",
    "grunt-wp-i18n": "^0.4.9"
  }
}

Issue 1:

When running makepot I get warning errors of the type show below (there are quite a lot of them). The resulting pot file seems to be fine so are these warning errors that can safely be ignored? If so is there a way of suppressing them?

Running "makepot:target" (makepot) task

Warning: scandir(.,.): Result too large in C:\Users\David\Documents\Websites\www
.wpgothemes.dev\wp-content\themes\modd-wpgothemes\node_modules\grunt-wp-i18n\ven
dor\wp-i18n-tools\extract.php on line 28

Call Stack:
    0.0004     157936   1. {main}() C:\Users\David\Documents\Websites\www.wpgoth
emes.dev\wp-content\themes\modd-wpgothemes\node_modules\grunt-wp-i18n\vendor\wp-
i18n-tools\grunt-makepot.php:0
    0.0064     611624   2. call_user_func:{C:\Users\David\Documents\Websites\www
.wpgothemes.dev\wp-content\themes\modd-wpgothemes\node_modules\grunt-wp-i18n\ven
dor\wp-i18n-tools\grunt-makepot.php:174}() C:\Users\David\Documents\Websites\www
.wpgothemes.dev\wp-content\themes\modd-wpgothemes\node_modules\grunt-wp-i18n\ven
dor\wp-i18n-tools\grunt-makepot.php:174
...
...
...

Issue 2:

I'd like to be able to rename the pot file after it is generated and include the theme name and version number automatically. This way the I could easily reuse the Grunt files across any theme without the need to edit the pot file name every time it's generated.

I know this may be a more general question and not related to the grunt-wp-i18n as such but any help would be appreciated.


This is a great plugin by the way! Thanks to all contributors for your work putting it together.

GaryJones commented 9 years ago

For 2), the .pot file name should match the slug of the theme, so version should not be included. The version indicator should go into the Project-Id-Version and that is already handled by grunt-wp-i18n.

What you can do is something like this where the values within this (or any) grunt config is extracted into a variable, and can therefore be set once and used multiple times.

dgwyer commented 9 years ago

Thanks Gary. I'm still pretty new to Grunt and fully get, from your comments, how I could rename the pot file name to the theme slug (version number doesn't matter if it's against best practices).

I know in the processPot callback I can access pot.translations['Project-Id-Version'] for the theme name and version, but is it possible to access the theme slug (i.e. the name of the folder Grunt is running in) and to then rename the pot file name to this slug?

bradyvercher commented 9 years ago

Hi @dgwyer, what does your directory structure look like? Are you trying to run Grunt for multiple projects at once?

Grunt configs are usually specific to each project, so after you've defined the potFilename during initial setup, you shouldn't need to change it again. For most projects, potFilename is optional, so if you leave it out, it will automatically use the theme's text domain or its directory name.

dgwyer commented 9 years ago

I'm running Grunt in my root theme folder. Is this the best approach for developing WordPress themes, or should I be looking at running Grunt in the root /wp-content/themes/ folder, or even just /wp-content/ for plugin and theme dev?

I forgot about potFilename so all I needed to do was to get the current working theme directory with this added near the top of Gruntfile.js

var fullPath = __dirname;
var filename = fullPath.replace(/^.*[\\\/]/, '');

And then adding the potFile name as: potFilename: filename + '.pot',

By the way, could you comment on the warning errors I posted above? I just want to know everything is as it should be. It would be nice to suppress the errors if they aren't anything to worry about though.

bradyvercher commented 9 years ago

I was asking about the structure to see if perhaps makepot.php was scanning more files than usual, which might cause that error you're receiving. If you do have a ton of files that shouldn't be scanned, you can exclude them using the exclude option, otherwise you might need to bump your PHP memory limit.

Running Grunt from the root of your theme folder is the standard approach, so you shouldn't have any problems with that setup.

I'm not sure why you're having issues with the potFilename option, though, since grunt-wp-i18n should handle that automatically. For themes, the name of the POT file will either be the text domain you defined in your theme headers or the name of the theme directory. What happens when you remove the potFilename option from your config altogether?

dgwyer commented 9 years ago

I tracked down the problem but it took me quite a while as it turned out to be something unexpected. I tried altering the PHP memory limit, and excluding all number of files/folders but nothing had any effect which I thought was weird.

Almost by chance I switched to a different local server that I had installed sometime ago and ran makepot on the (exact same) theme in that local server, and it worked! The only difference is total number of characters that make up the path to the themes folder. The path to the local server, that makepot threw errors, on was over 20 characters longer than on the one that worked.

There are some pretty deep levels inside the node_modules folder and Windows seems to have a fixed limit on the number of characters that can make up a path. So, I can only assume that the error was caused by a sensitivity to this character limit on one of my local servers!

As for the potFilename, you are correct. If I leave it blank then it defaults to the text domain. However, if I wanted that default fallback to be the theme directory instead of the text domain is there a way to set that? It's not a big issue if not as I can generate the name as described above.

GaryJones commented 9 years ago

As I said above, the theme directory (aka slug) should be the text domain (and .pot filename) anyway, since then everything matches up ready for language packs. See http://ottopress.com/2013/language-packs-101-prepwork/

bradyvercher commented 9 years ago

Thanks for reporting back on what you found out about that character limit -- I never would have guessed.

Regarding the potFilename, if you don't have a text domain in the headers, then it will attempt to use the theme directory name by default, which mimics the functionality in the underlying PHP tools. If you want the behavior to be different, then you'll need to manually configure that option for each project. Like @GaryJones mentioned, though, it's usually best add the text domain header and make it the same as the theme slug.