angular-gettext icon indicating copy to clipboard operation
angular-gettext copied to clipboard

Yet another attribute translation question

Open anilanar opened this issue 9 years ago • 12 comments

I looked through issues here and I couldn't find one that mentions this problem. Currently it's impossible to translate the following without translating strings in JS:

<input placeholder="Numbers between {{start}} and {{end}}">

Feature request:

Support one of the following syntaxes:

<input translate-placeholder placeholder="Numbers between {{start}} and {{end}}">

or

<input translate-attr="placeholder" placeholder="Numbers between {{start}} and {{end}}">

With this approach, translate-plural, translate-n, translate-context and any other directive related mechanisms can be used for attributes.

anilanar avatar Nov 27 '15 14:11 anilanar

Related to #226.

anilanar avatar Nov 27 '15 14:11 anilanar

<input placeholder="'Numbers between {{start}} and {{end}}'|translate"> See https://angular-gettext.rocketeer.be/dev-guide/annotate/

circlingthesun avatar Nov 27 '15 15:11 circlingthesun

There's no such example in docs. And your example obviously won't work.

anilanar avatar Nov 27 '15 15:11 anilanar

Right. I see your issue. You could pipe it to another filter to do interpolation. Something like:

app.filter('substitute', function($interpolate) {
return function(text, context) {
    var interpolateFn = $interpolate(text);
    return interpolateFn(context);
}
});

<input placeholder="'Numbers between {{start}} and {{end}}'|translate|substitute:{start: '1', end: '30'}">

circlingthesun avatar Nov 27 '15 15:11 circlingthesun

First of all, it needs to be

<input placeholder="{{'Numbers between {{start}} and {{end}}' | translate}}"> 

and this won't work because Angular won't like nested interpolatable stuff, i.e. nested curly brace expressions.

anilanar avatar Nov 27 '15 16:11 anilanar

How's this for a workaround: http://plnkr.co/edit/5vdiC8Nydm351BtPzap9?p=preview

circlingthesun avatar Nov 27 '15 17:11 circlingthesun

Yes, that's indeed a workaround. Don't you think such a workaround should make it into angular-gettext?

anilanar avatar Nov 28 '15 16:11 anilanar

So seeing the source code for grunt tools, it is possible to add placeholder to attributes and then one can create a custom directive to translate it.

But that doesn't solve the problem for the following example:

<a href="" tooltip="Click to go to page {{pageNumber}}" translate>Next</a>

Because for the grunt tool, tooltip and translate are the same. When extracting, content of an html element has priority over attribute value. So for this example, only "Next" is extracted. I believe this library needs to be adopted by a bigger organisation to speed up development.

anilanar avatar Nov 28 '15 16:11 anilanar

:+1: This is a much needed feature IMO...

gaffney avatar Dec 04 '15 22:12 gaffney

Much needed feature indeed. Just moving a project from angular-translate to angular-gettext and did not have those issues there. You could simple do:

{{ 'This is {{var1}} favourite {{var2}}.' | translate : { var1: 'my', var2: 'place' } }}

Which would translate into:

This is my favourite place

I understand that due to the minimalistic footprint you do not want to add this feature, but the substitute filter is a ridiculous (though: working!) workaround, that does not feel like it should be used like that. Particularly when translators are presented with using [[]] some times, and `{{}}`` other times.

Would be great if it would find its way into the library.

Finally, @anilanar, I see you live in Germany, so: "Der Ton macht die Musik" (= It's not what you say, but how you say it.).

rebers avatar Apr 05 '16 14:04 rebers

Stumbled upon this today: https://angular-gettext.rocketeer.be/dev-guide/custom-annotations/ You can just create a new directive called "placeholder" which will run all placeholders through translation automatically.

intellix avatar Aug 18 '16 14:08 intellix

@intellix that example doesn't support interpolation. You can duplicate everything in the translate directive logic, but I'm guessing that @anilanar doesn't want to do that.

alfaproject avatar Aug 19 '16 17:08 alfaproject