Skip to content

Commit

Permalink
feat: nuxt-i18n-micro module integration (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw authored Sep 20, 2024
1 parent 624a8c3 commit 39b917c
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"eslint-plugin-n": "^17.10.3",
"execa": "^9.4.0",
"nuxt": "^3.13.2",
"nuxt-i18n-micro": "^1.16.1",
"typescript": "5.6.2",
"vitest": "^2.1.1"
},
Expand Down
42 changes: 42 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from '@nuxt/kit'
import { joinURL, withBase, withLeadingSlash, withoutLeadingSlash, withoutTrailingSlash } from 'ufo'
import { installNuxtSiteConfig } from 'nuxt-site-config-kit'
import type { NuxtI18nOptions } from '@nuxtjs/i18n'
import { defu } from 'defu'
import type { NitroRouteConfig } from 'nitropack'
import { readPackageJSON } from 'pkg-types'
Expand All @@ -26,7 +25,7 @@ import type {
SitemapSourceBase,
SitemapSourceInput,
SitemapSourceResolved,
ModuleOptions as _ModuleOptions, FilterInput,
ModuleOptions as _ModuleOptions, FilterInput, I18nIntegrationOptions,
} from './runtime/types'
import { convertNuxtPagesToSitemapEntries, generateExtraRoutesFromNuxtConfig, resolveUrls } from './util/nuxtSitemap'
import { createNitroPromise, createPagesPromise, extendTypes, getNuxtModuleOptions, resolveNitroPreset } from './util/kit'
Expand Down Expand Up @@ -153,22 +152,26 @@ export default defineNuxtModule<ModuleOptions>({
let usingMultiSitemaps = !!config.sitemaps

let isI18nMapped = false
let nuxtI18nConfig = {} as NuxtI18nOptions
let nuxtI18nConfig = {} as I18nIntegrationOptions
let resolvedAutoI18n: false | AutoI18nConfig = typeof config.autoI18n === 'boolean' ? false : config.autoI18n || false
const hasDisabledAutoI18n = typeof config.autoI18n === 'boolean' && !config.autoI18n
let normalisedLocales: AutoI18nConfig['locales'] = []
let usingI18nPages = false
if (hasNuxtModule('@nuxtjs/i18n')) {
const i18nVersion = await getNuxtModuleVersion('@nuxtjs/i18n')
if (!await hasNuxtModuleCompatibility('@nuxtjs/i18n', '>=8'))
logger.warn(`You are using @nuxtjs/i18n v${i18nVersion}. For the best compatibility, please upgrade to @nuxtjs/i18n v8.0.0 or higher.`)
nuxtI18nConfig = (await getNuxtModuleOptions('@nuxtjs/i18n') || {}) as NuxtI18nOptions
const i18nModule = ['@nuxtjs/i18n', 'nuxt-i18n-micro'].find(s => hasNuxtModule(s))
if (i18nModule) {
const i18nVersion = await getNuxtModuleVersion(i18nModule)
if (i18nModule === '@nuxtjs/i18n' && !await hasNuxtModuleCompatibility(i18nModule, '>=8'))
logger.warn(`You are using ${i18nModule} v${i18nVersion}. For the best compatibility, please upgrade to ${i18nModule} v8.0.0 or higher.`)
nuxtI18nConfig = (await getNuxtModuleOptions(i18nModule) || {}) as I18nIntegrationOptions
if (typeof nuxtI18nConfig.includeDefaultLocaleRoute !== 'undefined') {
nuxtI18nConfig.strategy = nuxtI18nConfig.includeDefaultLocaleRoute ? 'prefix' : 'prefix_except_default'
}
normalisedLocales = normalizeLocales(nuxtI18nConfig)
usingI18nPages = !!Object.keys(nuxtI18nConfig.pages || {}).length
if (usingI18nPages && !hasDisabledAutoI18n) {
const i18nPagesSources: SitemapSourceBase = {
context: {
name: '@nuxtjs/i18n:pages',
name: `${i18nModule}:pages`,
description: 'Generated from your i18n.pages config.',
tips: [
'You can disable this with `autoI18n: false`.',
Expand Down Expand Up @@ -213,7 +216,7 @@ export default defineNuxtModule<ModuleOptions>({
}
else {
if (!normalisedLocales.length)
logger.warn(`You are using @nuxtjs/i18n but have not configured any locales, this will cause issues with ${name}. Please configure \`locales\`.`)
logger.warn(`You are using ${i18nModule} but have not configured any locales, this will cause issues with ${name}. Please configure \`locales\`.`)
}
const hasSetAutoI18n = typeof config.autoI18n === 'object' && Object.keys(config.autoI18n).length
const hasI18nConfigForAlternatives = nuxtI18nConfig.differentDomains || usingI18nPages || (nuxtI18nConfig.strategy !== 'no_prefix' && nuxtI18nConfig.locales)
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { FetchOptions } from 'ofetch'
import type { H3Event } from 'h3'
import type { ParsedURL } from 'ufo'
import type { NuxtI18nOptions } from '@nuxtjs/i18n'

// we need to have the module options within the runtime entry
// as we don't want to depend on the module entry as it can cause
Expand Down Expand Up @@ -446,3 +447,9 @@ export interface NitroUrlResolvers {
relativeBaseUrlResolver: (path: string) => string
fixSlashes: (path: string) => string
}

interface NuxtI18nMicro {
includeDefaultLocaleRoute?: boolean
}

export type I18nIntegrationOptions = NuxtI18nOptions & NuxtI18nMicro
3 changes: 3 additions & 0 deletions test/fixtures/i18n-micro/locales/en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
welcome: 'Welcome',
}
3 changes: 3 additions & 0 deletions test/fixtures/i18n-micro/locales/hr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
welcome: 'ようこそ',
}
3 changes: 3 additions & 0 deletions test/fixtures/i18n-micro/locales/ja.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
welcome: 'ようこそ',
}
3 changes: 3 additions & 0 deletions test/fixtures/i18n-micro/locales/nl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
welcome: 'Welcome',
}
3 changes: 3 additions & 0 deletions test/fixtures/i18n-micro/locales/zh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
welcome: '欢迎光临',
}
46 changes: 46 additions & 0 deletions test/fixtures/i18n-micro/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import NuxtSitemap from '../../../src/module'

export default defineNuxtConfig({
modules: [
NuxtSitemap,
'nuxt-i18n-micro',
],
site: {
url: 'https://nuxtseo.com',
},
nitro: {
prerender: {
failOnError: false,
ignore: ['/'],
},
},
sitemap: {
dynamicUrlsApiEndpoint: '/__sitemap',
autoLastmod: false,
credits: false,
debug: true,
},
i18n: {
baseUrl: 'https://nuxtseo.com',
detectBrowserLanguage: false,
defaultLocale: 'en',
strategy: 'prefix',
locales: [
{
code: 'en',
iso: 'en-US',
},
{
code: 'es',
iso: 'es-ES',
},
{
code: 'fr',
iso: 'fr-FR',
},
],
meta: true,
},

compatibilityDate: '2024-07-22',
})
3 changes: 3 additions & 0 deletions test/fixtures/i18n-micro/pages/dynamic/[page].vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div>{{ $route.params.page }}</div>
</template>
14 changes: 14 additions & 0 deletions test/fixtures/i18n-micro/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script setup lang="ts">
import { prerenderRoutes } from '#imports'
prerenderRoutes([
'/en/dynamic/foo',
'/en/dynamic/bar',
])
</script>

<template>
<div>
<h1>{{ $t('welcome') }}</h1>
</div>
</template>
5 changes: 5 additions & 0 deletions test/fixtures/i18n-micro/pages/test.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
<h1>{{ $t('welcome') }}</h1>
</div>
</template>
11 changes: 11 additions & 0 deletions test/fixtures/i18n-micro/server/routes/__sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineSitemapEventHandler } from '#imports'

export default defineSitemapEventHandler(() => {
return [
{
loc: '/__sitemap/url',
changefreq: 'weekly',
_i18nTransform: true,
},
]
})
21 changes: 21 additions & 0 deletions test/fixtures/i18n-micro/server/routes/i18n-urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineSitemapEventHandler } from '#imports'

export default defineSitemapEventHandler(() => {
return [
{
loc: '/en/dynamic/foo',
},
{
loc: '/fr/dynamic/foo',
},
{
loc: 'endless-dungeon', // issue with en being picked up as the locale
_i18nTransform: true,
},
{
loc: 'english-url', // issue with en being picked up as the locale
},
// absolute URL issue
{ loc: 'https://www.somedomain.com/abc/def' },
]
})

0 comments on commit 39b917c

Please sign in to comment.