1 install.core.inc install_check_translation_download($langcode)

Checks installation requirements, downloads a translation, and reports any errors.

This function is only called if the user selects a translation that is not English and for which there is no local translation file.

Parameters

string $langcode: Language code to check for download.

Return value

array: Requirements compliance array. If the translation cannot be downloaded this will contain a requirements error with detailed information.

See also

install_state_defaults()

install_download_translation()

File

core/includes/install.core.inc, line 1607
API functions for installing Backdrop.

Code

function install_check_translation_download($langcode) {
  $requirements = array();

  $readable = FALSE;
  $writable = FALSE;
  $translation_available = FALSE;

  $files_directory = conf_path() . '/files';
  $translations_directory = settings_get('locale_translate_file_directory', conf_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 (backdrop_verify_install_file($translations_directory, FILE_EXIST, 'dir')) {
    $readable = is_readable($translations_directory);
    $writable = is_writable($translations_directory);
    $translations_directory_exists = TRUE;
  }

  $version = BACKDROP_VERSION;
  $fallback_version = BACKDROP_CORE_COMPATIBILITY;

  // Get major and minor version, ignore any irrelevant dev or preview addition.
  if (preg_match("/^(\d+)\.(\d+)/", $version, $matches)) {
    $version = $matches[0];
    $fallback_version = $matches[1] . '.x';
  }

  // Build URL for the translation file and the translation server.
  $variables = array(
    '%project' => 'backdropcms',
    '%version' => $version,
    '%core' => 'all',
    '%language' => $langcode,
  );
  $translation_url = strtr(INSTALL_LOCALIZATION_SERVER_PATTERN, $variables);
  // Also build a generic fallback URL.
  $variables['%version'] = $fallback_version;
  $fallback_url = strtr(INSTALL_LOCALIZATION_SERVER_PATTERN, $variables);

  $elements = parse_url($translation_url);
  $server_url = $elements['scheme'] . '://' . $elements['host'];

  // Build the language name for display.
  include_once BACKDROP_ROOT . '/core/includes/standard.inc';

  $languages = standard_language_list();
  $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 (install_check_localization_server($server_url)) {
    $online = TRUE;
  }
  if ($online) {
    // We have to start with the fallback URL, otherwise the static cache
    // already returns FALSE.
    $translation_available = install_check_localization_server($fallback_url);
    if ($translation_available) {
      // Look up the version specific translation file. We can not rely on its
      // existence, as generation happens manually after release.
      // If it does not exist, yet, we stick with the generic fallback.
      if (!install_check_localization_server($translation_url)) {
        $translation_url = $fallback_url;
      }
    }
  }

  // If the translations directory does not exist, throw an error.
  if (!$translations_directory_exists) {
    $requirements['translations directory exists'] = array(
      'title' => st('Translations directory'),
      'value' => st('The translations directory does not exist.'),
      'severity' => REQUIREMENT_ERROR,
      'description' => st('The installation process requires a translations directory to be created under <code>%translations_directory</code>. The <a href="!install_docs">How to install Backdrop CMS</a> documentation page offers help on this and other topics.', array(
        '%translations_directory' => $translations_directory,
        '!install_docs' => 'https://docs.backdropcms.org/documentation/installation-instructions',
      )),
    );
  }
  else {
    $requirements['translations directory exists'] = array(
      'title' => st('Translations directory'),
      'value' => st('The directory <code>%translations_directory</code> exists.', array(
        '%translations_directory' => $translations_directory,
      )),
    );
    // If the translations directory is not readable, throw an error.
    if (!$readable) {
      $requirements['translations directory readable'] = array(
        'title' => st('Translations directory'),
        'value' => st('The translations directory is not readable.'),
        'severity' => REQUIREMENT_ERROR,
        'description' => st('The installer requires read permissions to <code>%translations_directory</code> at all times. The <a href="!handbook_url">File permissions and ownership</a> documentation section offers help on this and other topics.', array(
          '%translations_directory' => $translations_directory,
          '!handbook_url' => 'https://docs.backdropcms.org/documentation/file-permissions-and-ownership',
        )),
      );
    }
    // If translations directory is not writable, throw an error.
    if (!$writable) {
      $requirements['translations directory writable'] = array(
        'title' => st('Translations directory'),
        'value' => st('The translations directory is not writable.'),
        'severity' => REQUIREMENT_ERROR,
        'description' => st('The installer requires write permissions to the <code>%translations_directory<code> directory during the installation process. The <a href="!handbook_url">File permissions and ownership</a> documentation section offers help on this and other topics.', array(
          '%translations_directory' => $translations_directory,
          '!handbook_url' => 'https://docs.backdropcms.org/documentation/file-permissions-and-ownership',
        )),
      );
    }
    else {
      $requirements['translations directory writable'] = array(
        'title' => st('Translations directory'),
        'value' => st('The translations directory is writable.'),
      );
    }
  }

  // If the translations server can not be contacted, throw an error.
  if (!$online) {
    $requirements['online'] = array(
      'title' => st('Internet'),
      'value' => st('The translation server is offline or cannot be reached.'),
      'severity' => REQUIREMENT_ERROR,
      'description' => st('The installer was unable 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> , or <a href="!url">continue in English</a> and translate your website later.', array(
        '!server_url' => $server_url,
        '@server_url' => $server_url,
        '!url' => $_SERVER['SCRIPT_NAME'],
      )),
    );
  }
  else {
    $requirements['online'] = array(
      'title' => st('Internet'),
      'value' => st('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' => st('Translation'),
        'value' => st('The %language translation is not available.', array(
          '%language' => $language,
        )),
        'severity' => REQUIREMENT_ERROR,
        'description' => st('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' => st('Translation'),
        'value' => st('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' => st('Translation'),
        'value' => st('The %language translation could not be downloaded.', array(
          '%language' => $language,
        )),
        'severity' => REQUIREMENT_ERROR,
        'description' => st('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;
}