[英]i18next - Apply custom formatting
我有 2 个命名空间:
// global ns
{
"amendment": "amendment"
}
// page ns
{
"action": "Enter your $t(global:amendment) below."
}
我在反应组件中的用例:
import React from 'react'
import { useTranslation } from 'react-i18next';
export function MyComponent() {
const { t, i18n } = useTranslation();
return (
<div>
<h1>{t('amendment')}</h1>
<p>{t('action')}</p>
</div>
)
}
我想使用大写转换来格式化包含在我的h1
元素中的文本。
实现这一目标的最佳方法是什么?
我已经考虑使用上下文并执行以下操作:
// global ns
{
"amendment": "amendment",
"amendment_uppercase": "Amendment"
}
import React from 'react'
import { useTranslation } from 'react-i18next';
export function MyComponent() {
const { t, i18n } = useTranslation();
return (
<div>
<h1>{t('amendment', {context: 'uppercase'})}</h1>
<p>{t('action')}</p>
</div>
)
}
这里的问题是我必须复制我所有的翻译键,而且我有很多全局词汇表。
我会应用t方法结果的格式。
就像是:
import React from 'react'
import { useTranslation } from 'react-i18next';
export function MyComponent() {
const { t, i18n } = useTranslation();
return (
<div>
<h1>{t('amendment').toUpperCase()}</h1>
<p>{t('action')}</p>
</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.