Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Validation / DrupalTranslator.php
1 <?php
2
3 namespace Drupal\Core\Validation;
4
5 use Drupal\Component\Render\MarkupInterface;
6 use Drupal\Core\StringTranslation\TranslatableMarkup;
7
8 /**
9  * Translates strings using Drupal's translation system.
10  *
11  * This class is used by the Symfony validator to translate violation messages.
12  */
13 class DrupalTranslator implements TranslatorInterface {
14
15   /**
16    * The locale used for translating.
17    *
18    * @var string
19    */
20   protected $locale;
21
22   /**
23    * {@inheritdoc}
24    */
25   public function trans($id, array $parameters = [], $domain = NULL, $locale = NULL) {
26     // If a TranslatableMarkup object is passed in as $id, return it since the
27     // message has already been translated.
28     return $id instanceof TranslatableMarkup ? $id : t($id, $this->processParameters($parameters), $this->getOptions($domain, $locale));
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function transChoice($id, $number, array $parameters = [], $domain = NULL, $locale = NULL) {
35     // Violation messages can separated singular and plural versions by "|".
36     $ids = explode('|', $id);
37
38     if (!isset($ids[1])) {
39       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));
40     }
41
42     // Normally, calls to formatPlural() need to use literal strings, like
43     //   formatPlural($count, '1 item', '@count items')
44     // so that the Drupal project POTX string extractor will correctly
45     // extract the strings for translation and save them in a format that
46     // formatPlural() can work with. However, this is a special case, because
47     // Drupal is supporting a constraint message format from Symfony. So
48     // although $id looks like a variable here, it is actually coming from a
49     // static string in a constraint class that the POTX extractor knows about
50     // and has processed to work with formatPlural(), so this specific call to
51     // formatPlural() will work correctly.
52     return \Drupal::translation()->formatPlural($number, $ids[0], $ids[1], $this->processParameters($parameters), $this->getOptions($domain, $locale));
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function setLocale($locale) {
59     $this->locale = $locale;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function getLocale() {
66     return $this->locale ? $this->locale : \Drupal::languageManager()->getCurrentLanguage()->getId();
67   }
68
69   /**
70    * Processes the parameters array for use with t().
71    */
72   protected function processParameters(array $parameters) {
73     $return = [];
74     foreach ($parameters as $key => $value) {
75       // We allow the values in the parameters to be safe string objects. This
76       // can be useful when we want to use parameter values that are
77       // TranslatableMarkup.
78       if ($value instanceof MarkupInterface) {
79         $value = (string) $value;
80       }
81       if (is_object($value)) {
82         // t() does not work with objects being passed as replacement strings.
83       }
84       // Check for symfony replacement patterns in the form "{{ name }}".
85       elseif (strpos($key, '{{ ') === 0 && strrpos($key, ' }}') == strlen($key) - 3) {
86         // Transform it into a Drupal pattern using the format %name.
87         $key = '%' . substr($key, 3, strlen($key) - 6);
88         $return[$key] = $value;
89       }
90       else {
91         $return[$key] = $value;
92       }
93     }
94     return $return;
95   }
96
97   /**
98    * Returns options suitable for use with t().
99    */
100   protected function getOptions($domain = NULL, $locale = NULL) {
101     // We do not support domains, so we ignore this parameter.
102     // If locale is left NULL, t() will default to the interface language.
103     $locale = isset($locale) ? $locale : $this->locale;
104     return ['langcode' => $locale];
105   }
106
107 }