question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for onFailedLoading API

See original GitHub issue

🚀 Feature Proposal

I would like to be able to register a callback to the onFailedLoading i18next event. We cannot use the global i18n instance to do this because .init() is called before we are able to register the callback.

// _app.tsx
function MyApp({ Component, pageProps }: AppProps) {
  useEffect(() => {
    function handleFailedLoading() {
      logger.error('failed to load translations');
    }

    // This callback will never be called because the i18n instance
    // has already attempted to load the translation data. The event
    // would have already fired before this is registered.
    i18n.on('failedLoading', handleFailedLoading);

    return () => {
      i18n.off('failedLoading', handleFailedLoading);
    }
  }, [])

  //...
}

Motivation

We had a bug on our end that caused translations not to be loaded. We would like to be able to add logging/alerting when this happens.

Example

The example above already shows one way we might use this. I am guessing that next-i18next would need to provide some way in appWithTranslation to allow us to hook into the time between when the i18n instance is created and when it is initialized. Maybe a third argument that takes a callback that is passed the i18n instance before init?

export default appWithTranslation(MyApp, null, (i18n) => {
  i18n.on('failedLoading', handleFailedLoading)
});

Updating this

https://github.com/i18next/next-i18next/blob/a6affe3e0c5bf7471233e626ec2ce3d04ad1e4c4/src/createClient/browser.ts#L10-L15

To something like this:

  if (!instance.isInitialized) {
    config?.use?.forEach(x => instance.use(x))
    i18nCallback?.(instance)
    initPromise = instance.init(config)
  } else {
    i18nCallback?.(instance)
    initPromise = Promise.resolve(i18n.t)
  }

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
adraicommented, Sep 30, 2022

Is that something you would consider?

Usually, if there is the need of listening to failedLoading events or other custom api, there is also a backend plugin like i18next-http-backend in use… this means the config is unserializable anyway. btw: not only the appWithTranslation is affected, but also the serverSideTranslation call

0reactions
adraicommented, Sep 30, 2022

If you like this module don’t forget to star this repo. Make a tweet, share the word or have a look at our https://locize.com to support the devs of this project.

There are many ways to help this project 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

API - i18next documentation
onFailedLoading. i18next.on('failedLoading', function(lng, ns, msg) {}). Gets fired if loading resources failed (after the in-built retry algorithm).
Read more >
WebResourceLoadDelegate.OnFailedLoading ... - Microsoft Learn
WebResourceLoadDelegate.OnFailedLoading in the WebKit namespace. ... public virtual void OnFailedLoading (WebKit. ... Xamarin.Mac SDK, 14 ...
Read more >
C# API reference - Unity Documentation
This method returns the version of the Unity Mediation SDK that is currently being used. ... OnFailedLoad, Execute logic for the ad failing...
Read more >
.NET API Catalog
namespace WebKit { [Deprecated(PlatformName.MacOSX, 10, 14, PlatformArchitecture.None, "No longer supported.
Read more >
How to implement a scalable support strategy for your API
Your team might be great at supporting your primary product, but know nothing about APIs. Building a specialized API support team. You might ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found