gatsby-plugin-react-i18next icon indicating copy to clipboard operation
gatsby-plugin-react-i18next copied to clipboard

Gatsby Head API support?

Open matiaskorhonen opened this issue 2 years ago • 11 comments

Is it possible to use translations with Gatsby's Head API?

I tried to just use the useTranslation hook, but it doesn't seem to find any translations:

For example:

import * as React from "react"
import { useTranslation } from "gatsby-plugin-react-i18next"

const Page = () => {
   const { t } = useTranslation();
  return (<div>t("home.title")</div>);
}


export default Page

export function Head() {
  const { t } = useTranslation();
  return (
    <title>{t("home.title")}</title>
  )
}

That just resulted in the page title getting set to home.title even though the page itself got the correct translation…

matiaskorhonen avatar Aug 16 '22 09:08 matiaskorhonen

A workaround that I found, that does not use 'useTranslation'

export const Head = ({ data }: HeadProps<any>) => {
    const locales = data.locales.edges[0].node.data;
    let obj: any = undefined;
    if (locales) {
        obj = JSON.parse(locales);
    }
    return <title>{ obj?.title }</title>;
};

emberal avatar Aug 19 '22 16:08 emberal

@h600878 thank you for this workaround ... I'm hoping the useTranslation will be fixed with the Head API though ... it will be more readable, and we can use the entire message as keys (nice feature of gatsby-plugin-react-i18next)

ruizanthony avatar Aug 20 '22 19:08 ruizanthony

I hope the usei18next will be work on the Head API soon.

ahmet-cetinkaya avatar Aug 30 '22 17:08 ahmet-cetinkaya

@h600878 thank you so much for your snippet!

Nosferatu31 avatar Feb 03 '23 23:02 Nosferatu31

BTW, when using templated strings such as:

{
    "welcome_message": "Welcome, {{name}}!"
}

It helps to use:

String.prototype.format = function () {
	var i = 0, args = arguments
	return this.replace(/{{.*}}/g, function () {
		return typeof args[i] != 'undefined' ? args[i++] : ''
	})
}

So, for instance:

return <title>{ obj?.welcome_message.format('Bob') }</title>;

Outputs:

"Welcome, Bob!"

Note: the format arguments must follow the order of appearance of the translation string

Nosferatu31 avatar Feb 03 '23 23:02 Nosferatu31

Are there any current plans to support the Gatsby Head API in the future? What is the specific problem that this is not supported? As it is now, almost no i18n SEO support is possible 😕. @h600878's solution is more of a hack than a solution...

MuellerConstantin avatar Mar 02 '23 21:03 MuellerConstantin

Are there any current plans to support the Gatsby Head API in the future? What is the specific problem that this is not supported? As it is now, almost no i18n SEO support is possible 😕. @h600878's solution is more of a hack than a solution...

true!

newme616 avatar Mar 13 '23 10:03 newme616

The problem is the "place" where the I18next react context is declared, right? For declaration the wrapPageElement API is used in this plugin. However, according to the documentation, only the contexts declared using wrapRootElement, not using wrapPageElement, are reachable in the Head API.

As of [email protected], Head can access React Context that you defined in the wrapRootElement API. It’s important to note that wrapRootElement should only be used to set up context providers. UI components should be defined in wrapPageElement API.

Is it an option to just use wrapRootElement instead of wrapPageElement, or does it introduce new hidden problems?

MuellerConstantin avatar Mar 14 '23 13:03 MuellerConstantin

For me the problem was not using the Head API inside a "Page" i.e. a file under /pages. Read "Usage Notes" carefully https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-head/#usage-notes

kuzdogan avatar Sep 10 '23 20:09 kuzdogan

Can we also handle wrapRootElement as mentioned in Gatsby Head documentation in the next versions? Currently, the hacky workarounds are suggested, but Gatsby Head support can be provided in the plugin level.

burakozdemir32 avatar Oct 09 '23 10:10 burakozdemir32