【问题标题】:How to access current language in getStaticProps Next.js?如何在 getStaticProps Next.js 中访问当前语言?
【发布时间】:2022-01-04 13:49:36
【问题描述】:

我在下一个应用中使用 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】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2020-10-29
    • 2023-04-03
    • 2011-03-22
    • 2021-08-02
    相关资源
    最近更新 更多