-
Notifications
You must be signed in to change notification settings - Fork 473
-
Notifications
You must be signed in to change notification settings - Fork 473
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
Nuxt Layers - Absolute "langDir" path #1890
Comments
Hey @bgrand-ch I also have i18n in the shared layer. So maybe my solution will be helpful for you. I created an import { createResolver, defineNuxtModule } from '@nuxt/kit'
export default defineNuxtModule({
async setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
nuxt.options.i18n.vueI18n = resolver.resolve('../locale/config/vue-i18n.ts') // It's the path in my shared layer
},
}) |
Hello @lukaszflorczak, thanks a lot for your quick response! I will test your solution soon 🤞 |
It's based on that: https://nuxt.com/docs/guide/going-further/layers#relative-paths-and-aliases |
It looks like For reference: Line 67 in 029c634
|
I found an workaround ... it has a small disadvantage(but at least in my case until this issue is fixed is ok) that if you are using layers and try to add translations files in the Lang folder in the "non shared" package that contains this config it will not work(basically all translation files would need to be added in the import { createResolver } from '@nuxt/kit';
// https://nuxt.com/docs/guide/going-further/layers#relative-paths-and-aliases
const { resolve } = createResolver(import.meta.url);
export default defineNuxtConfig({
modules: [
'@nuxtjs/i18n',
{
strategy: 'prefix',
defaultLocale: 'fr',
locale: 'fr',
legacy: true,
locales: [{ code: 'fr' }, { code: 'en' }],
vueI18n: {
fallbackLocale: 'fr',
legacy: true,
messages: {
en: require(resolve('./lang/en-US.json')),
fr: require(resolve('./lang/fr-FR.json')),
},
},
},
]
]
}) The files are fetched and also the url prefix works as intended |
Using the following config as your i18n layer should work, though I don't think it looks like a clean solution. It changes the import { createResolver } from '@nuxt/kit'
const { resolve } = createResolver(import.meta.url)
export default defineNuxtConfig({
modules: [
(_, nuxt) => {
const projectLayer = nuxt.options._layers[0]
// @ts-ignore
const relativeToProject = '.' + resolve(nuxt.options.i18n.langDir).replace(projectLayer.config.rootDir, '')
// @ts-ignore
nuxt.options.i18n.langDir = relativeToProject
},
'@nuxtjs/i18n',
],
i18n: {
strategy: 'prefix',
defaultLocale: 'en',
lazy: true,
langDir: './lang',
locales: [{ code: 'en', file: 'en.json' }],
},
}) |
i tried your solution and it seems that the translations are only fetched from the "main" package. For example i have
It seems that with your approach even if i add the config in the |
@Techbinator I think I misunderstood, I thought your use case was a shared layer containing all i18n configuration and translations. It sounds like your goal is to combine/merge multiple Just to get a clear idea of the use case, does the following look like what you want to achieve?
// [main]/langDir/en.json
{
"title": "foo"
}
// [shared]/langDir/en.json
{
"description": "bar"
}
// intended result en.json
{
"title": "foo",
"description": "bar"
} I'm not sure if merging/combining files is currently supported/possible. If not, I think there could be a possible workaround by loading the files and using the I can imagine lazy loading wouldn't apply to such a workaround though, for that to work it may be required to merge each file and write the resulting file. |
@BobbieGoede that is more or less what i would like to do. |
@Techbinator maybe this workaround works better for your use case. import type { LocaleObject } from 'vue-i18n-routing'
import pathe from 'pathe'
export default defineNuxtConfig({
modules: [
(_, nuxt) => {
const projectLayer = nuxt.options._layers[0]
const projectI18n = projectLayer.config.i18n
if (projectI18n == null) throw new Error('Project layer `i18n` configuration is required')
if (projectI18n.langDir == null) throw new Error('Project layer `i18n.langDir` is required')
const getLocaleFiles = (locale: LocaleObject): string[] => {
if (locale.file != null) return [locale.file]
if (locale.files != null) return locale.files
return []
}
type FileRelativeOptions = {
projectLangDir: string
layerLangDir: string
layerRootDir: string
}
const filesToRelative = (files: string[], opts: FileRelativeOptions) =>
files.map((file) =>
pathe.relative(opts.projectLangDir, pathe.resolve(opts.layerRootDir, opts.layerLangDir, file))
)
const mergedLocales: LocaleObject[] = []
for (const layer of nuxt.options._layers) {
if (layer.config.i18n?.locales == null) continue
if (layer.config.i18n?.langDir == null) continue
for (const locale of layer.config.i18n.locales) {
if (typeof locale === 'string') continue
const { file, files, ...entry } = locale
const localeEntry = mergedLocales.find((x) => x.code === locale.code)
const fileEntries = getLocaleFiles(locale)
const relativeFiles = filesToRelative(fileEntries, {
projectLangDir: projectI18n.langDir,
layerLangDir: layer.config.i18n.langDir,
layerRootDir: layer.config.rootDir,
})
if (localeEntry == null) {
mergedLocales.push({ ...entry, files: relativeFiles })
} else {
localeEntry.files = [...relativeFiles, ...localeEntry.files]
}
}
}
// @ts-ignore
nuxt.options.i18n.locales = mergedLocales
},
'@nuxtjs/i18n',
],
i18n: {
strategy: 'prefix',
defaultLocale: 'en',
lazy: true,
langDir: 'lang',
locales: [{ code: 'en', file: 'en.json' }],
},
}) This approach tries to merge the locale entries of all layers, by taking the What it does: // project layer i18n
{
langDir: 'lang',
locales: [{ code: 'fr', file: 'fr.json'}],
}
// shared layer i18n
{
langDir: 'lang', // layers/shared-i18n/lang
locales: [{ code: 'fr', file: 'fr.json'}],
}
// transformed result by module
{
langDir: 'lang',
locales: [{
code: 'fr',
files: [
'../layers/shared-i18n/lang/fr.json',
'fr.json',
],
}],
}
|
Thanks @Techbinator and huge thanks @BobbieGoede |
This feature need to be integrated natively to Nuxt i18n, it's awesome! 🎉 |
@bgrand-ch it's implemented in the module now that #1925 has merged! |
Describe the feature
Hello,
Thanks for
@nuxtjs/i18n
!I have a monorepo with multiple Nuxt projects. I use a "shared" Nuxt project to extend two other Nuxt projects.
The Nuxt layer tip for paths:
I would like to use an absolute path with
langDir
in "shared"nuxt.config
, but I have the following error message:The
langDir
dynamically adds../
before path, presumably to get out of the.nuxt
folder.@kazupon Could you implement a different behavior when the path is absolute please? 🙏
"shared"
nuxt.config
content:Additional information
Final checks
The text was updated successfully, but these errors were encountered: