Skip to content

Commit

Permalink
feat!: configurable lang attribute for useLocaleHead (#3097)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored Sep 13, 2024
1 parent 34b7b4c commit a3fb89f
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 10 deletions.
6 changes: 5 additions & 1 deletion docs/content/docs/5.v9/2.guide/19.breaking-changes-in-v9.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ Some properties have changed or swapped names to better fit their functionality,
| v8 | v9 | Notes |
| --- | --- | --- |
| `locales` | `domainLocales` | This also changes the environment variable key to `NUXT_PUBLIC_I18N_DOMAIN_LOCALES_{code}_DOMAIN`, see [`runtimeConfig`](/docs/options/runtime-config#domainLocales) |
| `configLocales` | `locales` | |
| `configLocales` | `locales` | |

## SEO - `useLocaleHead`

We have added a `addLangAttribute` property to the options parameter of `useLocaleHead` and `$localeHead`, originally this was not configurable on its own. If you want to keep the same behavior, if you were passing `addSeoAttributes`, you will also have to pass `addLangAttribute: true`. See [`useLocaleHead`](/docs/v9/api#useLocaleHead)
2 changes: 2 additions & 0 deletions docs/content/docs/5.v9/2.guide/6.seo.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const route = useRoute()
const { t } = useI18n()
const head = useLocaleHead({
addDirAttribute: true,
addLangAttribute: true,
identifierAttribute: 'id',
addSeoAttributes: true
})
Expand Down Expand Up @@ -245,6 +246,7 @@ useHead({
```vue
<script setup>
const i18nHead = useLocaleHead({
addLangAttribute: true,
addSeoAttributes: {
canonicalQueries: ['foo']
}
Expand Down
6 changes: 6 additions & 0 deletions docs/content/docs/5.v9/4.api/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ An object accepting the following optional fields:

Adds a `dir` attribute to the HTML element. default `false`.

- `addLangAttribute`

**Type**: `Boolean`

Adds a `lang` attribute to the HTML element. default `false`.

- `addSeoAttributes`

**Type**: `boolean | SeoAttributesOptions`
Expand Down
2 changes: 1 addition & 1 deletion playground/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useLocaleHead } from '#i18n'
const route = useRoute()
const { t } = useI18n()
const head = useLocaleHead({ addDirAttribute: true, addSeoAttributes: true })
const head = useLocaleHead({ addLangAttribute: true, addDirAttribute: true, addSeoAttributes: true })
const title = computed(() => t('layouts.title', { title: t(route.meta.title ?? 'TBD') }))
</script>

Expand Down
2 changes: 1 addition & 1 deletion playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ definePageMeta({
<div v-for="item in items">
<p>{{ item }}</p>
</div>
<div>v-t directive: <code v-t="{ path: 'hello', args: { name: 'v-t' } }"></code></div>
<!-- <div>v-t directive: <code v-t="{ path: 'hello', args: { name: 'v-t' } }"></code></div> -->
</div>
</template>

Expand Down
1 change: 1 addition & 0 deletions specs/fixtures/basic/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import LangSwitcher from '../components/LangSwitcher.vue'
const { t } = useI18n()
const localePath = useLocalePath()
const i18nHead = useLocaleHead({
addLangAttribute: true,
addDirAttribute: true,
identifierAttribute: 'id',
addSeoAttributes: { canonicalQueries: ['page'] },
Expand Down
1 change: 1 addition & 0 deletions specs/fixtures/basic_usage/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const route = useRoute()
const { t } = useI18n()
const head = useLocaleHead({
addDirAttribute: true,
addLangAttribute: true,
identifierAttribute: 'id',
addSeoAttributes: { canonicalQueries: ['page'] }
})
Expand Down
1 change: 1 addition & 0 deletions specs/fixtures/basic_usage/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ definePageMeta({
const i18nHead = useLocaleHead({
addDirAttribute: true,
addLangAttribute: true,
identifierAttribute: 'id',
addSeoAttributes: { canonicalQueries: ['page'] }
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<p id="switch-locale-path">{{ $nuxt.$switchLocalePath('ja') }}</p>
<p id="locale-path">{{ $nuxt.$localePath('nuxt-context-extension', 'nl') }}</p>
<p id="locale-route">{{ JSON.stringify($nuxt.$localeRoute()) }}</p>
<p id="locale-head">{{ $nuxt.$localeHead({}) }}</p>
<p id="locale-head">{{ $nuxt.$localeHead({ addLangAttribute: true }) }}</p>
</div>
</template>
1 change: 1 addition & 0 deletions specs/fixtures/issues/2590/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { locale, locales, setLocale } = useI18n()
const head = useLocaleHead({
addDirAttribute: true,
addLangAttribute: true,
addSeoAttributes: true
})
Expand Down
2 changes: 1 addition & 1 deletion specs/fixtures/lazy/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import LangSwitcher from '../components/LangSwitcher.vue'
const { t } = useI18n()
const localePath = useLocalePath()
const i18nHead = useLocaleHead({ addSeoAttributes: { canonicalQueries: ['page'] } })
const i18nHead = useLocaleHead({ addLangAttribute: true, addSeoAttributes: { canonicalQueries: ['page'] } })
const { data, refresh } = useAsyncData('home', () =>
Promise.resolve({
aboutPath: localePath('about'),
Expand Down
2 changes: 1 addition & 1 deletion specs/fixtures/restructure/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import LangSwitcher from '../components/LangSwitcher.vue'
const { t } = useI18n()
const localePath = useLocalePath()
const i18nHead = useLocaleHead({ addSeoAttributes: { canonicalQueries: ['page'] } })
const i18nHead = useLocaleHead({ addLangAttribute: true, addSeoAttributes: { canonicalQueries: ['page'] } })
const { data, refresh } = useAsyncData('home', () =>
Promise.resolve({
aboutPath: localePath('about'),
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export type LocaleHeadFunction = (options: I18nHeadOptions) => ReturnType<typeof
*/
export function useLocaleHead({
addDirAttribute = false,
addLangAttribute = false,
addSeoAttributes = false,
identifierAttribute = 'hid'
}: I18nHeadOptions = {}): Ref<I18nHeadMetaInfo> {
Expand All @@ -156,6 +157,7 @@ export function useLocaleHead({
function updateMeta() {
metaObject.value = localeHead(common, {
addDirAttribute,
addLangAttribute,
addSeoAttributes,
identifierAttribute
})
Expand Down
10 changes: 6 additions & 4 deletions src/runtime/routing/compatibles/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function localeHead(
common: CommonComposableOptions,
{
addDirAttribute = false,
addLangAttribute = false,
addSeoAttributes: seoAttributes = true,
identifierAttribute: idAttribute = 'hid'
}: I18nHeadOptions
Expand All @@ -36,6 +37,7 @@ export function localeHead(
meta: []
}

// skip if no locales or baseUrl is set
if (unref(i18n.locales) == null || unref(i18n.baseUrl) == null) {
return metaObject
}
Expand All @@ -53,12 +55,12 @@ export function localeHead(
metaObject.htmlAttrs.dir = currentDir
}

if (addLangAttribute && currentLanguage) {
metaObject.htmlAttrs.lang = currentLanguage
}

// Adding SEO Meta
if (seoAttributes && locale && unref(i18n.locales)) {
if (currentLanguage) {
metaObject.htmlAttrs.lang = currentLanguage
}

metaObject.link.push(
...getHreflangLinks(common, unref(locales) as LocaleObject[], idAttribute),
...getCanonicalLink(common, idAttribute, seoAttributes)
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ export interface SeoAttributesOptions {
* @public
*/
export interface I18nHeadOptions {
/**
* Adds a `lang` attribute to the HTML element.
*
* @defaultValue false
*/
addLangAttribute?: boolean
/**
* Adds a `dir` attribute to the HTML element.
*
Expand Down

0 comments on commit a3fb89f

Please sign in to comment.