Javascript 如何将值从外包js文件解析为密钥

Javascript 如何将值从外包js文件解析为密钥,javascript,reactjs,Javascript,Reactjs,我有一个关于将不同来源的值解析为键的问题。以下是环境: Translation.js export default { en: { translation: { Text: 'Text', Welcome:"Welcome text", } }, tr: { translation: { Text: 'Selam',

我有一个关于将不同来源的值解析为键的问题。以下是环境:

Translation.js

export default {
    en: {
        translation: {
            Text: 'Text',
            Welcome:"Welcome text",
        }
    },
    tr: {
        translation: {
            Text: 'Selam',
            Welcome:"Hoşgeldiniz",
        }
    },
}
const openNotificationForNonActivated = type => {
  notification[type]({
    style:{marginTop: "42px"},
    message: 'Text',
    description: 'Welcome Text',
    duration: 5,
  });
};
现在,我想将sayWelcome文本插入这篇文章,这是Ant的一个组成部分。这里的设计:

在一个不同的文件中,比如example.js,我有这个文件,可以对translation.js做出反应

export default {
    en: {
        translation: {
            Text: 'Text',
            Welcome:"Welcome text",
        }
    },
    tr: {
        translation: {
            Text: 'Selam',
            Welcome:"Hoşgeldiniz",
        }
    },
}
const openNotificationForNonActivated = type => {
  notification[type]({
    style:{marginTop: "42px"},
    message: 'Text',
    description: 'Welcome Text',
    duration: 5,
  });
};
我通常可以通过以下代码获得翻译值:

{t("Welcome")}
我相信这是因为我试图以常量值对其作出反应。那么,我该怎么处理呢? 任何想法

最好的,谢谢;
Osman

如果您使用的是
react>=16.8.0
,您可以创建一个自定义钩子,返回
openNotificationForNonActivated
,这样您就可以访问
useTranlation
钩子,它将允许您显示翻译文本

import React from 'react';
import { useTranslation } from 'react-i18next';
import { notification } from 'antd';

export const useNotification = () => {

  const { t } = useTranslation();

  const openNotificationForNonActivated = type => {
  notification[type]({
    style:{marginTop: "42px"},
    message: 'Text',
    description: t('welcome'),
    duration: 5,
  });
 };

  return { openNotificationForNonActivated };
};
您可以在组件中调用函数,如下所示

const {openNotificationForNonActivated } = useNotification();

openNotificationForNonActivated('type');

你在用什么图书馆。。。next-i18next?这个:从'react-i18next'导入{withTranslation};