Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getT() in next13's generateMetadata() doesn't output any translations #1020

Closed
synoptase opened this issue Apr 3, 2023 · 10 comments
Closed

Comments

@synoptase
Copy link

What version of this package are you using?
v2.0.5-canary.1

What operating system, Node.js, and npm version?
macOS Ventura 13.2, node 18.x, yarn 3.5.0

What happened?

Using getT() in a simple generateMetadata like below doesn't translate the key passed in the t() function.

export async function generateMetadata() {
  const t = await getT('fr', 'common')

  return {
    title: t('title'),
    description: t('description'),
  }
}

Passing the { fallback: 'Super titre' } option will show the fallback. It seems that the namespace isn't found.

What did you expect to happen?
Translated keys.

Are you willing to submit a pull request to fix this bug?

@kiyasov
Copy link

kiyasov commented May 13, 2023

What version of this package are you using? v2.0.5-canary.1

What operating system, Node.js, and npm version? macOS Ventura 13.2, node 18.x, yarn 3.5.0

What happened?

Using getT() in a simple generateMetadata like below doesn't translate the key passed in the t() function.

export async function generateMetadata() {
  const t = await getT('fr', 'common')

  return {
    title: t('title'),
    description: t('description'),
  }
}

Passing the { fallback: 'Super titre' } option will show the fallback. It seems that the namespace isn't found.

What did you expect to happen? Translated keys.

Are you willing to submit a pull request to fix this bug?

Hi! Did you find a solution?

@nkabour
Copy link

nkabour commented Jun 5, 2023

Hey; did you manage to find a solution for that?
I'm having a similar problem; instead i get t('title') returns title and thats it. props.params.lang in the generateMeta props is not in sync with props.searchParams.lang; where props.params.lang is usually doesn't match the actual lang in the url.

@kiyasov
Copy link

kiyasov commented Jun 5, 2023

Hey; did you manage to find a solution for that? I'm having a similar problem; instead i get t('title') returns title and thats it. props.params.lang in the generateMeta props is not in sync with props.searchParams.lang; where props.params.lang is usually doesn't match the actual lang in the url.

Moved to https://next-intl-docs.vercel.app

@aralroca
Copy link
Owner

It should work in 2.4.2 👌 I close the issue, feel free to comment and I will reopen the issue if doesn't work, but now getT is fixed to work in app dir

@aralroca
Copy link
Owner

aralroca commented Jun 25, 2023

BTW; probably you can use useTranslation hook, because in reality now is not a real hook and can be used as helper. Probably make sense to rename a copy of useTranslation as getTranslation to be consistent in the ecosystem semantics (this only applies in appDir, in pages folder is a real hook)

@nkabour
Copy link

nkabour commented Jul 4, 2023

BTW; probably you can use useTranslation hook, because in reality now is not a real hook and can be used as helper. Probably make sense to rename a copy of useTranslation as getTranslation to be consistent in the ecosystem semantics (this only applies in appDir, in pages folder is a real hook)

true thats what i've been doing so far; but it felt more like a workaround and sometimes title get stale and doesn't change during client nav. however it works well so far with v2.3.1.

The new v2.4.4 still doesn't work for me but now i'm having the same behavior with the useTranslation hook in server components as well. This is the server component:

import useTranslation from "next-translate/useTranslation"
import getT from "next-translate/getT"
import { Fragment } from "react"
import React from "react"
import { LoginFrom } from "../components/LoginForm"
import { GenerateMetadataProps } from "@/types/generate_metadata_props"

export default async function Login() {
  const { t } = useTranslation("login")
  console.log("this is login component: ", t("title"), t("description"))
  return (
    <Fragment>
      <div className="flex flex-col gap-y-2 pt-6  md:px-32 md:pt-32">
        <h3 className="text-2xl mt-2 text-white">{t("title")}</h3>
        <hr className="mt-2 mb-6" />
        <LoginFrom />
      </div>
    </Fragment>
  )
}

export async function generateMetadata({ params: { lang } }: GenerateMetadataProps) {
  const t = await getT(lang, "login")

  console.log("this is generate metadata: ", t("title"), t("description"))
  return {
    title: t("title"),
    description: t("description"),
  }
}

These are the logs from the server:

next: next-translate - compiled page: /[lang]/(auth)/login - locale: en - namespaces: common, glossary, login - used loader: server /page
next: next-translate - compiled page: /[lang] - locale: en - namespaces: common, glossary - used loader: server /layout
next: this is generate metadata:  title description
next: this is login component:  title description

and here is my i18n.js:

module.exports = {
  locales: ["en", "ar"],
  defaultLocale: "en",
  pages: {
    "*": ["common", "glossary"],
    "/[lang]/(auth)/login": ["login"],
  },
}

@aralroca
Copy link
Owner

@nkabour is working for you in 2.5.0-canary.3? thanks

@nkabour
Copy link

nkabour commented Jul 15, 2023

@nkabour is working for you in 2.5.0-canary.3? thanks

with next-translate version 2.4.4 and next-translate-plugin version 2.5.0-canary.3; it seems like server & client components translations are working as expected, but getT still gives the same results. Here are the logs for server component and the getMetadata functions.

next: next-translate - compiled page: /[lang]/(auth)/login - locale: en - namespaces: common, glossary, login - used loader: server /page
next: next-translate - compiled page: /[lang] - locale: en - namespaces: common, glossary - used loader: server /layout
next: this is generate metadata title
next: this is server component Login

@wldhg
Copy link

wldhg commented Sep 26, 2023

still no translations returned in next-translate 2.5.3 with next-translate-plugin 2.5.3

BTW; probably you can use useTranslation hook, because in reality now is not a real hook and can be used as helper. Probably make sense to rename a copy of useTranslation as getTranslation to be consistent in the ecosystem semantics (this only applies in appDir, in pages folder is a real hook)

and the workaround works well in generateMetadata function 😀

@sawa-ko
Copy link

sawa-ko commented Apr 23, 2024

Still not working @aralroca :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants