Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Absolute langDir path isn't validated/resolved correctly #1913

Closed
Ni55aN opened this issue Mar 6, 2023 · 4 comments · Fixed by #1921
Closed

Absolute langDir path isn't validated/resolved correctly #1913

Ni55aN opened this issue Mar 6, 2023 · 4 comments · Fixed by #1921

Comments

@Ni55aN
Copy link

Ni55aN commented Mar 6, 2023

Environment

https://stackblitz.com/edit/github-wfnczp-7tzsju?file=nuxt.config.ts

Reproduction

a. Dev mode

npm run dev

✔ The page contains

Hello!

Welcome issue

b. Build

npm run build 
node .output/server/index.mjs

Open preview page with disabled JS

❌ The page contains

hello

welcome

It indicates that the translations were not loaded at all

c. Generate

npm run generate
npx serve .output/public/

❌ The page contains:

Hello!

Welcome {name}

Translations are loaded at generate, but interpolation doesn't work correctly

Describe the bug

First of all, at build stage langDir: '/locales' is accepted. Looks like it isn't recognized as an absolute path because somewhere join is used instead of resolve.

When I specify resolve('locales'), it throws an error Could not resolve "../home/username/i18n-interpolation-issue/locales/en.json" from ".nuxt/i18n.options.mjs"

Additional context

The most confusing things are"

  • regular message is translated correctly on the client side (with enabled JS, after hydration) or after npm run generate, but not message with interpolation.
  • everything works fine while development (npm run dev)

Logs

No response

@kazupon kazupon added the v8 label Mar 7, 2023 — with Volta.net
@BobbieGoede
Copy link
Collaborator

langDir is resolved relative to srcDir, your project should work with langDir: 'locales'. I think the docs probably need to explain this.

For reference:

const langPath = isString(options.langDir) ? resolve(nuxt.options.srcDir, options.langDir) : null

@Ni55aN
Copy link
Author

Ni55aN commented Mar 9, 2023

@BobbieGoede in this case langPath must be equal to langDir if langDir is an absolute path.

p.resolve('/path/to/project', '/locales') === '/locales'

But in dev mode and during generate it correctly extracts locales from /path/to/project/locales

@BobbieGoede
Copy link
Collaborator

@Ni55aN

p.resolve('/path/to/project', '/locales') === '/locales'

I think that's the issue, as the following is what happens when passing a relative path.

p.resolve('/path/to/project', 'locales') === '/path/to/project/locales'
p.resolve('/path/to/project', './locales') === '/path/to/project/locales'

I'm not sure why it does work in dev mode however.

@Ni55aN
Copy link
Author

Ni55aN commented Mar 9, 2023

Probably the issue is that generateLoaderOptions accepts unresolved options.langDir

i18n/src/module.ts

Lines 145 to 148 in 029c634

return generateLoaderOptions(
options.lazy,
options.langDir,
localesRelativeBasePath,

which is "resolved" inside by resolveLocaleRelativePath

i18n/src/gen.ts

Lines 171 to 173 in 029c634

function resolveLocaleRelativePath(relativeBase: string, langDir: string, file: string) {
return normalize(`${relativeBase}/${langDir}/${file}`)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants