angularjs-localizationservice icon indicating copy to clipboard operation
angularjs-localizationservice copied to clipboard

Dynamic model and translate

Open xavadu opened this issue 12 years ago • 1 comments

Hello, i want try to do something like that, but i think that the service don't support it, i want use dynamic data (model) passed by a filter and later translate it.

example:

<span>In the date that you choose 12/11/2013 you will receive the package.</span>

the date '12/11/2013' is a model, so: <foo ng-model="date"></foo> <span>In the date that you choose { date | date:'medium' } you will receive the package.</span>

how i can use this service to translate this sentence? i have a dynamic value { "key": "_Message_", "value": "In the date that you choose {0} you will receive the package." }

but it should be passed by a filter first and later to the i18n filter (like the date change on fly i think that i should use the i18n filter and not the directive)

Thanks

xavadu avatar Nov 07 '13 14:11 xavadu

solved, i modify the filter like that

// simple translation filter
// usage {{ 'TOKEN|VAR1|VAR2'| i18n }}
.filter('i18n', ['localize', function (localize) {
return function (token) {
var values = token.split('|');
if (values.length >= 1) {
// construct the tag to insert into the element
var tag = localize.getLocalizedString(values[0]);
// update the element only if data was returned
if ((tag !== null) && (tag !== undefined) && (tag !== '')) {
if (values.length > 1) {
for (var index = 1; index < values.length; index++) {
var target = '{' + (index - 1) + '}';
tag = tag.replace(target, values[index]);
}
}
// insert the text into the element
return tag;
};
}
};
}])

{
        'key' => '_ExchangeRateFrom_',
        'value' => 'Exchange rate {0} from',
        'description' => 'The exchange rate shown near to the calendar'
}
`
{ ('_ExchangeRateFrom_|'+ (baseExchangeRate | enhancedCurrency:'':4) ) | i18n }
`

xavadu avatar Nov 11 '13 11:11 xavadu