Commit fc4703df authored by Felip Manyer i Ballester's avatar Felip Manyer i Ballester
Browse files

Issue #3282696: Port of l10n_community: translation form (validation)

parent 85fc94d2
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
@@ -866,6 +866,51 @@ class TranslateForm extends FormBase implements TrustedCallbackInterface {
    return $this->renderer->renderPlain($element);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $strings = $form_state->getValue('strings');

    // Load original strings.
    // @todo could we avoid this database query? It looks like all the
    // information we need is already in $strings.
    $originals = $this->translator->getSourceStrings(array_keys($strings));

    // Iterate outer structure built in l10n_community_translate_form().
    $pattern = '/([!@%:]|<(ins|del)>[!@%:]<\/(ins|del)>)([\w-]+|<(ins|del)>[\w-]+<\/(ins|del)>)/';
    foreach ($strings as $sid => $string) {
      $matches = [];

      // If there are variables in the original string, check in new
      // suggestions. We need to explode the strings as they may contain plurals
      // and check the corresponding plural.
      $originals_plurals = $this->unpackString($originals[$sid]);
      foreach ($originals_plurals as $sourceid => $sourcevalue) {
        if (preg_match_all($pattern, $sourcevalue, $matches)) {
          foreach ($string['translation'] as $tid => $options) {
            // Only process new suggestions.
            if (isset($options['value']) && is_array($options['value']) && $tid === 'new') {
              $not_found = [];
              $value = $options['value'][$sourceid];
              if ($value !== (string) t('<New translation>')) {
                foreach ($matches[0] as $match) {
                  if (!strstr($value, $match)) {
                    $not_found[] = $match;
                  }
                }
              }
              if ($not_found) {
                $form_state->setErrorByName('strings][' . $sid . '][translation][new][value][0', $this->t('Missing variable(s) @variables', ['@variables' => implode(', ', $not_found)]));
              }
            }
          }

        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
+20 −0
Original line number Diff line number Diff line
@@ -31,6 +31,26 @@ class L10nTranslator {
    $this->connection = $connection;
  }

  /**
   * Get source strings, knowing their identifiers, for simple cases.
   *
   * @param int[] $sids
   *   Source strings IDs.
   *
   * @return array
   *   Source strings.
   *
   * @see \Drupal\l10n_community\Form\TranslateForm::validateForm()
   */
  public function getSourceStrings(array $sids) {
    return $this->connection
      ->select('l10n_server_string', 's')
      ->fields('s', ['sid', 'value'])
      ->condition('sid', $sids, 'IN')
      ->execute()
      ->fetchAllKeyed();
  }

  /**
   * Get strings under some conditions.
   *