Skip to content

Commit 07ede21

Browse files
committed
translatePlural and translateContext via chaining
1 parent 749d441 commit 07ede21

File tree

5 files changed

+199
-29
lines changed

5 files changed

+199
-29
lines changed

Diff for: dist/angular-gettext.js

+61-7
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
101101
},
102102

103103
getPlural: function (n, string, stringPlural, scope, context) {
104+
if (!n && n !== 0) {
105+
return this.getString(string, scope, context);
106+
}
107+
104108
var form = gettextPlurals(this.currentLanguage, n);
105109
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
106110
if (scope) {
@@ -206,13 +210,63 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
206210
};
207211
}]);
208212

209-
angular.module('gettext').filter('translate', ["gettextCatalog", function (gettextCatalog) {
210-
function filter(input, context) {
211-
return gettextCatalog.getString(input, null, context);
212-
}
213-
filter.$stateful = true;
214-
return filter;
215-
}]);
213+
(function () {
214+
var translate = function (gettextCatalog, $gettext) {
215+
var message = gettextCatalog.getPlural($gettext.n, $gettext.msgid, $gettext.plural, null, $gettext.context);
216+
if ($gettext.n || $gettext.n === 0) {
217+
return message.replace(/(^|\s)\$count\b/g, '$1' + $gettext.n);
218+
} else {
219+
return message;
220+
}
221+
};
222+
223+
angular.module('gettext').filter('translate', ["gettextCatalog", function (gettextCatalog) {
224+
function filter(msgid) {
225+
var $gettext = msgid.$gettext || { msgid: msgid };
226+
227+
// translate is the only filter that returns a string primitive
228+
return translate(gettextCatalog, $gettext);
229+
}
230+
filter.$stateful = true;
231+
return filter;
232+
}]);
233+
234+
angular.module('gettext').filter('translatePlural', ["gettextCatalog", function (gettextCatalog) {
235+
function filter(msgid, n, plural) {
236+
var $gettext = msgid.$gettext || { msgid: msgid };
237+
$gettext.n = n;
238+
$gettext.plural = plural;
239+
240+
/*jshint -W053 */
241+
// might as well return the correct String, even if it is a wrapper type
242+
var message = new String(translate(gettextCatalog, $gettext));
243+
/*jshint +W053 */
244+
245+
message.$gettext = $gettext;
246+
return message;
247+
}
248+
filter.$stateful = true;
249+
return filter;
250+
}]);
251+
252+
angular.module('gettext').filter('translateContext', ["gettextCatalog", function (gettextCatalog) {
253+
function filter(msgid, context) {
254+
var $gettext = msgid.$gettext || { msgid: msgid };
255+
$gettext.context = context;
256+
257+
var message = translate(gettextCatalog, $gettext);
258+
259+
/*jshint -W053 */
260+
message = new String(message);
261+
/*jshint +W053 */
262+
263+
message.$gettext = $gettext;
264+
return message;
265+
}
266+
filter.$stateful = true;
267+
return filter;
268+
}]);
269+
})();
216270

217271
// Do not edit this file, it is autogenerated using genplurals.py!
218272
angular.module("gettext").factory("gettextPlurals", function () {

Diff for: dist/angular-gettext.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/catalog.js

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
8989
},
9090

9191
getPlural: function (n, string, stringPlural, scope, context) {
92+
if (!n && n !== 0) {
93+
return this.getString(string, scope, context);
94+
}
95+
9296
var form = gettextPlurals(this.currentLanguage, n);
9397
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
9498
if (scope) {

Diff for: src/filter.js

+58-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,58 @@
1-
angular.module('gettext').filter('translate', function (gettextCatalog) {
2-
function filter(input, context) {
3-
return gettextCatalog.getString(input, null, context);
4-
}
5-
filter.$stateful = true;
6-
return filter;
7-
});
1+
(function () {
2+
var translate = function (gettextCatalog, $gettext) {
3+
var message = gettextCatalog.getPlural($gettext.n, $gettext.msgid, $gettext.plural, null, $gettext.context);
4+
if ($gettext.n || $gettext.n === 0) {
5+
// replace $count with n, preserving leading whitespace
6+
return message.replace(/(^|\s)\$count\b/g, '$1' + $gettext.n);
7+
} else {
8+
return message;
9+
}
10+
};
11+
12+
angular.module('gettext').filter('translate', function (gettextCatalog) {
13+
function filter(msgid) {
14+
var $gettext = msgid.$gettext || { msgid: msgid };
15+
16+
// translate is the only filter that returns a string primitive
17+
return translate(gettextCatalog, $gettext);
18+
}
19+
filter.$stateful = true;
20+
return filter;
21+
});
22+
23+
angular.module('gettext').filter('translatePlural', function (gettextCatalog) {
24+
function filter(msgid, n, plural) {
25+
var $gettext = msgid.$gettext || { msgid: msgid };
26+
$gettext.n = n;
27+
$gettext.plural = plural;
28+
29+
/*jshint -W053 */
30+
// might as well return the correct String, even if it is a wrapper type
31+
var message = new String(translate(gettextCatalog, $gettext));
32+
/*jshint +W053 */
33+
34+
message.$gettext = $gettext;
35+
return message;
36+
}
37+
filter.$stateful = true;
38+
return filter;
39+
});
40+
41+
angular.module('gettext').filter('translateContext', function (gettextCatalog) {
42+
function filter(msgid, context) {
43+
var $gettext = msgid.$gettext || { msgid: msgid };
44+
$gettext.context = context;
45+
46+
var message = translate(gettextCatalog, $gettext);
47+
48+
/*jshint -W053 */
49+
message = new String(message);
50+
/*jshint +W053 */
51+
52+
message.$gettext = $gettext;
53+
return message;
54+
}
55+
filter.$stateful = true;
56+
return filter;
57+
});
58+
})();

Diff for: test/unit/filter.js

+75-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe("Filter", function () {
1212
catalog.setStrings("nl", {
1313
Hello: "Hallo",
1414
"Hello {{name}}!": "Hallo {{name}}!",
15-
"One boat": ["Een boot", "{{count}} boten"],
15+
"One boat": ["Een boot", "$count boten"],
1616
Archive: { verb: "Archiveren", noun: "Archief", $$noContext: "Archief (no context)" }
1717
});
1818
}));
@@ -30,23 +30,84 @@ describe("Filter", function () {
3030
assert.equal(el.text(), "Hallo");
3131
});
3232

33-
it("Should translate known strings according to translate context", function () {
34-
catalog.setCurrentLanguage("nl");
35-
var el = $compile("<span>{{\"Archive\"|translate:'verb'}}</span>")($rootScope);
36-
$rootScope.$digest();
37-
assert.equal(el.text(), "Archiveren");
38-
el = $compile("<span>{{\"Archive\"|translate:'noun'}}</span>")($rootScope);
39-
$rootScope.$digest();
40-
assert.equal(el.text(), "Archief");
41-
el = $compile("<span>{{\"Archive\"|translate}}</span>")($rootScope);
42-
$rootScope.$digest();
43-
assert.equal(el.text(), "Archief (no context)");
44-
});
45-
4633
it("Can use filter in attribute values", function () {
4734
catalog.setCurrentLanguage("nl");
4835
var el = $compile("<input type=\"text\" placeholder=\"{{'Hello'|translate}}\" />")($rootScope);
4936
$rootScope.$digest();
5037
assert.equal(el.attr("placeholder"), "Hallo");
5138
});
39+
40+
describe("translatePlural", function () {
41+
42+
// not sure why you'd want to do this, but it's a good test case
43+
it("Should work if n is a number", function () {
44+
catalog.setCurrentLanguage("nl");
45+
var el = $compile("<span>{{'One boat' | translatePlural:2:'$count boten' | translate}}</span>")($rootScope);
46+
$rootScope.$digest();
47+
assert.equal(el.text(), "2 boten");
48+
});
49+
50+
it("Should work if n is a reference", function () {
51+
catalog.setCurrentLanguage("nl");
52+
var scope = $rootScope.$new();
53+
scope.count = 2;
54+
var el = $compile("<span>{{'One boat' | translatePlural:count:'$count boten' | translate}}</span>")(scope);
55+
$rootScope.$digest();
56+
assert.equal(el.text(), "2 boten");
57+
58+
scope.count = 1;
59+
el = $compile("<span>{{'One boat' | translatePlural:count:'$count boten' | translate}}</span>")(scope);
60+
$rootScope.$digest();
61+
assert.equal(el.text(), "Een boot");
62+
});
63+
64+
it("Should work if it precedes translateContext", function () {
65+
catalog.setCurrentLanguage("nl");
66+
catalog.setStrings("nl", {
67+
"One boat": { c1: ["Een boot1", "$count boten1"], c2: ["Een boot", "$count boten"] }
68+
});
69+
70+
var scope = $rootScope.$new();
71+
scope.count = 2;
72+
var el = $compile("<span>{{'One boat' | translatePlural:count:'$count boten' | translateContext:'c2' | translate}}</span>")(scope);
73+
$rootScope.$digest();
74+
assert.equal(el.text(), "2 boten");
75+
});
76+
});
77+
78+
describe("translateContext", function () {
79+
it("Should translate known strings according to translateContext", function () {
80+
catalog.setCurrentLanguage("nl");
81+
var el = $compile("<span>{{'Archive' | translateContext:'verb' | translate}}</span>")($rootScope);
82+
$rootScope.$digest();
83+
assert.equal(el.text(), "Archiveren");
84+
el = $compile("<span>{{'Archive' | translateContext:'noun' | translate}}</span>")($rootScope);
85+
$rootScope.$digest();
86+
assert.equal(el.text(), "Archief");
87+
});
88+
89+
it("Should work with no args", function () {
90+
// translateContext with no args is the same as translate
91+
catalog.setCurrentLanguage("nl");
92+
var el = $compile("<span>{{'Archive' | translateContext | translate}}</span>")($rootScope);
93+
$rootScope.$digest();
94+
assert.equal(el.text(), "Archief (no context)");
95+
el = $compile("<span>{{'Archive' | translate }}</span>")($rootScope);
96+
$rootScope.$digest();
97+
assert.equal(el.text(), "Archief (no context)");
98+
});
99+
100+
it("Should work if it precedes translatePlural", function () {
101+
catalog.setCurrentLanguage("nl");
102+
catalog.setStrings("nl", {
103+
"One boat": { c1: ["Een boot1", "$count boten1"], c2: ["Een boot", "$count boten"] }
104+
});
105+
106+
var scope = $rootScope.$new();
107+
scope.count = 2;
108+
var el = $compile("<span>{{'One boat' | translateContext:'c2' | translatePlural:count:'$count boten' | translate}}</span>")(scope);
109+
$rootScope.$digest();
110+
assert.equal(el.text(), "2 boten");
111+
});
112+
});
52113
});

0 commit comments

Comments
 (0)