Skip to content

Commit

Permalink
Fix hreflang default URL no browser detect (#198, #819)
Browse files Browse the repository at this point in the history
Only if the language is detected by the browser, enforce the
language shown to make the default URL deterministic for SEO.

Otherwise, allow the option "Hide URL language information
for default language".

Note the "x-default" item is not concerned by these changes,
this link always uses the default URL with no override.
  • Loading branch information
herrvigg committed May 31, 2020
1 parent c6df46b commit 65cd983
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions qtranslate_frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ function qtranxf_wp_head() {
return;
}

// set links to localized versions of current page: hreflang (locale/lang) -> href
// Set links to localized versions of the current page using for SEO (hreflang)
// See https://github.com/qtranslate/qtranslate-xt/wiki/Browser-redirection#localized-versions-for-seo-hreflang
$hreflangs = array();
foreach ( $q_config['enabled_languages'] as $lang ) {
$hreflang = ! empty ( $q_config['locale_html'][ $lang ] ) ? $q_config['locale_html'][ $lang ] : $lang;
// Default language is always shown, see: https://github.com/qtranslate/qtranslate-xt/wiki/Browser-redirection
$hreflangs[ $hreflang ] = qtranxf_convertURL( '', $lang, false, true );

// The default URL may be deterministic or not, depending on the option for language detection by the browser
// If language detected, enforce default language shown to make the default URL deterministic for SEO
// Otherwise, allow option "Hide URL language information for default language"
$hreflangs[ $hreflang ] = qtranxf_convertURL( '', $lang, false, $q_config['detect_browser_language'] );
}
// Fallback for unmatched language: https://support.google.com/webmasters/answer/189077
// Fallback for unmatched language (default hreflang for SEO)
$hreflangs['x-default'] = qtranxf_convertURL( '', $q_config['default_language'] );

$hreflangs = apply_filters( 'qtranslate_hreflang', $hreflangs );
Expand Down

0 comments on commit 65cd983

Please sign in to comment.