Skip to content
Open
10 changes: 8 additions & 2 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var formats = {
return ' gettextCatalog.setStrings(\'' + locale + '\', ' + JSON.stringify(strings) + ');\n';
},
format: function (locales, options) {
var module = 'angular.module(\'' + options.module + '\')' +
var module = 'angular.module(\'' + options.module + '\',[\'gettext\'])\n' +
'.run([\'gettextCatalog\', function (gettextCatalog) {\n' +
'/* jshint -W100 */\n' +
locales.join('') +
Expand All @@ -20,7 +20,13 @@ var formats = {
module += '}]);';

if (options.requirejs) {
return 'define([\'angular\', \'' + options.modulePath + '\'], function (angular) {\n' + module + '\n});';
var nameStr = (options.requirejsName) ? ('\'' + options.requirejsName + '\',') : '';
var deps = [];
if (options.requirejsAngular) { deps.push(options.requirejsAngular); }
if (options.requirejsModule) { deps.push(options.requirejsModule); }
var depsStr = JSON.stringify(deps).replace(/"/gi, '\'');
var paramStr = (options.requirejsAngular) ? 'angular' : '';
return 'define( ' + nameStr + depsStr + ', function (' + paramStr + ') {\nreturn ' + module + '\n});';
}

return module;
Expand Down
96 changes: 51 additions & 45 deletions test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,37 @@ function makeEnv(mod, catalog) {
};
}


// Fake Angular environment with RequireJS module loader
function makeRequireJsEnv(mod, modPath, catalog) {
function makeRequireJsExpliciteEnv(mod, name, module, catalog) {
return {
define: function (expliciteName, modules, callback) {
assert.equal(expliciteName, name);
assert.equal(modules[0], 'angular');
assert.equal(modules[1], module);

var angular = {
module: function (modDefined) {
assert.equal(modDefined, mod);
return {
run: function (block) {
assert.equal(block[0], 'gettextCatalog');
block[1](catalog);
}
};
}
};
callback(angular);
}
};
}

// Fake Angular environment with RequireJS module loader
function makeRequireJsEnv(mod, module, catalog) {
return {
define: function (modules, callback) {
assert.equal(modules[0], 'angular');
assert.equal(modules[1], modPath);
assert.equal(modules[1], module);

var angular = {
module: function (modDefined) {
Expand Down Expand Up @@ -116,7 +141,8 @@ describe('Compile', function () {
var output = testCompile(files, {
module: 'myApp',
requirejs: true,
modulePath: './test/fixtures/module'
requirejsAngular: 'angular',
requirejsModule: './test/fixtures/module'
});
var catalog = {
called: false,
Expand All @@ -130,6 +156,27 @@ describe('Compile', function () {
assert(catalog.called);
});

it('Accepts a requirejs and name parameter', function () {
var files = ['test/fixtures/nl.po'];
var output = testCompile(files, {
module: 'myApp',
requirejs: true,
requirejsName: 'testName',
requirejsAngular: 'angular',
requirejsModule: './test/fixtures/module'
});
var catalog = {
called: false,
setStrings: function (language, strings) {
this.called = true;
}
};

var context = vm.createContext(makeRequireJsExpliciteEnv('myApp', 'testName', './test/fixtures/module', catalog));
vm.runInContext(output, context);
assert(catalog.called);
});

it('Allows merging multiple languages', function () {
var files = ['test/fixtures/nl.po', 'test/fixtures/fr.po'];
var output = testCompile(files);
Expand Down Expand Up @@ -242,45 +289,4 @@ describe('Compile', function () {
vm.runInContext(output, context);
assert(catalog.called);
});

it('Leaves html-entities in msgids that are not converted in the browser in tact', function () {
var files = ['test/fixtures/inconvertible_html_entities.po'];
var output = testCompile(files, {
format: 'json'
});
var data = JSON.parse(output);
assert.deepEqual(data.nl, {
'non-breaking space': 'harde spatie',
'greater > than': 'groter > dan',
'less < than': 'kleiner < dan',
'and &': 'en <'
});
});

it('Converts html-entities in msgids that are also converted in the browser', function () {
var files = ['test/fixtures/convertible_html_entities.po'];
var output = testCompile(files, {
format: 'json'
});
var data = JSON.parse(output);
assert.deepEqual(data.nl, {
'dots…': 'puntjes…',
'cents ¢, pounds £ and euros €': 'centen ¢, ponden £ and euro’s €',
'«double» and ‹single› guillemets': '«dubbel» en ‹enkele› guillemets',
'copyright © and registered ® trade ™ marks': 'kopierecht © en geregistreerde ® handels ™ merken',
'§ sections and paragraphs¶': '§ secties en paragrafen¶',
'hot 10° cold': 'heet ° koud',
'± greater ≥ or less than ≤': '± groter ≥ of kleiner dan ≤',
'not ≠ or approximately ≈ equal': 'niet ≠ of ongeveer ≈ gelijk',
'middle · dot': 'middel · punt',
'en – and em — dash': 'gedachte– of aandachts— streepje',
'‘single’ ‘quotes‚': '‘enkele’ ‘aanhalingstekens‚',
'“double” “quotes„': '“dubbele” “aanhalingstekens„',
'† dagger ‡': '† obelisk ‡',
'• bullet': '• opsommingsteken',
'10′23″': '10′23″ tijd',
'square root ² and to the power of ³': 'kwadraat ² en to de macht ³',
'fraction two ½ three ⅓ four ¼ and three fourths ¾': 'helft ½ derde ⅓ kwart ¼ en drie-vierde ¾'
});
});
});
});