l10n_server-2.x-dev/l10n_community/src/Plugin/Block/LanguageProgressBlock.php

l10n_community/src/Plugin/Block/LanguageProgressBlock.php
<?php

declare(strict_types=1);

namespace Drupal\l10n_community\Plugin\Block;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\l10n_community\L10nStatistics;
use Drupal\l10n_server\Entity\L10nServerProjectInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a language progress block.
 *
 * @Block(
 *   id = "l10n_community_language_progress",
 *   admin_label = @Translation("Language progress per project"),
 *   category = @Translation("L10n")
 * )
 */
final class LanguageProgressBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * Constructs a new LanguageProgressBlock instance.
   *
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name. The special key 'context' may be used to
   *   initialize the defined contexts by setting it to an array of context
   *   values keyed by context names.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
   *   The language manager.
   * @param \Drupal\Core\Routing\CurrentRouteMatch $routeMatch
   *   The current route match service.
   * @param \Drupal\l10n_community\L10nStatistics $statistics
   *   The l10n_community.statistics service.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    private readonly LanguageManagerInterface $languageManager,
    private readonly CurrentRouteMatch $routeMatch,
    private readonly L10nStatistics $statistics,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(
    ContainerInterface $container,
    array $configuration,
    $plugin_id,
    $plugin_definition,
  ): self {
    return new LanguageProgressBlock(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('language_manager'),
      $container->get('current_route_match'),
      $container->get('l10n_community.statistics'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build(): array {
    $project = $this->routeMatch->getParameter('l10n_server_project');

    if (!$project instanceof L10nServerProjectInterface) {
      return [];
    }

    $num_source = $this->statistics->getProjectStringCount($project);
    $string_counts = $this->statistics->getLanguagesStringCount($project);
    $table_rows = [];

    foreach ($this->languageManager->getLanguages() as $langcode => $language) {
      // Need to load this again to get access to the third party settings :/.
      $language = ConfigurableLanguage::load($langcode);

      if (!$language instanceof EntityInterface) {
        continue;
      }

      if (!$language->getThirdPartySetting('l10n_pconfig', 'formula')) {
        $table_rows[] = [
          [
            'data' => t('@language (@langcode)', [
              '@language' => $language->getName(),
              '@langcode' => $language->getId(),
            ]),
            'class' => ['rowhead'],
          ],
          [
            'data' => $this->t('Uninitialized plural formula. Please set up the plural formula in <a href="@language-config">the language configuration</a> or alternatively <a href="@import-url">import a valid interface translation</a> for Drupal in this language.', [
              '@import-url' => Url::fromUri('internal:/admin/structure/translate/import')->toString(),
              '@language-config' => Url::fromUri('internal:/admin/config/regional/language')->toString(),
            ]),
            'colspan' => '3',
            'class' => ['error'],
          ],
        ];
      }
      else {
        $progress = [
          'data' => [
            '#theme' => 'l10n_community_progress_columns',
            '#sum' => $num_source,
            '#translated' => $string_counts[$language->getId()]['translations'] ?? 0,
            '#has_suggestion' => $string_counts[$language->getId()]['suggestions'] ?? 0,
          ],
        ];
        $table_rows[] = [
          [
            'data' => new FormattableMarkup('<a href=":link">@name (@langcode)</a>', [
              ':link' => Url::fromUri('internal:/translate/languages/' . $langcode . '/translate?project=' . $project->getUri())->toString(),
              '@name' => $language->getName(),
              '@langcode' => $langcode,
            ]),
          ],
          [
            'data' => $progress,
          ],
          [
            'data' => $num_source - ($string_counts[$language->getId()]['translations'] ?? 0),
          ],
          [
            'data' => $string_counts[$language->getId()]['suggestions'] ?? 0,
          ],
        ];
      }
    }

    $header = [
      $this->t('Language'),
      $this->t('Progress'),
      $this->t('Untranslated'),
      $this->t('Suggestions'),
    ];

    $build['content'] = [
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $table_rows,
      '#attributes' => [
        'class' => [
          'l10n-community-overview l10n-community-language-progress',
        ],
      ],
    ];
    $build['#attached']['library'][] = 'l10n_community/tables';

    return $build;
  }

  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account) {
    return AccessResult::allowedIf(
      $account->hasPermission('access localization community')
    );
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc