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\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
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")
 * )
 */
class LanguageProgressBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * 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 $language_manager
   *   The language manager.
   */
  public function __construct(
      array $configuration,
      $plugin_id,
      $plugin_definition,
      LanguageManagerInterface $language_manager
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->languageManager = $language_manager;
  }

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

  /**
   * {@inheritdoc}
   */
  public function build() {
    /** @var \Drupal\l10n_server\Entity\L10nServerProject $project */
    $project = \Drupal::routeMatch()->getParameter('l10n_server_project');
    if (!$project instanceof L10nServerProjectInterface) {
      return [];
    }

    /** @var \Drupal\l10n_community\L10nStatistics $statistics */
    $statistics = \Drupal::service('l10n_community.statistics');
    $languages = $this->languageManager->getLanguages();
    /** @var \Drupal\locale\PluralFormula $plural_formula */
    $plural_formula = \Drupal::service('locale.plural.formula');

    $num_source = $statistics->getProjectStringCount($project);
    $string_counts = $statistics->getLanguagesStringCount($project);

    $table_rows = [];
    foreach ($languages as $langcode => $language) {
      // Need to load this again to get access to the third party settings :/.
      $language = ConfigurableLanguage::load($langcode);

      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 ?: 0,
            '#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