HTML tags in i18next translation

前端 未结 8 1392
小鲜肉
小鲜肉 2020-12-08 18:30

I\'m using i18next to power i18n for my weblog. It works great on text-only content, but when I try to translate content that includes HTML markup, it is displaying the raw

8条回答
  •  北海茫月
    2020-12-08 19:06

    From the documentation:

    Hint 3: Escaping:

    // given resources
    {           
      'en-US': { 
        translation: { 
          key1: Not escaped __nameHTML__,
          key2: Escaped __name__ 
        } 
      }
    };
    
    i18n.t("key2", { name: '', escapeInterpolation: true }); // -> Escaped <tag>
    i18n.t("key1", { name: '', escapeInterpolation: false }); // -> Not escaped 
    

    Adding suffix 'HTML__' to your value will prevent the escaping even if option is set so.

    You could turn on escaping on init i18n.init({escapeInterpolation: true}); or by passing in option to t function like in the sample.

提交回复
热议问题