language.module 32.6 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Add language handling functionality to Drupal.
 */

8
use Drupal\node\NodeTypeInterface;
9 10
use Drupal\Core\Language\Language;

11 12 13 14 15 16 17 18
/**
 * Implements hook_help().
 */
function language_help($path, $arg) {
  switch ($path) {
    case 'admin/help#language':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
19
      $output .= '<p>' . t('The Language module allows you to maintain a list of languages used on your Drupal site for providing language information for content and for interface translation (using the Locale module). For more information, see the online handbook entry for <a href="@language">Language module</a>.', array('@language' => 'http://drupal.org/documentation/modules/language')) . '</p>';
20 21 22 23
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Configuring the list of languages') . '</dt>';
      $output .= '<dd>' . t('<a href="@configure-languages">Configure the list of languages</a> either using the built-in language list or providing any custom languages you wish.', array('@configure-languages' => url('admin/config/regional/language'))) . '</dd>';
24 25
      $output .= '<dt>' . t('Configuring a multilingual site') . '</dt>';
      $output .= '<dd>' . t("Language negotiation allows your site to automatically change language based on the domain or path used for each request. Users may (optionally) select their preferred language on their <em>My account</em> page, and your site can be configured to honor a web browser's preferred language settings. Site content can be translated using the <a href='@content-help'>Content Translation module</a>.", array('@content-help' => url('admin/help/translation'))) . '</dd>';
26 27 28 29
      $output .= '</dl>';
      return $output;

    case 'admin/config/regional/language':
30
      return '<p>' . t('With multiple languages enabled, registered users may select their preferred language and authors can assign a specific language to content. The selection of what language is used to display page elements is made depending on the detection menthod settings in the <a href="@detection">Detection and Selection</a> tab.', array('@detection' => url('admin/config/regional/language/detection'))) . '</p>';
31 32 33

    case 'admin/config/regional/language/add':
      return '<p>' . t('Add a language to be supported by your site. If your desired language is not available, pick <em>Custom language...</em> at the end and provide a language code and other details manually.') . '</p>';
34 35

    case 'admin/config/regional/language/detection':
36
      $output = '<p>' . t('Define how to decide which language is used to display page elements (primarily text provided by Drupal and modules, such as field labels and help text). This decision is made by evaluating a series of detection methods for languages; the first detection method that gets a result will determine which language is used for that type of text. Be aware that some language negotiation methods are unreliable under certain conditions, such as browser detection when page-caching is enabled and a user is not currently logged in. Define the order of evaluation of language detection methods on this page. Default language can be changed at the <a href="@region-settings">Regional settings</a> page.', array('@region-settings' => url('admin/config/regional/settings'))) . '</p>';
37 38 39 40 41 42
      return $output;

    case 'admin/config/regional/language/detection/session':
      $output = '<p>' . t('Determine the language from a request/session parameter. Example: "http://example.com?language=de" sets language to German based on the use of "de" within the "language" parameter.') . '</p>';
      return $output;

43 44 45 46
    case 'admin/config/regional/language/detection/browser':
      $output = '<p>' . t('Browsers use different language codes to refer to the same languages. You can add and edit mappings from browser language codes to the <a href="@configure-languages">languages used by Drupal</a>.', array('@configure-languages' => url('admin/config/regional/language'))) . '</p>';
      return $output;

47 48 49 50 51 52
    case 'admin/config/regional/language/detection/selected':
      $output = '<p>' . t('Changing the selected language here (and leaving this option as the last among the detection and selection options) is the easiest way to change the fallback language for the website, if you need to change how your site works by default (eg. when using an empty path prefix or using the default domain). <a href="@admin-change-language">Changing the site\'s default language</a> itself might have other undesired side effects.
', array('@admin-change-language' => url('admin/config/regional/language'))) . '</p>';

      return $output;

53 54
    case 'admin/structure/block/manage/%':
    case 'admin/structure/block/add/%/%':
55
      if ($arg[4] == 'language' && $arg[5] == 'language_interface') {
56
        return '<p>' . t('With multiple languages enabled, registered users can select their preferred language and authors can assign a specific language to content.') . '</p>';
57 58
      }
      break;
59 60 61

    case 'admin/config/regional/content-language':
      return t('Change language settings for <em>content types</em>, <em>taxonomy vocabularies</em>, <em>user profiles</em>, or any other supported element on your site. By default, language settings hide the language selector and the language is the site\'s default language.');
62 63 64 65 66 67 68
  }
}

/**
 * Implements hook_menu().
 */
function language_menu() {
69
  // Base language management and configuration.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  $items['admin/config/regional/language'] = array(
    'title' => 'Languages',
    'description' => 'Configure languages for content and the user interface.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('language_admin_overview_form'),
    'access arguments' => array('administer languages'),
    'file' => 'language.admin.inc',
    'weight' => -10,
  );
  $items['admin/config/regional/language/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/config/regional/language/add'] = array(
    'title' => 'Add language',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('language_admin_add_form'),
    'access arguments' => array('administer languages'),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'language.admin.inc',
  );
  $items['admin/config/regional/language/edit/%language'] = array(
    'title' => 'Edit language',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('language_admin_edit_form', 5),
95 96
    'access callback' => 'language_access_language_edit_or_delete',
    'access arguments' => array(5),
97 98 99 100 101 102
    'file' => 'language.admin.inc',
  );
  $items['admin/config/regional/language/delete/%language'] = array(
    'title' => 'Confirm delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('language_admin_delete_form', 5),
103 104
    'access callback' => 'language_access_language_edit_or_delete',
    'access arguments' => array(5),
105 106
    'file' => 'language.admin.inc',
  );
107 108 109 110 111 112 113 114 115 116 117 118 119

  // Language negotiation.
  $items['admin/config/regional/language/detection'] = array(
    'title' => 'Detection and selection',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('language_negotiation_configure_form'),
    'access arguments' => array('administer languages'),
    'weight' => 10,
    'file' => 'language.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/config/regional/language/detection/url'] = array(
    'title' => 'URL language detection configuration',
120
    'route_name' => 'language_negotiation_url',
121 122 123 124
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  );
  $items['admin/config/regional/language/detection/session'] = array(
    'title' => 'Session language detection configuration',
125
    'route_name' => 'language_negotiation_session',
126 127
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  );
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  $items['admin/config/regional/language/detection/browser'] = array(
    'title' => 'Browser language detection configuration',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('language_negotiation_configure_browser_form'),
    'access arguments' => array('administer languages'),
    'file' => 'language.admin.inc',
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  );
  $items['admin/config/regional/language/detection/browser/delete/%'] = array(
    'title' => 'Delete language mapping',
    'page arguments' => array('language_negotiation_configure_browser_delete_form', 7),
    'type' => MENU_CALLBACK,
    'access arguments' => array('administer languages'),
    'file' => 'language.admin.inc',
  );
143 144
  $items['admin/config/regional/language/detection/selected'] = array(
    'title' => 'Selected language detection configuration',
145
    'route_name' => 'language_negotiation_selected',
146 147
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  );
148

149 150 151 152 153 154 155 156 157
  // Content language settings.
  $items['admin/config/regional/content-language'] = array(
    'title' => 'Content language settings',
    'description' => 'Configure content language support for any multilingual element.',
    'page callback' => 'language_content_settings_page',
    'access arguments' => array('administer languages'),
    'file' => 'language.admin.inc',
  );

158 159 160
  return $items;
}

161 162 163 164 165 166 167
/**
 * Editing or deleting locked languages should not be possible.
 */
function language_access_language_edit_or_delete($language) {
  return !$language->locked && user_access('administer languages');
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/**
 * Implements hook_permission().
 */
function language_permission() {
  return array(
    'administer languages' => array(
      'title' => t('Administer languages'),
    ),
  );
}

/**
 * Implements hook_theme().
 */
function language_theme() {
  return array(
184 185 186
    'language_negotiation_configure_form' => array(
      'render element' => 'form',
    ),
187 188 189 190
    'language_negotiation_configure_browser_form_table' => array(
      'render element' => 'form',
      'file' => 'language.admin.inc',
    ),
191 192 193 194
    'language_content_settings_table' => array(
      'render element' => 'element',
      'file' => 'language.admin.inc',
    ),
195 196 197
  );
}

198 199 200 201 202 203 204 205 206
/**
 * Returns a list of supported entity types.
 *
 * @return array
 *   An array of entity type names.
 */
function language_entity_supported() {
  $supported = array();
  foreach (entity_get_info() as $entity_type => $info) {
207
    if (!empty($info['translatable'])) {
208 209 210 211 212 213
      $supported[$entity_type] = $entity_type;
    }
  }
  return $supported;
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
/**
 * Implements hook_element_info_alter().
 */
function language_element_info_alter(&$type) {
  // Alter the language_select element so that it will be rendered like a select
  // field.
  if (isset($type['language_select'])) {
    if (!isset($type['language_select']['#process'])) {
      $type['language_select']['#process'] = array();
    }
    if (!isset($type['language_select']['#theme_wrappers'])) {
      $type['language_select']['#theme_wrappers'] = array();
    }
    $type['language_select']['#process'] = array_merge($type['language_select']['#process'], array('language_process_language_select', 'form_process_select', 'ajax_process_form'));
    $type['language_select']['#theme'] = 'select';
    $type['language_select']['#theme_wrappers'] = array_merge($type['language_select']['#theme_wrappers'], array('form_element'));
230
    $type['language_select']['#languages'] = Language::STATE_CONFIGURABLE;
231 232 233 234 235 236 237 238 239 240 241 242 243 244
    $type['language_select']['#multiple'] = FALSE;
  }
}

/**
 * Processes a language select list form element.
 *
 * @param array $element
 *   The form element to process.
 *
 * @return array $element
 *   The processed form element.
 */
function language_process_language_select($element) {
245
  // Don't set the options if another module (translation for example) already
246 247 248 249 250 251 252 253 254 255
  // set the options.
  if (!isset($element['#options'])) {
    $element['#options'] = array();
    foreach (language_list($element['#languages']) as $langcode => $language) {
      $element['#options'][$langcode] = $language->locked ? t('- @name -', array('@name' => $language->name)) : $language->name;
    }
  }
  return $element;
}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
/**
 * Implements hook_element_info().
 */
function language_element_info() {
  $types['language_configuration'] = array(
    '#input' => TRUE,
    '#tree' => TRUE,
    '#process' => array('language_configuration_element_process'),
  );
  return $types;
}

/**
 * Returns the default options for the language configuration form element.
 *
 * @return array
 *   An array containing the default options.
 */
function language_configuration_element_default_options() {
  $language_options = array();

277
  $languages = language_list(Language::STATE_ALL);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  foreach ($languages as $langcode => $language) {
    $language_options[$langcode] = $language->locked ? t('- @name -', array('@name' => $language->name)) : $language->name;
  }

  $language_options += array(
    'site_default' => t("Site's default language (!language)", array('!language' => language_default()->name)),
    'current_interface' => t('Current interface language'),
    'authors_default' => t("Author's preferred language"),
  );
  return $language_options;
}

/**
 * Process handler for the language_configuration form element.
 */
function language_configuration_element_process($element, &$form_state, &$form) {
  $options = isset($element['#options']) ? $element['#options'] : array();
  // Avoid validation failure since we are moving the '#options' key in the
  // nested 'language' select element.
  unset($element['#options']);
  $element['langcode'] = array(
    '#type' => 'select',
    '#title' => t('Default language'),
    '#options' => $options + language_configuration_element_default_options(),
    '#description' => t('Explanation of the language options is found on the <a href="@languages_list_page">languages list page</a>.', array('@languages_list_page' => url('admin/config/regional/language'))),
    '#default_value' => isset($element['#default_value']['langcode']) ? $element['#default_value']['langcode'] : NULL,
  );

306
  $element['language_show'] = array(
307
    '#type' => 'checkbox',
308 309
    '#title' => t('Show language selector on create and edit pages'),
    '#default_value' => isset($element['#default_value']['language_show']) ? $element['#default_value']['language_show'] : NULL,
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  );

  // Add the entity type and bundle information to the form if they are set.
  // They will be used, in the submit handler, to generate the names of the
  // variables that will store the settings and are a way to uniquely identify
  // the entity.
  if (!isset($form_state['language'])) {
    $form_state['language'] = array();
  }
  $form_state['language'] += array(
    $element['#name'] => array(
      'entity_type' => $element['#entity_information']['entity_type'],
      'bundle' => $element['#entity_information']['bundle'],
    ),
  );

326 327 328 329 330 331 332 333 334 335 336 337
  // Do not add the submit callback for the language content settings page,
  // which is handled separately.
  if (array_search('language_content_settings_form_submit', $form['#submit']) === FALSE) {
    // Determine where to attach the language_configuration element submit handler.
    // @todo Form API: Allow form widgets/sections to declare #submit handlers.
    if (isset($form['actions']['submit']['#submit']) && array_search('language_configuration_element_submit', $form['actions']['submit']['#submit']) === FALSE) {
      $form['actions']['submit']['#submit'][] = 'language_configuration_element_submit';
    }
    elseif (array_search('language_configuration_element_submit', $form['#submit']) === FALSE) {
      $form['#submit'][] = 'language_configuration_element_submit';
    }
  }
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  return $element;
}

/**
 * Submit handler for the forms that have a language_configuration element.
 */
function language_configuration_element_submit(&$form, &$form_state) {
  // Iterate through all the language_configuration elements and save their
  // values.
  if (isset($form_state['language'])) {
    foreach ($form_state['language'] as $element_name => $values) {
      language_save_default_configuration($values['entity_type'], $values['bundle'],  $form_state['values'][$element_name]);
    }
  }
}

/**
 * Saves a language configuration that is attached to an entity type and bundle.
 *
 * @param string $entity_type
 *   A string representing the entity type.
 * @param string $bundle
 *   A string representing the bundle.
 * @param array $values
 *   An array holding the values to be saved having the following keys:
 *   - langcode: the language code.
364
 *   - language_show: if the language element should be hidden or not.
365 366
 */
function language_save_default_configuration($entity_type, $bundle, $values = array()) {
367
  config('language.settings')->set(language_get_default_configuration_settings_key($entity_type, $bundle), array('langcode' => $values['langcode'], 'language_show' => $values['language_show']))->save();
368 369 370 371 372 373 374 375 376 377 378 379 380
}

/**
 * Returns the language configuration stored for an entity type and bundle.
 *
 * @param string $entity_type
 *   A string representing the entity type.
 * @param string $bundle
 *   A string representing the bundle.
 *
 * @return array
 *   An array with the following keys:
 *   - langcode: the language code.
381
 *   - language_show: if the language element is hidden or not.
382 383 384 385 386 387
 */
function language_get_default_configuration($entity_type, $bundle) {
  $configuration = config('language.settings')->get(language_get_default_configuration_settings_key($entity_type, $bundle));
  if (is_null($configuration)) {
    $configuration = array();
  }
388
  $configuration += array('langcode' => 'site_default', 'language_show' => FALSE);
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  return $configuration;
}

/**
 * Clears the default language configuration for an entity type and bundle.
 *
 * @param string $entity_type
 *   A string representing the entity type.
 * @param string $bundle
 *   A string representing the bundle.
 */
function language_clear_default_configuration($entity_type, $bundle) {
  config('language.settings')->clear(language_get_default_configuration_settings_key($entity_type, $bundle))->save();
}

/**
 * Returns the root name of the variables used to store the configuration.
 *
 * Based on the entity type and bundle, the variables used to store the
 * configuration will have a common root name.
 *
 * @param string $entity_type
 *   A string representing the entity type.
 * @param string $bundle
 *   A string representing the bundle.
 *
 * @return string
 *   The root name of the variables.
 */
function language_get_default_configuration_settings_key($entity_type, $bundle) {
  // Replace all the characters that are not letters, numbers or "_" with "_".
  $entity_type = preg_replace('/[^0-9a-zA-Z_]/', "_", $entity_type);
  $bundle = preg_replace('/[^0-9a-zA-Z_]/', "_", $bundle);
  return $entity_type . '.' . $bundle . '.language.default_configuration';
}

/**
 * Implements hook_node_type_update().
 */
428 429 430 431
function language_node_type_update(NodeTypeInterface $type) {
  if ($type->original->id() != $type->id()) {
    language_save_default_configuration('node', $type->id(), language_get_default_configuration('node', $type->original->id()));
    language_clear_default_configuration('node', $type->original->id());
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
  }
}

/**
 * Returns the default language code assigned to an entity type and a bundle.
 *
 * @param string $entity_type
 *   The entity type.
 * @param string $bundle
 *   The bundle name.
 *
 * @return string
 *   The language code.
 */
function language_get_default_langcode($entity_type, $bundle) {
  $configuration = language_get_default_configuration($entity_type, $bundle);

  if (!isset($configuration['langcode'])) {
    $configuration['langcode'] = 'site_default';
  }

  $default_value = NULL;
454
  $language_interface = language(Language::TYPE_INTERFACE);
455 456
  switch ($configuration['langcode']) {
    case 'site_default':
457
      $default_value = language_default()->id;
458 459 460
      break;

    case 'current_interface':
461
      $default_value = $language_interface->id;
462 463 464 465
      break;

    case 'authors_default':
      global $user;
466 467 468
      $language_code = $user->getPreferredLangcode();
      if (!empty($language_code)) {
        $default_value = $language_code;
469 470
      }
      else {
471
        $default_value = $language_interface->id;
472 473 474 475 476 477 478 479 480 481 482 483
      }
      break;
  }
  if ($default_value) {
    return $default_value;
  }

  // If we still do not have a default value, just return the value stored in
  // the configuration; it has to be an actual language code.
  return $configuration['langcode'];
}

484 485 486 487
/**
 * API function to add or update a language.
 *
 * @param $language
488 489
 *   Language object with properties corresponding to the 'language'
 *   configuration properties.
490 491
 */
function language_save($language) {
492 493 494 495 496 497 498
  $language_entity = entity_load('language_entity', $language->id);
  if (!$language_entity) {
    $language_entity = entity_create('language_entity', array(
      'id' => $language->id,
    ));
  }
  $language->is_new = $language_entity->isNew();
499 500 501 502

  // Let other modules modify $language before saved.
  module_invoke_all('language_presave', $language);

503 504 505 506 507 508
  // Assign language properties to language entity.
  $language_entity->label = isset($language->name) ? $language->name : '';
  $language_entity->direction = isset($language->direction) ? $language->direction : '0';
  $language_entity->locked = isset($language->locked) ? $language->locked : '0';
  $language_entity->weight = isset($language->weight) ? $language->weight : '0';

509
  // Save the record and inform others about the change.
510 511
  $language_entity->save();
  $t_args = array('%language' => $language->name, '%langcode' => $language->id);
512 513 514 515 516 517 518 519 520 521 522
  if ($language->is_new) {
    module_invoke_all('language_insert', $language);
    watchdog('language', 'The %language (%langcode) language has been created.', $t_args);
  }
  else {
    module_invoke_all('language_update', $language);
    watchdog('language', 'The %language (%langcode) language has been updated.', $t_args);
  }

  if (!empty($language->default)) {
    // Set the new version of this language as default in a variable.
523
    variable_set('language_default', (array) $language);
524 525
  }

526
  // Update language count based on unlocked language count.
527
  language_update_count();
528

529 530 531
  // Update weight of locked system languages.
  language_update_locked_weights();

532 533 534
  // Kill the static cache in language_list().
  drupal_static_reset('language_list');

535 536
  language_negotiation_include();

537 538 539 540
  // Update URL Prefixes for all languages after the new default language is
  // propagated and the language_list() cache is flushed.
  language_negotiation_url_prefixes_update();

541 542 543
  return $language;
}

544 545 546 547 548 549 550 551
/**
 * Updates the language_count variable.
 *
 * This is used to check if a site is multilingual or not.
 *
 * @see language_multilingual()
 */
function language_update_count() {
552 553 554 555 556 557 558
  $count = 0;
  foreach (entity_load_multiple('language_entity') as $language) {
    if (!$language->locked) {
      $count++;
    }
  }
  variable_set('language_count', $count);
559 560
}

561 562 563 564 565
/**
 * Delete a language.
 *
 * @param $langcode
 *   Language code of the language to be deleted.
566
 *
567 568 569 570
 * @return
 *   TRUE if language is successfully deleted. Otherwise FALSE.
 */
function language_delete($langcode) {
571
  $languages = language_list(Language::STATE_ALL);
572
  if (isset($languages[$langcode]) && !$languages[$langcode]->locked) {
573 574 575 576 577
    $language = $languages[$langcode];

    module_invoke_all('language_delete', $language);

    // Remove the language.
578
    entity_delete_multiple('language_entity', array($language->id));
579

580
    language_update_count();
581

582 583 584
    // Update weight of locked system languages.
    language_update_locked_weights();

585 586
    drupal_static_reset('language_list');

587
    $t_args = array('%language' => $language->name, '%langcode' => $language->id);
588 589 590 591 592
    watchdog('language', 'The %language (%langcode) language has been removed.', $t_args);
    return TRUE;
  }
  return FALSE;
}
593

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
/**
 * Implements hook_library_info().
 */
function language_library_info() {
  $libraries['language.admin'] = array(
    'title' => 'Language detection admin',
    'version' => VERSION,
    'js' => array(
      drupal_get_path('module', 'language') . '/language.admin.js' => array(),
    ),
    'dependencies' => array(
      array('system', 'jquery'),
      array('system', 'drupal'),
      array('system', 'jquery.once'),
    ),
  );

  return $libraries;
}

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
/**
 * Implements hook_language_types_info().
 *
 * Defines the three core language types:
 * - Interface language is the only configurable language type in core. It is
 *   used by t() as the default language if none is specified.
 * - Content language is by default non-configurable and inherits the interface
 *   language negotiated value. It is used by the Field API to determine the
 *   display language for fields if no explicit value is specified.
 * - URL language is by default non-configurable and is determined through the
 *   URL language negotiation method or the URL fallback language negotiation
 *   method if no language can be detected. It is used by l() as the default
 *   language if none is specified.
 */
function language_language_types_info() {
  language_negotiation_include();

  return array(
632
    Language::TYPE_INTERFACE => array(
633 634
      'name' => t('User interface text'),
      'description' => t('Order of language detection methods for user interface text. If a translation of user interface text is available in the detected language, it will be displayed.'),
635
      'locked' => TRUE,
636
    ),
637
    Language::TYPE_CONTENT => array(
638 639 640
      'name' => t('Content'),
      'description' => t('Order of language detection methods for content. If a version of content is available in the detected language, it will be displayed.'),
      'fixed' => array(LANGUAGE_NEGOTIATION_INTERFACE),
641
      'locked' => TRUE,
642
    ),
643
    Language::TYPE_URL => array(
644
      'fixed' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_URL_FALLBACK),
645
      'locked' => TRUE,
646 647 648 649 650 651 652 653 654 655 656 657 658
    ),
  );
}

/**
 * Implements hook_language_negotiation_info().
 */
function language_language_negotiation_info() {
  language_negotiation_include();
  $file = drupal_get_path('module', 'language') . '/language.negotiation.inc';

  $negotiation_info = array();
  $negotiation_info[LANGUAGE_NEGOTIATION_URL] = array(
659
    'types' => array(Language::TYPE_CONTENT, Language::TYPE_INTERFACE, Language::TYPE_URL),
660 661 662 663 664 665 666
    'callbacks' => array(
      'negotiation' => 'language_from_url',
      'language_switch' => 'language_switcher_url',
    ),
    'file' => $file,
    'weight' => -8,
    'name' => t('URL'),
667
    'description' => t('Language from the URL (Path prefix or domain).'),
668 669 670 671 672 673 674 675 676 677 678 679
    'config' => 'admin/config/regional/language/detection/url',
  );

  $negotiation_info[LANGUAGE_NEGOTIATION_SESSION] = array(
    'callbacks' => array(
      'negotiation' => 'language_from_session',
      'language_switch' => 'language_switcher_session',
      'url_rewrite' => 'language_url_rewrite_session',
    ),
    'file' => $file,
    'weight' => -6,
    'name' => t('Session'),
680
    'description' => t('Language from a request/session parameter.'),
681 682 683 684 685 686 687
    'config' => 'admin/config/regional/language/detection/session',
  );

  $negotiation_info[LANGUAGE_NEGOTIATION_USER] = array(
    'callbacks' => array('negotiation' => 'language_from_user'),
    'file' => $file,
    'weight' => -4,
688 689
    'name' => t('Account preference for site'),
    'description' => t("The language setting for the site in the user's account."),
690 691 692 693 694 695 696 697
  );

  $negotiation_info[LANGUAGE_NEGOTIATION_BROWSER] = array(
    'callbacks' => array('negotiation' => 'language_from_browser'),
    'file' => $file,
    'weight' => -2,
    'cache' => 0,
    'name' => t('Browser'),
698
    'description' => t("Language from the browser's language settings."),
699
    'config' => 'admin/config/regional/language/detection/browser',
700 701 702
  );

  $negotiation_info[LANGUAGE_NEGOTIATION_INTERFACE] = array(
703
    'types' => array(Language::TYPE_CONTENT),
704 705 706 707 708 709 710 711
    'callbacks' => array('negotiation' => 'language_from_interface'),
    'file' => $file,
    'weight' => 8,
    'name' => t('Interface'),
    'description' => t('Use the detected interface language.'),
  );

  $negotiation_info[LANGUAGE_NEGOTIATION_URL_FALLBACK] = array(
712
    'types' => array(Language::TYPE_URL),
713 714 715 716 717 718 719
    'callbacks' => array('negotiation' => 'language_url_fallback'),
    'file' => $file,
    'weight' => 8,
    'name' => t('URL fallback'),
    'description' => t('Use an already detected language for URLs if none is found.'),
  );

720
  $negotiation_info[LANGUAGE_NEGOTIATION_USER_ADMIN] = array(
721
    'types' => array(Language::TYPE_INTERFACE),
722 723 724
    'callbacks' => array('negotiation' => 'language_from_user_admin'),
    'file' => $file,
    'weight' => 10,
725 726
    'name' => t('Account preference for administration pages'),
    'description' => t("The language setting for account administration pages in the user's account."),
727 728
  );

729 730 731 732 733 734 735 736
  return $negotiation_info;
}

/**
 * Include negotiation backend functionality.
 */
function language_negotiation_include() {
  include_once DRUPAL_ROOT . '/core/includes/language.inc';
737
  include_once __DIR__ . '/language.negotiation.inc';
738 739 740 741 742 743
}

/**
 * Implements hook_modules_enabled().
 */
function language_modules_enabled($modules) {
744 745 746
  include_once DRUPAL_ROOT . '/core/includes/language.inc';
  // Load configurability options from configuration.
  language_types_set(array());
747 748 749 750 751 752 753 754 755 756 757 758 759 760
  language_negotiation_purge();
}

/**
 * Implements hook_modules_disabled().
 */
function language_modules_disabled($modules) {
  language_modules_enabled($modules);
}

/**
 * Implements hook_language_insert().
 */
function language_language_insert($language) {
761 762 763 764
  if (!empty($language->locked)) {
    return;
  }

765 766 767 768
  language_negotiation_include();

  // Add language to the list of language domains.
  $domains = language_negotiation_url_domains();
769
  $domains[$language->id] = '';
770 771 772 773 774 775 776 777 778 779 780
  language_negotiation_url_domains_save($domains);
}

/**
 * Implements hook_language_delete().
 */
function language_language_delete($language) {
  language_negotiation_include();

  // Remove language from language prefix list.
  $prefixes = language_negotiation_url_prefixes();
781
  unset($prefixes[$language->id]);
782 783 784 785
  language_negotiation_url_prefixes_save($prefixes);

  // Remove language from language domain list.
  $domains = language_negotiation_url_domains();
786
  unset($domains[$language->id]);
787 788 789 790
  language_negotiation_url_domains_save($domains);
}

/**
791
 * Implements hook_preprocess_HOOK() for block.html.twig.
792 793
 */
function language_preprocess_block(&$variables) {
794
  if ($variables['configuration']['module'] == 'language') {
795
    $variables['attributes']['role'] = 'navigation';
796 797 798
  }
}

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
/**
 * Returns language mappings between browser and Drupal language codes.
 *
 * @return array
 *   An array containing browser language codes as keys with corresponding
 *   Drupal language codes as values.
 */
function language_get_browser_drupal_langcode_mappings() {
  $config = config('language.mappings');
  if ($config->isNew()) {
    return array();
  }
  return $config->get();
}

/**
 * Stores language mappings between browser and Drupal language codes.
 *
 * @param array $mappings
 *   An array containing browser language codes as keys with corresponding
 *   Drupal language codes as values.
 */
function language_set_browser_drupal_langcode_mappings($mappings) {
  $config = config('language.mappings');
  $config->setData($mappings);
  $config->save();
}
826 827 828 829 830

/**
 * Updates locked system language weights.
 */
function language_update_locked_weights() {
831 832
  $max_weight = 0;

833
  // Get maximum weight to update the system languages to keep them on bottom.
834 835 836 837 838 839
  foreach (language_list(Language::STATE_CONFIGURABLE) as $language) {
    if (!$language->locked && $language->weight > $max_weight) {
      $max_weight = $language->weight;
    }
  }

840
  // Loop locked languages to maintain the existing order.
841
  foreach (language_list(Language::STATE_LOCKED) as $language) {
842 843
    $max_weight++;
    // Update system languages weight.
844 845 846
    Drupal::config('language.entity.' . $language->id)
      ->set('weight', $max_weight)
      ->save();
847 848
  }
}
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863

/**
 * Implements hook_form_FORM_ID_alter for system_regional_settings().
 *
 * @see language_system_regional_settings_form_submit()
 */
function language_form_system_regional_settings_alter(&$form, &$form_state) {
  $languages = language_list();
  $default = language_default();
  foreach ($languages as $key => $language) {
    $language_options[$key] = $language->name;
  }
  $form['locale']['site_default_language'] = array(
    '#type' => 'select',
    '#title' => t('Default language'),
864
    '#default_value' => $default->id,
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    '#options' => $language_options,
    '#description' => t('It is not recommended to change the default language on a working site. <a href="@language-detection">Configure the Selected language</a> setting on the detection and selection page to change the fallback language for language selection.', array('@language-detection' => url('admin/config/regional/language/detection'))),
    '#weight' => -1,
  );
  // Add submit handler to save default language.
  $form['#submit'][] = 'language_system_regional_settings_form_submit';
}

/**
 * Form submission handler for system_regional_settings().
 *
 * @see language_form_system_regional_settings_alter()
 */
function language_system_regional_settings_form_submit($form, &$form_state) {
  $languages = language_list();
  $language = $languages[$form_state['values']['site_default_language']];
  $language->default = TRUE;
  language_save($language);
}