Unverified Commit 2b287338 authored by Stefan Auditor's avatar Stefan Auditor Committed by Stefan Auditor
Browse files

Issue #3282696 by sanduhrs: Port of l10n_community: Update various things

parent 0de2c7e5
Loading
Loading
Loading
Loading
+42 −11
Original line number Diff line number Diff line
@@ -5,6 +5,41 @@
 * Primary module hooks for Localization community UI module.
 */

/**
 * Strings with any status.
 */
define('L10N_STATUS_ALL', 0);

/**
 * Untranslated strings only.
 */
define('L10N_STATUS_UNTRANSLATED', 1);

/**
 * Translated (and approved) strings only.
 */
define('L10N_STATUS_TRANSLATED', 2);

/**
 * Has no outstanding suggested translations.
 */
define('L10N_STATUS_NO_SUGGESTION', 4);

/**
 * Has outstanding suggested translations.
 */
define('L10N_STATUS_HAS_SUGGESTION', 8);

/**
 * Is a translation.
 */
define('L10N_STATUS_IS_TRANSLATION', 16);

/**
 * Is a suggestion.
 */
define('L10N_STATUS_IS_SUGGESTION', 32);

/**
 * Implements hook_theme().
 */
@@ -81,18 +116,14 @@ function l10n_community_preprocess_page_title(&$variables) {
      $group = \Drupal::routeMatch()->getParameter('group');
      $langcode = $group->get('field_translation_language')->first()->getValue()['target_id'];
      $language = \Drupal::languageManager()->getLanguage($langcode);
      if ($language) {
      $variables['title'] = t('@language overview', [
        '@language' => $language->getName(),
      ]);
      }
      break;
    case 'view.l10n_groups_ron.page_1':
      if ($language) {
      $variables['title'] = t('Board - @language', [
        '@language' => $language->getName(),
      ]);
      }
      break;
  }

+15 −38
Original line number Diff line number Diff line
l10n_community.welcome_page:
  path: '/translate'
  defaults:
    _title: 'Latest translation status'
    _title_callback:  '\Drupal\l10n_community\Controller\L10nCommunityWelcomePageController::title'
    _controller: '\Drupal\l10n_community\Controller\L10nCommunityWelcomePageController::build'
  requirements:
@@ -21,50 +20,39 @@ l10n_community.project.explore:
  requirements:
    _permission: 'access localization community'

l10n_community.language.translate:
  path: '/translate/languages/{language}'
  defaults:
    _title: 'Translate'
    _controller: '\Drupal\l10n_community\Controller\L10nCommunityLanguagesController::overview'
  requirements:
    _permission: 'access localization community'
  options:
    parameters:
      language:
        type: language
l10n_community.language.translate.translate:
  path: '/translate/languages/{language}/translate'
  path: '/group/{group}/translate'
  defaults:
    _title: 'Translate'
    _title_callback: '\Drupal\l10n_community\Controller\L10nCommunityLanguagesController::translateTitle'
    _controller: '\Drupal\l10n_community\Controller\L10nCommunityLanguagesController::translate'
  requirements:
    _permission: 'access localization community'
    _permission: 'browse translations'
  options:
    parameters:
      language:
        type: language
      group:
        type: entity:group
l10n_community.language.translate.import:
  path: '/translate/languages/{language}/import'
  path: '/group/{group}/import'
  defaults:
    _title: 'Import'
    _title_callback: '\Drupal\l10n_community\Controller\L10nCommunityLanguagesController::importTitle'
    _controller: '\Drupal\l10n_community\Controller\L10nCommunityLanguagesController::import'
  requirements:
    _permission: 'access localization community'
    _permission: 'import gettext files'
  options:
    parameters:
      language:
        type: language
      group:
        type: entity:group
l10n_community.language.translate.export:
  path: '/translate/languages/{language}/export'
  path: '/group/{group}/export'
  defaults:
    _title: 'Export'
    _title_callback: '\Drupal\l10n_community\Controller\L10nCommunityLanguagesController::exportTitle'
    _controller: '\Drupal\l10n_community\Controller\L10nCommunityLanguagesController::export'
  requirements:
    _permission: 'access localization community'
    _permission: 'export gettext templates and translations'
  options:
    parameters:
      language:
        type: language
      group:
        type: entity:group

l10n_community.project.export:
  path: '/translate/projects/{project}/export'
@@ -77,17 +65,6 @@ l10n_community.project.export:
    parameters:
      project:
        type: entity:l10n_server_project
l10n_community.project.releases:
  path: '/translate/projects/{project}/releases'
  defaults:
    _title: 'Overview'
    _controller: '\Drupal\l10n_community\Controller\L10nCommunityProjectsController::releases'
  requirements:
    _permission: 'access localization community'
  options:
    parameters:
      project:
        type: entity:l10n_server_project
l10n_community.project.releases.release:
  path: '/translate/projects/{project}/releases/{release}'
  defaults:
+6 −0
Original line number Diff line number Diff line
@@ -2,3 +2,9 @@ services:
  l10n_community.statistics:
    class: Drupal\l10n_community\L10nStatistics
    arguments: ['@database']
  l10n_community.exporter:
    class: Drupal\l10n_community\L10nExporter
    arguments: ['@config.manager']
  l10n_community.access:
    class: Drupal\l10n_community\L10nAccess
    arguments: ['@current_route_match', '@module_handler']
+209 −27
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Language\Language;
use Drupal\Core\Url;
use Drupal\group\Entity\Group;
use Drupal\l10n_server\Entity\L10nServerProjectInterface;
use Drupal\l10n_server\Entity\L10nServerReleaseInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -195,61 +196,119 @@ class L10nCommunityLanguagesController extends ControllerBase {
  /**
   * Builds the response.
   *
   * @param \Drupal\Core\Language\Language $language
   * @param \Drupal\group\Entity\Group $group
   *   The group entity.
   *
   * @return array
   *   An associative render array.
   */
  public function overview(Language $language): array {
  public function translate(Group $group): array {
    $langcode = $group->get('field_translation_language')->first()->getValue()['target_id'];
    $language = \Drupal::languageManager()->getLanguage($langcode);

    $filters = self::buildFilterValues($_GET);
    $drupal_settings = [
      'l10nServerURLs' => $this->addUrlModifiers($langcode, $filters),
    ];
//    $strings = l10n_community_get_strings($langcode, $filters, $filters['limit']);
//
    // Add RTL style if the current language's direction is RTL
    if ($language->getDirection() == LANGUAGE::DIRECTION_RTL) {
      $build['#attached']['library'][] = 'l10n_community/editor-rtl';
    }

//    // Set the most appropriate title.
//    if ($filters['project']) {
//      drupal_set_title(t('Translate %project to @language', array('%project' => $filters['project']->title, '@language' => t($language->name))), PASS_THROUGH);
//    }
//    else {
//      drupal_set_title(t('Translate to @language', array('@language' => t($language->name))), PASS_THROUGH);
//    }

    // Add the filter form.
    $build['filter'] = \Drupal::formBuilder()->getForm('Drupal\l10n_community\Form\FilterForm');
    //    $output = array(
    //      drupal_get_form('l10n_community_filter_form', $langcode, $filters)
    //    );

//    // Output the actual strings.
//    if (!count($strings)) {
//      drupal_set_message(t('No strings found with this filter. Try adjusting the filter options.'));
//    }
//    else {
//      $output[] = drupal_get_form('l10n_community_translate_form', $language, $filters, $strings);
//    }

    $build['content'] = [
      '#type' => 'item',
      '#markup' => __METHOD__ . '::' . $language->getId(),
      '#markup' => __METHOD__ . '::' . $group->get('field_translation_language')->first()->getValue()['target_id'],
    ];
    $build['#attached']['drupalSettings'] = $drupal_settings;
    return $build;
  }

  /**
   * Builds the response.
   *
   * @param \Drupal\Core\Language\Language $language
   * Title callback.
   *
   * @return array
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   */
  public function translate(Language $language): array {
    $build['content'] = [
      '#type' => 'item',
      '#markup' => __METHOD__ . '::' . $language->getId(),
    ];
    return $build;
  public function translateTitle() {
    return $this->t('Translate');
  }

  /**
   * Builds the response.
   *
   * @param \Drupal\Core\Language\Language $language
   * @param \Drupal\group\Entity\Group $group
   *   The group entity.
   *
   * @return array
   *   An associative render array.
   */
  public function import(Language $language): array {
    $build['content'] = [
      '#type' => 'item',
      '#markup' => __METHOD__ . '::' . $language->getId(),
    ];
    return $build;
  public function import(Group $group): array {
    $group = \Drupal::routeMatch()->getParameter('group');
    $langcode = $group->get('field_translation_language')->first()->getValue()['target_id'];
    return \Drupal::formBuilder()->getForm('Drupal\l10n_community\Form\ImportForm', $langcode);
  }

  /**
   * Title callback.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   */
  public function importTitle() {
    /** @var \Drupal\group\Entity\Group $group */
    $group = \Drupal::routeMatch()->getParameter('group');
    $langcode = $group->get('field_translation_language')->first()->getValue()['target_id'];
    $language = \Drupal::languageManager()->getLanguage($langcode);
    return $this->t('Import to @language', ['@language' => $language->getName()]);
  }

  /**
   * Builds the response.
   *
   * @param \Drupal\Core\Language\Language $language
   * @param \Drupal\group\Entity\Group $group
   *   The group entity.
   *
   * @return array
   *   An associative render array.
   */
  public function export(Language $language): array {
    $build['content'] = [
      '#type' => 'item',
      '#markup' => __METHOD__ . '::' . $language->getId(),
    ];
    return $build;
  public function export(Group $group): array {
    $langcode = $group->get('field_translation_language')->first()->getValue()['target_id'];
    return \Drupal::formBuilder()->getForm('Drupal\l10n_community\Form\ExportForm', NULL, $langcode);
  }

  /**
   * Title callback.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   */
  public function exportTitle() {
    /** @var \Drupal\group\Entity\Group $group */
    $group = \Drupal::routeMatch()->getParameter('group');
    $langcode = $group->get('field_translation_language')->first()->getValue()['target_id'];
    $language = \Drupal::languageManager()->getLanguage($langcode);
    return $this->t('Export @language translations', ['@language' => $language->getName()]);
  }

  /**
@@ -269,4 +328,127 @@ class L10nCommunityLanguagesController extends ControllerBase {
    return $build;
  }

  /**
   * Check and sanitize arguments and build filter array.
   *
   * @param array $params
   *   Associative array with unsanitized values.
   *
   * @return array
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public static function buildFilterValues(array $params) {
    $project = $release = NULL;

    // Convert array representation of flags to one integer.
    if (isset($params['status']) && is_array($params['status'])) {
      if (isset($params['status']['suggestion'])) {
        $params['status'] = ((int) $params['status']['translation']) | ((int) $params['status']['suggestion']);
      }
      else {
        $params['status'] = (int) $params['status']['translation'];
      }
    }

    $filter = [
      'project' => NULL,
      'status' => isset($params['status']) ? (int) $params['status'] : 0,
      'release' => 'all',
      'search' => !empty($params['search']) ? (string) $params['search'] : '',
      'author' => NULL,
      // Dropdown, validated by form API.
      'context' => isset($params['context']) ? (string) $params['context'] : 'all',
      'limit' => (isset($params['limit']) && in_array($params['limit'], [5, 10, 20, 30, 50])) ? (int) $params['limit'] : 10,
      'sid' => (!empty($params['sid']) && is_numeric($params['sid'])) ? $params['sid'] : 0,
    ];

    if (isset($params['author'])) {
      $user_storage = \Drupal::entityTypeManager()
        ->getStorage('user');
      if (is_numeric($params['author'])) {
        $user = $user_storage->load($params['author']);
      }
      else {
        $users = $user_storage->loadByProperties(['name' => $params['author']]);
        $user = reset($users);
      }
      if (isset($user)) {
        $filter['author'] = $user->id();
      }
    }

    // The project can be a dropdown or text field depending on number of
    // projects. So we need to sanitize its value.
    if (isset($params['project'])) {
      // Try to load project by uri or id, but give URI priority. URI is used
      // to shorten the URL and have simple redirects. ID is used if the
      // filter form was submitted.
      $project_storage = \Drupal::entityTypeManager()
        ->getStorage('l10n_server_project');

      if (is_numeric($params['project'])) {
        $project = $project_storage->load($params['project']);
      }
      else {
        $projects = $project_storage->loadByProperties(['uri' => $params['project']]);
        $project = reset($projects);
      }

      /** @var \Drupal\l10n_server\Entity\L10nServerProject $project */
      if ($project instanceof \Drupal\l10n_server\Entity\L10nServerProject) {
        $filter['project'] = $project->id();

        $release_storage = \Drupal::entityTypeManager()
          ->getStorage('l10n_server_release');
        if (isset($params['release'])
            && ($releases = $release_storage->loadByProperties(['pid' => $project->id(), 'rid' => $params['release']]))) {
          // Allow to select this release, if belongs to current project only.
          $filter['release'] = $params['release'];
        }
      }
    }
    return $filter;
  }

  /**
   * Generate and add JS for URL replacements. These ensure we keep filter values.
   *
   * @param string $langcode
   * @param array $filters
   *
   * @return array
   */
  public function addUrlModifiers(string $langcode, array $filters) {
    $filters = self::flatFilters($filters);
    $urls = [
      'translate/languages/' . $langcode . '/translate',
      'translate/languages/' . $langcode . '/export',
    ];
    $replacements = [];
    foreach ($urls as $url) {
      $replacements[Url::fromUri('internal:/' . $url)->toString()] = Url::fromUri('internal:/' . $url, ['query' => $filters])->toString();
    }
    return $replacements;
  }

  /**
   * Replace complex data filters (objects or arrays) with string representations.
   *
   * @param array $filters
   *   Associative array with filters passed.
   *
   * @return array
   *   The modified filter array only containing string and number values.
   */
  public static function flatFilters(array $filters) {
//    foreach (['project' => 'uri', 'author' => 'name'] as $name => $key) {
//      if (!empty($filters[$name])) {
//        $filters[$name] = isset($filters[$name]->$key) ?? $filters[$name]->$key;
//      }
//    }
    return $filters;
  }

}
+21 −0
Original line number Diff line number Diff line
@@ -59,6 +59,27 @@ class L10nCommunityProjectsController extends ControllerBase {
    );
  }

  /**
   * Title callback.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup|void
   *   Translatable markup object.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function title() {
    /** @var \Drupal\l10n_server\Entity\L10nServerProject $project */
    $project = \Drupal::routeMatch()->getParameter('project');

    /** @var \Drupal\l10n_server\Entity\L10nServerProjectInterface $project */
    if ($project) {
      return $this->t('@project_name releases', [
        '@project_name' => $project->label(),
      ]);
    }
  }

  /**
   * Builds the response.
   */
Loading