3 namespace Drupal\Core\StringTranslation;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Component\Utility\ToStringTrait;
9 * Provides translatable markup class.
11 * This object, when cast to a string, will return the formatted, translated
12 * string. Avoid casting it to a string yourself, because it is preferable to
13 * let the rendering system do the cast as late as possible in the rendering
14 * process, so that this object itself can be put, untranslated, into render
15 * caches and thus the cache can be shared between different language contexts.
17 * @see \Drupal\Component\Render\FormattableMarkup
18 * @see \Drupal\Core\StringTranslation\TranslationManager::translateString()
19 * @see \Drupal\Core\Annotation\Translation
21 class TranslatableMarkup extends FormattableMarkup {
26 * The translated markup without placeholder replacements.
30 protected $translatedMarkup;
33 * The translation options.
40 * The string translation service.
42 * @var \Drupal\Core\StringTranslation\TranslationInterface
44 protected $stringTranslation;
47 * Constructs a new class instance.
49 * When possible, use the
50 * \Drupal\Core\StringTranslation\StringTranslationTrait $this->t(). Otherwise
51 * create a new \Drupal\Core\StringTranslation\TranslatableMarkup object
54 * Calling the trait's t() method or instantiating a new TranslatableMarkup
55 * object serves two purposes:
56 * - At run-time it translates user-visible text into the appropriate
58 * - Static analyzers detect calls to t() and new TranslatableMarkup, and add
59 * the first argument (the string to be translated) to the database of
60 * strings that need translation. These strings are expected to be in
61 * English, so the first argument should always be in English.
62 * To allow the site to be localized, it is important that all human-readable
63 * text that will be displayed on the site or sent to a user is made available
64 * in one of the ways supported by the
65 * @link https://www.drupal.org/node/322729 Localization API @endlink.
66 * See the @link https://www.drupal.org/node/322729 Localization API @endlink
67 * pages for more information, including recommendations on how to break up or
68 * not break up strings for translation.
70 * @section sec_translating_vars Translating Variables
71 * $string should always be an English literal string.
73 * $string should never contain a variable, such as:
75 * new TranslatableMarkup($text)
77 * There are several reasons for this:
78 * - Using a variable for $string that is user input is a security risk.
79 * - Using a variable for $string that has even guaranteed safe text (for
80 * example, user interface text provided literally in code), will not be
81 * picked up by the localization static text processor. (The parameter could
82 * be a variable if the entire string in $text has been passed into t() or
83 * new TranslatableMarkup() elsewhere as the first argument, but that
84 * strategy is not recommended.)
86 * It is especially important never to call new TranslatableMarkup($user_text)
87 * or t($user_text) where $user_text is some text that a user entered -- doing
88 * that can lead to cross-site scripting and other security problems. However,
89 * you can use variable substitution in your string, to put variable text such
90 * as user names or link URLs into translated text. Variable substitution
93 * new TranslatableMarkup("@name's blog", array('@name' => $account->getDisplayName()));
95 * Basically, you can put placeholders like @name into your string, and the
96 * method will substitute the sanitized values at translation time. (See the
97 * Localization API pages referenced above and the documentation of
98 * \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
99 * for details about how to safely and correctly define variables in your
100 * string.) Translators can then rearrange the string as necessary for the
101 * language (e.g., in Spanish, it might be "blog de @name").
103 * @param string $string
104 * A string containing the English text to translate.
105 * @param array $arguments
106 * (optional) An associative array of replacements to make after
107 * translation. Based on the first character of the key, the value is
108 * escaped and/or themed. See
109 * \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
111 * @param array $options
112 * (optional) An associative array of additional options, with the following
114 * - 'langcode' (defaults to the current language): A language code, to
115 * translate to a language other than what is used to display the page.
116 * - 'context' (defaults to the empty context): The context the source
118 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
119 * (optional) The string translation service.
121 * @throws \InvalidArgumentException
122 * Exception thrown when $string is not a string.
124 * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
125 * @see \Drupal\Core\StringTranslation\StringTranslationTrait::t()
127 * @ingroup sanitization
129 public function __construct($string, array $arguments = [], array $options = [], TranslationInterface $string_translation = NULL) {
130 if (!is_string($string)) {
131 $message = $string instanceof TranslatableMarkup ? '$string ("' . $string->getUntranslatedString() . '") must be a string.' : '$string ("' . (string) $string . '") must be a string.';
132 throw new \InvalidArgumentException($message);
134 parent::__construct($string, $arguments);
135 $this->options = $options;
136 $this->stringTranslation = $string_translation;
140 * Gets the untranslated string value stored in this translated string.
143 * The string stored in this wrapper.
145 public function getUntranslatedString() {
146 return $this->string;
150 * Gets a specific option from this translated string.
152 * @param string $name
156 * The value of this option or empty string of option is not set.
158 public function getOption($name) {
159 return isset($this->options[$name]) ? $this->options[$name] : '';
163 * Gets all options from this translated string.
166 * The array of options.
168 public function getOptions() {
169 return $this->options;
173 * Gets all arguments from this translated string.
176 * The array of arguments.
178 public function getArguments() {
179 return $this->arguments;
183 * Renders the object as a string.
186 * The translated string.
188 public function render() {
189 if (!isset($this->translatedMarkup)) {
190 $this->translatedMarkup = $this->getStringTranslation()->translateString($this);
193 // Handle any replacements.
194 if ($args = $this->getArguments()) {
195 return $this->placeholderFormat($this->translatedMarkup, $args);
197 return $this->translatedMarkup;
201 * Magic __sleep() method to avoid serializing the string translator.
203 public function __sleep() {
204 return ['string', 'arguments', 'options'];
208 * Gets the string translation service.
210 * @return \Drupal\Core\StringTranslation\TranslationInterface
211 * The string translation service.
213 protected function getStringTranslation() {
214 if (!$this->stringTranslation) {
215 $this->stringTranslation = \Drupal::service('string_translation');
218 return $this->stringTranslation;
222 * Returns the string length.
225 * The length of the string.
227 public function count() {
228 return mb_strlen($this->render());