forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: module locale option generation breaking SEO (nuxt-modules#2598)
* fix: module locale option generation breaking SEO * test: add issue nuxt-modules#2590 test
Whoa there!
You have triggered an abuse detection mechanism.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
1 parent
872a23c
commit be4fdff
Showing
8 changed files
with
133 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<template> | ||
<div> | ||
{{ $t('welcome') }} | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
const { locale, locales, setLocale } = useI18n() | ||
const head = useLocaleHead({ | ||
addDirAttribute: true, | ||
addSeoAttributes: true | ||
}) | ||
useHead({ | ||
htmlAttrs: head.value.htmlAttrs | ||
}) | ||
console.log(head.value.htmlAttrs) | ||
const availableLocales = computed(() => { | ||
return (locales.value as LocaleObject[]) | ||
.filter(item => { | ||
return item.code !== locale.value | ||
}) | ||
.map(item => { | ||
return { | ||
label: item.name, | ||
key: item.code | ||
} | ||
}) | ||
}) | ||
const currentLocale = computed(() => { | ||
return (locales.value as LocaleObject[]).filter(item => { | ||
return item.code === locale.value | ||
}) | ||
}) | ||
function changeLocal(code: any) { | ||
setLocale(code) | ||
} | ||
</script> |
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 @@ | ||
export default defineI18nConfig(() => ({ | ||
legacy: false | ||
})) |
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,35 @@ | ||
import { createResolver, defineNuxtModule, installModule } from '@nuxt/kit' | ||
|
||
export default defineNuxtModule({ | ||
async setup(options, nuxt) { | ||
const { resolve } = createResolver(import.meta.url) | ||
|
||
nuxt.hook('i18n:registerModule', registerModule => | ||
registerModule({ | ||
langDir: resolve('lang'), | ||
locales: [ | ||
{ | ||
code: 'en', | ||
iso: 'en-US', | ||
file: 'en-US.ts', | ||
name: 'English' | ||
} | ||
] | ||
}) | ||
) | ||
|
||
await installModule('@nuxtjs/i18n', { | ||
lazy: true, | ||
defaultLocale: 'en', | ||
experimental: { | ||
jsTsFormatResource: true | ||
}, | ||
detectBrowserLanguage: { | ||
useCookie: true, | ||
cookieKey: 'i18n_lang', | ||
redirectOn: 'root' | ||
}, | ||
vueI18n: './i18n.config.ts' | ||
}) | ||
} | ||
}) |
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,5 @@ | ||
export default defineI18nLocale(async () => { | ||
return { | ||
welcome: 'welcome translated' | ||
} | ||
}) |
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,5 @@ | ||
// https://nuxt.com/docs/api/configuration/nuxt-config | ||
export default defineNuxtConfig({ | ||
devtools: { enabled: false }, | ||
modules: ['./modules/i18n-module'] | ||
}) |
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,14 @@ | ||
{ | ||
"name": "nuxt3-test-issues-2473", | ||
"private": true, | ||
"scripts": { | ||
"build": "nuxt build", | ||
"dev": "nuxt dev", | ||
"generate": "nuxt generate", | ||
"preview": "nuxt preview" | ||
}, | ||
"devDependencies": { | ||
"@nuxtjs/i18n": "latest", | ||
"nuxt": "latest" | ||
} | ||
} |
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,22 @@ | ||
import { test, expect, describe } from 'vitest' | ||
import { fileURLToPath } from 'node:url' | ||
import { URL } from 'node:url' | ||
import { setup } from '../utils' | ||
import { renderPage } from '../helper' | ||
|
||
describe('#2590', async () => { | ||
await setup({ | ||
rootDir: fileURLToPath(new URL(`../fixtures/issues/2590`, import.meta.url)), | ||
browser: true | ||
}) | ||
|
||
test('Locale ISO code is required to generate alternate link', async () => { | ||
const { page } = await renderPage('/') | ||
|
||
// html tag `lang` attribute | ||
expect(await page.getAttribute('html', 'lang')).toMatch('en') | ||
|
||
// html tag `dir` attribute | ||
expect(await page.getAttribute('html', 'dir')).toMatch('ltr') | ||
}) | ||
}) |
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