-
-
Notifications
You must be signed in to change notification settings - Fork 760
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
No i18n object while server rendering #1668
Comments
+1 I noticed this issue for the first time upgrading from nextjs Unlike the OP we were previously able to access the
Upon updating we were getting the same type error as OP: We had stored a users' language in localStorage for another feature so we bypassed this issue by leveraging the localstorage value instead of |
@gritaliana Please see an answer to the global i18n client question here. In general it will be @neverether That's interesting, and @gritaliana has also mentioned using NextJs v12, although they didn't mention the specific version. Have you been able to track this down to a single version (or even better, a canary)? Anecdotally I have seen a lot of issues with NextJs v12 across the board. |
I see, yes - the i18n object is initialised quickly at runtime. Thank you for your answer! @isaachinman Although I have another question. Maybe you could give an idea for solution. It is said on Next docs that custom Error page do not accept new Data Fetching functions and only work with old
Here is the question: how then should I provide a translation in custom |
This is not exactly what I needed. We are using Error page for other errors, not 404 or 500. To show custom message for unauthorised error, for example. And as it is described in #1058 is not working. |
@gritaliana That is a separate issue, and can be handled separately. |
@isaachinman Thanks |
@MailboxArsonist One option would be to use this functionality. |
Sorry to reopen the original OP issue, but I think I missed what the fix was? and in our page layout const Layout = ({ children }) => {
const { i18n } = useTranslation()
const language = i18n.language
console.log({ language })
return (
<Page className={`lang-${language}`}>
<SkipToContentBar />
<Header siteTitle={siteMetadata.title} />
<Main id="main-content">{children}</Main>
<Footer divider={false} noLocalPage={['contact']} />
<AccessibilityDialog />
</Page>
)
} |
@iDVB make sure you called serverSideTranslations... |
Thanks @adrai .... but I think I'm still confused. import { useTranslation } from 'next-i18next'
...
const { i18n } = useTranslation()
const { language, changeLanguage } = i18n
|
Sorry @adrai , what I'm seeing when implemented is not what I understood from your guidence above. Simply doing this...
is fixing that issue where any component nested at any level below to HAVE a value for
where as this used to be undefined. I guess I don't understand what is happening under the covers to make this true. |
serverSideTranslations is just needed on page level |
Thanks @adrai Are we supposed to use
I'm seeing the initial default language coming through |
For next.js projects I never use changeLanguage... I always use a route redirect....
or like this:
using changeLanguage() would nevertheless only work on client side... btw. I would always recommend to use i18n.resolvedLanguage in that case: https://www.i18next.com/overview/api#resolvedlanguage In general, if you have an issue with next-i18next... create a minimal reproducible example and open a new issue... and to ask general questions you may ask at stackoverflow... |
Thanks, yes what we have now works in GatsbyJS with raw We indeed need to be able to Not sure what you are meaning in using The docs sound similar to me between
additionally, I don't see any mention of how to change the language in the |
This may help to understand: 6ddb2d0 Please compare your code with this example: https://github.com/i18next/next-i18next/tree/master/examples/simple |
Got it! |
Describe the bug
In a component which has a locale selector I am using
import { i18n } from 'next-i18next'
.And in render method I am using
i18n.language
. But while server rendering there is no i18n object.Therefore a component is producing a
TypeError: Cannot read properties of null (reading 'language')
.But in your migration doc it is said:
How should I access to i18n object?
Because I also have utils files, or use translation strings in mobx stores. How should I access the
t
function though?Occurs in next-i18next version
The latest next-i18next package - 10.2.0, along with Next updating to v.12
Steps to reproduce
I have migrated configuration due to the docs:
next-i18next.config.js
next.config.js
Then I wrap the
_app.js
component, need to handle config due to unserialisable config:export default appWithTranslation(withMobx(getStores)(MyApp), nextI18NextConfig)
And in
index.js
page provide new syntax with serverSideTranslations also with next config as the second param.I can use
import { useTranslation } from 'next-i18next'
- it works.But no access to
i18n
object.OS (please complete the following information)
The text was updated successfully, but these errors were encountered: