简体   繁体   English

Angular Translate HTML标签

[英]Angular Translate HTML tags

I know this has been asked here before but none of the answers seem to work for my case 我知道之前已经问过这个问题但是没有一个答案似乎适用于我的情况

I bought this theme Angle which is working with Angular 1.4.2 and Angular translate 2.6.0 (even updated to last 2.7.2) 我买了这个主题Angle ,它正在使用Angular 1.4.2和Angular翻译2.6.0(甚至更新到2.7.2)

The template by default has the Translate module on it 默认情况下,模板上有Translate模块

This is the config file 这是配置文件

  $translateProvider.useStaticFilesLoader({
      prefix : 'app/i18n/',
      suffix : '.json'
  });
  $translateProvider.preferredLanguage('es');
  $translateProvider.useLocalStorage();
  $translateProvider.usePostCompiling(true);
   // Enable escaping of HTML
  $translateProvider.useSanitizeValueStrategy('sanitize'); // I added this line based on Docs wasn't before

And the translation files in JSON format 并以JSON格式翻译文件

  {
   "page": {
    "PAGES_WELCOME" : "Welcome to <br> MY APPLICATION, HEY THERE IS A BR TAG BEFORE ME"
  },

  "login": {
    .
    .
    .
    .
  },

But i cannot add HTML tags inside the text, on the JSON file, instead of getting 但我无法在文本内部,JSON文件中添加HTML标记,而不是获取

Welcome to MY APP 欢迎来到我的APP

I'm getting 我越来越

Welcome to < br > MY APP 欢迎来到我的APP

How can i fix this? 我怎样才能解决这个问题?

EDIT 编辑

I do NOT want to remove the tags, my JSON file is modified by the backend, and it can and will contain HTML Tags, i want those tags to work on the output. 我不想删除标签,我的JSON文件被后端修改,它可以并且将包含HTML标签,我希望这些标签能够在输出上工作。

JADE Example Where the content is binding JADE示例内容具有约束力

div(class="col-lg-4 col-md-4 col-sm-4 col-xs-12 footer-left")
  p(class="text-center") 
    {{ 'page.PAGES_WELCOME' | translate }} 

Angular sanitizes any html strings during its interpolation. Angular在插值过程中清理任何html字符串。 In order to get around this you will need to mark the HTML as safe in $sce before injecting. 为了解决这个问题,您需要在注入之前将HTML标记为安全。 Then also use ngBindHtml to output the html. 然后还使用ngBindHtml输出html。

I've not used angular-translate before, but this may work: 我之前没有使用过angular-translate,但这可能有效:

//app is the main module
app.filter("htmlSafe", ['$sce', function($sce) {
    return function(htmlCode){
        return $sce.trustAsHtml(htmlCode);
    };
}]);

//then to output it
<span data-ng-bind-html="'page.PAGES_WELCOME' | translate | htmlSafe"></span>

Install ngSanitize module. 安装ngSanitize模块。

It makes you able to bind html content, then change your code like this: 它使您能够绑定html内容,然后像这样更改您的代码:

ng-bind-html="'a_key_with_html' | translate"

But i cannot add HTML tags inside the text, on the JSON file, instead of getting 但我无法在文本内部,JSON文件中添加HTML标记,而不是获取

Welcome to MY APP 欢迎来到我的APP

I'm getting 我越来越

Welcome to 欢迎来到

MY APP 我的APP

You have a <br> which is breaking the line like you said you do not want so remove it like so: 你有一个<br>它像你说你不想因此将其删除,像这样被打破了行:

{
   "page": {
    "PAGES_WELCOME" : "Welcome to {{appName}}"
  },

  "login": {
    .
    .
    .
    .
  },

Tested with AngularJS 1.4.7, I just use this: 使用AngularJS 1.4.7测试,我只是使用它:

<span data-ng-bind-html="'my.i18n.code.to.translate.html.tags' | translate"></span>

Since I do not want to inject any filter but above is just worked on my own trusted i18n string. 因为我不想注入任何过滤器,但上面只是在我自己的可信任i18n字符串上工作。 Of course, the accepted answer would be more safe but this one is just work right away. 当然,接受的答案会更安全,但这个答案就是马上工作。

I actually really don't want to use the tags in my html template. 我实际上真的不想在我的html模板中使用这些标签。 The tags isn't meaningful. 标签没有意​​义。

I finally got it to work. 我终于开始工作了。 Environment: Angular 1.5.8, angular-tranlsate: 2.11.0 环境:Angular 1.5.8,angular-tranlsate:2.11.0

My solution is: 1. load ngSanitize and init the module 我的解决方案是:1。加载ngSanitize并初始化模块

  1. $translateProvider.useSanitizeValueStrategy('sanitize');

  2. <p ng-bind-html="'Please <strong>refresh</strong> the browser' | translate"></p>

您可以保留JSON文件,然后只需使用HTMLinnerHTML属性来渲染文本,如下所示:

 <div [innerHTML]="'page.PAGES_WELCOME' | translate"></div>

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

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