Skip to content

Instantly share code, notes, and snippets.

@marten-de-vries
Created December 21, 2014 14:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marten-de-vries/d734b71fde92617e717c to your computer and use it in GitHub Desktop.
Save marten-de-vries/d734b71fde92617e717c to your computer and use it in GitHub Desktop.
A base for a node js jekyll gettext preprocessor - requires docs/nl to exist and expects the first user visible string in api.md to be wrapped in a {%t %} tag.
msgid ""
msgstr ""
msgid "Most of the PouchDB API is exposed as `fun(arg, [options], [callback])` where both the options and the callback are optional. Callbacks use the `function(err, result)` idiom where the first argument will be undefined unless there is an error, and the second argument holds the result."
msgstr ""
var fs = require('fs');
var PO = require('pofile');
fs.readFile('docs/api.md', {encoding: 'UTF-8'}, function (err, content) {
// build api.pot based on docs/api.md
buildPOTFile(content, 'api.pot');
// use nl.po to create a docs/nl/api.md
buildTranslatedMarkdown(content, 'nl.po', 'docs/nl/api.md');
});
function buildPOTFile(content, filename) {
var pot = new PO();
findTranslationsInMarkdown(content, function (english) {
var item = new PO.Item();
item.msgid = english;
pot.items.push(item);
});
pot.save(filename);
}
function buildTranslatedMarkdown(content, poName, mdName) {
PO.load(poName, function (err, po) {
var newContent = findAndReplaceTranslationsInMarkdown(content, function (english) {
var result;
for (var i = 0; i < po.items.length; i += 1) {
var item = po.items[i];
if (item.msgid && item.msgid.trim() === english && item.msgstr) {
return item.msgstr[0].trim();
}
}
return english;
});
fs.writeFile(mdName, newContent, {encoding: 'UTF-8'});
});
}
var re = /{%t (.+) %}/g;
function findTranslationsInMarkdown(content, onFound) {
var match;
while (match = re.exec(content)) {
var text = match[1].trim();
var start = match.index;
var length = match[0].length;
onFound(text, start, length);
}
}
function findAndReplaceTranslationsInMarkdown(content, replace) {
var newContent = "";
var lastMatchEnd = 0;
findTranslationsInMarkdown(content, function (text, start, length) {
newContent += content.slice(lastMatchEnd, start);
newContent += replace(text);
lastMatchEnd += start + length;
});
newContent += content.slice(lastMatchEnd);
return newContent;
}
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: Marten de Vries <marten94@gmail.com>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=iso-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.5.4\n"
msgid ""
"Most of the PouchDB API is exposed as `fun(arg, [options], [callback])` "
"where both the options and the callback are optional. Callbacks use the "
"`function(err, result)` idiom where the first argument will be undefined "
"unless there is an error, and the second argument holds the result. "
msgstr ""
"Het grootste deel van de PouchDB API wordt beschikbaar gemaakt in het "
"formaat `fun(arg, [options], [callback])` waarbij zowel options als callback "
"optioneel zijn. Callsbacks gebruiken het `function (err, result)`-formaat "
"waarbij het eerste argument undefined zal zijn tenzij er een fout optreedt, "
"en het tweede argument het resultaat bevat."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment