Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visitor pattern and POT adapter #51

Closed
wants to merge 6 commits into from
Closed
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
11 changes: 9 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,15 @@ module.exports = function(grunt) {
'tt-default="\'((?:\\\\.|[^\'\\\\])*)\'\\|translate"'
],
dest: 'tmp'
}
},

extract_to_pot: {
adapter: 'pot',
prefix: 'template',
src: [ 'test/fixtures/*.html', 'test/fixtures/*.js' ],
lang: [ '' ],
dest: 'tmp'
}
},

// Unit tests.
Expand Down Expand Up @@ -233,7 +240,7 @@ module.exports = function(grunt) {

// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['clean', 'i18nextract', 'nodeunit', 'clean']);
grunt.registerTask('test', ['clean', 'i18nextract', 'nodeunit']);

// By default, lint and run all tests.
grunt.registerTask('default', ['test']);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"grunt-contrib-clean": "^0.5.0",
"grunt-contrib-copy": "^0.5.0",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt-contrib-nodeunit": "~0.4.1",
"grunt-markdox": "^0.1.0"
},
"peerDependencies": {
Expand Down Expand Up @@ -53,7 +53,8 @@
"readmeFilename": "README.md",
"dependencies": {
"flat": "^1.2.0",
"json-stable-stringify": "^1.0.0",
"lodash": "~2.4.1",
"json-stable-stringify": "^1.0.0"
"pofile": "^0.2.12"
}
}
89 changes: 25 additions & 64 deletions tasks/angular-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,16 @@ module.exports = function (grunt) {
dest = this.data.dest || '.',
jsonSrc = _file.expand(this.data.jsonSrc || []),
jsonSrcName = _.union(this.data.jsonSrcName || [], ['label']),
defaultLang = this.data.defaultLang || '.',
interpolation = this.data.interpolation || {startDelimiter: '{{', endDelimiter: '}}'},
source = this.data.source || '',
nullEmpty = this.data.nullEmpty || false,
namespace = this.data.namespace || false,
prefix = this.data.prefix || '',
safeMode = this.data.safeMode ? true : false,
suffix = this.data.suffix || '.json',
suffix = this.data.suffix,
customRegex = _.isArray(this.data.customRegex) ? this.data.customRegex : [],
stringify_options = this.data.stringifyOptions || null,
adapter = this.data.adapter || 'json',
results = {};

var customStringify = function (val) {
if (stringify_options) {
return stringify(val, _.isObject(stringify_options) ? stringify_options : {
space: ' ',
cmp: function (a, b) {
var lower = function (a) {
return a.toLowerCase();
};
return lower(a.key) < lower(b.key) ? -1 : 1;
}
});
}
return JSON.stringify(val, null, 4);
};

// Use to escape some char into regex patterns
var escapeRegExp = function (str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
Expand Down Expand Up @@ -286,51 +269,29 @@ module.exports = function (grunt) {
"nullEmpty": nullEmpty
}, results);

// Build all output langage files
this.data.lang.forEach(function (lang) {

var destFilename = dest + '/' + prefix + lang + suffix,
filename = source,
translations = {},
json = {};

// Test source filename
if (filename === '' || !_file.exists(filename)) {
filename = destFilename;
}

_log.subhead('Process ' + lang + ' : ' + filename);

var isDefaultLang = (defaultLang === lang);
if (!_file.exists(filename)) {
_log.debug('File doesn\'t exist');

_log.writeln('Create file: ' + destFilename + (isDefaultLang ? ' (' + lang + ' is the default language)' : ''));
translations = _translation.getMergedTranslations({}, isDefaultLang);

} else {
_log.debug('File exist');
json = _file.readJSON(filename);
translations = _translation.getMergedTranslations(Translations.flatten(json), isDefaultLang);
}

var stats = _translation.getStats();
var statEmptyType = nullEmpty ? "null" : "empty";
var statPercentage = Math.round(stats[statEmptyType] / stats["total"] * 100);
statPercentage = isNaN(statPercentage) ? 100 : statPercentage;
var statsString = "Statistics : " +
statEmptyType + ": " + stats[statEmptyType] + " (" + statPercentage + "%)" +
" / Updated: " + stats["updated"] +
" / Deleted: " + stats["deleted"] +
" / New: " + stats["new"];

_log.writeln(statsString);

// Write JSON file for lang
_file.write(destFilename, customStringify(translations));

});
// Prepare some params to pass to the adapter
var params = {
lang: this.data.lang,
dest: dest,
prefix: prefix,
suffix: suffix,
source: this.data.source,
defaultLang: this.data.defaultLang,
stringifyOptions: this.data.stringifyOptions
};

switch(adapter) {
case 'pot':
var PotAdapter = require('./lib/pot-adapter.js');
var toPot = new PotAdapter(grunt);
toPot.init(params);
_translation.persist(toPot);
break;
default:
var JsonAdapter = require('./lib/json-adapter.js');
var toJson = new JsonAdapter(grunt);
toJson.init(params);
_translation.persist(toJson);
}
});

};
89 changes: 89 additions & 0 deletions tasks/lib/json-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* grunt-angular-translate
* https://github.com/firehist/grunt-angular-translate
*
* Copyright (c) 2013 "firehist" Benjamin Longearet, contributors
* Licensed under the MIT license.
*
*/

(function() {
'use strict';

var _log, _file;
var Utils = require('./utils.js');
var Translations = require('./translations.js');

function JsonAdapter(grunt) {
_log = grunt.log;
_file = grunt.file;
}

JsonAdapter.prototype.init = function(params) {
this.dest = params.dest || '.';
this.lang = params.lang;
this.prefix = params.prefix;
this.suffix = params.suffix || '.json';
this.source = params.source;
this.defaultLang = params.defaultLang;
this.stringifyOptions = params.stringifyOptions;
};

JsonAdapter.prototype.persist = function(_translation) {
var lang = this.lang;
var dest = this.dest;
var prefix = this.prefix;
var suffix = this.suffix;
var source = this.source || '';
var defaultLang = this.defaultLang || '.';
var stringify_options = this.stringifyOptions || null;

// Build all output language files
lang.forEach(function (lang) {

var destFilename = dest + '/' + prefix + lang + suffix,
filename = source,
translations = {},
json = {};

// Test source filename
if (filename === '' || !_file.exists(filename)) {
filename = destFilename;
}

_log.subhead('Process ' + lang + ' : ' + filename);

var isDefaultLang = (defaultLang === lang);
if (!_file.exists(filename)) {
_log.debug('File doesn\'t exist');

_log.writeln('Create file: ' + destFilename + (isDefaultLang ? ' (' + lang + ' is the default language)' : ''));
translations = _translation.getMergedTranslations({}, isDefaultLang);

} else {
_log.debug('File exist');
json = _file.readJSON(filename);
translations = _translation.getMergedTranslations(Translations.flatten(json), isDefaultLang);
}

var stats = _translation.getStats();
var statEmptyType = _translation.params.nullEmpty ? "null" : "empty";
var statPercentage = Math.round(stats[statEmptyType] / stats["total"] * 100);
statPercentage = isNaN(statPercentage) ? 100 : statPercentage;
var statsString = "Statistics : " +
statEmptyType + ": " + stats[statEmptyType] + " (" + statPercentage + "%)" +
" / Updated: " + stats["updated"] +
" / Deleted: " + stats["deleted"] +
" / New: " + stats["new"];

_log.writeln(statsString);

// Write JSON file for lang
var utils = new Utils();
_file.write(destFilename, utils.customStringify(translations, stringify_options));

});
};

module.exports = JsonAdapter;
}());
62 changes: 62 additions & 0 deletions tasks/lib/pot-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* grunt-angular-translate
* https://github.com/firehist/grunt-angular-translate
*
* Copyright (c) 2015 "originof" Manuel Mazzuola, contributors
* Licensed under the MIT license.
*
*/

(function() {
'use strict';

var _file;
var Po = require('pofile');

function PotObject(id, msg, ctx) {
this.id = id;
this.msg = msg || '';
this.ctx = ctx || '';
}

PotObject.prototype.toString = function() {
return "" +
"msgctxt \"" + String(this.ctx).replace(/"/g, '\\"') + "\"\n" +
"msgid \"" + String(this.id).replace(/"/g, '\\"') + "\"\n" +
"msgstr \"" + String(this.msg).replace(/"/g, '\\"') + "\"";
};

function PotAdapter(grunt) {
_file = grunt.file;
}

PotAdapter.prototype.init = function(params) {
this.dest = params.dest || '.';
this.prefix = params.prefix;
this.suffix = params.suffix || '.pot';
};

PotAdapter.prototype.persist = function(_translation) {
var translations = _translation.getMergedTranslations({});
var catalog = new Po();

catalog.headers = {
'Content-Type': 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding': '8bit',
'Project-Id-Version': ''
};

for (var msg in translations) {
catalog.items.push(new PotObject(msg, translations[msg]));
}

catalog.items.sort(function(a, b) {
return a.id.toLowerCase().localeCompare(b.id.toLowerCase());
});

_file.write(this.dest + '/' + this.prefix + this.suffix, catalog.toString());
};

module.exports = PotAdapter;
}());

8 changes: 8 additions & 0 deletions tasks/lib/translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ Translations.prototype.incStat = function (type) {
this._stats[type]++;
}
}

/**
* Call the adapter to persist to disk
* @param {Function} adapter Function to call
*/
Translations.prototype.persist = function (adapter) {
adapter.persist(this);
}
/**
* Wrap of flat.flatten method
* @type {Function}
Expand Down
26 changes: 26 additions & 0 deletions tasks/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(function() {
'use strict';

var _ = require('lodash');
var stringify = require('json-stable-stringify');

function Utils() {
}

Utils.prototype.customStringify = function(val, options) {
if (options) {
return stringify(val, _.isObject(options) ? options : {
space: ' ',
cmp: function (a, b) {
var lower = function (a) {
return a.toLowerCase();
};
return lower(a.key) < lower(b.key) ? -1 : 1;
}
});
}
return JSON.stringify(val, null, 4);
};

module.exports = Utils;
}());
10 changes: 10 additions & 0 deletions test/angular-translate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ exports.i18nextract = {
var expected = grunt.file.read( 'test/expected/10_fr_FR.json' );
test.equal( actual, expected, 'Should equal.' );

test.done();
},

extract_to_pot: function(test) {
test.expect(1);

var actual = grunt.file.read( 'tmp/template.pot' );
var expected = grunt.file.read( 'test/expected/template.pot' );
test.equal( actual, expected, 'Should equal.' );

test.done();
}

Expand Down
Loading