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

Allow loading of translations from packages #2999

Open
3 of 4 tasks
markbrockhoff opened this issue Jun 27, 2024 · 3 comments
Open
3 of 4 tasks

Allow loading of translations from packages #2999

markbrockhoff opened this issue Jun 27, 2024 · 3 comments

Comments

@markbrockhoff
Copy link

markbrockhoff commented Jun 27, 2024

Describe the feature

Hi,

first of all thanks for creating and maintaining this amazing nuxt module. ❤️

Context

I'm currently working on a nuxt module for a component library that ships with default translations. I integrated this module to automatically handle all the translations by registering the component libraries translations under a scope, so it's easy to extend and overwrite them.
All of this works great with just the one exception that all locales of the module will now show up inside the project using it.

For example: If the component library registers the languages en-US and de-DE but the project only defines en-US, de-DE will still be shown as a locale.
All of this could be simplified by just importing the translations for the component library inside the project manually.

Proposed feature

Allow using node modules inside the files property of a locale

The feature could look something like this:

export default defineNuxtConfig({
  modules: [
    '@sit-onyx/nuxt',
    '@nuxtjs/i18n',
  ],
  i18n: {
    defaultLocale: 'de-DE',
    locales: [
      { code: 'de-DE', iso: 'de-DE', name: 'Deutsch', files: [{ path: '@sit-onyx/nuxt/locales/de-DE.json', external: true }, 'de-DE.json'] },
      { code: 'en-US', iso: 'en-US', name: 'English', files: [{ path: '@sit-onyx/nuxt/locales/en-US.json', external: true, scope: 'onyx' }, 'en_US.json']  },
    ],
    langDir: 'i18n',
    lazy: true,
  },
});

By adding a property external to the LocalFile type it would be possible to ignore the langDir and load it from node_modules instead.
Optionally a second property scope could be useful to scope the imported messages to avoid conflicts between the different files. Like in the example for the locale "en-US" where the messages for the component library would later be accessible under the key "onyx.*".

Additional information

  • Would you be willing to help implement this feature?
  • Could this feature be implemented as a module?

Final checks

@markbrockhoff
Copy link
Author

Sorry if I'm being a bit hasty here, but I already created a minimal PR (#3003) implementing the proposed feature for loading translation files from npm packages.
Please let me know what you think @kazupon :)

@kazupon
Copy link
Collaborator

kazupon commented Jun 28, 2024

Thank you for your feedback and pull-requesting! ❤️

I like your idea as it makes it easy to load locals.
As you can see in the URL below, nuxt-i18n v9 is considering reviewing or improving locales configuration.
#3002

locales conifguration also depends on nuxt layers, so we need to take that into account.
This feature needs to be considered, together @BobbieGoede

I have considered lightly for alternatives. I think that loading a locale from a package can be done by specifying it in path so that its package path can be resolved in nuxt.config.

@kazupon kazupon added the locale label Jun 28, 2024
@markbrockhoff
Copy link
Author

markbrockhoff commented Jun 28, 2024

Thanks for the quick feedback @kazupon! 👍

I agree, getting rid of the langDir would make things easier as all files could be resolved from the base of the project, including npm packages.

So if I got that right for v9 you'd imagine something like this?

export default defineNuxtConfig({
  i18n: {
    lazy: true,
    defaultLocale: 'en'
    locales: [
      {
        code: 'en',
        name: 'English',
        files: ['my-module/locales/en.json', './i18n/locales/en.json']
      }
    ],
  }
})

I like the idea but would it be possible to add this feature as non breaking within v8 already? From the checkboxes inside the roadmap it looks like v9 could still take a moment but at least to me this feature would already be pretty useful right now. :)

larsrickert pushed a commit to SchwarzIT/onyx that referenced this issue Jul 1, 2024
This PR fixes an issue with the registration order warning of the nuxt
module.
It also requires the user to specify a mapping for onyx locales to the
locales from nuxt-i18n specific to their project.
This fixes a bug where all projects would display all locales of onyx as
supported languages even if they were not specified within the project.
An issue and PR were already created for nuxt-i18n to support loading
locales from npm packages.
(nuxt-modules/i18n#2999)
This way it would be possible to decouple onyx and nuxt-i18n while
preserving easy support and customizability. This PR should make the
onyx module useable until the feature is available inside nuxt-i18n.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants