简体   繁体   English

角度6:ngx-translate无法与工具提示的data-title属性一起使用

[英]Angular 6: ngx-translate not working with data-title attribute for tooltips

I'm developing an Angular 6 app which uses ngx-translate for localization and I'm also using Bootstrap 4 tooltips and the problem I'm facing is I'm not being able use localization keeping Bootstrap tooltip style. 我正在开发一个使用ngx-translate进行本地化的Angular 6应用程序,我还使用了Bootstrap 4工具提示,而我面临的问题是我无法使用本地化保持Bootstrap工具提示样式。

Without localization I would use an input this way: 如果没有本地化,我将以这种方式使用输入:

<input type="text" class="form-control text-truncate" id="position"
placeholder="position" data-title="position" />

And this will show a very nice Bootstrap tooltip as it can be seen below: 这将显示一个非常不错的Bootstrap工具提示,如下所示:

带样式提示的输入

With ngx-translate localization I would use an input this way: 使用ngx-translate本地化时,我将以这种方式使用输入:

<input type="text" data-toggle="tooltip" class="form-control tooltipped text-truncate" id="position" 
    [attr.placeholder]="'wfrh_vacancyform_position' | translate" 
    [attr.data-title]="'wfrh_vacancyform_position' | translate" />

and the problem here is data-title attribute. 而这里的问题是data-title属性。 "data-title" attribute is used to display the tooltip but I guess ngx-translate doesn't recognize it (maybe?). “数据标题”属性用于显示工具提示,但我猜ngx-translate无法识别它(也许吗?)。

Placeholder is working fine this way (the text is translated and shown correctly) but tooltip won't show. 占位符可以这样正常工作(文本已正确翻译和显示),但工具提示不会显示。

I've also tried this way: 我也尝试过这种方式:

<input type="text" data-toggle="tooltip" class="form-control tooltipped text-truncate" id="position"
placeholder="{{'wfrh_vacancyform_position' | translate}}" 
data-title="{{'wfrh_vacancyform_position' | translate}}" />

which is also not working (it only works for placeholder) so I'm stuck. 这也不起作用(它仅适用于占位符),所以我被卡住了。

If I do: 如果我做:

<input type="text" data-toggle="tooltip" class="form-control tooltipped text-truncate" id="position" 
    [attr.placeholder]="'wfrh_vacancyform_position' | translate" 
    [attr.title]="'wfrh_vacancyform_position' | translate" />

then the tooltip shows but with no style as it can be seen in the next image: 然后显示工具提示,但没有样式,如在下一张图片中所示:

使用默认工具提示输入

And this is the way I create the tooltips in code (in ngOnInit): 这就是我在代码中创建工具提示的方式(在ngOnInit中):

ngOnInit() {
   setTooltip()
}

  setTooltip() {

    $('.tooltipped').tooltip({
      placement: 'auto',
      trigger: 'focus',
      template: '<div class="tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner bg-dark text-light"></div></div>'
    });

    $('.tooltipped').bind("mouseover", function () {
      var _this = $(this);
      _this.tooltip("show");
    });

    $('.tooltipped').bind("mouseout", function () {
      var _this = $(this);
      _this.tooltip("hide");
    });

    $('.tooltipped').bind("keyup", function () {
      var _this = $(this);
      _this.tooltip("hide");
    });
  }

Well I'm stuck. 好吧,我被困住了。 I need to be able to display this nice styled tooltip with translation. 我需要能够显示带有翻译的漂亮样式的工具提示。 Any help / ideas? 有什么帮助/想法吗?

Thanks. 谢谢。

Ok, after long time investigating I was able to find a solution and I'll post it here in case it helps anyone else. 好的,经过长时间的研究,我能够找到一个解决方案,我将在这里张贴它,以防其他人使用。

The problem is I was setting tooltip in onInit (which is fired only once when the component is created) and wasn't setting any tooltip text, just leaving it to pickup the one set with: 问题是我在onInit中设置了工具提示(在创建组件时仅触发一次),并且没有设置任何工具提示文本,而只是通过以下方式设置了它:

[attr.data-title]="'text_to_translate_key' | translate"

(the initial text translation) and after changing language tooltip was not refreshing (the text was static with the initial value) but you can use a function with the tooltip "title" property this way: (初始文本翻译),并且在更改语言后,工具提示没有刷新(带有初始值的文本是静态的),但是您可以通过以下方式使用带有工具提示“ title”属性的函数:

    $('.tooltipped').tooltip({
      placement: 'auto',
      trigger: 'focus',
      template: '<div class="tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner bg-dark text-light"></div></div>',
      title: this.setTitle
    });

  setTitle() {

    return $(this).attr("placeholder");
  }

and this function (which has current object reference -this- as an implicit input parameter) acts as a binding which updates the title property continuosly so when placeholder text changes (placeholder does not need to be refreshed after language changes and that's why it works) the tooltip "title" property will be updated and as a consequence tooltip text will change and user will see updated text. 并且此函数(具有当前对象引用-this-作为隐式输入参数)用作绑定,可连续更新title属性,因此当占位符文本更改时(占位符在语言更改后无需刷新,这就是它起作用的原因)工具提示的“标题”属性将被更新,结果工具提示文本将更改,并且用户将看到更新的文本。

"The end" :) “结束” :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM