Skip to content

Commit

Permalink
feat: Add og:locale support & fix i18n.seo component option
Browse files Browse the repository at this point in the history
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
  • Loading branch information
paulgv committed May 1, 2018
1 parent b9c47cd commit 8c1588e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 45 deletions.
81 changes: 48 additions & 33 deletions src/plugins/seo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,60 @@ import Vue from 'vue'
Vue.mixin({
head () {
if (
this.$i18n &&
this.$i18n.locales &&
this.i18n !== false &&
(typeof this.i18n === 'undefined' || this.i18n.seo !== false) &&
this._hasMetaInfo
!this._hasMetaInfo ||
!this.$i18n ||
!this.$i18n.locales ||
this.$options.i18n === false ||
(this.$options.i18n && this.$options.i18n.seo === false)
) {
const LOCALE_CODE_KEY = '<%= options.LOCALE_CODE_KEY %>'
const LOCALE_ISO_KEY = '<%= options.LOCALE_ISO_KEY %>'
return {};
}
const LOCALE_CODE_KEY = '<%= options.LOCALE_CODE_KEY %>'
const LOCALE_ISO_KEY = '<%= options.LOCALE_ISO_KEY %>'

// Prepare html lang attribute
const currentLocaleData = this.$i18n.locales.find(l => l[LOCALE_CODE_KEY] === this.$i18n.locale)
const htmlAttrs = {}
if (currentLocaleData && currentLocaleData[LOCALE_ISO_KEY]) {
htmlAttrs.lang = currentLocaleData[LOCALE_ISO_KEY]
}
// Prepare html lang attribute
const currentLocaleData = this.$i18n.locales.find(l => l[LOCALE_CODE_KEY] === this.$i18n.locale)
const htmlAttrs = {}
if (currentLocaleData && currentLocaleData[LOCALE_ISO_KEY]) {
htmlAttrs.lang = currentLocaleData[LOCALE_ISO_KEY]
}

// hreflang tags
const link = this.$i18n.locales
.map(locale => {
if (locale[LOCALE_ISO_KEY]) {
return {
hid: 'alternate-hreflang-' + locale[LOCALE_ISO_KEY],
rel: 'alternate',
href: this.switchLocalePath(locale.code),
hreflang: locale[LOCALE_ISO_KEY]
}
} else {
console.warn('[<%= options.MODULE_NAME %>] Locale ISO code is required to generate alternate link')
return null
// hreflang tags
const link = this.$i18n.locales
.map(locale => {
if (locale[LOCALE_ISO_KEY]) {
return {
hid: 'alternate-hreflang-' + locale[LOCALE_ISO_KEY],
rel: 'alternate',
href: this.switchLocalePath(locale.code),
hreflang: locale[LOCALE_ISO_KEY]
}
})
.filter(item => !!item)
} else {
console.warn('[<%= options.MODULE_NAME %>] Locale ISO code is required to generate alternate link')
return null
}
})
.filter(item => !!item)

// og:locale meta
const meta = [
// Replace dash with underscore as defined in spec: language_TERRITORY
{ hid: 'og:locale', name: 'og:locale', property: 'og:locale', content: currentLocaleData[LOCALE_ISO_KEY].replace(/-/g, '_') },
...this.$i18n.locales
.filter(l => l[LOCALE_ISO_KEY] !== currentLocaleData[LOCALE_ISO_KEY])
.map(locale => ({
hid: 'og:locale:alternate-' + locale[LOCALE_ISO_KEY],
name: 'og:locale:alternate',
property: 'og:locale:alternate',
content: locale[LOCALE_ISO_KEY].replace(/-/g, '_')
}))
]

return {
htmlAttrs,
link
}
return {
htmlAttrs,
link,
meta
}
return {}
}
})

0 comments on commit 8c1588e

Please sign in to comment.