-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
translatePlural and translateContext via chaining
- Loading branch information
1 parent
749d441
commit 095972b
Showing
5 changed files
with
196 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters