You must select at least one Language Detection Method when you enable translation on Drupal 7 with Internationalization or Entity Translation. The language.inc file provides the Default method and locale.module provides four itself:

  • URL - Determine the language from the URL (Path prefix or domain).
  • Session - Determine the language from a request/session parameter.
  • User - Follow the user’s language preference.
  • Browser - Determine the language from the browser’s language settings.

You can build a site using these default methods in many cases. Sometimes you cannot and need hook_language_negotiation_info() to create your own detection methods. I have a site which has multiple versions (depending on your location and preferences), but only one of those version is in a different language. So, I check the cookie set elsewhere (in someone else’s code), and set the language accordingly. Here an example of the code.

site_i18n.info:

name = Internationalization
description = Custom language detection method, for now.
core = 7.x
version = 7.x-1.0
package = "Site Customizations"

site_i18n.module:

<?php
function site_i18n_language_negotiation_info() {
  return array(
    'site_version' => array(
      'callbacks' => array(
        'language' => 'site_i18n_language_from_cookie',
      ),
      'weight' => -10,
      'name' => t('Version'),
      // NOTE: The callback cannot be in this file, unless you specifically include it.
      // Language detection runs before Drupal Bootstrap completes.
      'file' => drupal_get_path('module', 'site_i18n') . '/site_i18n.inc',
      'description' => t('Determine the language from the version cookie.'),
    ),
  );
}

site_i18n.inc:

function site_i18n_language_from_cookie($languages) {
  // Check that the cookie exists, and if it is set to the spanish site.
  if (isset($_COOKIE['site_version']) && $_COOKIE['site_version'] == 'spanish') {
    //Return the short name of the Spanish language.
    return 'es';
  }
  // Otherwise return English
  return 'en';
}