Skip to content

Commit 2bb2992

Browse files
committed
translatePlural and translateContext via chaining
1 parent 749d441 commit 2bb2992

File tree

5 files changed

+207
-41
lines changed

5 files changed

+207
-41
lines changed

dist/angular-gettext.js

+60-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,62 @@ 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+
// replace $count with n, preserving leading whitespace
218+
return message.replace(/(^|\s)\$count\b/g, '$1' + $gettext.n);
219+
} else {
220+
return message;
221+
}
222+
};
223+
224+
angular.module('gettext').filter('translate', ["gettextCatalog", function (gettextCatalog) {
225+
function filter(msgid) {
226+
var $gettext = msgid.$gettext || { msgid: msgid };
227+
228+
// translate is the only filter that returns a string primitive
229+
return translate(gettextCatalog, $gettext);
230+
}
231+
filter.$stateful = true;
232+
return filter;
233+
}]);
234+
235+
angular.module('gettext').filter('translatePlural', ["gettextCatalog", function (gettextCatalog) {
236+
function filter(msgid, n, plural) {
237+
var $gettext = msgid.$gettext || { msgid: msgid };
238+
$gettext.n = n;
239+
$gettext.plural = plural;
240+
241+
/*jshint -W053 */
242+
// might as well return the correct String, even if it is a wrapper type
243+
var message = new String(translate(gettextCatalog, $gettext));
244+
/*jshint +W053 */
245+
246+
message.$gettext = $gettext;
247+
return message;
248+
}
249+
filter.$stateful = true;
250+
return filter;
251+
}]);
252+
253+
angular.module('gettext').filter('translateContext', ["gettextCatalog", function (gettextCatalog) {
254+
function filter(msgid, context) {
255+
var $gettext = msgid.$gettext || { msgid: msgid };
256+
$gettext.context = context;
257+
258+
/*jshint -W053 */
259+
var message = new String(translate(gettextCatalog, $gettext));
260+
/*jshint +W053 */
261+
262+
message.$gettext = $gettext;
263+
return message;
264+
}
265+
filter.$stateful = true;
266+
return filter;
267+
}]);
268+
})();
216269

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

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.

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) {

src/filter.js

+56-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,56 @@
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+
/*jshint -W053 */
47+
var message = new String(translate(gettextCatalog, $gettext));
48+
/*jshint +W053 */
49+
50+
message.$gettext = $gettext;
51+
return message;
52+
}
53+
filter.$stateful = true;
54+
return filter;
55+
});
56+
})();

0 commit comments

Comments
 (0)