You are here

function install_check_translations in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/includes/install.core.inc \install_check_translations()

Checks installation requirements and reports any errors.

Parameters

string $langcode: Language code to check for download.

string $server_pattern: Server access pattern (to replace language code, version number, etc. in).

Return value

array Requirements compliance array. If the translation was downloaded successfully then an empty array is returned. Otherwise the requirements error with detailed information.

1 call to install_check_translations()
install_download_translation in core/includes/install.core.inc
Download a translation file for the selected language.
1 string reference to 'install_check_translations'
install_download_additional_translations_operations in core/includes/install.core.inc
Prepares the system for import and downloads additional translations.

File

core/includes/install.core.inc, line 1806
API functions for installing Drupal.

Code

function install_check_translations($langcode, $server_pattern) {
  $requirements = array();
  $readable = FALSE;
  $writable = FALSE;

  // @todo: Make this configurable.
  $site_path = \Drupal::service('site.path');
  $files_directory = $site_path . '/files';
  $translations_directory = $site_path . '/files/translations';
  $translations_directory_exists = FALSE;
  $online = FALSE;

  // First attempt to create or make writable the files directory.
  file_prepare_directory($files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);

  // Then, attempt to create or make writable the translations directory.
  file_prepare_directory($translations_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);

  // Get values so the requirements errors can be specific.
  if (drupal_verify_install_file($translations_directory, FILE_EXIST, 'dir')) {
    $readable = is_readable($translations_directory);
    $writable = is_writable($translations_directory);
    $translations_directory_exists = TRUE;
  }

  // The file already exists, no need to attempt to download.
  if ($existing_file = glob($translations_directory . '/drupal-*.' . $langcode . '.po')) {
    return;
  }

  // Build URL for the translation file and the translation server.
  $variables = array(
    '%project' => 'drupal',
    '%version' => \Drupal::VERSION,
    '%core' => \Drupal::CORE_COMPATIBILITY,
    '%language' => $langcode,
  );
  $translation_url = strtr($server_pattern, $variables);
  $elements = parse_url($translation_url);
  $server_url = $elements['scheme'] . '://' . $elements['host'];

  // Build the language name for display.
  $languages = LanguageManager::getStandardLanguageList();
  $language = isset($languages[$langcode]) ? $languages[$langcode][0] : $langcode;

  // Check if any of the desired translation files are available or if the
  // translation server can be reached. In other words, check if we are online
  // and have an internet connection.
  if ($translation_available = install_check_localization_server($translation_url)) {
    $online = TRUE;
  }
  if (!$translation_available) {
    if (install_check_localization_server($server_url)) {
      $online = TRUE;
    }
  }

  // If the translations directory does not exists, throw an error.
  if (!$translations_directory_exists) {
    $requirements['translations directory exists'] = array(
      'title' => t('Translations directory'),
      'value' => t('The translations directory does not exist.'),
      'severity' => REQUIREMENT_ERROR,
      'description' => t('The installer requires that you create a translations directory as part of the installation process. Create the directory %translations_directory . More details about installing Drupal are available in <a href=":install_txt">INSTALL.txt</a>.', array(
        '%translations_directory' => $translations_directory,
        ':install_txt' => base_path() . 'core/INSTALL.txt',
      )),
    );
  }
  else {
    $requirements['translations directory exists'] = array(
      'title' => t('Translations directory'),
      'value' => t('The directory %translations_directory exists.', array(
        '%translations_directory' => $translations_directory,
      )),
    );

    // If the translations directory is not readable, throw an error.
    if (!$readable) {
      $requirements['translations directory readable'] = array(
        'title' => t('Translations directory'),
        'value' => t('The translations directory is not readable.'),
        'severity' => REQUIREMENT_ERROR,
        'description' => t('The installer requires read permissions to %translations_directory at all times. The <a href=":handbook_url">webhosting issues</a> documentation section offers help on this and other topics.', array(
          '%translations_directory' => $translations_directory,
          ':handbook_url' => 'https://www.drupal.org/server-permissions',
        )),
      );
    }

    // If translations directory is not writable, throw an error.
    if (!$writable) {
      $requirements['translations directory writable'] = array(
        'title' => t('Translations directory'),
        'value' => t('The translations directory is not writable.'),
        'severity' => REQUIREMENT_ERROR,
        'description' => t('The installer requires write permissions to %translations_directory during the installation process. The <a href=":handbook_url">webhosting issues</a> documentation section offers help on this and other topics.', array(
          '%translations_directory' => $translations_directory,
          ':handbook_url' => 'https://www.drupal.org/server-permissions',
        )),
      );
    }
    else {
      $requirements['translations directory writable'] = array(
        'title' => t('Translations directory'),
        'value' => t('The translations directory is writable.'),
      );
    }
  }

  // If the translations server can not be contacted, throw an error.
  if (!$online) {
    $requirements['online'] = array(
      'title' => t('Internet'),
      'value' => t('The translation server is offline.'),
      'severity' => REQUIREMENT_ERROR,
      'description' => t('The installer requires to contact the translation server to download a translation file. Check your internet connection and verify that your website can reach the translation server at <a href=":server_url">@server_url</a>.', array(
        ':server_url' => $server_url,
        '@server_url' => $server_url,
      )),
    );
  }
  else {
    $requirements['online'] = array(
      'title' => t('Internet'),
      'value' => t('The translation server is online.'),
    );

    // If translation file is not found at the translation server, throw an
    // error.
    if (!$translation_available) {
      $requirements['translation available'] = array(
        'title' => t('Translation'),
        'value' => t('The %language translation is not available.', array(
          '%language' => $language,
        )),
        'severity' => REQUIREMENT_ERROR,
        'description' => t('The %language translation file is not available at the translation server. <a href=":url">Choose a different language</a> or select English and translate your website later.', array(
          '%language' => $language,
          ':url' => $_SERVER['SCRIPT_NAME'],
        )),
      );
    }
    else {
      $requirements['translation available'] = array(
        'title' => t('Translation'),
        'value' => t('The %language translation is available.', array(
          '%language' => $language,
        )),
      );
    }
  }
  if ($translations_directory_exists && $readable && $writable && $translation_available) {
    $translation_downloaded = install_retrieve_file($translation_url, $translations_directory);
    if (!$translation_downloaded) {
      $requirements['translation downloaded'] = array(
        'title' => t('Translation'),
        'value' => t('The %language translation could not be downloaded.', array(
          '%language' => $language,
        )),
        'severity' => REQUIREMENT_ERROR,
        'description' => t('The %language translation file could not be downloaded. <a href=":url">Choose a different language</a> or select English and translate your website later.', array(
          '%language' => $language,
          ':url' => $_SERVER['SCRIPT_NAME'],
        )),
      );
    }
  }
  return $requirements;
}