Skip to content

Commit

Permalink
translatePlural and translateContext via chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
gabegorelick committed Dec 10, 2014
1 parent 749d441 commit 2bb2992
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 41 deletions.
67 changes: 60 additions & 7 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
},

getPlural: function (n, string, stringPlural, scope, context) {
if (!n && n !== 0) {
return this.getString(string, scope, context);
}

var form = gettextPlurals(this.currentLanguage, n);
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
Expand Down Expand Up @@ -206,13 +210,62 @@ 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);
}
filter.$stateful = true;
return filter;
}]);
(function () {
var translate = function (gettextCatalog, $gettext) {
var message = gettextCatalog.getPlural($gettext.n, $gettext.msgid, $gettext.plural, null, $gettext.context);
if ($gettext.n || $gettext.n === 0) {
// replace $count with n, preserving leading whitespace
return message.replace(/(^|\s)\$count\b/g, '$1' + $gettext.n);
} else {
return message;
}
};

angular.module('gettext').filter('translate', ["gettextCatalog", function (gettextCatalog) {
function filter(msgid) {
var $gettext = msgid.$gettext || { msgid: msgid };

// translate is the only filter that returns a string primitive
return translate(gettextCatalog, $gettext);
}
filter.$stateful = true;
return filter;
}]);

angular.module('gettext').filter('translatePlural', ["gettextCatalog", function (gettextCatalog) {
function filter(msgid, n, plural) {
var $gettext = msgid.$gettext || { msgid: msgid };
$gettext.n = n;
$gettext.plural = plural;

/*jshint -W053 */
// might as well return the correct String, even if it is a wrapper type
var message = new String(translate(gettextCatalog, $gettext));
/*jshint +W053 */

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

angular.module('gettext').filter('translateContext', ["gettextCatalog", function (gettextCatalog) {
function filter(msgid, context) {
var $gettext = msgid.$gettext || { msgid: msgid };
$gettext.context = context;

/*jshint -W053 */
var message = new String(translate(gettextCatalog, $gettext));
/*jshint +W053 */

message.$gettext = $gettext;
return message;
}
filter.$stateful = true;
return filter;
}]);
})();

// Do not edit this file, it is autogenerated using genplurals.py!
angular.module("gettext").factory("gettextPlurals", function () {
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 @@ -89,6 +89,10 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
},

getPlural: function (n, string, stringPlural, scope, context) {
if (!n && n !== 0) {
return this.getString(string, scope, context);
}

var form = gettextPlurals(this.currentLanguage, n);
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
Expand Down
63 changes: 56 additions & 7 deletions src/filter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
angular.module('gettext').filter('translate', function (gettextCatalog) {
function filter(input, context) {
return gettextCatalog.getString(input, null, context);
}
filter.$stateful = true;
return filter;
});
(function () {
var translate = function (gettextCatalog, $gettext) {
var message = gettextCatalog.getPlural($gettext.n, $gettext.msgid, $gettext.plural, null, $gettext.context);
if ($gettext.n || $gettext.n === 0) {
// replace $count with n, preserving leading whitespace
return message.replace(/(^|\s)\$count\b/g, '$1' + $gettext.n);
} else {
return message;
}
};

angular.module('gettext').filter('translate', function (gettextCatalog) {
function filter(msgid) {
var $gettext = msgid.$gettext || { msgid: msgid };

// translate is the only filter that returns a string primitive
return translate(gettextCatalog, $gettext);
}
filter.$stateful = true;
return filter;
});

angular.module('gettext').filter('translatePlural', function (gettextCatalog) {
function filter(msgid, n, plural) {
var $gettext = msgid.$gettext || { msgid: msgid };
$gettext.n = n;
$gettext.plural = plural;

/*jshint -W053 */
// might as well return the correct String, even if it is a wrapper type
var message = new String(translate(gettextCatalog, $gettext));
/*jshint +W053 */

message.$gettext = $gettext;
return message;
}
filter.$stateful = true;
return filter;
});

angular.module('gettext').filter('translateContext', function (gettextCatalog) {
function filter(msgid, context) {
var $gettext = msgid.$gettext || { msgid: msgid };
$gettext.context = context;

/*jshint -W053 */
var message = new String(translate(gettextCatalog, $gettext));
/*jshint +W053 */

message.$gettext = $gettext;
return message;
}
filter.$stateful = true;
return filter;
});
})();
Loading

0 comments on commit 2bb2992

Please sign in to comment.