I don't know what exactly are the specification for hreflang but I got a bug if I have at least one language disabled. That occurred for me at least for taxonomy terms.
Disabled languages have hreflang. And they refer to a false page.
Then I decided to remove them from the $translations array by:
foreach ($languages[0] as $language) {
$disabledlanguage=$language->language;
unset($translations[$disabledlanguage]);
}Then proposed code is:
/**
* Implements hook_init().
*/
function i18n_hreflang_init() {
// Get path and translations
$path = drupal_is_front_page() ? '<front>' : $_GET['q'];
$translations = i18n_get_path_translations($path);
$languages = language_list('enabled');
$vari18n_langcode=i18n_langcode();
unset($translations[i18n_langcode()]);
foreach ($languages[0] as $language) {
$disabledlanguage=$language->language;
unset($translations[$disabledlanguage]);
}
// Get alternate urls if we've got translations.
if ($translations) {
foreach($translations as $lang => $translation) {
$attributes = array(
'href' => url($translation['href'], $translation + array('language' => i18n_language($lang))),
'rel' => 'alternate',
'hreflang' => $lang,
'title' => isset($translation['title']) ? $translation['title'] : i18n_language_name($lang),
);
drupal_add_html_head_link($attributes);
}
}
}What do you think about it? Should be a better way to do.
Comments
Comment #1
Cyclodex commentedTrue, disabled languages should not be in here.
Will change this soon.
Comment #2
Cyclodex commentedI fixed this in this commit.
Please test and review. (use development release)
Comment #3
rondev commentedOK for me.
Comment #4
Cyclodex commentedgreat thanks