Skip to content

Commit

Permalink
[localization] [automation] Adjusted script to update Transifex resou…
Browse files Browse the repository at this point in the history
…rce and pull latest translations automatically from Transifex.
  • Loading branch information
vovafeldman committed May 15, 2017
1 parent ac1739d commit 2ba148a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ node_modules
languages/_freemius-en.mo
languages/_freemius-en.po
foobox-image-lightbox.png
transifex-config.json
113 changes: 97 additions & 16 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
var gulp = require('gulp');
var path = require('path');
var filesystem = require('fs');
var wpPot = require('gulp-wp-pot');
var gettext = require('gulp-gettext');
var sort = require('gulp-sort');
var pofill = require('gulp-pofill');
var rename = require('gulp-rename');
var clean = require('gulp-clean');

gulp.task('default', function () {
// Create POT out of i18n.php.
var languagesFolder = './languages/';

var options = require('./transifex-config.json')

function getFolders(dir) {
return filesystem.readdirSync(dir)
.filter(function (file) {
return filesystem.statSync(path.join(dir, file)).isDirectory();
});
}

var transifex = require('gulp-transifex').createClient(options);

// Create POT out of i18n.php.
gulp.task('prepare-source', function () {
gulp.src('includes/i18n.php')
.pipe(sort())
.pipe(wpPot( {
destFile:'freemius.pot',
package: 'freemius',
bugReport: 'https://github.com/Freemius/wordpress-sdk/issues',
lastTranslator: 'Vova Feldman <[email protected]>',
team: 'Freemius Team <[email protected]>',
.pipe(wpPot({
destFile : 'freemius.pot',
package : 'freemius',
bugReport : 'https://github.com/Freemius/wordpress-sdk/issues',
lastTranslator : 'Vova Feldman <[email protected]>',
team : 'Freemius Team <[email protected]>',
gettextFunctions: [
{name: '_fs_text'},
{name: '_fs_echo'},
Expand All @@ -31,14 +47,14 @@ gulp.task('default', function () {
{name: '_fs_nx', plural: 2, context: 4},
{name: '_fs_nx_noop', plural: 2, context: 3}
]
} ))
.pipe(gulp.dest('languages/freemius.pot'));
}))
.pipe(gulp.dest(languagesFolder + 'freemius.pot'));

// Create English PO out of the POT.
gulp.src('languages/freemius.pot')
return gulp.src(languagesFolder + 'freemius.pot')
.pipe(pofill({
items: function(item) {
// If msgstr is empty, use identity translation
items: function (item) {
// If msgstr is empty, use identity translation
if (!item.msgstr.length) {
item.msgstr = [''];
}
Expand All @@ -49,10 +65,75 @@ gulp.task('default', function () {
}
}))
.pipe(rename('freemius-en.po'))
.pipe(gulp.dest('languages/'));
.pipe(gulp.dest(languagesFolder));
});

// Push updated po resource to transifex.
gulp.task('update-transifex', ['prepare-source'], function () {
return gulp.src(languagesFolder + 'freemius-en.po')
.pipe(transifex.pushResource());
});

// Download latest *.po translations.
gulp.task('download-translations', ['update-transifex'], function () {
return gulp.src(languagesFolder + 'freemius-en.po')
.pipe(transifex.pullResource());
});

// Move translations to languages root.
gulp.task('prepare-translations', ['download-translations'], function () {
var folders = getFolders(languagesFolder);

return folders.map(function (folder) {
return gulp.src(path.join(languagesFolder, folder, 'freemius-en.po'))
.pipe(rename('freemius-' + folder + '.po'))
.pipe(gulp.dest(languagesFolder));
});
});

// Feel up empty translations with English.
gulp.task('translations-feelup', ['prepare-translations'], function () {
return gulp.src(languagesFolder + '*.po')
.pipe(pofill({
items: function (item) {
// If msgstr is empty, use identity translation
if (0 == item.msgstr.length) {
item.msgstr = [''];
}
if (0 == item.msgstr[0].length) {
// item.msgid[0] = item.msgid;
item.msgstr[0] = item.msgid;
}
return item;
}
}))
.pipe(gulp.dest(languagesFolder));
});

// Cleanup temporary translation folders.
gulp.task('cleanup', ['prepare-translations'], function () {
var folders = getFolders(languagesFolder);

return folders.map(function (folder) {
return gulp.src(path.join(languagesFolder, folder), {read: false})
.pipe(clean());
});
});

// Compile *.po to *.mo binaries for usage.
gulp.task('compile-translations', ['translations-feelup'], function () {
// Compile POs to MOs.
gulp.src('languages/*.po')
return gulp.src(languagesFolder + '*.po')
.pipe(gettext())
.pipe(gulp.dest('languages/'))
.pipe(gulp.dest(languagesFolder))
});

gulp.task('default', [], function () {
gulp.run('prepare-source');
gulp.run('update-transifex');
gulp.run('download-translations');
gulp.run('prepare-translations');
gulp.run('translations-feelup');
gulp.run('cleanup');
gulp.run('compile-translations');
});
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
"main": "gulpfile.js",
"dependencies": {
"gulp": "^3.9.1",
"gulp-clean": "^0.3.2",
"gulp-fs": "0.0.2",
"gulp-gettext": "^0.3.0",
"gulp-path": "^3.0.3",
"gulp-pofill": "^1.0.0",
"gulp-rename": "^1.2.2",
"gulp-sort": "^2.0.0",
"gulp-wp-pot": "^2.0.4"
"gulp-wp-pot": "^2.0.4",
"gulp-transifex": "^0.1.17"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand All @@ -23,10 +27,14 @@
"homepage": "https://freemius.com",
"devDependencies": {
"gulp": "^3.9.1",
"gulp-clean": "^0.3.2",
"gulp-fs": "0.0.2",
"gulp-gettext": "^0.3.0",
"gulp-path": "^3.0.3",
"gulp-pofill": "^1.0.0",
"gulp-rename": "^1.2.2",
"gulp-sort": "^2.0.0",
"gulp-wp-pot": "^2.0.4"
"gulp-wp-pot": "^2.0.4",
"gulp-transifex": "^0.1.17"
}
}

0 comments on commit 2ba148a

Please sign in to comment.