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

Cyclodex’s picture

Version: 7.x-1.0-alpha1 » 7.x-1.x-dev
Assigned: Unassigned » Cyclodex
Status: Needs review » Active

True, disabled languages should not be in here.
Will change this soon.

Cyclodex’s picture

Status: Active » Needs review

I fixed this in this commit.
Please test and review. (use development release)

rondev’s picture

Status: Needs review » Reviewed & tested by the community

OK for me.

Cyclodex’s picture

Status: Reviewed & tested by the community » Fixed

great thanks

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.