-
-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support layer locales and pages
- Loading branch information
1 parent
5fc911e
commit 11cd655
Showing
12 changed files
with
189 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ | |
"source.fixAll.eslint": true | ||
}, | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"layerText": "This is a merged locale key" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"layerText": "This is a merged locale key in French" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"layerText": "This is a merged locale key in Dutch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// import type { NuxtApp } from 'nuxt/dist/app/index' | ||
|
||
// https://nuxt.com/docs/guide/directory-structure/nuxt.config | ||
export default defineNuxtConfig({ | ||
modules: ['@nuxtjs/i18n'], | ||
i18n: { | ||
langDir: 'locales', | ||
lazy: true, | ||
baseUrl: 'http://localhost:3000', | ||
customRoutes: 'config', | ||
pages: { | ||
history: { | ||
en: '/history', | ||
fr: '/history-fr', | ||
ja: '/history-ja' | ||
} | ||
}, | ||
locales: [ | ||
{ | ||
code: 'en', | ||
iso: 'en-US', | ||
file: 'en.json', | ||
// domain: 'localhost', | ||
name: 'English' | ||
}, | ||
{ | ||
code: 'fr', | ||
iso: 'fr-FR', | ||
file: 'fr.json', | ||
// domain: 'localhost', | ||
name: 'Francais' | ||
}, | ||
{ | ||
code: 'nl', | ||
iso: 'nl-NL', | ||
file: 'nl.json', | ||
// domain: 'localhost', | ||
name: 'Nederlands' | ||
} | ||
// { | ||
// code: 'en-GB', | ||
// iso: 'en-GB', | ||
// files: ['en.json', 'en-GB.json'], | ||
// name: 'English (UK)' | ||
// }, | ||
// { | ||
// code: 'ja', | ||
// iso: 'ja-JP', | ||
// file: 'ja.json', | ||
// domain: 'mydomain.com', | ||
// name: 'Japanses' | ||
// }, | ||
// { | ||
// code: 'fr', | ||
// iso: 'fr-FR', | ||
// file: 'fr.json', | ||
// domain: 'mydomain.fr', | ||
// name: 'Français' | ||
// } | ||
] | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div></div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Nuxt } from '@nuxt/schema' | ||
import { LocaleObject } from 'vue-i18n-routing' | ||
import { NuxtI18nOptions } from './types' | ||
import createDebug from 'debug' | ||
import pathe from 'pathe' | ||
|
||
const debug = createDebug('@nuxtjs/i18n:layers') | ||
|
||
const getLocaleFiles = (locale: LocaleObject): string[] => { | ||
if (locale.file != null) return [locale.file] | ||
if (locale.files != null) return locale.files | ||
return [] | ||
} | ||
|
||
const localeFilesToRelative = (projectLangDir: string, layerLangDir: string, files: string[]) => { | ||
const absoluteFiles = files.map(file => pathe.resolve(layerLangDir, file)) | ||
const relativeFiles = absoluteFiles.map(file => pathe.relative(projectLangDir, file)) | ||
return relativeFiles | ||
} | ||
|
||
const getProjectPath = (nuxt: Nuxt, ...target: string[]) => { | ||
const projectLayer = nuxt.options._layers[0] | ||
return pathe.resolve(projectLayer.config.rootDir, ...target) | ||
} | ||
|
||
export const applyLayerOptions = (options: NuxtI18nOptions, nuxt: Nuxt) => { | ||
const project = nuxt.options._layers[0] | ||
const layers = nuxt.options._layers | ||
|
||
const resolvedLayerPaths = layers.map(l => pathe.resolve(project.config.rootDir, l.config.rootDir)) | ||
debug('using layers at paths -', resolvedLayerPaths) | ||
|
||
const mergedLocales = mergeLayerLocales(nuxt) | ||
debug('merged locales - ', mergedLocales) | ||
options.locales = mergedLocales | ||
} | ||
|
||
export const mergeLayerPages = (analyzer: (pathOverride: string) => void, nuxt: Nuxt) => { | ||
const project = nuxt.options._layers[0] | ||
const layers = nuxt.options._layers | ||
|
||
for (const l of layers) { | ||
const lPath = pathe.resolve(project.config.rootDir, l.config.rootDir, l.config.dir?.pages ?? 'pages') | ||
analyzer(lPath) | ||
} | ||
} | ||
|
||
export const mergeLayerLocales = (nuxt: Nuxt) => { | ||
const projectLayer = nuxt.options._layers[0] | ||
const projectI18n = projectLayer.config.i18n | ||
|
||
if (projectI18n == null) { | ||
debug('project layer `i18n` configuration is required') | ||
return [] | ||
} | ||
if (projectI18n.langDir == null) { | ||
debug('project layer `i18n.langDir` is required') | ||
return [] | ||
} | ||
|
||
const mergedLocales: LocaleObject[] = [] | ||
const projectLangDir = getProjectPath(nuxt, projectI18n.langDir) | ||
debug('project path', getProjectPath(nuxt)) | ||
for (const layer of nuxt.options._layers) { | ||
if (layer.config.i18n?.locales == null) continue | ||
if (layer.config.i18n?.langDir == null) continue | ||
|
||
const layerLangDir = pathe.resolve(layer.config.rootDir, layer.config.i18n.langDir) | ||
debug('layer langDir -', layerLangDir) | ||
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 = localeFilesToRelative(projectLangDir, layerLangDir, fileEntries) | ||
|
||
if (localeEntry == null) { | ||
mergedLocales.push({ ...entry, files: relativeFiles }) | ||
} else { | ||
localeEntry.files = [...relativeFiles, ...(localeEntry?.files ?? [])] | ||
} | ||
} | ||
} | ||
|
||
return mergedLocales | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters