-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0bf1273
Showing
5 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.*.swp | ||
.idea | ||
/node_modules | ||
.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Dmitry Serpakov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "grunt-angular-gettext-generate-html", | ||
"description": "Tasks for compiling angular-gettext strings and generating project structure per language.", | ||
"version": "0.0.1", | ||
"homepage": "https://github.com/divampo/grunt-angular-gettext-generate-html", | ||
"author": { | ||
"name": "Dmitry Serpakov", | ||
"email": "[email protected]", | ||
"url": "http://divampo.com" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "[email protected]:divampo/grunt-angular-gettext-generate-html.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/divampo/grunt-angular-gettext-generate-html/issues" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/divampo/grunt-angular-gettext-generate-html/blob/master/LICENSE" | ||
} | ||
], | ||
"main": "Gruntfile.js", | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
}, | ||
"devDependencies": { | ||
"grunt": "~0.4.1" | ||
}, | ||
"peerDependencies": { | ||
"grunt": "~0.4.1" | ||
}, | ||
"keywords": [ | ||
"gruntplugin", | ||
"angular", | ||
"gettext", | ||
"generate", | ||
"l10n", | ||
"i18n" | ||
], | ||
"dependencies": { | ||
"cheerio": "~0.18.0", | ||
"lodash": "~2.4.1", | ||
"pofile": "~0.2.8" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
module.exports = function (grunt) { | ||
var Translator = require('./lib/translate').init(grunt).Translator; | ||
|
||
grunt.registerMultiTask('gt_generate_html', 'Generate translated files from given path using .po files', function () { | ||
var options = this.options(); | ||
|
||
// Scanning a file structure | ||
var output = {}; | ||
this.data.source.forEach(function (input) { | ||
var files = grunt.file.expandMapping(input.src, input.dest, input.options || {}); | ||
|
||
files.forEach(function(file) { | ||
output[file.dest] = { | ||
dest: file.dest, | ||
path: file.src, | ||
text: grunt.file.read(file.src) | ||
}; | ||
}); | ||
}); | ||
|
||
// Scanning .po files | ||
var l10n = []; | ||
this.data.po.forEach(function (input) { | ||
var files = grunt.file.expand(input.options || {}, input.src || input); | ||
|
||
files.forEach(function(file) { | ||
l10n.push(grunt.file.read(file)); | ||
}); | ||
}); | ||
|
||
var translator = new Translator(l10n, options); | ||
|
||
translator.iterateLocales(function(locale) { | ||
// Loop through the file structure | ||
for (var dest in output) { | ||
grunt.file.write(dest.replace("{Language}", locale.Language), translator.parse(output[dest], locale.Language)); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
'use strict'; | ||
|
||
var po = require('pofile'); | ||
var _ = require('lodash'); | ||
var cheerio = require('cheerio'); | ||
|
||
exports.init = function(grunt) { | ||
var exports = {}; | ||
|
||
function Translator(po_inputs, options) { | ||
var me = this; | ||
me.options = _.extend({ | ||
preserveScripts: false, | ||
startDelim: "{{", | ||
endDelim: "}}", | ||
startScript: "<%", | ||
endScript: "%>", | ||
}, options); | ||
|
||
// initializing locales | ||
me.locales = {}; | ||
po_inputs.forEach(function(input) { | ||
var catalog = po.parse(input); | ||
|
||
if (!catalog.headers.Language) { | ||
throw new Error('No Language header found!'); | ||
} | ||
|
||
me.locales[catalog.headers.Language] = catalog; | ||
}); | ||
|
||
var escapeRegex = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g; | ||
var start; | ||
var end; | ||
|
||
// initializing comment / inline translation regexp | ||
start = me.options.startDelim.replace(escapeRegex, '\\$&'); | ||
end = me.options.endDelim.replace(escapeRegex, '\\$&'); | ||
if (start === '' && end === '') { | ||
start = '^'; | ||
} | ||
me.stringTokenRegex = new RegExp(start + '\\s*(\'|"|"|')(.*?)\\1\\s*\\|\\s*translate\\s*' + end, 'g'); | ||
|
||
// initializing script safety regexp | ||
me.scripts = {}; | ||
start = me.options.startScript.replace(escapeRegex, '\\$&'); | ||
end = me.options.endScript.replace(escapeRegex, '\\$&'); | ||
me.scriptTokenRegex = new RegExp(start + '[\\s\\S]*?' + end, 'g'); | ||
} | ||
|
||
Translator.prototype.iterateLocales = function (callback) { | ||
for (var lang in this.locales) { | ||
callback.call(this, this.locales[lang].headers); | ||
} | ||
}; | ||
|
||
Translator.prototype.parse = function (input, lang) { | ||
var me = this; | ||
|
||
var translated_text = me.options.preserveScripts === true ? me.preserveScriptsOn(input.text) : input.text; | ||
var $ = cheerio.load(translated_text, _.extend({}, me.options, { | ||
decodeEntities: false, | ||
withStartIndices: true | ||
})); | ||
|
||
$('*').each(function (index, n) { | ||
var node = $(n); | ||
var attr = node.attr(); | ||
var str = node.html().trim(); | ||
|
||
var hasAttr = function(a) { | ||
return attr.hasOwnProperty(a) || attr.hasOwnProperty("data-" + a); | ||
}; | ||
var removeAttr = function (a) { | ||
return node | ||
.removeAttr(a) | ||
.removeAttr("data-" + a); | ||
}; | ||
|
||
var string; | ||
var translation = ""; | ||
|
||
// translating `translate` tag | ||
if (node.is('translate')) { | ||
string = me.findString(str, lang); | ||
node.replaceWith(string === null ? str : string.msgstr[0]); | ||
} | ||
|
||
// translating `translate` attribute | ||
if (hasAttr('translate') && !hasAttr('translate-plural')) { | ||
removeAttr('translate') | ||
removeAttr('translate-comment') | ||
removeAttr('translate-context'); | ||
|
||
string = me.findString(str, lang); | ||
node.text(string === null ? str : string.msgstr[0]); | ||
} | ||
}); | ||
|
||
translated_text = $.html(); | ||
|
||
if (me.options.preserveScripts === true) { | ||
translated_text = me.preserveScriptsOff(translated_text); | ||
} | ||
|
||
// replacing inline translations | ||
var matches; | ||
var string; | ||
var html = ""; | ||
var prevPos = 0; | ||
while ((matches = me.stringTokenRegex.exec(translated_text)) !== null) { | ||
var str = matches[2].replace(/\\\'/g, '\''); | ||
string = me.findString(str, lang); | ||
|
||
html += translated_text.substr(prevPos, this.stringTokenRegex.lastIndex - matches[0].length - prevPos) | ||
+ (string === null ? str : string.msgstr[0]); | ||
|
||
prevPos = me.stringTokenRegex.lastIndex; | ||
} | ||
|
||
// append rest part of html | ||
html += translated_text.substr(prevPos); | ||
|
||
return html; | ||
}; | ||
|
||
Translator.prototype.preserveScriptsOn = function (input) { | ||
var me = this; | ||
var matches; | ||
var out = ""; | ||
var prevPos = 0; | ||
while ((matches = me.scriptTokenRegex.exec(input)) !== null) { | ||
var str = matches[0].replace(/\\\'/g, '\''); | ||
var label = "script_safety_" + me.scriptTokenRegex.lastIndex; | ||
out += input.substr(prevPos, me.scriptTokenRegex.lastIndex - matches[0].length - prevPos) + label; | ||
|
||
me.scripts[label] = str; | ||
prevPos = me.scriptTokenRegex.lastIndex; | ||
} | ||
out += input.substr(prevPos); | ||
|
||
return out; | ||
}; | ||
|
||
Translator.prototype.preserveScriptsOff = function (input) { | ||
var plh; | ||
for (plh in this.scripts) { | ||
input = input.replace(plh, this.scripts[plh]); | ||
} | ||
return input; | ||
}; | ||
|
||
Translator.prototype.findString = function (str, lang) { | ||
var string = null; | ||
this.locales[lang].items.forEach(function(item) { | ||
if (str == item.msgid) { | ||
string = item; | ||
return; | ||
} | ||
}); | ||
return string; | ||
}; | ||
|
||
exports.Translator = Translator; | ||
|
||
return exports; | ||
}; |