Skip to content

Instantly share code, notes, and snippets.

@dasilvaluis
Last active September 27, 2021 09:34
Show Gist options
  • Select an option

  • Save dasilvaluis/ebca42b8b8d70e81f8917f675a784060 to your computer and use it in GitHub Desktop.

Select an option

Save dasilvaluis/ebca42b8b8d70e81f8917f675a784060 to your computer and use it in GitHub Desktop.

Revisions

  1. Luís Silva revised this gist Sep 1, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    "name": "twig-gettext-pot-parser",
    "version": "1.2.0",
    "dependencies": {
    "gulp": "^3.9.1",
    "gulp": "3.9.1",
    "del": "^3.0.0",
    "gulp-if": "^2.0.2",
    "gulp-rename": "^1.2.2",
  2. Luís Silva revised this gist Dec 11, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gulpfile.js
    Original file line number Diff line number Diff line change
    @@ -152,7 +152,7 @@ gulp.task('compile-twigs', () => {
    // Rename file with .php extension
    .pipe(rename({
    extname: '.php',
    })
    }))
    // Output the result to the cache folder as a .php file.
    .pipe(gulp.dest(config.cacheFolder));
    });
  3. Luís Silva revised this gist Dec 11, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion twig-gettext.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@

    // Init twig engine
    global $twig;
    $loader = new Twig_Loader_Filesystem( UAWAPP_PLUGIN_DIR . '/views' );
    $loader = new Twig_Loader_Filesystem( '/views' );
    $twig = new Twig_Environment( $loader, [] );

    // Add wordpress gettext functions
  4. Luís Silva revised this gist Nov 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion twig-gettext.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <?php
    /**
    * Include Wordpress gettext functions in Twig
    * Include Wordpress gettext functions in Twig if necessary
    */

    // Init twig engine
  5. Luís Silva revised this gist Nov 20, 2018. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions gulpfile.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /**
    * Gettext Scanner Script for Twig Projects
    * v1.2
    * v1.3
    *
    * Developed by Luís Silva
    * https://github.com/luism-s
    @@ -16,9 +16,10 @@
    * script was built to parse .twig files, wrap occurrences of gettext function calls in php tags and
    * output the result as a .php file and from that generate a POT file.
    * It also scans PHP files as well.
    *
    * Context: https://github.com/timber/timber/issues/1465
    *
    * Usage:
    * `gulp pot`
    * Usage: `gulp pot`
    *
    * Logic:
    * - Iterates over all given .twig files
  6. Luís Silva revised this gist Nov 20, 2018. 1 changed file with 67 additions and 81 deletions.
    148 changes: 67 additions & 81 deletions gulpfile.js
    Original file line number Diff line number Diff line change
    @@ -36,13 +36,13 @@
    * TODO:
    * Cover `translate_nooped_plural` function.
    */
    var gulp = require('gulp');
    var gulpif = require('gulp-if');
    var del = require('del');
    var wpPot = require('gulp-wp-pot');
    var replace = require('gulp-replace');
    var rename = require('gulp-rename');
    var runSequence = require('run-sequence');
    const gulp = require('gulp');
    const gulpif = require('gulp-if');
    const del = require('del');
    const wpPot = require('gulp-wp-pot');
    const replace = require('gulp-replace');
    const rename = require('gulp-rename');
    const runSequence = require('run-sequence');

    /**
    * Configuration Options
    @@ -51,7 +51,7 @@ var runSequence = require('run-sequence');
    * located in the root of the theme and all the Twig
    * files are located under /views
    */
    var config = {
    const config = {
    "text_domain" : "theme-test", // Replace with your domain
    "twig_files" : "views/**/*.twig", // Twig Files
    "php_files" : "**/*.php", // PHP Files
    @@ -60,6 +60,50 @@ var config = {
    "keepCache" : true // Delete cache files after script finishes
    };


    /**
    * __
    * _e
    * _x
    * _xn
    * _ex
    * _n_noop
    * _nx_noop
    * translate -> Match __, _e, _x and so on
    * \( -> Match (
    * \s*? -> Match empty space 0 or infinite times, as few times as possible (ungreedy)
    * ['"] -> Match ' or "
    * .+? -> Match any character, 1 to infinite times, as few times as possible (ungreedy)
    * , -> Match ,
    * .+? -> Match any character, 1 to infinite times, as few times as possible (ungreedy)
    * \) -> Match )
    */
    const gettext_regex = {

    // _e( "text", "domain" )
    // __( "text", "domain" )
    // translate( "text", "domain" )
    // esc_attr__( "text", "domain" )
    // esc_attr_e( "text", "domain" )
    // esc_html__( "text", "domain" )
    // esc_html_e( "text", "domain" )
    simple: /(__|_e|translate|esc_attr__|esc_attr_e|esc_html__|esc_html_e)\(\s*?['"].+?['"]\s*?,\s*?['"].+?['"]\s*?\)/g,

    // _n( "single", "plural", number, "domain" )
    plural: /_n\(\s*?['"].*?['"]\s*?,\s*?['"].*?['"]\s*?,\s*?.+?\s*?,\s*?['"].+?['"]\s*?\)/g,

    // _x( "text", "context", "domain" )
    // _ex( "text", "context", "domain" )
    // esc_attr_x( "text", "context", "domain" )
    // esc_html_x( "text", "context", "domain" )
    // _nx( "single", "plural", "number", "context", "domain" )
    disambiguation: /(_x|_ex|_nx|esc_attr_x|esc_html_x)\(\s*?['"].+?['"]\s*?,\s*?['"].+?['"]\s*?,\s*?['"].+?['"]\s*?\)/g,

    // _n_noop( "singular", "plural", "domain" )
    // _nx_noop( "singular", "plural", "context", "domain" )
    noop: /(_n_noop|_nx_noop)\((\s*?['"].+?['"]\s*?),(\s*?['"]\w+?['"]\s*?,){0,1}\s*?['"].+?['"]\s*?\)/g,
    };

    /**
    * Main Task
    */
    @@ -71,13 +115,14 @@ gulp.task('pot', function(callback) {
    * Generate POT file from all .php files in the theme,
    * including the cache folder.
    */
    gulp.task('generate-pot', function() {
    return gulp.src(config.php_files)
    .pipe(wpPot( {
    gulp.task('generate-pot', () => {
    const output = gulp.src(config.php_files)
    .pipe(wpPot({
    domain: config.text_domain
    } ))
    .pipe(gulp.dest(config.destFolder + '/' + config.text_domain + '.pot'))
    .pipe(gulpif(!config.keepCache, del.bind(null, [config.cacheFolder], {force: true})));
    }))
    .pipe(gulp.dest(`${config.destFolder}/${config.text_domain}.pot`))
    .pipe(gulpif(!config.keepCache, del.bind(null, [config.cacheFolder], { force: true })));
    return output;
    });

    /**
    @@ -93,79 +138,20 @@ gulp.task('generate-pot', function() {
    * Plural: _n()
    * Disambiguation: _x(), _ex(), _nx()
    * Noop: _n_loop(), _nx_noop()
    *
    * TODO:
    * - Support translate_nooped_plural() function
    * - Skip gettext calls insied Twig comments (ex. {# __('string', 'domain') #})
    */
    gulp.task('compile-twigs', function() {
    gulp.task('compile-twigs', () => {
    del.bind(null, [config.cacheFolder], {force: true})

    /**
    * __
    * _e
    * _x
    * _xn
    * _ex
    * _n_noop
    * _nx_noop
    * translate -> Match __, _e, _x and so on
    * \( -> Match (
    * \s*? -> Match empty space 0 or infinite times, as few times as possible (ungreedy)
    * [\'\"] -> Match ' or "
    * .+? -> Match any character, 1 to infinite times, as few times as possible (ungreedy)
    * , -> Match ,
    * .+? -> Match any character, 1 to infinite times, as few times as possible (ungreedy)
    * \) -> Match )
    */
    var gettext_regex = {

    // _e( "text", "domain" )
    // __( "text", "domain" )
    // translate( "text", "domain" )
    // esc_attr__( "text", "domain" )
    // esc_attr_e( "text", "domain" )
    // esc_html__( "text", "domain" )
    // esc_html_e( "text", "domain" )
    simple : /(__|_e|translate|esc_attr__|esc_attr_e|esc_html__|esc_html_e)\(\s*?[\'\"].+?[\'\"]\s*?,\s*?[\'\"].+?[\'\"]\s*?\)/g,

    // _n( "single", "plural", number, "domain" )
    plural : /_n\(\s*?[\'\"].*?[\'\"]\s*?,\s*?[\'\"].*?[\'\"]\s*?,\s*?.+?\s*?,\s*?[\'\"].+?[\'\"]\s*?\)/g,

    // _x( "text", "context", "domain" )
    // _ex( "text", "context", "domain" )
    // esc_attr_x( "text", "context", "domain" )
    // esc_html_x( "text", "context", "domain" )
    // _nx( "single", "plural", "number", "context", "domain" )
    disambiguation : /(_x|_ex|_nx|esc_attr_x|esc_html_x)\(\s*?[\'\"].+?[\'\"]\s*?,\s*?[\'\"].+?[\'\"]\s*?,\s*?[\'\"].+?[\'\"]\s*?\)/g,

    // _n_noop( "singular", "plural", "domain" )
    // _nx_noop( "singular", "plural", "context", "domain" )
    noop : /(_n_noop|_nx_noop)\((\s*?[\'\"].+?[\'\"]\s*?),(\s*?[\'\"]\w+?[\'\"]\s*?,){0,1}\s*?[\'\"].+?[\'\"]\s*?\)/g
    };

    // Iterate over .twig files
    gulp.src(config.twig_files)

    // Search for Gettext function calls and wrap them around PHP tags.
    .pipe(replace(gettext_regex.simple, function( match ){
    return '<?php ' + match + '; ?>';
    }))
    .pipe(replace(gettext_regex.plural, function( match ){
    return '<?php ' + match + '; ?>';
    }))
    .pipe(replace(gettext_regex.disambiguation, function( match ){
    return '<?php ' + match + '; ?>';
    }))
    .pipe(replace(gettext_regex.noop, function( match ){
    return '<?php ' + match + '; ?>';
    }))

    .pipe(replace(gettext_regex.simple, match => `<?php ${match}; ?>`))
    .pipe(replace(gettext_regex.plural, match => `<?php ${match}; ?>`))
    .pipe(replace(gettext_regex.disambiguation, match => `<?php ${match}; ?>`))
    .pipe(replace(gettext_regex.noop, match => `<?php ${match}; ?>`))
    // Rename file with .php extension
    .pipe(rename(function( file_path ) {
    file_path.extname = ".php"
    }))

    .pipe(rename({
    extname: '.php',
    })
    // Output the result to the cache folder as a .php file.
    .pipe(gulp.dest(config.cacheFolder));
    });
  7. Luís Silva revised this gist Sep 15, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gulpfile.js
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,8 @@
    * While working with Wordpress using the Twig template engine, one might find easier to use gettext
    * functions in .twig files for string translation. To simplify the scanning of .twig files for those same functions, this
    * script was built to parse .twig files, wrap occurrences of gettext function calls in php tags and
    * output the result as a .php file so it can be parsed normally by POEdit.
    * output the result as a .php file and from that generate a POT file.
    * It also scans PHP files as well.
    *
    * Usage:
    * `gulp pot`
  8. Luís Silva revised this gist Sep 15, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions gulpfile.js
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,9 @@
    */

    /**
    * Purpose:
    * Scan Twig and PHP files in the given directories for gettext function calls and output a POT file for translation.
    *
    * Description:
    * While working with Wordpress using the Twig template engine, one might find easier to use gettext
    * functions in .twig files for string translation. To simplify the scanning of .twig files for those same functions, this
  9. Luís Silva revised this gist Sep 15, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions twig-gettext.php
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,14 @@
    <?php
    /**
    * Include Wordpress gettext functions in Twig
    */

    // Init twig engine
    global $twig;
    $loader = new Twig_Loader_Filesystem( UAWAPP_PLUGIN_DIR . '/views' );
    $twig = new Twig_Environment( $loader, [] );

    // Add wordpress gettext functions
    $twig->addFunction(new Twig_SimpleFunction('__', function( $text, $domain = 'default' ) {
    return __($text, $domain);
    } ));
  10. Luís Silva revised this gist Sep 15, 2018. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions twig-gettext.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?php

    // Init twig engine
    global $twig;
    $loader = new Twig_Loader_Filesystem( UAWAPP_PLUGIN_DIR . '/views' );
    $twig = new Twig_Environment( $loader, [] );

    $twig->addFunction(new Twig_SimpleFunction('__', function( $text, $domain = 'default' ) {
    return __($text, $domain);
    } ));
    $twig->addFunction(new Twig_SimpleFunction('_n', function( $single, $plural, $number, $domain = 'default' ) {
    return _n($single, $plural, $number, $domain);
    } ));
    $twig->addFunction(new Twig_SimpleFunction('_x', function( $text, $context, $domain = 'default' ) {
    return _x($text, $context, $domain);
    } ));
    $twig->addFunction(new Twig_SimpleFunction('_ex', function( $text, $context, $domain = 'default' ) {
    return _ex($text, $context, $domain);
    } ));
    $twig->addFunction(new Twig_SimpleFunction('_nx', function( $single, $plural, $number, $context, $domain = 'default' ) {
    return _nx($single, $plural, $number, $context, $domain);
    } ));
  11. Luís Silva revised this gist Sep 15, 2018. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions gulpfile.js
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,8 @@

    /**
    * Description:
    * While developing templates using the Twig template engine, one might find easier to use gettext
    * functions in .twig files for string translation, or wordpress translate functions if it's the case
    * (Ex: Timber WP Plugin). To simplify the scanning of .twig files for those same functions, this
    * While working with Wordpress using the Twig template engine, one might find easier to use gettext
    * functions in .twig files for string translation. To simplify the scanning of .twig files for those same functions, this
    * script was built to parse .twig files, wrap occurrences of gettext function calls in php tags and
    * output the result as a .php file so it can be parsed normally by POEdit.
    *
  12. Luís Silva revised this gist Sep 15, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gulpfile.js
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@

    /**
    * Description:
    * While developing templates using the Twig Template Engine, one might find easier to use gettext
    * While developing templates using the Twig template engine, one might find easier to use gettext
    * functions in .twig files for string translation, or wordpress translate functions if it's the case
    * (Ex: Timber WP Plugin). To simplify the scanning of .twig files for those same functions, this
    * script was built to parse .twig files, wrap occurrences of gettext function calls in php tags and
  13. Luís Silva revised this gist Sep 15, 2018. No changes.
  14. Luís Silva renamed this gist Sep 15, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  15. Luís Silva renamed this gist Sep 15, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  16. Luís Silva revised this gist May 1, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    {
    "name": "twig-gettext-pot-parser",
    "version": "1.2",
    "version": "1.2.0",
    "dependencies": {
    "gulp": "^3.9.1",
    "del": "^3.0.0",
  17. Luís Silva renamed this gist Apr 20, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  18. Luís Silva renamed this gist Apr 20, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  19. Luís Silva revised this gist Apr 1, 2018. 2 changed files with 16 additions and 0 deletions.
    File renamed without changes.
    16 changes: 16 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    {
    "name": "twig-gettext-pot-parser",
    "version": "1.2",
    "dependencies": {
    "gulp": "^3.9.1",
    "del": "^3.0.0",
    "gulp-if": "^2.0.2",
    "gulp-rename": "^1.2.2",
    "gulp-replace": "^0.6.1",
    "gulp-wp-pot": "^2.2.0",
    "run-sequence": "^2.2.1"
    },
    "scripts": {
    "twig-pot": "gulp pot"
    }
    }
  20. Luís Silva revised this gist Apr 1, 2018. 1 changed file with 22 additions and 26 deletions.
    48 changes: 22 additions & 26 deletions pot.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /**
    * Gettext Scanner Script for Twig Projects
    * v1.1.1
    * v1.2
    *
    * Developed by Luís Silva
    * https://github.com/luism-s
    @@ -33,7 +33,6 @@
    * TODO:
    * Cover `translate_nooped_plural` function.
    */

    var gulp = require('gulp');
    var gulpif = require('gulp-if');
    var del = require('del');
    @@ -42,7 +41,6 @@ var replace = require('gulp-replace');
    var rename = require('gulp-rename');
    var runSequence = require('run-sequence');


    /**
    * Configuration Options
    *
    @@ -51,17 +49,13 @@ var runSequence = require('run-sequence');
    * files are located under /views
    */
    var config = {
    "text_domain" : "text_domain", // Replace with your domain
    "twig_files" : [ // Twig Files
    "views/**/*.twig"
    ],
    "php_files" : [ // PHP Files
    "**/*.php"
    ],
    "destFolder" : "lang", // Folder where .pot file will be saved
    "cacheFolder" : "views/cache", // Cache folder
    "keepCache" : true // Delete cache files after script finishes
    }
    "text_domain" : "theme-test", // Replace with your domain
    "twig_files" : "views/**/*.twig", // Twig Files
    "php_files" : "**/*.php", // PHP Files
    "cacheFolder" : "views/cache", // Cache Folder
    "destFolder" : "languages", // Folder where .pot file will be saved
    "keepCache" : true // Delete cache files after script finishes
    };

    /**
    * Main Task
    @@ -72,9 +66,9 @@ gulp.task('pot', function(callback) {

    /**
    * Generate POT file from all .php files in the theme,
    * including ../views/chache/ folder
    * including the cache folder.
    */
    gulp.task('generate-pot', function () {
    gulp.task('generate-pot', function() {
    return gulp.src(config.php_files)
    .pipe(wpPot( {
    domain: config.text_domain
    @@ -92,17 +86,18 @@ gulp.task('generate-pot', function () {
    *
    * Functions supported:
    *
    * Simple: __(), translate(), _e()
    * Simple: __(), _e(), translate()
    * Plural: _n()
    * Disambiguation: _x(), _ex(), _nx()
    * Noop: _n_loop(), _nx_noop()
    *
    * TODO:
    * Support translate_nooped_plural() function
    * - Support translate_nooped_plural() function
    * - Skip gettext calls insied Twig comments (ex. {# __('string', 'domain') #})
    */
    gulp.task('compile-twigs', function(){
    gulp.task('compile-twigs', function() {
    del.bind(null, [config.cacheFolder], {force: true})

    /**
    * __
    * _e
    @@ -117,7 +112,7 @@ gulp.task('compile-twigs', function(){
    * [\'\"] -> Match ' or "
    * .+? -> Match any character, 1 to infinite times, as few times as possible (ungreedy)
    * , -> Match ,
    * \w+? -> Match any word character, 1 to infinite times, as few times as possible (ungreedy)
    * .+? -> Match any character, 1 to infinite times, as few times as possible (ungreedy)
    * \) -> Match )
    */
    var gettext_regex = {
    @@ -129,21 +124,21 @@ gulp.task('compile-twigs', function(){
    // esc_attr_e( "text", "domain" )
    // esc_html__( "text", "domain" )
    // esc_html_e( "text", "domain" )
    simple : /(__|_e|translate|esc_attr__|esc_attr_e|esc_html__|esc_html_e)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*\)/g,
    simple : /(__|_e|translate|esc_attr__|esc_attr_e|esc_html__|esc_html_e)\(\s*?[\'\"].+?[\'\"]\s*?,\s*?[\'\"].+?[\'\"]\s*?\)/g,

    // _n( "single", "plural", number, "domain" )
    plural : /(_n)\(\s*([\'\"].*?[\'\"])\s*,\s*([\'\"].*?[\'\"])\s*,\s*(.+?)\s*,\s*([\'\"]\w+?[\'\"])\s*/g,
    plural : /_n\(\s*?[\'\"].*?[\'\"]\s*?,\s*?[\'\"].*?[\'\"]\s*?,\s*?.+?\s*?,\s*?[\'\"].+?[\'\"]\s*?\)/g,

    // _x( "text", "context", "domain" )
    // _ex( "text", "context", "domain" )
    // esc_attr_x( "text", "context", "domain" )
    // esc_html_x( "text", "context", "domain" )
    // _nx( "single", "plural", "number", "context", "domain" )
    disambiguation : /(_x|_ex|_nx|esc_attr_x|esc_html_x)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"].+?[\'\"])\s*,\s*([\'\"].+?[\'\"])\s*\)/g,
    disambiguation : /(_x|_ex|_nx|esc_attr_x|esc_html_x)\(\s*?[\'\"].+?[\'\"]\s*?,\s*?[\'\"].+?[\'\"]\s*?,\s*?[\'\"].+?[\'\"]\s*?\)/g,

    // _n_noop( "singular", "plural", "domain" )
    // _nx_noop( "singular", "plural", "context", "domain" )
    noop : /(_n_noop|_nx_noop)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*\)/g
    noop : /(_n_noop|_nx_noop)\((\s*?[\'\"].+?[\'\"]\s*?),(\s*?[\'\"]\w+?[\'\"]\s*?,){0,1}\s*?[\'\"].+?[\'\"]\s*?\)/g
    };

    // Iterate over .twig files
    @@ -162,11 +157,12 @@ gulp.task('compile-twigs', function(){
    .pipe(replace(gettext_regex.noop, function( match ){
    return '<?php ' + match + '; ?>';
    }))

    // Rename file with .php extension
    .pipe(rename(function( file_path ) {
    file_path.extname = ".php"
    }))

    // Output the result to the cache folder as a .php file.
    .pipe(gulp.dest(config.cacheFolder));

    });
  21. Luís Silva revised this gist Dec 29, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions pot.js
    Original file line number Diff line number Diff line change
    @@ -146,7 +146,10 @@ gulp.task('compile-twigs', function(){
    noop : /(_n_noop|_nx_noop)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*\)/g
    };

    // Iterate over .twig files
    gulp.src(config.twig_files)

    // Search for Gettext function calls and wrap them around PHP tags.
    .pipe(replace(gettext_regex.simple, function( match ){
    return '<?php ' + match + '; ?>';
    }))
    @@ -162,6 +165,8 @@ gulp.task('compile-twigs', function(){
    .pipe(rename(function( file_path ) {
    file_path.extname = ".php"
    }))

    // Output the result to the cache folder as a .php file.
    .pipe(gulp.dest(config.cacheFolder));

    });
  22. Luís Silva revised this gist Dec 29, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion pot.js
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,6 @@
    *
    * TODO:
    * Cover `translate_nooped_plural` function.
    *
    */

    var gulp = require('gulp');
  23. Luís Silva revised this gist Dec 29, 2017. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions pot.js
    Original file line number Diff line number Diff line change
    @@ -24,19 +24,17 @@
    * - Scan all .php files for gettext functions using 'gulp-wp-pot' (cache included)
    * - Generate .pot file
    *
    * Dependencies:
    * `npm install gulp gulp-if del gulp-wp-pot gulp-replace gulp-rename run-sequence`
    *
    * Warning:
    * This script has only ben tested in the context of Wordpress theme development using Timber.
    *
    * TODO:
    * Cover `translate_nooped_plural` function.
    *
    */

    /**
    * Dependencies:
    *
    * `npm install gulp gulp-if del gulp-wp-pot gulp-replace gulp-rename run-sequence`
    */
    var gulp = require('gulp');
    var gulpif = require('gulp-if');
    var del = require('del');
  24. Luís Silva revised this gist Dec 29, 2017. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions pot.js
    Original file line number Diff line number Diff line change
    @@ -29,8 +29,7 @@
    * This script has only ben tested in the context of Wordpress theme development using Timber.
    *
    * TODO:
    * Cover `translate_nooped_plural`function usage
    *
    * Cover `translate_nooped_plural` function.
    */

    /**
  25. Luís Silva revised this gist Dec 29, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions pot.js
    Original file line number Diff line number Diff line change
    @@ -84,7 +84,7 @@ gulp.task('generate-pot', function () {
    domain: config.text_domain
    } ))
    .pipe(gulp.dest(config.destFolder + '/' + config.text_domain + '.pot'))
    .pipe(gulpif(!config.pot.keepCache, del.bind(null, [config.pot.cacheFolder], {force: true})));
    .pipe(gulpif(!config.keepCache, del.bind(null, [config.cacheFolder], {force: true})));
    });

    /**
    @@ -150,7 +150,7 @@ gulp.task('compile-twigs', function(){
    noop : /(_n_noop|_nx_noop)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*\)/g
    };

    gulp.src(config.pot.twig_files)
    gulp.src(config.twig_files)
    .pipe(replace(gettext_regex.simple, function( match ){
    return '<?php ' + match + '; ?>';
    }))
  26. Luís Silva revised this gist Dec 29, 2017. 1 changed file with 39 additions and 19 deletions.
    58 changes: 39 additions & 19 deletions pot.js
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,42 @@
    /**
    * Gettext Scanner Script for Twig Projects
    * v1.0.1
    * v1.1.1
    *
    * Developed by Luís Silva
    * https://github.com/luism-s
    */

    /**
    * Usage:
    * `gulp pot`
    *
    * Logic:
    *
    * - Iterates over all given .twig files
    * - Search and replace for gettext functions in Twig files and wraps them around PHP tags
    * - Outputs each file as .php into a cache folder
    * - Scan all .php files for gettext functions using 'gulp-wp-pot' (cache included)
    * - Generate .pot file
    */
    /**
    * Description:
    * While developing templates using the Twig Template Engine, one might find easier to use gettext
    * functions in .twig files for string translation, or wordpress translate functions if it's the case
    * (Ex: Timber WP Plugin). To simplify the scanning of .twig files for those same functions, this
    * script was built to parse .twig files, wrap occurrences of gettext function calls in php tags and
    * output the result as a .php file so it can be parsed normally by POEdit.
    *
    * Usage:
    * `gulp pot`
    *
    * Logic:
    * - Iterates over all given .twig files
    * - Search and replace for gettext functions in Twig files and wraps them around PHP tags
    * - Outputs each file as .php into a cache folder
    * - Scan all .php files for gettext functions using 'gulp-wp-pot' (cache included)
    * - Generate .pot file
    *
    *
    * Warning:
    * This script has only ben tested in the context of Wordpress theme development using Timber.
    *
    * TODO:
    * Cover `translate_nooped_plural`function usage
    *
    */

    /**
    * Dependencies:
    *
    * `npm install gulp gulp-if del gulp-wp-pot gulp-replace gulp-rename run-sequence --save-dev`
    * `npm install gulp gulp-if del gulp-wp-pot gulp-replace gulp-rename run-sequence`
    */
    var gulp = require('gulp');
    var gulpif = require('gulp-if');
    @@ -70,7 +84,7 @@ gulp.task('generate-pot', function () {
    domain: config.text_domain
    } ))
    .pipe(gulp.dest(config.destFolder + '/' + config.text_domain + '.pot'))
    .pipe(gulpif(!config.keepCache, del.bind(null, [config.cacheFolder], {force: true})));
    .pipe(gulpif(!config.pot.keepCache, del.bind(null, [config.pot.cacheFolder], {force: true})));
    });

    /**
    @@ -111,26 +125,32 @@ gulp.task('compile-twigs', function(){
    * \) -> Match )
    */
    var gettext_regex = {

    // _e( "text", "domain" )
    // __( "text", "domain" )
    // translate( "text", "domain" )
    simple : /(__|_e|translate)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*\)/g,
    // esc_attr__( "text", "domain" )
    // esc_attr_e( "text", "domain" )
    // esc_html__( "text", "domain" )
    // esc_html_e( "text", "domain" )
    simple : /(__|_e|translate|esc_attr__|esc_attr_e|esc_html__|esc_html_e)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*\)/g,

    // _n( "single", "plural", number, "domain" )
    plural : /(_n)\(\s*([\'\"].*?[\'\"])\s*,\s*([\'\"].*?[\'\"])\s*,\s*(.+?)\s*,\s*([\'\"]\w+?[\'\"])\s*/g,

    // _x( "text", "context", "domain" )
    // _ex( "text", "context", "domain" )
    // esc_attr_x( "text", "context", "domain" )
    // esc_html_x( "text", "context", "domain" )
    // _nx( "single", "plural", "number", "context", "domain" )
    disambiguation : /(_x|_ex|_nx)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"].+?[\'\"])\s*,\s*([\'\"].+?[\'\"])\s*\)/g,
    disambiguation : /(_x|_ex|_nx|esc_attr_x|esc_html_x)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"].+?[\'\"])\s*,\s*([\'\"].+?[\'\"])\s*\)/g,

    // _n_noop( "singular", "plural", "domain" )
    // _nx_noop( "singular", "plural", "context", "domain" )
    noop : /(_n_noop|_nx_noop)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*\)/g
    };

    gulp.src(config.twig_files)
    gulp.src(config.pot.twig_files)
    .pipe(replace(gettext_regex.simple, function( match ){
    return '<?php ' + match + '; ?>';
    }))
  27. Luís Silva revised this gist Dec 29, 2017. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions pot.js
    Original file line number Diff line number Diff line change
    @@ -19,11 +19,11 @@
    * - Generate .pot file
    */

    /**
    * Dependencies:
    *
    * `npm install gulp gulp-if del gulp-wp-pot gulp-replace gulp-rename run-sequence`
    */
    /**
    * Dependencies:
    *
    * `npm install gulp gulp-if del gulp-wp-pot gulp-replace gulp-rename run-sequence --save-dev`
    */
    var gulp = require('gulp');
    var gulpif = require('gulp-if');
    var del = require('del');
  28. Luís Silva revised this gist Dec 29, 2017. 1 changed file with 17 additions and 4 deletions.
    21 changes: 17 additions & 4 deletions pot.js
    Original file line number Diff line number Diff line change
    @@ -111,10 +111,23 @@ gulp.task('compile-twigs', function(){
    * \) -> Match )
    */
    var gettext_regex = {
    simple : /(__|_e|translate)\(\s*?[\'\"].+?[\'\"]\s*?,\s*?[\'\"]\w+?[\'\"]\s*?\)/g,
    plural : /_n\(\s*?[\'\"].*?[\'\"]\s*?,\s*?[\'\"].*?[\'\"]\s*?,\s*?.+?\s*?,\s*?[\'\"]\w+?[\'\"]\s*?\)/g,
    disambiguation : /(_x|_ex|_nx)\(\s*?[\'\"].+?[\'\"]\s*?,\s*?[\'\"].+?[\'\"]\s*?,\s*?[\'\"].+?[\'\"]\s*?\)/g,
    noop : /(_n_noop|_nx_noop)\((\s*?[\'\"].+?[\'\"]\s*?),(\s*?[\'\"]\w+?[\'\"]\s*?,){0,1}\s*?[\'\"]\w+?[\'\"]\s*?\)/g

    // _e( "text", "domain" )
    // __( "text", "domain" )
    // translate( "text", "domain" )
    simple : /(__|_e|translate)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*\)/g,

    // _n( "single", "plural", number, "domain" )
    plural : /(_n)\(\s*([\'\"].*?[\'\"])\s*,\s*([\'\"].*?[\'\"])\s*,\s*(.+?)\s*,\s*([\'\"]\w+?[\'\"])\s*/g,

    // _x( "text", "context", "domain" )
    // _ex( "text", "context", "domain" )
    // _nx( "single", "plural", "number", "context", "domain" )
    disambiguation : /(_x|_ex|_nx)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"].+?[\'\"])\s*,\s*([\'\"].+?[\'\"])\s*\)/g,

    // _n_noop( "singular", "plural", "domain" )
    // _nx_noop( "singular", "plural", "context", "domain" )
    noop : /(_n_noop|_nx_noop)\(\s*([\'\"].+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*,\s*([\'\"]\w+?[\'\"])\s*\)/g
    };

    gulp.src(config.twig_files)
  29. Luís Silva revised this gist Dec 29, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pot.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    * Gettext Scanner Script for Twig Projects
    * v1.0.1
    *
    * Developed by luism-s
    * Developed by Luís Silva
    * https://github.com/luism-s
    */

  30. Luís Silva revised this gist Dec 29, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pot.js
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ var runSequence = require('run-sequence');
    * files are located under /views
    */
    var config = {
    "text_domain" : "text_domain", // Replace with your domain
    "text_domain" : "text_domain", // Replace with your domain
    "twig_files" : [ // Twig Files
    "views/**/*.twig"
    ],