Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the use of element refs in translation variables #1417

Open
duncte123 opened this issue May 10, 2023 · 0 comments
Open

Allow the use of element refs in translation variables #1417

duncte123 opened this issue May 10, 2023 · 0 comments

Comments

@duncte123
Copy link

Current behavior

Currently ngx-translate does not support this feature and will call toString on the element ref causing it to render as [object Object]

Expected behavior

With the following translation keys

{
  "schedule": {
    "startsIn": "Begins {{start-time}}"
  },
}

Vue allows us to do the following ($formatTime is a custom function defined by me)

<i18n path="schedule.startsIn" tag="span">
    <template #start-time>
        <time :datetime="startTime">{{ $formatTime(startTime) }}</time>
    </template>
</i18n>

This will render as the following

With the HTML rendering as

<span>Begins <time datetime="2023-05-12T13:00:00Z">15:00</time></span>

What is the motivation / use case for changing the behavior?

The main use case for this is accessibility. Inserting the time element allows computers (such as search engines) to read and use the time properly, or allows your phone to detect it and set a reminder. (MDN docs)

Besides that, it's also safer when it comes to links for example. While using [innerHTML] is fine for allowing links it is also very susceptible for bad actors to inject scripts this way for community-translated projects. (that is a bit of a reach tbh)

Over all, it is just really nice to not make your translators deal with HTML but just have them deal with the translation strings and the front-end code being responsible for adding in the appropriate HTML tags.

How do you think that we should implement this?

I think taking inspiration from vue-i81n would be a good idea and create something like this.

<ngx-translate path="schedule.startsIn" [translateParams]="{'start-time': startTimeTemplate}">
    <ng-template #startTimeTemplate>
        <time [dateTime]="startTime">{{ $formatTime(startTime) }}</time>
    </ng-template>
</ngx-translate>

Or, alternatively, just add support for element refs inside the params where it just takes the contents of the referenced element and dumps that in place of the variable. This would look something like this.

<span [innerHTML]="'schedule.startsIn' | translate: {'start-time': startTimeTemplate}"></span>
<ng-template #startTimeTemplate>
    <time [dateTime]="startTime">{{ $formatTime(startTime) }}</time>
</ng-template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant