Search localization strings in .handlebars files

This commit is contained in:
Dmitriy Simushev 2014-08-13 12:14:35 +00:00
parent 95d2593b28
commit 3ca9395d69
2 changed files with 68 additions and 11 deletions

View File

@ -3,6 +3,10 @@ var fs = require('fs'),
exec = require('child_process').exec,
eventStream = require('event-stream'),
runSequence = require('run-sequence'),
through = require('through2'),
lodash = require('lodash'),
PoFile = require('pofile'),
strftime = require('strftime'),
gulp = require('gulp'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
@ -149,21 +153,29 @@ gulp.task('page-styles', function() {
// Generate .pot files based on the sources
gulp.task('generate-pot', function() {
return gulp.src([
config.mibewPath + '/**/*.php',
'!' + config.phpVendorPath + '/**/*.*',
'!' + config.pluginsPath + '/**/*.*'
])
.pipe(xgettext({
language: 'PHP',
keywords: [
{name: 'getlocal'}
]
}))
return eventStream.merge(
gulp.src([
config.mibewPath + '/**/*.php',
'!' + config.phpVendorPath + '/**/*.*',
'!' + config.pluginsPath + '/**/*.*'
])
.pipe(xgettext({
language: 'PHP',
keywords: [
{name: 'getlocal'}
]
})),
gulp.src([
config.chatStylesPath + '/default/templates_src/**/*.handlebars',
config.pageStylesPath + '/default/templates_src/**/*.handlebars'
], {base: config.mibewPath})
.pipe(xgettextHandlebars())
)
.pipe(concatPo('translation.pot', {
headers: {
'Project-Id-Version': config.package.version,
'Report-Msgid-Bugs-To': config.package.bugs.email,
'POT-Creation-Date': strftime('%Y-%m-%d %H:%M%z'),
'PO-Revision-Date': '',
'Last-Translator': '',
'Language-Team': '',
@ -262,3 +274,44 @@ var wrapHandlebarsTemplate = function() {
}
});
}
/**
* Extracts gettext messages from handlebars templates.
*
* @returns {Function} A function that can be used in pipe() method of a file
* stream.
*/
var xgettextHandlebars = function() {
var helperRegExp = /\{{2}l10n\s*('|")(.*?[^\\])\1.*?\}{2}/g;
return through.obj(function (file, enc, callback) {
var po = new PoFile();
match = false,
contents = file.contents.toString();
while (match = helperRegExp.exec(contents)) {
// Try to find item in the .po file by its name.
var item = lodash.find(po.items, function(item) {
return match[2] === item.msgid;
});
var line = contents.substr(0, match.index).split(/\r?\n|\r/g).length;
if (!item) {
// There is no such item. Create new one.
item = new PoFile.Item();
item.msgid = match[2].replace(/\\'/g, "'").replace(/\\"/g, '"');
po.items.push(item);
}
// Add new reference
item.references.push(file.relative + ':' + line);
}
// Update file contents
file.contents = new Buffer(po.toString());
this.push(file);
callback();
});
}

View File

@ -23,6 +23,10 @@
"gulp-xgettext": "~0.2.0",
"gulp-concat-po": "~0.1.0",
"run-sequence": "~0.3.6",
"through2": "^0.5.1",
"pofile": "~0.2.12",
"lodash": "~2.4.1",
"strftime": "~0.8.2",
"event-stream": "~3.1.7"
}
}