-
Notifications
You must be signed in to change notification settings - Fork 153
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
/libpeerconnection.log | ||
/unit-results.xml | ||
/e2e-results.xml | ||
/.idea | ||
/.idea | ||
.project |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
string = context ? $interpolate(string)(context) : string; | ||
return addTranslatedMarkers(string); | ||
}, | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
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) { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, {}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]"); | ||
|
There was a problem hiding this comment.
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).