3 namespace Drupal\Core\Validation;
5 use Drupal\Component\Render\MarkupInterface;
6 use Drupal\Core\StringTranslation\TranslatableMarkup;
9 * Translates strings using Drupal's translation system.
11 * This class is used by the Symfony validator to translate violation messages.
13 class DrupalTranslator implements TranslatorInterface {
16 * The locale used for translating.
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));
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);
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));
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));
58 public function setLocale($locale) {
59 $this->locale = $locale;
65 public function getLocale() {
66 return $this->locale ? $this->locale : \Drupal::languageManager()->getCurrentLanguage()->getId();
70 * Processes the parameters array for use with t().
72 protected function processParameters(array $parameters) {
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;
81 if (is_object($value)) {
82 // t() does not work with objects being passed as replacement strings.
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;
91 $return[$key] = $value;
98 * Returns options suitable for use with t().
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];