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

问题描述 投票:0回答:1

我在我的下一个应用程序中使用 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;
};
reactjs next.js i18next
1个回答
6
投票

您可以从传递给 getStaticProps 的对象中获取本地。 你可以在这个例子中看到它来自 Vercel

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

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

  return {
    props: { data },
  };
};
© www.soinside.com 2019 - 2024. All rights reserved.