1.20.x locale.install locale_update_1003()

Move language negotiation settings from variables to config.

Related topics

File

modules/locale/locale.install, line 274
Install, update and uninstall functions for the locale module.

Code

function locale_update_1003() {
  // Set up new config file for language negotiation.
  $config = config('locale.settings');
  $config->set('_config_static', true);

  $config->set('language_negotiation_session_parameter', update_variable_get('locale_language_negotiation_session_param', 'language'));
  // Defined constants for locale_language_negotiation_url_part have changed in Backdrop.
  // (LOCALE_)LANGUAGE_NEGOTIATION_URL_PREFIX: 0 -> 'path_prefix'.
  // (LOCALE_)LANGUAGE_NEGOTIATION_URL_DOMAIN: 1 -> 'domain'.
  $negotiation_url = update_variable_get('locale_language_negotiation_url_part', 'path_prefix');
  if ($negotiation_url === 1) {
    $config->set('language_negotiation_url_part', 'domain');
  }
  else {
    $config->set('language_negotiation_url_part', 'path_prefix');
  }

  // Ensure defaults are set for all enabled languages.
  $languages = db_query('SELECT * FROM {language} ORDER BY weight ASC, name ASC')->fetchAllAssoc('langcode');
  $prefixes_old = update_variable_get('locale_language_negotiation_url_prefixes', array('en' => ''));
  $domains_old = update_variable_get('locale_language_negotiation_url_domains', array('en' => ''));
  $default_language = update_variable_get('language_default', (object) array(
    'langcode' => 'en',
    'name' => 'English',
    'direction' => 0,
    'enabled' => TRUE,
    'weight' => 0,
  ));
  $prefixes = array();
  $domains = array();

  foreach ($languages as $langcode => $language) {
    // Keep the old prefix or fill in based on whether the language is default.
    $prefixes[$langcode] = empty($prefixes_old[$langcode]) ? ($default_language->langcode == $langcode ? '' : $langcode) : $prefixes_old[$langcode];
    // Keep the old domain or fill in empty value.
    $domains[$langcode] = empty($domains_old[$langcode]) ? '' : $domains_old[$langcode];
  }
  $config->set('language_negotiation_url_prefixes', $prefixes);
  $config->set('language_negotiation_url_domains', $domains);
  $config->save();

  // Delete variables.
  update_variable_del('locale_language_negotiation_session_param');
  update_variable_del('locale_language_negotiation_url_part');
  update_variable_del('locale_language_negotiation_url_prefixes');
  update_variable_del('locale_language_negotiation_url_domains');
}