You are here

function install_check_translations in Drupal 9

Same name and namespace in other branches
  1. 8 core/includes/install.core.inc \install_check_translations()
  2. 10 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 cannot be downloaded this will contain a 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 1903
API functions for installing Drupal.

Code

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

  // @todo: Make this configurable.
  $site_path = \Drupal::getContainer()
    ->getParameter('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.

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  $file_system
    ->prepareDirectory($files_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);

  // Then, attempt to create or make writable the translations directory.
  $file_system
    ->prepareDirectory($translations_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::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;
  }
  $version = \Drupal::VERSION;

  // For dev releases, remove the '-dev' part and trust the translation server
  // to fall back to the latest stable release for that branch.
  // @see locale_translation_build_projects()
  if (preg_match("/^(\\d+\\.\\d+\\.).*-dev\$/", $version, $matches)) {

    // Example match: 8.0.0-dev => 8.0.x (Drupal core)
    $version = $matches[1] . 'x';
  }

  // Build URL for the translation file and the translation server.
  $variables = [
    '%project' => 'drupal',
    '%version' => $version,
    '%core' => 'all',
    '%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 exist, throw an error.
  if (!$translations_directory_exists) {
    $requirements['translations directory exists'] = [
      '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>.', [
        '%translations_directory' => $translations_directory,
        ':install_txt' => base_path() . 'core/INSTALL.txt',
      ]),
    ];
  }
  else {
    $requirements['translations directory exists'] = [
      'title' => t('Translations directory'),
      'value' => t('The directory %translations_directory exists.', [
        '%translations_directory' => $translations_directory,
      ]),
    ];

    // If the translations directory is not readable, throw an error.
    if (!$readable) {
      $requirements['translations directory readable'] = [
        '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.', [
          '%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'] = [
        '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.', [
          '%translations_directory' => $translations_directory,
          ':handbook_url' => 'https://www.drupal.org/server-permissions',
        ]),
      ];
    }
    else {
      $requirements['translations directory writable'] = [
        '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'] = [
      '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>.', [
        ':server_url' => $server_url,
        '@server_url' => $server_url,
      ]),
    ];
  }
  else {
    $requirements['online'] = [
      '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'] = [
        'title' => t('Translation'),
        'value' => t('The %language translation is not available.', [
          '%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.', [
          '%language' => $language,
          ':url' => $_SERVER['SCRIPT_NAME'],
        ]),
      ];
    }
    else {
      $requirements['translation available'] = [
        'title' => t('Translation'),
        'value' => t('The %language translation is available.', [
          '%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'] = [
        'title' => t('Translation'),
        'value' => t('The %language translation could not be downloaded.', [
          '%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.', [
          '%language' => $language,
          ':url' => $_SERVER['SCRIPT_NAME'],
        ]),
      ];
    }
  }
  return $requirements;
}