Skip to content

Commit 8c1588e

Browse files
committed
feat: Add og:locale support & fix i18n.seo component option
This apply changes from PR #75 to add support for og:locale meta tags and fixes an issue where SEO plugin would be unable to access page-specific options
1 parent b9c47cd commit 8c1588e

File tree

2 files changed

+60
-45
lines changed

2 files changed

+60
-45
lines changed

src/plugins/seo.js

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,60 @@ import Vue from 'vue'
33
Vue.mixin({
44
head () {
55
if (
6-
this.$i18n &&
7-
this.$i18n.locales &&
8-
this.i18n !== false &&
9-
(typeof this.i18n === 'undefined' || this.i18n.seo !== false) &&
10-
this._hasMetaInfo
6+
!this._hasMetaInfo ||
7+
!this.$i18n ||
8+
!this.$i18n.locales ||
9+
this.$options.i18n === false ||
10+
(this.$options.i18n && this.$options.i18n.seo === false)
1111
) {
12-
const LOCALE_CODE_KEY = '<%= options.LOCALE_CODE_KEY %>'
13-
const LOCALE_ISO_KEY = '<%= options.LOCALE_ISO_KEY %>'
12+
return {};
13+
}
14+
const LOCALE_CODE_KEY = '<%= options.LOCALE_CODE_KEY %>'
15+
const LOCALE_ISO_KEY = '<%= options.LOCALE_ISO_KEY %>'
1416

15-
// Prepare html lang attribute
16-
const currentLocaleData = this.$i18n.locales.find(l => l[LOCALE_CODE_KEY] === this.$i18n.locale)
17-
const htmlAttrs = {}
18-
if (currentLocaleData && currentLocaleData[LOCALE_ISO_KEY]) {
19-
htmlAttrs.lang = currentLocaleData[LOCALE_ISO_KEY]
20-
}
17+
// Prepare html lang attribute
18+
const currentLocaleData = this.$i18n.locales.find(l => l[LOCALE_CODE_KEY] === this.$i18n.locale)
19+
const htmlAttrs = {}
20+
if (currentLocaleData && currentLocaleData[LOCALE_ISO_KEY]) {
21+
htmlAttrs.lang = currentLocaleData[LOCALE_ISO_KEY]
22+
}
2123

22-
// hreflang tags
23-
const link = this.$i18n.locales
24-
.map(locale => {
25-
if (locale[LOCALE_ISO_KEY]) {
26-
return {
27-
hid: 'alternate-hreflang-' + locale[LOCALE_ISO_KEY],
28-
rel: 'alternate',
29-
href: this.switchLocalePath(locale.code),
30-
hreflang: locale[LOCALE_ISO_KEY]
31-
}
32-
} else {
33-
console.warn('[<%= options.MODULE_NAME %>] Locale ISO code is required to generate alternate link')
34-
return null
24+
// hreflang tags
25+
const link = this.$i18n.locales
26+
.map(locale => {
27+
if (locale[LOCALE_ISO_KEY]) {
28+
return {
29+
hid: 'alternate-hreflang-' + locale[LOCALE_ISO_KEY],
30+
rel: 'alternate',
31+
href: this.switchLocalePath(locale.code),
32+
hreflang: locale[LOCALE_ISO_KEY]
3533
}
36-
})
37-
.filter(item => !!item)
34+
} else {
35+
console.warn('[<%= options.MODULE_NAME %>] Locale ISO code is required to generate alternate link')
36+
return null
37+
}
38+
})
39+
.filter(item => !!item)
40+
41+
// og:locale meta
42+
const meta = [
43+
// Replace dash with underscore as defined in spec: language_TERRITORY
44+
{ hid: 'og:locale', name: 'og:locale', property: 'og:locale', content: currentLocaleData[LOCALE_ISO_KEY].replace(/-/g, '_') },
45+
...this.$i18n.locales
46+
.filter(l => l[LOCALE_ISO_KEY] !== currentLocaleData[LOCALE_ISO_KEY])
47+
.map(locale => ({
48+
hid: 'og:locale:alternate-' + locale[LOCALE_ISO_KEY],
49+
name: 'og:locale:alternate',
50+
property: 'og:locale:alternate',
51+
content: locale[LOCALE_ISO_KEY].replace(/-/g, '_')
52+
}))
53+
]
3854

39-
return {
40-
htmlAttrs,
41-
link
42-
}
55+
return {
56+
htmlAttrs,
57+
link,
58+
meta
4359
}
44-
return {}
4560
}
4661
})
4762

0 commit comments

Comments
 (0)