Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,34 @@ var Compiler = (function () {
throw new Error('No Language header found!');
}

var strings = {};
for (var i = 0; i < catalog.items.length; i++) {
var item = catalog.items[i];
if (item.msgstr[0].length > 0 && !item.flags.fuzzy) {
strings[item.msgid] = item.msgstr.length === 1 ? item.msgstr[0] : item.msgstr;
var strings = catalog.items.reduce(function (items, item) {
if (item.msgstr[0].length === 0 || item.flags.fuzzy) {
return items;
}
}
var hasContext = !!item.msgctxt || item.msgctxt === '';

locales.push(format.addLocale(catalog.headers.Language, strings));
var msgstr = item.msgstr.length === 1 ? item.msgstr[0] : item.msgstr;
var existingItem = items[item.msgid];
if (!existingItem) {
if (hasContext) {
items[item.msgid] = {};
items[item.msgid][item.msgctxt] = msgstr;
} else {
items[item.msgid] = msgstr;
}
} else if (_.isString(existingItem)) {
items[item.msgid] = {
'$$noContext': existingItem
};
items[item.msgid][item.msgctxt] = msgstr;
} else {
items[item.msg][item.msgctxt] = msgstr;
}

return items;
}, {});

locales.push(format.addLocale(catalog.headers.Language, strings));
});

return format.format(locales, this.options);
Expand Down
68 changes: 59 additions & 9 deletions lib/extract.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var cheerio = require('cheerio');
var Po = require('pofile');
var esprima = require('esprima');
Expand Down Expand Up @@ -62,6 +64,7 @@ var Extractor = (function () {
postProcess: function (po) {}
}, options);
this.options.markerNames.unshift(this.options.markerName);

this.strings = {};
this.attrRegex = mkAttrRegex(this.options.startDelim, this.options.endDelim);
}
Expand All @@ -72,19 +75,55 @@ var Extractor = (function () {

Extractor.mkAttrRegex = mkAttrRegex;

Extractor.prototype.addString = function (file, string, plural, extractedComment) {
Extractor.prototype.addString = function (file, string, plural, extractedComment, context) {
string = string.trim();

if (string.length === 0) {
return;
}

if (!this.strings[string]) {
this.strings[string] = new Po.Item();
}
var self = this;
var findItem = function () {
var item;
var existingItem = self.strings[string];
var hasContext = !!context || context === '';

if (!existingItem) {
item = new Po.Item();
if (hasContext) {
self.strings[string] = {};
self.strings[string][context] = item;
} else {
self.strings[string] = item;
}
return item;
}

if (existingItem instanceof Po.Item) {
if (hasContext) {
self.strings[string] = {
'$$noContext': existingItem
};
item = new Po.Item();
self.strings[string][context] = item;
return item;
} else {
return existingItem;
}
} else {
var ctx = hasContext ? context : '$$noContext';
if (!existingItem[ctx]) {
existingItem[ctx] = new Po.Item();
}

return existingItem[ctx];
}
};

var item = findItem();

var item = this.strings[string];
item.msgid = string;
item.msgctxt = context;
if (item.references.indexOf(file) < 0) {
item.references.push(file);
}
Expand Down Expand Up @@ -194,6 +233,7 @@ var Extractor = (function () {
var str;
var plural;
var extractedComment;
var context;
node = $(n);

var getAttr = function (attr) {
Expand All @@ -205,7 +245,8 @@ var Extractor = (function () {
str = node.html();
plural = getAttr('translate-plural');
extractedComment = getAttr('translate-comment');
self.addString(filename, str, plural, extractedComment);
context = getAttr('translate-context');
self.addString(filename, str, plural, extractedComment, context);
} else if (matches = noDelimRegex.exec(node.attr(attr))) {
str = matches[2].replace(/\\\'/g, '\'');
self.addString(filename, str);
Expand Down Expand Up @@ -244,10 +285,19 @@ var Extractor = (function () {
'Project-Id-Version': ''
};

for (var key in this.strings) {
catalog.items.push(this.strings[key]);
}
var self = this;
Object.keys(this.strings).forEach(function (msgstr) {
var item = self.strings[msgstr];
if (item instanceof Po.Item) {
catalog.items.push(item);
} else {
Object.keys(item).forEach(function (context) {
catalog.items.push(item[context]);
});
}
});

// TODO sort by context
catalog.items.sort(function (a, b) {
return a.msgid.localeCompare(b.msgid);
});
Expand Down
17 changes: 17 additions & 0 deletions test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,21 @@ describe('Compile', function () {
'Goodbye!': 'Au revoir!'
});
});

it('Can handle contexts', function () {
var files = ['test/fixtures/context.po'];
var output = testCompile(files, {
format: 'json'
});
var data = JSON.parse(output);

assert.deepEqual(data.nl, {
'Hello!': {
'$$noContext': 'Hallo!',
'male': 'Hallo (male)!'
},
'Goodbye': 'Vaarwel',
'Ciao': { female: 'Ciao (female)' }
});
});
});
16 changes: 16 additions & 0 deletions test/extract.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,19 @@ describe 'Extract', ->
extractor.parse(filename, fs.readFileSync(filename, 'utf8'))
poText = extractor.toString()
assert.equal(/\n"Project-Id-Version: \\n"\n/.test(poText), true)

it 'Should extract context from HTML', ->
files = [
'test/fixtures/context.html'
]
catalog = testExtract(files)

assert.equal(catalog.items.length, 2)

assert.equal(catalog.items[0].msgid, 'Hello!')
assert.equal(catalog.items[0].msgstr, '')
assert.strictEqual(catalog.items[0].msgctxt, null)

assert.equal(catalog.items[1].msgid, 'Hello!')
assert.equal(catalog.items[1].msgstr, '')
assert.equal(catalog.items[1].msgctxt, 'male')
2 changes: 2 additions & 0 deletions test/fixtures/context.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div translate>Hello!</div>
<div translate translate-context="male">Hello!</div>
29 changes: 29 additions & 0 deletions test/fixtures/context.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
msgid ""
msgstr ""
"Language: nl\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Project-Id-Version: \n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
"X-Generator: Poedit 1.5.7\n"

#: test/fixtures/context.html
msgid "Hello!"
msgstr "Hallo!"

#: test/fixtures/context.html
msgctxt "male"
msgid "Hello!"
msgstr "Hallo (male)!"

msgid "Goodbye"
msgstr "Vaarwel"

msgctxt "female"
msgid "Ciao"
msgstr "Ciao (female)"