如何在 getStaticProps Next.js 中访问当前语言?

Hay*_*yan 3 i18next reactjs next.js

我在下一个应用程序中使用 i18n,我需要在 getStaticProps 中访问当前页面语言,然后获取数据

export const getStaticProps = async () => {
  //need to get language here

  return {
    props: { data },
  };
};

const App = ({ data }) => {
  //my component where i can get language

  const { t, i18n } = useTranslation();

  const currentLang = i18n.language;
};
Run Code Online (Sandbox Code Playgroud)

pau*_*btw 6

您可以从传递给 getStaticProps 的对象中获取本地值。您可以在Vercel 的示例中看到它

在你的情况下,它可能看起来像这样:

export const getStaticProps = async ({ locale }) => {
  doSomethingWithLocale(locale)

  return {
    props: { data },
  };
};
Run Code Online (Sandbox Code Playgroud)