class LocaleProjectChecker

Same name and namespace in other branches
  1. main core/modules/locale/src/LocaleProjectChecker.php \Drupal\locale\LocaleProjectChecker

Provide Locale Project Checker helper methods.

Hierarchy

Expanded class hierarchy of LocaleProjectChecker

4 files declare their use of LocaleProjectChecker
install.core.inc in core/includes/install.core.inc
API functions for installing Drupal.
locale.batch.inc in core/modules/locale/locale.batch.inc
locale.compare.inc in core/modules/locale/locale.compare.inc
LocaleController.php in core/modules/locale/src/Controller/LocaleController.php

File

core/modules/locale/src/LocaleProjectChecker.php, line 20

Namespace

Drupal\locale
View source
class LocaleProjectChecker {
  use StringTranslationTrait;
  public function __construct(protected readonly LocaleSource $localeSource, protected readonly LocaleProjectRepository $localeProjectRepository, protected readonly LocaleFetch $localeFetch, protected readonly StateInterface $state, protected readonly TimeInterface $time, protected readonly MessengerInterface $messenger, protected readonly ModuleHandlerInterface $moduleHandler, protected readonly TranslationInterface $translationManager, protected readonly AccountProxyInterface $currentUser) {
  }
  
  /**
   * Check for the latest release of project translations.
   *
   * @param array $projects
   *   Array of project names to check.
   * @param array $langcodes
   *   Array of language codes. Defaults to all translatable languages.
   */
  public function checkProjects(array $projects, array $langcodes = []) : void {
    if (locale_translation_use_remote_source()) {
      // Retrieve the status of both remote and local translation sources by
      // using a batch process.
      $this->triggerBatch($projects, $langcodes);
    }
    else {
      // Retrieve and save the status of local translations only.
      $this->checkLocalProjects($projects, $langcodes);
      $this->state
        ->set('locale.translation_last_checked', $this->time
        ->getRequestTime());
    }
  }
  
  /**
   * Check and store the status and timestamp of local po files.
   *
   * Only po files in the local file system are checked. Any remote translation
   * files will be ignored.
   *
   * Projects may contain a server_pattern option containing a pattern of the
   * path to the po source files. If no server_pattern is defined, the default
   * translation directory is checked for the po file. When a server_pattern is
   * defined, the specified location is checked. The server_pattern can be set
   * in the module's .info.yml file or by using
   * hook_locale_translation_projects_alter().
   *
   * @param array $projects
   *   Array of project names for which to check the state of translation files.
   *   Defaults to all translatable projects.
   * @param array $langcodes
   *   Array of language codes. Defaults to all translatable languages.
   */
  public function checkLocalProjects(array $projects, array $langcodes = []) : void {
    $projects = $this->localeProjectRepository
      ->getMultiple($projects);
    $langcodes = $langcodes ?: array_keys(locale_translatable_language_list());
    // For each project and each language we check if a local po file is
    // available. When found the source object is updated with the appropriate
    // type and timestamp of the po file.
    foreach ($projects as $name => $project) {
      foreach ($langcodes as $langcode) {
        $source = $this->localeSource
          ->sourceBuild($project, $langcode);
        $file = $this->localeSource
          ->sourceCheckFile($source);
        locale_translation_status_save($name, $langcode, LOCALE_TRANSLATION_LOCAL, $file);
      }
    }
  }
  
  /**
   * Builds a batch to get the status of remote and local translation files.
   *
   * The batch process fetches the state of both local and (if configured)
   * remote translation files. The data of the most recent translation is
   * stored per project and per language. This data is stored in a state
   * variable 'locale.translation_status'. The timestamp it was last updated is
   * stored in the state variable 'locale.translation_last_checked'.
   *
   * @param array $projects
   *   Array of project names for which to check the state of translation files.
   *   Defaults to all translatable projects.
   * @param array $langcodes
   *   Array of language codes. Defaults to all translatable languages.
   */
  public function triggerBatch(array $projects, array $langcodes = []) : void {
    $langcodes = $langcodes ?: array_keys(locale_translatable_language_list());
    $options = LocaleDefaultOptions::updateOptions();
    $operations = $this->localeFetch
      ->getStatusOperations($projects, $langcodes, $options);
    $batch_builder = (new BatchBuilder())->setTitle($this->t('Checking translations'))
      ->setErrorMessage($this->t('Error checking translation updates.'))
      ->setFinishCallback(self::class . ':batchFinished');
    foreach ($operations as $operation) {
      $batch_builder->addOperation(...$operation);
    }
    batch_set($batch_builder->toArray());
  }
  
  /**
   * Implements callback_batch_finished().
   *
   * Set result message.
   *
   * @param bool $success
   *   TRUE if batch successfully completed.
   * @param array $results
   *   Batch results.
   */
  public function batchFinished(bool $success, array $results) : void {
    if ($success) {
      if (isset($results['failed_files'])) {
        if ($this->moduleHandler
          ->moduleExists('dblog') && $this->currentUser
          ->hasPermission('access site reports')) {
          $message = $this->translationManager
            ->formatPlural(count($results['failed_files']), 'One translation file could not be checked. <a href=":url">See the log</a> for details.', '@count translation files could not be checked. <a href=":url">See the log</a> for details.', [
            ':url' => Url::fromRoute('dblog.overview')->toString(),
          ]);
        }
        else {
          $message = $this->translationManager
            ->formatPlural(count($results['failed_files']), 'One translation files could not be checked. See the log for details.', '@count translation files could not be checked. See the log for details.');
        }
        $this->messenger
          ->addError($message);
      }
      if (isset($results['files'])) {
        $this->messenger
          ->addStatus($this->translationManager
          ->formatPlural(count($results['files']), 'Checked available interface translation updates for one project.', 'Checked available interface translation updates for @count projects.'));
      }
      if (!isset($results['failed_files']) && !isset($results['files'])) {
        $this->messenger
          ->addStatus($this->t('Nothing to check.'));
      }
      $this->state
        ->set('locale.translation_last_checked', $this->time
        ->getRequestTime());
    }
    else {
      $this->messenger
        ->addError($this->t('An error occurred trying to check available interface translation updates.'));
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
LocaleProjectChecker::batchFinished public function Implements callback_batch_finished().
LocaleProjectChecker::checkLocalProjects public function Check and store the status and timestamp of local po files.
LocaleProjectChecker::checkProjects public function Check for the latest release of project translations.
LocaleProjectChecker::triggerBatch public function Builds a batch to get the status of remote and local translation files.
LocaleProjectChecker::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.