All of a sudden I've started getting an error at runtime when using i18next (version 22.4.5
) when executing the following code (at services.formatter.add
):
"Uncaught TypeError: Cannot read properties of undefined (reading 'add')"
Upon debugging, the services
object on i18nextInstance
is empty ( {}
) after initialisation (see screenshot) which would cause the TypeError, but I can't find out why this object is now empty.
const i18nextInstance = i18next.createInstance({
debug: true,
lng: currentLanguage,
fallbackLng: 'en-GB', // Fallback to English strings and don't show an error
fallbackNS: defaultNamespace, // Fallback for strings translated with `t`
defaultNS: defaultNamespace,
resources: resources
});
i18nextInstance.t('common:Hello World');
/**
* Formatter function for locale-specific relative time strings
*/
i18nextInstance.services.formatter.add('fromnow', (value: Date, lng, options): string => {
return timeAgo(value, lng);
});
I was working with this exact version of the package last week and it was functioning as expected - what would cause this all of a sudden? My coworker ran the same code (we're both on Windows) and he had the exact same problem. I only encountered this when trying to get the component code Jest tests working, but now it's appearing in Chrome.
For context, I'm using this library inside a StencilJS project, inside its own module that is imported at the start of the app by some components.
I tried installing a later version of the library (22.4.9) with no luck.
I've tried resetting my node_modules folder and re-installing the library, as well as deleting all stencil build-related folders (WWW, dist, loader).
I did a hard reload on Chrome of the offending page.
Figured it out - turns out createInstance
doesn't initialise the instance, so I needed to init
add after createInstance
:
i18nextInstance.init();
This is really weird as I didn't need to use that function before, but I guess there may have been something cached?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.