Skip to content

Commit

Permalink
translatePlural and translateContext via chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
gabegorelick committed Dec 10, 2014
1 parent 749d441 commit 07ede21
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 29 deletions.
68 changes: 61 additions & 7 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
},

getPlural: function (n, string, stringPlural, scope, context) {
if (!n && n !== 0) {
return this.getString(string, scope, context);
}

var form = gettextPlurals(this.currentLanguage, n);
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
Expand Down Expand Up @@ -206,13 +210,63 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
};
}]);

angular.module('gettext').filter('translate', ["gettextCatalog", function (gettextCatalog) {
function filter(input, context) {
return gettextCatalog.getString(input, null, context);
}
filter.$stateful = true;
return filter;
}]);
(function () {
var translate = function (gettextCatalog, $gettext) {
var message = gettextCatalog.getPlural($gettext.n, $gettext.msgid, $gettext.plural, null, $gettext.context);
if ($gettext.n || $gettext.n === 0) {
return message.replace(/(^|\s)\$count\b/g, '$1' + $gettext.n);
} else {
return message;
}
};

angular.module('gettext').filter('translate', ["gettextCatalog", function (gettextCatalog) {
function filter(msgid) {
var $gettext = msgid.$gettext || { msgid: msgid };

// translate is the only filter that returns a string primitive
return translate(gettextCatalog, $gettext);
}
filter.$stateful = true;
return filter;
}]);

angular.module('gettext').filter('translatePlural', ["gettextCatalog", function (gettextCatalog) {
function filter(msgid, n, plural) {
var $gettext = msgid.$gettext || { msgid: msgid };
$gettext.n = n;
$gettext.plural = plural;

/*jshint -W053 */
// might as well return the correct String, even if it is a wrapper type
var message = new String(translate(gettextCatalog, $gettext));
/*jshint +W053 */

message.$gettext = $gettext;
return message;
}
filter.$stateful = true;
return filter;
}]);

angular.module('gettext').filter('translateContext', ["gettextCatalog", function (gettextCatalog) {
function filter(msgid, context) {
var $gettext = msgid.$gettext || { msgid: msgid };
$gettext.context = context;

var message = translate(gettextCatalog, $gettext);

/*jshint -W053 */
message = new String(message);
/*jshint +W053 */

message.$gettext = $gettext;
return message;
}
filter.$stateful = true;
return filter;
}]);
})();

// Do not edit this file, it is autogenerated using genplurals.py!
angular.module("gettext").factory("gettextPlurals", function () {
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-gettext.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
},

getPlural: function (n, string, stringPlural, scope, context) {
if (!n && n !== 0) {
return this.getString(string, scope, context);
}

var form = gettextPlurals(this.currentLanguage, n);
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
Expand Down
65 changes: 58 additions & 7 deletions src/filter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
angular.module('gettext').filter('translate', function (gettextCatalog) {
function filter(input, context) {
return gettextCatalog.getString(input, null, context);
}
filter.$stateful = true;
return filter;
});
(function () {
var translate = function (gettextCatalog, $gettext) {
var message = gettextCatalog.getPlural($gettext.n, $gettext.msgid, $gettext.plural, null, $gettext.context);
if ($gettext.n || $gettext.n === 0) {
// replace $count with n, preserving leading whitespace
return message.replace(/(^|\s)\$count\b/g, '$1' + $gettext.n);
} else {
return message;
}
};

angular.module('gettext').filter('translate', function (gettextCatalog) {
function filter(msgid) {
var $gettext = msgid.$gettext || { msgid: msgid };

// translate is the only filter that returns a string primitive
return translate(gettextCatalog, $gettext);
}
filter.$stateful = true;
return filter;
});

angular.module('gettext').filter('translatePlural', function (gettextCatalog) {
function filter(msgid, n, plural) {
var $gettext = msgid.$gettext || { msgid: msgid };
$gettext.n = n;
$gettext.plural = plural;

/*jshint -W053 */
// might as well return the correct String, even if it is a wrapper type
var message = new String(translate(gettextCatalog, $gettext));
/*jshint +W053 */

message.$gettext = $gettext;
return message;
}
filter.$stateful = true;
return filter;
});

angular.module('gettext').filter('translateContext', function (gettextCatalog) {
function filter(msgid, context) {
var $gettext = msgid.$gettext || { msgid: msgid };
$gettext.context = context;

var message = translate(gettextCatalog, $gettext);

/*jshint -W053 */
message = new String(message);
/*jshint +W053 */

message.$gettext = $gettext;
return message;
}
filter.$stateful = true;
return filter;
});
})();
89 changes: 75 additions & 14 deletions test/unit/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("Filter", function () {
catalog.setStrings("nl", {
Hello: "Hallo",
"Hello {{name}}!": "Hallo {{name}}!",
"One boat": ["Een boot", "{{count}} boten"],
"One boat": ["Een boot", "$count boten"],
Archive: { verb: "Archiveren", noun: "Archief", $$noContext: "Archief (no context)" }
});
}));
Expand All @@ -30,23 +30,84 @@ describe("Filter", function () {
assert.equal(el.text(), "Hallo");
});

it("Should translate known strings according to translate context", function () {
catalog.setCurrentLanguage("nl");
var el = $compile("<span>{{\"Archive\"|translate:'verb'}}</span>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Archiveren");
el = $compile("<span>{{\"Archive\"|translate:'noun'}}</span>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Archief");
el = $compile("<span>{{\"Archive\"|translate}}</span>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Archief (no context)");
});

it("Can use filter in attribute values", function () {
catalog.setCurrentLanguage("nl");
var el = $compile("<input type=\"text\" placeholder=\"{{'Hello'|translate}}\" />")($rootScope);
$rootScope.$digest();
assert.equal(el.attr("placeholder"), "Hallo");
});

describe("translatePlural", function () {

// not sure why you'd want to do this, but it's a good test case
it("Should work if n is a number", function () {
catalog.setCurrentLanguage("nl");
var el = $compile("<span>{{'One boat' | translatePlural:2:'$count boten' | translate}}</span>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "2 boten");
});

it("Should work if n is a reference", function () {
catalog.setCurrentLanguage("nl");
var scope = $rootScope.$new();
scope.count = 2;
var el = $compile("<span>{{'One boat' | translatePlural:count:'$count boten' | translate}}</span>")(scope);
$rootScope.$digest();
assert.equal(el.text(), "2 boten");

scope.count = 1;
el = $compile("<span>{{'One boat' | translatePlural:count:'$count boten' | translate}}</span>")(scope);
$rootScope.$digest();
assert.equal(el.text(), "Een boot");
});

it("Should work if it precedes translateContext", function () {
catalog.setCurrentLanguage("nl");
catalog.setStrings("nl", {
"One boat": { c1: ["Een boot1", "$count boten1"], c2: ["Een boot", "$count boten"] }
});

var scope = $rootScope.$new();
scope.count = 2;
var el = $compile("<span>{{'One boat' | translatePlural:count:'$count boten' | translateContext:'c2' | translate}}</span>")(scope);
$rootScope.$digest();
assert.equal(el.text(), "2 boten");
});
});

describe("translateContext", function () {
it("Should translate known strings according to translateContext", function () {
catalog.setCurrentLanguage("nl");
var el = $compile("<span>{{'Archive' | translateContext:'verb' | translate}}</span>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Archiveren");
el = $compile("<span>{{'Archive' | translateContext:'noun' | translate}}</span>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Archief");
});

it("Should work with no args", function () {
// translateContext with no args is the same as translate
catalog.setCurrentLanguage("nl");
var el = $compile("<span>{{'Archive' | translateContext | translate}}</span>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Archief (no context)");
el = $compile("<span>{{'Archive' | translate }}</span>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Archief (no context)");
});

it("Should work if it precedes translatePlural", function () {
catalog.setCurrentLanguage("nl");
catalog.setStrings("nl", {
"One boat": { c1: ["Een boot1", "$count boten1"], c2: ["Een boot", "$count boten"] }
});

var scope = $rootScope.$new();
scope.count = 2;
var el = $compile("<span>{{'One boat' | translateContext:'c2' | translatePlural:count:'$count boten' | translate}}</span>")(scope);
$rootScope.$digest();
assert.equal(el.text(), "2 boten");
});
});
});

0 comments on commit 07ede21

Please sign in to comment.