如何将 i18next 用作我的组件库的对等依赖项?

How to use i18next as a peer dependency for my component library?

我正在构建一个包含一些翻译的 React 组件库,但我希望这些翻译由我的包的用户提供。

这样做的原因是这些翻译应该是可定制的,我不想在这个包中包含所有可能的语言。

所以我想要实现的是在我的组件库中使用 useTranslations,如下所示:

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

const MyComponent = () => {
    const { t } = useTranslation();

    return <div>{t('helloWorld')}</div>;
};

而且 'helloWorld' 翻译应该可以由库的用户配置。

有什么办法可以实现吗?

显然这是由于 yarn link 使用了两个版本的 react-i18next

为了解决这个问题,我将我的应用配置为使用我的包的 react-i18next 版本:

cd my-package/node_modules/react-i18next
yarn link

cd my-app
yarn link react-i18next

现在我的包使用在我的应用程序中配置的 i18next 实例。