i18next / i18next-http-backend

i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.
MIT License
453 stars 70 forks source link

Custom request function for missing translations #65

Closed Mottoweb closed 3 years ago

Mottoweb commented 3 years ago

🚀 Feature Proposal

There is a possibility to define a custom request function for getting the translations but looks like there is no such param for adding a missing translation. A feature would define a custom add missing functions that would be called when a missing translation found, just like a custom request,

Motivation

My project uses typed api helpers generated from open API spec, so it makes sense to use API methods provided rather than configuring request options by hand.

Example

request: (
    payload,
    callback: RequestCallback,
  ) :void => {
    const {lng, ns, key} = payload;
    const params: addMissingParameters = {
      lng,
      ns,
      key,
    };
    addMissing(params).then(({ status, data }) => {
      const response: RequestResponse = {
        status,
        data: JSON.stringify(data?.[0]?.namespaces?.[0]?.values),
      };
      callback(null, response);
    }).catch((error: AxiosError) => callback(error, { status: error?.response?.status || 500, data: '' }));
  },
adrai commented 3 years ago

i18next has a missingKeyHandler: https://www.i18next.com/overview/configuration-options#missing-keys This may help.

btw: the passed in custom request function is also called for missing requests, you may just check if it is a POST or a GET request

Mottoweb commented 3 years ago

There are workarounds for sure yes, I could use I onMissing callback as well, just thought it makes sense to have it in the backend options.

I will check your 'btw' now thanks :)

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.