Skip to content

Add translatePlural filter #122

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/libpeerconnection.log
/unit-results.xml
/e2e-results.xml
/.idea
/.idea
.project
6 changes: 5 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
"beforeEach",
"browser",
"describe",
"ddescribe",
"element",
"expect",
"inject",
"it"
"it",
"iit",
"xdescribe",
"xit"
]
}
12 changes: 12 additions & 0 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
getPlural: function (n, string, stringPlural, context) {
var form = gettextPlurals(this.currentLanguage, n);
string = this.getStringForm(string, form) || prefixDebug(n === 1 ? string : stringPlural);

// Replace {} pattern with number
string = string.replace(/\{\}/g, n);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct way to do this is by using interpolation (which is supported below).

string = context ? $interpolate(string)(context) : string;
return addTranslatedMarkers(string);
},
Expand Down Expand Up @@ -178,6 +182,14 @@ angular.module('gettext').filter('translate', ["gettextCatalog", function (gette
return filter;
}]);

angular.module('gettext').filter('translatePlural', ['gettextCatalog', function (gettextCatalog) {
function filter(string, n, stringPlural) {
return gettextCatalog.getPlural(n, string, stringPlural);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should become:

return gettextCatalog.getPlural(n, string, stringPlural, {
    $count: n,
});

That should make the {{$count}} placeholder work.

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

// Do not edit this file, it is autogenerated using genplurals.py!
angular.module("gettext").factory("gettextPlurals", function () {
return function (langCode, n) {
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 @@ -69,6 +69,10 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
getPlural: function (n, string, stringPlural, context) {
var form = gettextPlurals(this.currentLanguage, n);
string = this.getStringForm(string, form) || prefixDebug(n === 1 ? string : stringPlural);

// Replace {} pattern with number
string = string.replace(/\{\}/g, n);

string = context ? $interpolate(string)(context) : string;
return addTranslatedMarkers(string);
},
Expand Down
8 changes: 8 additions & 0 deletions src/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ angular.module('gettext').filter('translate', function (gettextCatalog) {
filter.$stateful = true;
return filter;
});

angular.module('gettext').filter('translatePlural', ['gettextCatalog', function (gettextCatalog) {
function filter(string, n, stringPlural) {
return gettextCatalog.getPlural(n, string, stringPlural);
}
filter.$stateful = true;
return filter;
}]);
10 changes: 10 additions & 0 deletions test/unit/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ describe("Catalog", function () {
assert.equal(catalog.getPlural(1, "There is {{count}} bird", "There are {{count}} birds", { count: 1 }), "There is 1 bird");
});

it("Can return an interpolated plural string for special {} pattern", function () {
assert.deepEqual(catalog.strings, {});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to have this assertion.

catalog.currentLanguage = "gb";
catalog.setStrings("gb", {
"There is {} bird": ["There is {} bird", "There are {} birds"]
});
assert.equal(catalog.getPlural(2, "There is {} bird", "There are {} birds"), "There are 2 birds");
assert.equal(catalog.getPlural(1, "There is {} bird", "There are {} birds"), "There is 1 bird");
});

it("Should add translation markers when enabled", function () {
catalog.showTranslatedMarkers = true;
assert.equal(catalog.getString("Bye"), "[Bye]");
Expand Down
53 changes: 53 additions & 0 deletions test/unit/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,56 @@ describe("Filter", function () {
assert.equal(el.attr("placeholder"), "Hallo");
});
});

describe("Filter: translatePlural", function () {
var catalog = null;
var $rootScope = null;
var $compile = null;

beforeEach(module("gettext"));

beforeEach(inject(function ($injector, gettextCatalog) {
$rootScope = $injector.get("$rootScope");
$compile = $injector.get("$compile");
catalog = gettextCatalog;
catalog.setStrings("nl", {
Hello: "Hallo",
"Hello {{name}}!": "Hallo {{name}}!",
"One boat": ["Een boot", "{} boten"]
});

$rootScope.n = 1;
}));

it("Should have a translatePlural filter", function () {
var el = $compile("<h1>{{\"One boat\"|translatePlural:n:\"{} boats\"}}</h1>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "One boat");

$rootScope.n = 2;
$rootScope.$digest();
assert.equal(el.text(), "2 boats");
});

it("Should translate known strings", function () {
catalog.currentLanguage = "nl";
var el = $compile("<span>{{\"One boat\"|translatePlural:n:\"{} boten\"}}</span>")($rootScope);
$rootScope.$digest();
assert.equal(el.text(), "Een boot");

$rootScope.n = 2;
$rootScope.$digest();
assert.equal(el.text(), "2 boten");
});

it("Can use filter in attribute values", function () {
catalog.currentLanguage = "nl";
var el = $compile("<input type=\"text\" placeholder=\"{{'One boat'|translatePlural:n:'{} boten'}}\" />")($rootScope);
$rootScope.$digest();
assert.equal(el.attr("placeholder"), "Een boot");

$rootScope.n = 2;
$rootScope.$digest();
assert.equal(el.attr("placeholder"), "2 boten");
});
});