Closed InfernalAzazel closed 9 months ago
问题:纯粹 React 使用 SettingDrawer 国际化没有效果的问题如何解决?
解决方案:
src/app.tsx
文件中将其删除。关于时间类组件的国际化 locale 设置不生效的问题,请确保正确设置了 dayjs 语言包,并在代码中进行加载:
import 'dayjs/locale/zh-cn';
dayjs.locale('zh-cn');
另外,还要检查是否有两个版本的 dayjs 共存,可以通过运行 npm ls dayjs
查看项目中依赖的 dayjs 版本。
如果项目中依赖的 dayjs 版本和 antd 依赖的 dayjs 版本无法兼容,会导致使用两个不同版本的 dayjs 实例,从而导致国际化失效。
dayjs.locale('zh-cn')
。
如果日期组件的国际化仍未生效,请检查本地的 dayjs 版本和 antd 依赖的 dayjs 版本是否一致。formatMessage
这个语法糖,虽然 formatMessage
使用方便,但是它脱离了 React 的生命周期,
最严重的问题是切换语言时无法触发 DOM 的重新渲染,从而导致用户体验较差。
推荐使用 useIntl
或者 injectIntl
来实现同样的功能。最后,关于格式化字符串的问题,可以将不同语言的配置分别放在相应的文件(如 en-US.ts
和 zh-CN.ts
)中,在组件中使用 FormattedMessage
组件进行使用:
import { FormattedMessage } from 'umi';
export default () => {
return (
<div>
<FormattedMessage id="navbar.lang" />
</div>
);
};
这样,根据当前的语言设置,会自动显示对应的文本内容。
他是跟着 antd 的 ConfigProvide 跑的,顺便包一个
问题:纯粹 React 使用 SettingDrawer 国际化没有效果的问题如何解决?
解决方案:
- 首先,关闭 SettingDrawer 动态设置可以在
src/app.tsx
文件中将其删除。关于时间类组件的国际化 locale 设置不生效的问题,请确保正确设置了 dayjs 语言包,并在代码中进行加载:
import 'dayjs/locale/zh-cn'; dayjs.locale('zh-cn');
另外,还要检查是否有两个版本的 dayjs 共存,可以通过运行
npm ls dayjs
查看项目中依赖的 dayjs 版本。 如果项目中依赖的 dayjs 版本和 antd 依赖的 dayjs 版本无法兼容,会导致使用两个不同版本的 dayjs 实例,从而导致国际化失效。- 如果想将组件的默认语言切回中文,可以尝试使用 ConfigProvider 组件来包裹应用,并在代码中配置
dayjs.locale('zh-cn')
。 如果日期组件的国际化仍未生效,请检查本地的 dayjs 版本和 antd 依赖的 dayjs 版本是否一致。- 关于为什么不直接使用
formatMessage
这个语法糖,虽然formatMessage
使用方便,但是它脱离了 React 的生命周期, 最严重的问题是切换语言时无法触发 DOM 的重新渲染,从而导致用户体验较差。 推荐使用useIntl
或者injectIntl
来实现同样的功能。最后,关于格式化字符串的问题,可以将不同语言的配置分别放在相应的文件(如
en-US.ts
和zh-CN.ts
)中,在组件中使用FormattedMessage
组件进行使用:import { FormattedMessage } from 'umi'; export default () => { return ( <div> <FormattedMessage id="navbar.lang" /> </div> ); };
这样,根据当前的语言设置,会自动显示对应的文本内容。
可以适应 react-i18next 国际化库吗?
提问前先看看:
https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/blob/main/README-zh_CN.md
🧐 问题描述
代码仓库
adminPro
💻 示例代码
export const AppWith = () => { const appTheme = useAppStore((state: any) => state.theme); const locale = useAppStore((state: any) => state.locale);
};
ReactDOM.createRoot(document.getElementById('root')!).render( )
🚑 其他信息