public TranslatableMarkup::__construct($string, array $arguments = array(), array $options = array(), TranslationInterface $string_translation = NULL)
Constructs a new class instance.
When possible, use the \Drupal\Core\StringTranslation\StringTranslationTrait $this->t(). Otherwise create a new \Drupal\Core\StringTranslation\TranslatableMarkup object directly.
Calling the trait's t() method or instantiating a new TranslatableMarkup object serves two purposes:
To allow the site to be localized, it is important that all human-readable text that will be displayed on the site or sent to a user is made available in one of the ways supported by the Localization API. See the Localization API pages for more information, including recommendations on how to break up or not break up strings for translation.
$string should always be an English literal string.
$string should never contain a variable, such as:
new TranslatableMarkup($text)
There are several reasons for this:
It is especially important never to call new TranslatableMarkup($user_text) or t($user_text) where $user_text is some text that a user entered -- doing that can lead to cross-site scripting and other security problems. However, you can use variable substitution in your string, to put variable text such as user names or link URLs into translated text. Variable substitution looks like this:
new TranslatableMarkup("@name's blog", array('@name' => $account->getDisplayName()));
Basically, you can put placeholders like @name into your string, and the method will substitute the sanitized values at translation time. (See the Localization API pages referenced above and the documentation of \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details about how to safely and correctly define variables in your string.) Translators can then rearrange the string as necessary for the language (e.g., in Spanish, it might be "blog de @name").
string $string: A string containing the English text to translate.
array $arguments: (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.
array $options: (optional) An associative array of additional options, with the following elements:
\Drupal\Core\StringTranslation\TranslationInterface $string_translation: (optional) The string translation service.
\InvalidArgumentException Exception thrown when $string is not a string.
Overrides FormattableMarkup::__construct
\Drupal\Component\Render\FormattableMarkup::placeholderFormat()
\Drupal\Core\StringTranslation\StringTranslationTrait::t()
public function __construct($string, array $arguments = array(), array $options = array(), TranslationInterface $string_translation = NULL) { if (!is_string($string)) { $message = $string instanceof TranslatableMarkup ? '$string ("' . $string->getUntranslatedString() . '") must be a string.' : '$string ("' . (string) $string . '") must be a string.'; throw new \InvalidArgumentException($message); } $this->string = $string; $this->arguments = $arguments; $this->options = $options; $this->stringTranslation = $string_translation; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!StringTranslation!TranslatableMarkup.php/function/TranslatableMarkup::__construct/8.1.x