[英]How to include href in const and export
I am importing all the constants for my application from a single constant.js file. 我正在从单个constant.js文件中导入应用程序的所有常量。
export const ERR_DOB = "Enter your Date of birth"
I wish to add href in the that constant. 我希望在该常量中添加href。
For eg: 例如:
export const txt = Click <a href="index.html">Here!<a>
But this not working as expected. 但这不能按预期工作。 Printing the same
印刷相同
"Click <a href='index.html'>Here!</a>"
with react, you can not just set innerHtml like that, the proper way to fix is to render children of your react virtual DOM. 使用react时,您不能仅仅这样设置innerHtml,正确的解决方法是呈现react虚拟DOM的子级。 Or, you can use
或者,您可以使用
dangerouslySetInnerHTML={{ __html: your html code }}
https://zhenyong.github.io/react/tips/dangerously-set-inner-html.html https://zhenyong.github.io/react/tips/dangerously-set-inner-html.html
Given that you are trying for internationalization, if you are not open to use any other library, then dangerourslySetInnterHTML
is the only way to go ( see official docs ) 鉴于您正在尝试进行国际化,如果您不愿意使用任何其他库,那么走
dangerourslySetInnterHTML
的路是onlyourslySetInnterHTML( 请参阅官方文档 )
But internationalization has many other use cases which may not easy to be handled by home grown solution (plurals, contenxtual translations etc.), for this I personally use and recommend: react-i18next ( website ) 但是国际化还有许多其他用例,这些用例可能不是由本地解决方案(复数,内容翻译等)来解决的,为此,我个人使用并推荐:react-i18next( 网站 )
Your case where you want to use anchor
tags, the Trans Component
of the library will come handy. 如果您要使用
anchor
标记,则库的Trans Component
会派上用场。
To internationalize 国际化
Click <a href='index.html'>Here!</a>
You will have to wrap it within the Trans component: 您必须将其包装在Trans组件中:
<Trans i18nKey="myHref">
Click <a href='index.html'>Here!</a>
</Trans>
Your translation will look like: 您的翻译将如下所示:
"myHref": "Click <1><0>Here!</1>,
To change it to some other message, you will not have to change your code, just change the dictionary: 要将其更改为其他消息,您无需更改代码,只需更改字典:
"myHref": "Please <1><0>click here</1>,
Do look through their official site. 请浏览他们的官方网站。 They have decent documentation and also host examples on their github repo.
他们有不错的文档,还在github存储库上托管了示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.