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

languages from useI18next() contains only 1 language #141

Open
gabric098 opened this issue Jul 4, 2022 · 6 comments
Open

languages from useI18next() contains only 1 language #141

gabric098 opened this issue Jul 4, 2022 · 6 comments

Comments

@gabric098
Copy link

gabric098 commented Jul 4, 2022

I'm running gatsby 4.12.1 with react 18.2.0 using latest version of gatsby-plugin-react-i18next.
I followed the examples included in the plugin. When I try to obtain the list of available languages using the following code:

const {languages, originalPath} = useI18next();

I keep getting only ['en'] in the languages array even if there's no en language defined in my setup.

Here's how my gatsby-config.js looks like:

...
  plugins: [
{
      resolve: `gatsby-source-filesystem`,
      options: {
        path: `${__dirname}/locales`,
        name: `locale`
      }
    },
    {
      resolve: `gatsby-plugin-react-i18next`,
      options: {
        localeJsonSourceName: `locale`,
        languages: [`de`, `es`],
        defaultLanguage: `de`,
        siteUrl: `http://localhost:8000/`,
        i18nextOptions: {
          interpolation: {
            escapeValue: false 
          },
          keySeparator: false,
          nsSeparator: false
        },
      }
    }
]
...

I've created the following files:

  • locales/de/translation.json
  • locales/es/translation.json

All seems to be working fine, If I access http://localhost:8000/ I get the German translations, If I access http://localhost:8000/es I get the Spanish. However, when I try to list the available languages by using the following code:

const {languages, originalPath} = useI18next();

no matter what, languages always contains a single element en.

Does anyone have any idea about what I'm doing wrong?

@Dennnnny
Copy link

same issue here , and I found it can get two in pages/index , but can only get one in component/Header/index
try to find what's wrong too ;(
any help/suggestions welcome

@Dennnnny
Copy link

I think it's because of my gatsby-browser.tsx. so I can not access the whole languages.
but, still I don't know how to fix this yet, keep working on it ;(

@Dennnnny
Copy link

I think I found some solution, maybe could work.
so, I found it in someone else blog structure.
It's gatsby-browser file :

I change mine to :

export const wrapPageElement: GatsbyBrowser["wrapPageElement"] = ({
  props,
  element,
}) => {
  return React.cloneElement(
    element, // I18nextProvider
    props,
    element.props.children &&
      React.cloneElement(
        element.props.children, // I18nextContext.Provider
        element.props.children?.props,
        React.createElement(
          Container,
          props,
          element.props.children?.props.children
        )
      )
  );
};

and this work foe me.
It's a typescript version btw.
hope it works for you too

@gabric098
Copy link
Author

gabric098 commented Oct 19, 2022

Hey @Dennnnny It does work indeed! would you mind sharing the source where you found an example of this solution? I can't quite figure out why it's working.

Thank you!

@Dennnnny
Copy link

Dennnnny commented Nov 5, 2022

@gabric098 here's what I found : https://andremonteiro.pt/gatsby-i18next-wrap-page-element/

@gabric098
Copy link
Author

Thank you @Dennnnny, really appreciated

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

2 participants