Next-i18next: Add support for "public" directory

Created on 8 Oct 2019  路  7Comments  路  Source: isaachinman/next-i18next

Is your feature request related to a problem? Please describe.

From nextjs 9.0.6 and above the static directory is moved to public/static. Is not mandatory to move it, but the static directory will be deprecated in the future. More information here.

Moving the static folder to public/static throws the next error:

***/app/node_modules/next-i18next/dist/commonjs/config/create-config.js:54
        throw new Error("Default namespace not found at ".concat(defaultNSPath));
        ^

Error: Default namespace not found at ***/app/static/locales/es/common.json
    at _default (***/app/node_modules/next-i18next/dist/commonjs/config/create-config.js:54:15)
    at new NextI18Next (***/app/node_modules/next-i18next/dist/commonjs/index.js:52:46)
    at Object.<anonymous> (***/app/i18n.js:12:18)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Module.require (internal/modules/cjs/loader.js:830:19)
    at require (internal/modules/cjs/helpers.js:68:18)
    at Object.<anonymous> (***/app/server.js:14:14)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Describe the solution you'd like

The path ***/app/static/locales/es/common.json should be ***/app/public/static/locales/es/common.json

Describe alternatives you've considered

For an easy fix, we could keep as temp solution the locales at the static path.

Additional context

...

help wanted

Most helpful comment

This worked for me:

  1. Move locales to /path/to/project/public/locales/{LOCALE}/{NAMESPACE}.json

  2. In /path/to/project/src/i18n.ts :

    export const nextI18next = new NextI18next({
      // ...
      localePath: typeof window === "undefined" ? "public/locales" : "locales",
    });
    

All 7 comments

PRs welcome.

This worked for me:

  1. Move locales to /path/to/project/public/locales/{LOCALE}/{NAMESPACE}.json

  2. In /path/to/project/src/i18n.ts :

    export const nextI18next = new NextI18next({
      // ...
      localePath: typeof window === "undefined" ? "public/locales" : "locales",
    });
    

@kachkaev Yes, that will work. I think what we should do is:

  1. Change the default localePath to include public.
  2. If the default localePath is not found, try looking via the filesystem without public, and then print a console warning about deprecation

It'd be nice to support the changes to NextJs core while allowing a clear non-breaking "upgrade" path for users.

This worked for me:

1. Move `locales` to `/path/to/project/public/locales/{LOCALE}/{NAMESPACE}.json`

2. In `/path/to/project/src/i18n.ts` :
   ```ts
   export const nextI18next = new NextI18next({
     // ...
     localePath: typeof window === "undefined" ? "public/locales" : "locales",
   });
   ```

You can also check for process.browser, which is added by Next.js. If the module is on the client, that boolean will be true.

I found that this doesn't work on Now where, when built, the localePath for the server needs to be locales as well.

Thanks @nickrttn. The last bit about now deployments is interesting - @wootsbot are you aware of this? Can we detect now deployment envs?

I was still deploying my Next.js app as serverless lambda's, should probably compile to a server target 馃槉

Thanks @nickrttn. The last bit about now deployments is interesting - @wootsbot are you aware of this? Can we detect now deployment envs?

I'm sorry, I think it's been a long time.

Was this page helpful?
0 / 5 - 0 ratings