DrupalTranslator.php 3.8 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Validation;

5
use Drupal\Component\Render\MarkupInterface;
6
use Drupal\Core\StringTranslation\TranslatableMarkup;
7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
 * Translates strings using Drupal's translation system.
 *
 * This class is used by the Symfony validator to translate violation messages.
 */
class DrupalTranslator implements TranslatorInterface {

  /**
   * The locale used for translating.
   *
   * @var string
   */
  protected $locale;

  /**
23
   * {@inheritdoc}
24
   */
25
  public function trans($id, array $parameters = [], $domain = NULL, $locale = NULL) {
26 27
    // If a TranslatableMarkup object is passed in as $id, return it since the
    // message has already been translated.
28 29 30 31
    if ($id instanceof TranslatableMarkup) {
      return $id;
    }
    return new TranslatableMarkup($id, $this->processParameters($parameters), $this->getOptions($domain, $locale));
32 33 34
  }

  /**
35
   * {@inheritdoc}
36
   */
37
  public function transChoice($id, $number, array $parameters = [], $domain = NULL, $locale = NULL) {
38 39 40 41 42 43
    // Violation messages can separated singular and plural versions by "|".
    $ids = explode('|', $id);

    if (!isset($ids[1])) {
      throw new \InvalidArgumentException(sprintf('The message "%s" cannot be pluralized, because it is missing a plural (e.g. "There is one apple|There are @count apples").', $id));
    }
44 45 46 47 48 49 50 51 52 53 54

    // Normally, calls to formatPlural() need to use literal strings, like
    //   formatPlural($count, '1 item', '@count items')
    // so that the Drupal project POTX string extractor will correctly
    // extract the strings for translation and save them in a format that
    // formatPlural() can work with. However, this is a special case, because
    // Drupal is supporting a constraint message format from Symfony. So
    // although $id looks like a variable here, it is actually coming from a
    // static string in a constraint class that the POTX extractor knows about
    // and has processed to work with formatPlural(), so this specific call to
    // formatPlural() will work correctly.
55
    return \Drupal::translation()->formatPlural($number, $ids[0], $ids[1], $this->processParameters($parameters), $this->getOptions($domain, $locale));
56 57 58
  }

  /**
59
   * {@inheritdoc}
60 61 62 63 64 65
   */
  public function setLocale($locale) {
    $this->locale = $locale;
  }

  /**
66
   * {@inheritdoc}
67 68
   */
  public function getLocale() {
69
    return $this->locale ? $this->locale : \Drupal::languageManager()->getCurrentLanguage()->getId();
70 71 72
  }

  /**
73
   * Processes the parameters array for use with TranslatableMarkup.
74 75
   */
  protected function processParameters(array $parameters) {
76
    $return = [];
77
    foreach ($parameters as $key => $value) {
78 79
      // We allow the values in the parameters to be safe string objects. This
      // can be useful when we want to use parameter values that are
80 81
      // TranslatableMarkup.
      if ($value instanceof MarkupInterface) {
82 83
        $value = (string) $value;
      }
84
      if (is_object($value)) {
85 86
        // TranslatableMarkup does not work with objects being passed as
        // replacement strings.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      }
      // Check for symfony replacement patterns in the form "{{ name }}".
      elseif (strpos($key, '{{ ') === 0 && strrpos($key, ' }}') == strlen($key) - 3) {
        // Transform it into a Drupal pattern using the format %name.
        $key = '%' . substr($key, 3, strlen($key) - 6);
        $return[$key] = $value;
      }
      else {
        $return[$key] = $value;
      }
    }
    return $return;
  }

  /**
102
   * Returns options suitable for use with TranslatableMarkup.
103 104 105
   */
  protected function getOptions($domain = NULL, $locale = NULL) {
    // We do not support domains, so we ignore this parameter.
106 107
    // If locale is left NULL, TranslatableMarkup will default to the interface
    // language.
108
    $locale = isset($locale) ? $locale : $this->locale;
109
    return ['langcode' => $locale];
110
  }
111

112
}