I am currently trying to learn how to use Angular and I was tasked with rewriting an existing functional page to use Angular.
One of our core functionalities lies with translating strings with a JavaScript function, however, this translate method is only supposed to be called after the translations strings are loaded into the script, and that loading is done asynchronously.
We currently use callbacks to achieve this behavior, but due to Angular nature (at least in my understanding) my data is displayed as soon as the page is rendered.
I have the to be translated strings, but I wanted to display the translated strings in my table, and for that I tried creating a filter "translate" that would call my existing translate method and return its value. What I believe it's happening is that at the moment the filter gets called, I still don't have the translation strings yet, as they may not be loaded already.
What I expected was to something like {{ object.name | translate }}, where object.name is a to be translated string like "access.granted", to give me the corresponding translation string for a given language, for instance "You were given permission to access this area.".
I am not positive this is the best way to achieve what I want, I haven't had much time to delve deep into angular yet, but one requirement I have is, I cannot change the way translations are dealt with, since we have a huge system already using that code, and the new pages using angular must be fully compatible.
What I have tried: (my current filter code)
function ngTranslate (text, backoff) {
if (backoff != undefined)
backoff = 100;
if (backoff > 5000)
backoff = 5000;
if (internationalization != undefined && Object.keys(internationalization.messages).length > 0)
return translate(text);
setTimeout(function () {
return ngTranslate(text, backoff * 2);
}, backoff);
}
app.filter("translate", function ($log) {
return function (key) {
$log.info(key);
return ngTranslate(key);
};
});
What I tried to achieve here was verify if the translations strings are already loaded, and if not, try again in a short time, but even after the translation strings loaded, nothing showed on the table.
Here is my table code:
<tr data-ng-repeat="product in productList">
<td>{{ product.name | translate }}</td>
</tr>
Thanks a lot in advance
{{ product.name | translate }}. when theproduct.namechange the translation will be updated automatically