Core: Using translations within html attribute

Created on 23 Feb 2016  路  11Comments  路  Source: ngx-translate/core

I don't get this seem to work, using Ionic2 and ng2-translate.

<ion-refresher pullingText="{{ 'Pull to refresh...' | translate }}">

Getting error:

EXCEPTION: SyntaxError: Unexpected token }

Is there a way to fix this? Thank you for all the help.

Most helpful comment

You should either use [title]="'HELLO' | translate" or title="{{ 'HELLO' | translate }}", but [title]="{{ 'HELLO' | translate }}" is not correct Angular

All 11 comments

You're supposed to use your key, something like that:

<ion-refresher pullingText="{{ 'PULL' | translate }}">

And then you define the translation in your json file (or you manually) like that: "PULL": "Pull to refresh..."

In this case "PULL" is the key, and "Pull to refresh..." is your translation.

I apologise, this was an error triggered by some other issue.

@ocombe it also works with hardcoded string.

What if we have the HTML attribute tied to a component property? For example...

[title]="{{ 'HELLO' | translate }}"

Or even using a method like this in the component doesn't work for me. It just puts undefined in the view:

[title]="fetchTranslation('HELLO')"

fetchTranslation(key: string, params = {}) {
    this.translate.get(key, params).subscribe((res: string) => {
        return res;
    });
}

You should either use [title]="'HELLO' | translate" or title="{{ 'HELLO' | translate }}", but [title]="{{ 'HELLO' | translate }}" is not correct Angular

Got it. Thanks for the quick reply! :) any idea how to get the service thing working though? That would really help with keeping the views tidy.

fetchTranslation(key: string, params = {}) {
    return this.translate.instant(key, params);
}

or you could just use the service:

[title]="translate.instant('HELLO')"

But be careful because if you use the change detection OnPush this will not update your view, you should use the pipe or directive for that.

Worked like a charm! Thank you 馃憤

By the way, is there any extra documentation available? Like would I have needed to read the source to find out about that method?

I have the above working, but I become stuck when using data-title on tables.

data-title="{{fetchTranslation('name')}}"

for example does not work.

Is there a was to get this working with data-title? This is a key property for displaying data on mobile.

I had this problem,
[title]="'Location' | translate" shows a tool-tip

but,

[title]="'Historical Development' | translate" does not show a tool-tip
same for
title="{{ 'Historical Development' | translate}}"
but when I remove the translate pipe -
title="'Historical Development'"does show a tool-tip

Was this page helpful?
0 / 5 - 0 ratings