TranslatableMarkup
class TranslatableMarkup extends FormattableMarkup (View source)
Provides translatable markup class.
This object, when cast to a string, will return the formatted, translated string. Avoid casting it to a string yourself, because it is preferable to let the rendering system do the cast as late as possible in the rendering process, so that this object itself can be put, untranslated, into render caches and thus the cache can be shared between different language contexts.
Traits
Wraps __toString in a trait to avoid some fatals.
Properties
| protected string | $string | The string containing placeholders. |
from FormattableMarkup |
| protected array | $arguments | The arguments to replace placeholders with. |
from FormattableMarkup |
| protected string | $translatedMarkup | The translated markup without placeholder replacements. |
|
| protected array | $options | The translation options. |
|
| protected TranslationInterface | $stringTranslation | The string translation service. |
Methods
Constructs a new class instance.
Returns the string length.
Returns a representation of the object for use in JSON serialization.
Replaces placeholders in a string with values.
Renders the object as a string.
Gets the untranslated string value stored in this translated string.
Gets a specific option from this translated string.
Gets all options from this translated string.
Gets all arguments from this translated string.
Magic __sleep() method to avoid serializing the string translator.
Gets the string translation service.
Details
__construct(string $string, array $arguments = [], array $options = [], 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:
- At run-time it translates user-visible text into the appropriate language.
- Static analyzers detect calls to t() and new TranslatableMarkup, and add the first argument (the string to be translated) to the database of strings that need translation. These strings are expected to be in English, so the first argument should always be in English. 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
__toString()
Implements the magic __toString() method.
int
count()
Returns the string length.
string
jsonSerialize()
Returns a representation of the object for use in JSON serialization.
static protected string
placeholderFormat(string $string, array $args)
Replaces placeholders in a string with values.
static protected string
placeholderEscape(string|MarkupInterface $value)
Escapes a placeholder replacement value if needed.
protected
_die()
For test purposes, wrap die() in an overridable method.
string
render()
Renders the object as a string.
string
getUntranslatedString()
Gets the untranslated string value stored in this translated string.
mixed
getOption(string $name)
Gets a specific option from this translated string.
array
getOptions()
Gets all options from this translated string.
array
getArguments()
Gets all arguments from this translated string.
__sleep()
Magic __sleep() method to avoid serializing the string translator.
protected TranslationInterface
getStringTranslation()
Gets the string translation service.