next-translate icon indicating copy to clipboard operation
next-translate copied to clipboard

i18n config, pages function callback not working as expected

Open sigginjals opened this issue 4 years ago • 3 comments

According to https://github.com/vinissimus/next-translate#3-configuration

I should be able to do something like this:

pages: { "/": ({ req }) => req.headers.hostname === 'example' ? ['example'] : []}

This however doesn't work, when logging out the value passed in the callback it just returns me the i18n config 🤷‍♂️

Steps to reproduce

  1. Use the "complex" example
  2. Change this line: https://github.com/vinissimus/next-translate/blob/master/examples/complex/i18n.js#L8 to: '/': ({ req }) => (req.headers.hostname === 'example' ? ['example'] : []),
  3. Observe errors in the console like this: Cannot read properties of undefined (reading 'headers')

sigginjals avatar Jan 28 '22 10:01 sigginjals

I managed to get it to work, I had to make sure I was spreading the context inside of loadNamespaces in my getInitialProps. await loadNamespaces({ ...ctx, locale, pathname: '/' });, I'm getting a typescript error which I have to ignore... Any thoughts on this?

sigginjals avatar Jan 28 '22 14:01 sigginjals

@sigginjals don't you use the Webpack loader we provide? Are you using loadNamespaces instead?

If this is the case, I understand is a thing to improve in the documentation. However, if you use the Webpack loader and also you need to use the loadNamespaces to fix this, is not the way (maybe as a temporal workaround) and needs to be fixed.

aralroca avatar Jan 28 '22 15:01 aralroca

@sigginjals I'm not using the webpack loader, I'm using loadNamespaces.

sigginjals avatar Jan 28 '22 15:01 sigginjals