nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.65k stars 465 forks source link

I am not able to load a js file in locale that contains other attached json files. #2950

Closed joaquinCaceres closed 1 week ago

joaquinCaceres commented 1 month ago

Environment


Reproduction

.

Describe the bug

I'm trying to config a locale whose file is a javascript file that return a object. if the dictionary is simple like {‘title’: ‘about’, ‘data’: ‘the data’} everything works correctly and I can access those values via $t(title) without problems. However, if the dictionary includes other elements imported from other js files, I am not able to access them.

The configuration of my environment is as follows:

NUXT.CONFIG.TS:

export default defineNuxtConfig({ devtools: { enabled: true }, modules: ["@nuxtjs/i18n"], i18n: { lazy: true, langDir: "./locales", strategy: 'prefix_and_default', locales: [ { code: "en", name: "English(US)", file: "en/index.js", }, { code: "es", name: "Español", file: "es/index.js", }, ], defaultLocale: "es", } })

INDEX.JS

import portfolioMain from './pages/portfolioMain' import blogMain from './pages/blogMain'

export default { portfolioMain, blogMain, "section-name": "about me"

}

PORTFOLIOMAIN.JS

export default { title: 'the house' }

In this configuration, I can access to section-name without problems. Could I help me, please? My intention is to have the files as tidy as possible within the directory

Additional context

No response

Logs

WARN  [intlify] Not found 'portfolioMain.title' key in 'es' locale messages.
dawid-ziobro commented 1 month ago

Hi @joaquinCaceres I've had the same issue. Try using defineI18nLocale function in your translation file.

So instead of this:

export default {
portfolioMain,
blogMain,
"section-name": "about me"
}

do this:

export default defineI18nLocale(() => {
  portfolioMain,
  blogMain,
  "section-name": "about me"

});

You can read more about that here -> https://i18n.nuxtjs.org/docs/guide/lazy-load-translations

github-actions[bot] commented 2 weeks ago

Would you be able to provide a reproduction? 🙏

More info ### Why do I need to provide a reproduction? Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making. ### What will happen? If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritise it based on its severity and how many people we think it might affect. If `needs reproduction` labeled issues don't receive any substantial activity (e.g., new comments featuring a reproduction link), we'll close them. That's not because we don't care! At any point, feel free to comment with a reproduction and we'll reopen it. ### How can I create a reproduction? We have a couple of templates for starting with a minimal reproduction: 👉 [Reproduction starter (v8 and higher)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/v8) 👉 [Reproduction starter (edge)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/edge) A public GitHub repository is also perfect. 👌 Please ensure that the reproduction is as **minimal** as possible. See more details [in our guide](https://nuxt.com/docs/community/reporting-bugs/#create-a-minimal-reproduction). You might also find these other articles interesting and/or helpful: - [The Importance of Reproductions](https://antfu.me/posts/why-reproductions-are-required) - [How to Generate a Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve)
kazupon commented 1 week ago

Closing due to inactivity. Please open a new issue with a reference to this one if you can follow up with more information.

Thanks!