aralroca / next-translate-plugin

Next-translate plugin for i18n in Next.js 🌍 - Load page translations and use them in an easy way!
MIT License
30 stars 17 forks source link

Can't render proper title in next.js generateMetadata #52

Closed brunoscopelliti closed 1 year ago

brunoscopelliti commented 1 year ago
"next": "^13.4.7"
"next-translate": "^2.4.2"
"next-translate-plugin": "^2.4.2"
node v18.15.0
macOS Ventura

I'm attaching a repro, repro.zip. Download it, install dependencies, and visit /demo. In browser's title bar you should read something like "common:page_title" instead of the label.

export const generateMetadata = async ({ searchParams }: any) => {
  const t = await getT(searchParams.lang, ["common"]);
  return {
    title: t("common:page_title"),
  };
};
aralroca commented 1 year ago

Ok, the problem is that we serialized the config in the last release... And after serialize it getT can't access to loadLocaleFrom... So probably we introduced this issue in 2.4.3... Meanwhile, as a workaround you can overwrite the global config:

import config from "../../../i18n";
import getT from "next-translate/getT";

export const generateMetadata = async ({ searchParams }: any) => {
  globalThis.__NEXT_TRANSLATE__ = { lang: searchParams.lang, config };  // temporal workaround
  const t = await getT(searchParams.lang, "common");

  return {
    title: t("common:page_title"),
  };
};
aralroca commented 1 year ago

Fixed in next-translate-plugin 2.4.4. Thanks for your contributions reporting these errors @brunoscopelliti

brunoscopelliti commented 1 year ago

Confirm version 2.4.4 fixes all problems. Thank you