如何在 next-i18next 中使用 changeLanguage 方法来更改语言环境?

时间:2021-07-11 12:57:17

标签: next.js i18next react-i18next next-i18next

我正在尝试通过单击按钮来更改项目中的默认语言环境。我不想通过推送像 fooo.com/fa 这样的子路径来更改我的 URL。

这是我的 next-i18next 配置:

module.exports = {
  i18n: {
    defaultLocale: 'en',
    locales: ['en', 'fa'],
  },
};

这是我更改语言环境并使用它的简单代码:

const { t, i18n } = useTranslation('common');
   ///
<button onClick={() => i18n.changeLanguage('fa')}>
    Click to Change Language
</button>
<p>{t('title')}</p>

但它不起作用并停留在默认的 EN 语言环境中。

0 个答案:

没有答案
相关问题