class TranslationWrapper in Service Container 7
Same name and namespace in other branches
- 7.2 lib/Drupal/Core/StringTranslation/TranslationWrapper.php \Drupal\Core\StringTranslation\TranslationWrapper
Provides a class to wrap a translatable string.
This class can be used to delay translating strings until the translation system is ready. This is useful for using translation in very low level subsystems like entity definition and stream wrappers.
Hierarchy
- class \Drupal\Core\StringTranslation\TranslationWrapper
Expanded class hierarchy of TranslationWrapper
See also
\Drupal\Core\Annotation\Translation
2 files declare their use of TranslationWrapper
- RfcLogLevel.php in lib/
Drupal/ Core/ Logger/ RfcLogLevel.php - Contains \Drupal\Core\Logger\RfcLogLevel.
- Translation.php in lib/
Drupal/ Core/ Annotation/ Translation.php - Contains \Drupal\Core\Annotation\Translation.
File
- lib/
Drupal/ Core/ StringTranslation/ TranslationWrapper.php, line 19 - Contains \Drupal\Core\StringTranslation\TranslationWrapper.
Namespace
Drupal\Core\StringTranslationView source
class TranslationWrapper {
/**
* The string to be translated.
*
* @var string
*/
protected $string;
/**
* The translation arguments.
*
* @var array
*/
protected $arguments;
/**
* The translation options.
*
* @var array
*/
protected $options;
/**
* Constructs a new class instance.
*
* Parses values passed into this class through the t() function in Drupal and
* handles an optional context for the string.
*
* @param string $string
* The string that is to be translated.
* @param array $arguments
* (optional) An array with placeholder replacements, keyed by placeholder.
* @param array $options
* (optional) An array of additional options.
*/
public function __construct($string, array $arguments = array(), array $options = array()) {
$this->string = $string;
$this->arguments = $arguments;
$this->options = $options;
}
/**
* Gets the untranslated string value stored in this translation wrapper.
*
* @return string
* The string stored in this wrapper.
*/
public function getUntranslatedString() {
return $this->string;
}
/**
* Gets a specific option from this translation wrapper.
*
* @param $name
* Option name.
*
* @return mixed
* The value of this option or empty string of option is not set.
*/
public function getOption($name) {
return isset($this->options[$name]) ? $this->options[$name] : '';
}
/**
* Gets all options from this translation wrapper.
*
* @return mixed[]
* The array of options.
*/
public function getOptions() {
return $this->options;
}
/**
* Implements the magic __toString() method.
*/
public function __toString() {
return $this
->render();
}
/**
* Renders the object as a string.
*
* @return string
* The translated string.
*/
public function render() {
return $this
->t($this->string, $this->arguments, $this->options);
}
/**
* Magic __sleep() method to avoid serializing the string translator.
*/
public function __sleep() {
return array(
'string',
'arguments',
'options',
);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TranslationWrapper:: |
protected | property | The translation arguments. | |
TranslationWrapper:: |
protected | property | The translation options. | |
TranslationWrapper:: |
protected | property | The string to be translated. | |
TranslationWrapper:: |
public | function | Gets a specific option from this translation wrapper. | |
TranslationWrapper:: |
public | function | Gets all options from this translation wrapper. | |
TranslationWrapper:: |
public | function | Gets the untranslated string value stored in this translation wrapper. | |
TranslationWrapper:: |
public | function | Renders the object as a string. | |
TranslationWrapper:: |
public | function | Constructs a new class instance. | |
TranslationWrapper:: |
public | function | Magic __sleep() method to avoid serializing the string translator. | |
TranslationWrapper:: |
public | function | Implements the magic __toString() method. |