React i18n,检查翻译是否存在?

Pan*_*iou 13 internationalization react-i18next

有没有办法检查反应应用程序上是否存在区域设置?就像布尔值 true/false

小智 27

是的,您可以使用以下方法检查您的语言变量是否存在i18n.exists()

您可以i18nuseTranslation钩子中提取。

const { t, i18n } = useTranslation();
Run Code Online (Sandbox Code Playgroud)

然后调用i18n.exists('my.key'),例如:

i18n.exists("common.values.active")
Run Code Online (Sandbox Code Playgroud)

https://www.i18next.com/overview/api#exists