Skip to content
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
27 changes: 24 additions & 3 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,30 @@ 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);
angular.module('gettext').filter('translate', ["$parse", "$interpolate", "gettextCatalog", function ($parse, $interpolate, gettextCatalog) {
function filter(msgid, options) {
options = options || {};
msgid = options.msgid || msgid;

if (!angular.isObject(options)) {
options = $parse(options)(this) || {};
}

var plural = options.plural;
var pluralScope = options.scope;
if (angular.isString(plural)) {
if (!pluralScope) {
throw new Error('You need to pass options.scope to translateFilter when using plural');
}
pluralScope = pluralScope.$new();
pluralScope.$count = angular.isString(options.n) ? $parse(options.n)(pluralScope) : options.n;
plural = $interpolate(plural)(pluralScope);
}
if (plural || plural === 0) {
return gettextCatalog.getPlural(pluralScope.$count, msgid, plural, null, options.context);
}

return gettextCatalog.getString(msgid, options.scope, options.context);
}
filter.$stateful = true;
return filter;
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.

27 changes: 24 additions & 3 deletions src/filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
angular.module('gettext').filter('translate', function (gettextCatalog) {
function filter(input, context) {
return gettextCatalog.getString(input, null, context);
angular.module('gettext').filter('translate', function ($parse, $interpolate, gettextCatalog) {
function filter(msgid, options) {
options = options || {};
msgid = options.msgid || msgid;

if (!angular.isObject(options)) {
options = $parse(options)(this) || {};
}

var plural = options.plural;
var pluralScope = options.scope;
if (angular.isString(plural)) {
if (!pluralScope) {
throw new Error('You need to pass options.scope to translateFilter when using plural');
}
pluralScope = pluralScope.$new();
pluralScope.$count = angular.isString(options.n) ? $parse(options.n)(pluralScope) : options.n;
plural = $interpolate(plural)(pluralScope);
}
if (plural || plural === 0) {
return gettextCatalog.getPlural(pluralScope.$count, msgid, plural, null, options.context);
}

return gettextCatalog.getString(msgid, options.scope, options.context);
}
filter.$stateful = true;
return filter;
Expand Down
47 changes: 40 additions & 7 deletions test/unit/filter.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
describe("Filter", function () {
var catalog = null;
var $rootScope = null;
var $compile = null;
var catalog;
var filter;
var $rootScope;
var $compile;
var $injector;

beforeEach(module("gettext"));

beforeEach(inject(function ($injector, gettextCatalog) {
beforeEach(inject(function (_$injector_, gettextCatalog, translateFilter) {
$injector = _$injector_;
$rootScope = $injector.get("$rootScope");
$compile = $injector.get("$compile");
filter = translateFilter;
catalog = gettextCatalog;
catalog.setStrings("nl", {
Hello: "Hallo",
Expand All @@ -32,21 +36,50 @@ describe("Filter", function () {

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

it("Should support passing object directly to filter", function () {
catalog.setCurrentLanguage("nl");
assert.equal(filter("Archive", { context: "verb" }), "Archiveren");
});

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("plurals", function () {
it("Should work if n is a number", function () {
catalog.setCurrentLanguage("nl");
var scope = $rootScope.$new();

assert.equal(filter("Een boot", { plural: "{{$count}} boten", n: 2, scope: scope }), "2 boten");
assert.equal(filter("Een boot", { plural: "{{$count}} boten", n: 1, scope: scope }), "Een boot");
});

it("Should work if n is an expression string", function () {
catalog.setCurrentLanguage("nl");
var scope = $rootScope.$new();

scope.count = 2;
$rootScope.$digest();
assert.equal(filter("Een boot", { plural: "{{$count}} boten", n: "count", scope: scope }), "2 boten");

scope.count = 1;
$rootScope.$digest();
assert.equal(filter("Een boot", { plural: "{{$count}} boten", n: "count", scope: scope }), "Een boot");
});
});

});