Unverified Commit 8d3a8b78 authored by Stefan Auditor's avatar Stefan Auditor Committed by Stefan Auditor
Browse files

Issue #3282696 by sanduhrs: Port of l10n_community: Merge language related controllers

parent 9088c6f3
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ l10n_community.language.explore:
  path: '/translate/languages'
  defaults:
    _title: 'Explore languages'
    _controller: '\Drupal\l10n_community\Controller\L10nCommunityExploreLanguagesController::build'
    _controller: '\Drupal\l10n_community\Controller\L10nCommunityLanguagesController::explore'
  requirements:
    _permission: 'access localization community'
l10n_community.project.explore:
+0 −177
Original line number Diff line number Diff line
<?php
declare(strict_types=1);

namespace Drupal\l10n_community\Controller;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Returns responses for Localization community UI routes.
 */
class L10nCommunityExploreLanguagesController extends ControllerBase {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The controller constructor.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   */
  public function __construct(Connection $connection) {
    $this->connection = $connection;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('database')
    );
  }

  /**
   * Order listing table by column for language overview columns.
   */
  public static function sortByColumnLanguage($a, $b) {
    $sortkey = ($_GET['order'] == t('Language') ? 0 : ($_GET['order'] == t('Contributors') ? 2 : 1));
    if (@$a[$sortkey]['sortdata'] == @$b[$sortkey]['sortdata']) {
      return 0;
    }
    return ((@$a[$sortkey]['sortdata'] < @$b[$sortkey]['sortdata']) ? -1 : 1) * ($_GET['sort'] == 'asc' ? 1 : -1);
  }

  /**
   * Builds the response.
   */
  public function build() {
    /** @var \Drupal\locale\PluralFormula $plural_formula */
    $plural_formula = \Drupal::service('locale.plural.formula');
    /** @var \Drupal\l10n_community\L10nStatistics $statistics */
    $statistics = \Drupal::service('l10n_community.statistics');

    // Checking whether we have languages to translate to.
    if (!$languages = \Drupal::languageManager()->getLanguages()) {
      $build['content'] = [
        '#type' => 'item',
        '#markup' => $this->t('No languages to list.'),
      ];
      return $build;
    }

    // Checking whether we have strings to translate.
    if (!$num_source = $statistics->getStringCount()) {
      $build['content'] = [
        '#type' => 'item',
        '#markup' => $this->t('No strings to translate.'),
      ];
      return $build;
    }

    // Generate listing of all languages with summaries. The list of languages
    // is relatively "short", compared to projects, so we don't need a pager
    // here.
    $table_rows = [];
    $string_counts = $statistics->getLanguagesStringCount();
    foreach ($languages as $langcode => $language) {
      if (!$plural_formula->getFormula($language->getId())) {
        $table_rows[] = [
          [
            'data' => t('@language', [
              '@language' => $language->getName(),
            ]),
            'sortdata' => t('@language', [
              '@language' => $language->getName(),
            ]),
            'class' => ['rowhead'],
          ],
          [
            'data' => t('Uninitialized plural formula. Please set up the plural formula in <a href="@language-config">the langauge 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(),
            ]),
            'class' => ['error'],
          ],
          ['data' => ''],
        ];
      }
      else {
        $stats = $statistics->getLanguageStatisticsByLanguage($langcode);
        $progress = [
          'data' => [
            '#theme' => 'l10n_community_progress_columns',
            '#sum' => $stats['strings'],
            '#translated' => $stats['translations'],
            '#has_suggestion' => $stats['suggestions'],
          ],
        ];
        $table_rows[] = [
          [
            'data' => new FormattableMarkup('<a href=":link">@name</a>', [
              ':link' => Url::fromUri('internal:/translate/languages/' . $langcode)->toString(),
              '@name' => $language->getName(),
            ]),
            'sortdata' => t('@language', [
              '@language' => $language->getName(),
            ]),
            'class' => ['rowhead'],
          ],
          [
            'data' => $progress,
            'sortdata' => ($num_source == 0 ? 0 : round(@$string_counts[$langcode]['translations'] / $num_source * 100, 2)),
          ],
          [
            'data' => $stats['users'],
            'sortdata' => $stats['users'],
          ],
        ];
      }
    }

    if (!empty($_GET['sort']) && !empty($_GET['order'])) {
      usort($table_rows, static::class . '::sortByColumnLanguage');
    }

    $header = [
      [
        'data' => t('Language'),
        'class' => ['rowhead'],
        'field' => 'language',
      ],
      [
        'data' => t('Overall progress'),
        'field' => 'progress',
      ],
      [
        'data' => t('Contributors'),
        'field' => 'contributors',
      ],
    ];
    $build['content'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $table_rows,
      '#attributes' => [
        'class' => [
          'l10n-community-overview l10n-community-highlighted',
        ],
      ],
      '#cache' => [
        'max-age' => Cache::PERMANENT,
      ],
    ];
    return $build;
  }

}
+147 −0
Original line number Diff line number Diff line
@@ -3,9 +3,13 @@ declare(strict_types=1);

namespace Drupal\l10n_community\Controller;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Language\Language;
use Drupal\Core\Url;
use Drupal\l10n_server\Entity\L10nServerProjectInterface;
use Drupal\l10n_server\Entity\L10nServerReleaseInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -15,6 +19,13 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
 */
class L10nCommunityLanguagesController extends ControllerBase {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The config manager.
   *
@@ -29,8 +40,10 @@ class L10nCommunityLanguagesController extends ControllerBase {
   *   The config manager.
   */
  public function __construct(
      Connection $connection,
      ConfigManagerInterface $config_manager
  ) {
    $this->connection = $connection;
    $this->configManager = $config_manager;
  }

@@ -41,10 +54,144 @@ class L10nCommunityLanguagesController extends ControllerBase {
      ContainerInterface $container
  ) {
    return new static(
      $container->get('database'),
      $container->get('config.manager')
    );
  }

  /**
   * Order listing table by column for language overview columns.
   */
  public static function sortByColumnLanguage($a, $b) {
    $sortkey = ($_GET['order'] == t('Language') ? 0 : ($_GET['order'] == t('Contributors') ? 2 : 1));
    if (@$a[$sortkey]['sortdata'] == @$b[$sortkey]['sortdata']) {
      return 0;
    }
    return ((@$a[$sortkey]['sortdata'] < @$b[$sortkey]['sortdata']) ? -1 : 1) * ($_GET['sort'] == 'asc' ? 1 : -1);
  }

  /**
   * Builds the response.
   */
  public function explore() {
    /** @var \Drupal\locale\PluralFormula $plural_formula */
    $plural_formula = \Drupal::service('locale.plural.formula');
    /** @var \Drupal\l10n_community\L10nStatistics $statistics */
    $statistics = \Drupal::service('l10n_community.statistics');

    // Checking whether we have languages to translate to.
    if (!$languages = \Drupal::languageManager()->getLanguages()) {
      $build['content'] = [
        '#type' => 'item',
        '#markup' => $this->t('No languages to list.'),
      ];
      return $build;
    }

    // Checking whether we have strings to translate.
    if (!$num_source = $statistics->getStringCount()) {
      $build['content'] = [
        '#type' => 'item',
        '#markup' => $this->t('No strings to translate.'),
      ];
      return $build;
    }

    // Generate listing of all languages with summaries. The list of languages
    // is relatively "short", compared to projects, so we don't need a pager
    // here.
    $table_rows = [];
    $string_counts = $statistics->getLanguagesStringCount();
    foreach ($languages as $langcode => $language) {
      if (!$plural_formula->getFormula($language->getId())) {
        $table_rows[] = [
          [
            'data' => t('@language', [
              '@language' => $language->getName(),
            ]),
            'sortdata' => t('@language', [
              '@language' => $language->getName(),
            ]),
            'class' => ['rowhead'],
          ],
          [
            'data' => t('Uninitialized plural formula. Please set up the plural formula in <a href="@language-config">the langauge 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(),
            ]),
            'class' => ['error'],
          ],
          ['data' => ''],
        ];
      }
      else {
        $stats = $statistics->getLanguageStatisticsByLanguage($langcode);
        $progress = [
          'data' => [
            '#theme' => 'l10n_community_progress_columns',
            '#sum' => $stats['strings'],
            '#translated' => $stats['translations'],
            '#has_suggestion' => $stats['suggestions'],
          ],
        ];
        $table_rows[] = [
          [
            'data' => new FormattableMarkup('<a href=":link">@name</a>', [
              ':link' => Url::fromUri('internal:/translate/languages/' . $langcode)->toString(),
              '@name' => $language->getName(),
            ]),
            'sortdata' => t('@language', [
              '@language' => $language->getName(),
            ]),
            'class' => ['rowhead'],
          ],
          [
            'data' => $progress,
            'sortdata' => ($num_source == 0 ? 0 : round(@$string_counts[$langcode]['translations'] / $num_source * 100, 2)),
          ],
          [
            'data' => $stats['users'],
            'sortdata' => $stats['users'],
          ],
        ];
      }
    }

    if (!empty($_GET['sort']) && !empty($_GET['order'])) {
      usort($table_rows, static::class . '::sortByColumnLanguage');
    }

    $header = [
      [
        'data' => t('Language'),
        'class' => ['rowhead'],
        'field' => 'language',
      ],
      [
        'data' => t('Overall progress'),
        'field' => 'progress',
      ],
      [
        'data' => t('Contributors'),
        'field' => 'contributors',
      ],
    ];
    $build['content'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $table_rows,
      '#attributes' => [
        'class' => [
          'l10n-community-overview l10n-community-highlighted',
        ],
      ],
      '#cache' => [
        'max-age' => Cache::PERMANENT,
      ],
    ];
    return $build;
  }

  /**
   * Builds the response.
   *