You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to setup i18n in my app using i18Next. It works just fine, but something bothers me: here's a piece of code taken from the official article on how to add i18n to a Next.js 14 app
asyncfunctioninitI18next(lng: string,ns: string){// on server side we create a new instance for each render, because during compilation everything seems to be executed in parallelconsti18nInstance=createInstance();awaiti18nInstance.use(initReactI18next).use(resourcesToBackend((language: string,namespace: string)=>import(`./locales/${language}/${namespace}.json`),),).init(getOptions(lng,ns));returni18nInstance;}interfaceI18nOptions{keyPrefix?: string;}exportdefaultasyncfunctionuseTranslation(lng: string,ns: string,options: I18nOptions={},){consti18nextInstance=awaitinitI18next(lng,ns);return{t: i18nextInstance.getFixedT(lng,ns,options.keyPrefix),i18n: i18nextInstance,};}
When using this, it creates a new instance on every render, which is not optimal.
Is it possible to define a singleton for the instance, so that it is only created once ?
I dug up a bit, and it appears that this might cause some issues regarding the locale in the request, but my locale is not stored in my URL. Instead, it is fetched once from my user settings.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm trying to setup i18n in my app using i18Next. It works just fine, but something bothers me: here's a piece of code taken from the official article on how to add i18n to a Next.js 14 app
When using this, it creates a new instance on every render, which is not optimal.
Is it possible to define a singleton for the instance, so that it is only created once ?
I dug up a bit, and it appears that this might cause some issues regarding the locale in the request, but my locale is not stored in my URL. Instead, it is fetched once from my user settings.
Beta Was this translation helpful? Give feedback.
All reactions