-
Notifications
You must be signed in to change notification settings - Fork 1.1k
support for markup inside translated strings #69
Comments
Hey @Skivvies ! You're right, this is a common use case and angular-translate doesn't cover this very well. This is exactly how I use it. If you have any idea how to make this better (at least in theory) lemme know! :) |
Thanks for the super fast response! Is there an example of how you use it On Sunday, June 2, 2013, Pascal Precht wrote:
|
Wait a sec, I'll set up a plunk for you |
Here you go: http://plnkr.co/edit/oyTsOV?p=preview The plunk shows, how to use translation id's with ng-bind-html-unsafe. It also shows, that it doesn't work with bindings :( And unfortunately I can not say how hard that is to implement. Have to take a deeper look at it, but we're currently working on pluralization. I hope you can live with that for now? |
Thank you so much for taking the time to write the plunk. I've been away I'm following progress on the pluralization work, interested in that too. On Sunday, June 2, 2013, Pascal Precht wrote:
|
Awesome :) Ideas and Pull Requests are always welcome! :) So can I close this one for now? |
Hey @PascalPrecht, it looks like using a new directive along the lines of the In the meantime, if you'd be open to one of us adding something to the docs demonstrating the included-markup use case, we could leave this open until then, or for sure feel free to just close the issue too. |
Feel free to update the docs! :) Either in the wiki or the auto generated docs under |
Thanks @PascalPrecht, when I have a patch I'll convert this issue into a pull request and attach some commits! |
This'd be awesome! :) On Friday, June 14, 2013, skivvies wrote:
/pp Sent from Gmail Mobile |
Closing this for now |
Did anyone get this working ? So we can insert html ? |
ng-bind-html-unsafe is deprecated, and $sce.trustAsHtml should be used in conjunction with ng-bind-html. Is this implemented right now in translate? |
That is for AngularJS 1.2+, isn't it? Translate depends currently on stable angular ( which is 1.0). @PascalPrecht perhaps introducing the next major with early 1.2 support? |
I could really use this feature too :) |
@guilload @knalli Actually it turned out, when using html within translations and using translate directive instead of translate filter, the given html is parsed and interpreted correctly. So you should stick with translate directive until we introduce a new angular-translate 2 version which officially supports angular 1.2 |
Great! |
I recently took a crack at a directive that would allow live angular bindings inside translations: .directive('compileUnsafe', function ($compile) {
return function (scope, element, attr) {
scope.$watch(attr.compileUnsafe, function (val, oldVal) {
if (!val || (val === oldVal && element[0].innerHTML)) return;
element.html(val);
$compile(element)(scope);
});
};
}) Here is an example plunk that demonstrates it in action: http://plnkr.co/edit/Stb6IexTjG0AfzrB896Z?p=preview Not confident this is the best way to implement this, so feedback would be much appreciated! |
Oh, just saw the new translate-compile directive in angular-translate 2.0 (congrats on the new release!) in http://angular-translate.github.io/docs/#/guide/05_using-translate-directive. That page doesn't actually demonstrate any live bindings inside a translated string though. I updated my plunk to demonstrate this next to the compile-unsafe method I've been using with angular-translate < 2.0: |
I think the example on the directive could be better but it works out of box with 2.0. |
The original question was also about using links and actually I'm not able to get that to work. I have a situation where I would like to feed the href url from the scope using curly braces. I have a plunker here: http://plnkr.co/edit/8eArYIXlcpybRqfhwcsl?p=preview. This is probably more a question of understanding how Angular works. |
As of the current version (please don't refer or use to 2.0.0 anymore as it is very old now), you can use it in the directive (your second example) this way:
|
Sometimes you need to translate something like:
Or even just:
Seems like a common use case but didn't see it documented, so I thought I'd open a ticket.
What's the best way to achieve this? You could divide the single translated string into three parts (e.g.
before_markup
,inside_markup
,after_markup
), but this doesn't scale to more nested markup:In a case like this, it would be nice to include everything in the
<p>
element in a single translated string, and each translation would include the necessary contained markup. Is this possible?ng-bind-html
andng-bind-html-unsafe
can be used to include sanitized or unsanitized markup. Do they help? Ideally the angular-translate-injected markup could also support live angular behavior too:Is that possible?
Thanks for reading, and thanks for the great work on angular-translate!
The text was updated successfully, but these errors were encountered: