javascript - angular-translate-based custom filter -
i'm using pascal precht's angular-translate module. i've got translations namespaced, e.g.
{ "customer.account.header.main": "stgsdefghdsf", "customer.account.column.name": "sdfszdfs" "customer.account.column.address": "dsghfg", "customer.account.enum.value1": "dfsgsdf", "customer.account.enum.value2": "sdfgsdfg" }
and i'm using them in templates way:
{{ "customer.account.enum.value1" | translate }}
it's working fine, need make custom translation filter, use translate
beneath. need pass namespace prefix, e.g.
{{ enumvalue | translateenum:"customer.account.enum" }}
where enumvalue
value1
, value2
, coming directly api.
unfortunately, following not solution, because mechanism used several higher-level mechanisms , needs accept filter (and possibly filter parameters), not expression such ... + ... | filter
{{ "customer.account.enum." + enumvalue | translate }}
the problem translate
asynchronous, since fetching language asynchronous. don't know how can cope that, because i've got no reference object might assigned after then
promise resolved. i've got value. original translate
uses.
so far i've tried those:
module.filter('translateenuminstant', [ '$translate', function($translate) { return function(input, namespace) { return $translate.instant(namespace + '.' + input); }; } ]).filter('translateenumfilter', [ '$filter', function($filter) { var translatefilter; translatefilter = $filter('translate'); return function(input, namespace) { return translatefilter(namespace + '.' + input); }; } ]).filter('translateenumpromise', [ '$translate', function($translate) { return function(input, namespace) { return $translate(namespace + '.' + input); }; } ]);
but none of them work fine. first 2 synchronous , load previous language, since don't wait new language fetched via json. last 1 returns promise, displays {}
worse.
please tell me can write such directive.
this custom filter should work :
.filter('translateenum', function($filter) { var translatefilter = function (value, param) { return $filter('translate')(param+"."+value); } translatefilter.$stateful = true; return translatefilter; })
Comments
Post a Comment