1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
<?php
namespace Drupal\Core\StringTranslation;
/**
* Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.
*
* Using this trait will add t() and formatPlural() methods to the class. These
* must be used for every translatable string, similar to how procedural code
* must use the global functions t() and \Drupal::translation()->formatPlural().
* This allows string extractor tools to find translatable strings.
*
* If the class is capable of injecting services from the container, it should
* inject the 'string_translation' service and assign it to
* $this->stringTranslation.
*
* @see \Drupal\Core\StringTranslation\TranslationInterface
* @see container
*
* @ingroup i18n
*/
trait StringTranslationTrait {
/**
* The string translation service.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $stringTranslation;
/**
* Translates a string to the current language or to a given language.
*
* See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for
* important security information and usage guidelines.
*
* In order for strings to be localized, make them available in one of the
* ways supported by the
* @link https://www.drupal.org/node/322729 Localization API @endlink. When
* possible, use the \Drupal\Core\StringTranslation\StringTranslationTrait
* $this->t(). Otherwise create a new
* \Drupal\Core\StringTranslation\TranslatableMarkup object.
*
* @param string $string
* A string containing the English text to translate.
* @param array $args
* (optional) An associative array of replacements to make after
* translation. Based on the first character of the key, the value is
* escaped and/or themed. See
* \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
* details.
* @param array $options
* (optional) An associative array of additional options, with the following
* elements:
* - 'langcode' (defaults to the current language): A language code, to
* translate to a language other than what is used to display the page.
* - 'context' (defaults to the empty context): The context the source
* string belongs to. See the
* @link i18n Internationalization topic @endlink for more information
* about string contexts.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* An object that, when cast to a string, returns the translated string.
*
* @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
* @see \Drupal\Core\StringTranslation\TranslatableMarkup::__construct()
*
* @ingroup sanitization
*/
protected function t($string, array $args = [], array $options = []) {
// phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
return new TranslatableMarkup($string, $args, $options, $this->getStringTranslation());
}
/**
* Formats a string containing a count of items.
*
* @see \Drupal\Core\StringTranslation\TranslationInterface::formatPlural()
*
* @return \Drupal\Core\StringTranslation\PluralTranslatableMarkup
* An object that, when cast to a string, returns the translated string.
*/
protected function formatPlural($count, $singular, $plural, array $args = [], array $options = []) {
return new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $this->getStringTranslation());
}
/**
* Returns the number of plurals supported by a given language.
*
* @see \Drupal\locale\PluralFormulaInterface::getNumberOfPlurals()
*
* @return int
* The number of plurals supported.
*/
protected function getNumberOfPlurals($langcode = NULL) {
if (\Drupal::hasService('locale.plural.formula')) {
return \Drupal::service('locale.plural.formula')->getNumberOfPlurals($langcode);
}
// We assume 2 plurals if Locale's services are not available.
return 2;
}
/**
* Gets the string translation service.
*
* @return \Drupal\Core\StringTranslation\TranslationInterface
* The string translation service.
*/
protected function getStringTranslation() {
if (!$this->stringTranslation) {
$this->stringTranslation = \Drupal::service('string_translation');
}
return $this->stringTranslation;
}
/**
* Sets the string translation service to use.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* The string translation service.
*
* @return $this
*/
public function setStringTranslation(TranslationInterface $translation) {
$this->stringTranslation = $translation;
return $this;
}
}
|