reactjs - React:在本地化文本中插入换行符
问题描述
我从 JSON 文件获取本地化文本并将其显示到页面,我的问题是\n
和<br>
符号被忽略。
我在这里制作了一个代码沙箱,以显示文本如何不超过一行。
import * as React from "react";
import { render } from "react-dom";
const App: React.FC = props => {
const Translations: {
[key: string]: {
fname: string;
};
} = {
en: {
fname: "I want to insert a break in this line of text so it shows on two lines \n doesnt work and <br> does either",
},
cn: {
fname: "我想在这一行文本中插入一个中断,以便在两行中显示\ n不起作用,而<br>可以",
}
};
const txt = Translations["en"];
return (
<div className="App">
<p>{txt.fname}</p>
</div>
);
};
const rootElement = document.getElementById("root");
render(<App />, rootElement);
解决方案
使用 CSS 属性white-space: pre-line
<p style={{ whiteSpace: "pre-line" }}>{txt.fname}</p>
推荐阅读
- android - 出现软键盘时如何避免只有BottomNavbar向上推
- google-app-engine - 错误:(gcloud.app.deploy)错误响应:[5] 无法在全局作业中获取云区域
- css - 有没有办法将填充或 boxshadow 添加到 css 圆锥渐变部分
- java - 心率查询返回空集
- python - AttributeError:从 Windows Server 使用 Paramiko 连接时,模块“paramiko.win_pageant”没有属性“can_talk_to_agent”
- unity3d - ARKit 跟踪能否与 Vuforia 的区域目标一样有效?
- sql - 使用 IIF() 替换 SELECT 语句中的空值
- postgresql - 将 execute$$ 替换为 plpgsql 格式
- json - 调用 Ruby,Rspec 错误时,应用程序是否返回 JSON?
- android - 如何从(和替换) TabLayout 和 ViewPager 移动到新片段?