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

loadPath accepts a Promise #77

Closed jdeniau closed 3 years ago

jdeniau commented 3 years ago

loadPath might return a Promise.

For example, I want to call some endpoint to get a hashkey :

call /translations/hash/some-file.en.json

the hash is needed to get the real translation file.

adrai commented 3 years ago

thank you, it's included in v1.3.0

rehamjuma commented 3 years ago

Hello @jdeniau @adrai , can you help me please I'm trying to use this feature by returning a promise in loadPath from an external URL

     backend: {
        loadPath:  (lng, ns) => {
          return  await axios.get(`https://s3.amazonaws.com/x/global/translations/{{lng}}/{{ns}}.json`);
        },
        crossDomain: true,
      },

but I keep got this error


/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/extname.js:10
  if (filename.indexOf('.') < 0) return undefined;
               ^
TypeError: filename.indexOf is not a function
    at _default (/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/extname.js:10:16)
    at readFile (/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/readFile.js:159:34)
    at Backend.read (/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/index.js:91:30)
    at Connector.read (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1888:34)
    at Connector.loadOne (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1946:12)
    at /var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1922:16
    at Array.forEach (<anonymous>)
    at Connector.prepareLoading (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1921:21)
    at Connector.load (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1928:12)
    at I18n.loadResources (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:2301:40)

any help would be appreciated #77

adrai commented 3 years ago

Hello @jdeniau @adrai , can you help me please

I'm trying to use this feature by returning a promise in loadPath from an external URL


     backend: {

        loadPath:  (lng, ns) => {

          return  await axios.get(`https://s3.amazonaws.com/x/global/translations/{{lng}}/{{ns}}.json`);

        },

        crossDomain: true,

      },

but I keep got this error


/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/extname.js:10

  if (filename.indexOf('.') < 0) return undefined;

               ^

TypeError: filename.indexOf is not a function

    at _default (/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/extname.js:10:16)

    at readFile (/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/readFile.js:159:34)

    at Backend.read (/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/index.js:91:30)

    at Connector.read (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1888:34)

    at Connector.loadOne (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1946:12)

    at /var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1922:16

    at Array.forEach (<anonymous>)

    at Connector.prepareLoading (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1921:21)

    at Connector.load (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1928:12)

    at I18n.loadResources (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:2301:40)

any help would be appreciated #77

it seems you're using i18next-fs-backend... there is no loadPath function option... https://github.com/i18next/i18next-fs-backend#backend-options

rehamjuma commented 3 years ago

Hello @jdeniau @adrai , can you help me please I'm trying to use this feature by returning a promise in loadPath from an external URL


     backend: {

        loadPath:  (lng, ns) => {

          return  await axios.get(`https://s3.amazonaws.com/x/global/translations/{{lng}}/{{ns}}.json`);

        },

        crossDomain: true,

      },

but I keep got this error


/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/extname.js:10

  if (filename.indexOf('.') < 0) return undefined;

               ^

TypeError: filename.indexOf is not a function

    at _default (/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/extname.js:10:16)

    at readFile (/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/readFile.js:159:34)

    at Backend.read (/var/www/syarah_react/node_modules/i18next-fs-backend/cjs/index.js:91:30)

    at Connector.read (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1888:34)

    at Connector.loadOne (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1946:12)

    at /var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1922:16

    at Array.forEach (<anonymous>)

    at Connector.prepareLoading (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1921:21)

    at Connector.load (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:1928:12)

    at I18n.loadResources (/var/www/syarah_react/node_modules/i18next/dist/cjs/i18next.js:2301:40)

any help would be appreciated #77

it seems you're using i18next-fs-backend... there is no loadPath function option... https://github.com/i18next/i18next-fs-backend#backend-options

@adrai ooh yes thank you! actually I'm trying to do razzle SSR for API loadPath, is there any example for doing it using i18next-http-backend ?

adrai commented 3 years ago

https://github.com/i18next/react-i18next/tree/master/example/razzle-ssr

rehamjuma commented 3 years ago

https://github.com/i18next/react-i18next/tree/master/example/razzle-ssr

Hi @adrai , thanks for your fast reply since i18next-fs-backend does not accept external paths , I replaced it with i18next-http-backend
it's working fine in the client side and in the SSR but I'm facing an issue ,

the updates in the content of the external translations file is just reflected directly on the client side not on the SSR , I have to create a new build on each change to see it on the SSR, witch is not practical, it seems like it's cached or sth on Somewhere on the backend !

is there any configuration or option to keep it always updated on the server side that I should use it ?

adrai commented 3 years ago

I'm sorry, I'm not an razzle user...