You are here

class TranslationWrapper in Service Container 7.2

Same name and namespace in other branches
  1. 7 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

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\StringTranslation
View 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

Namesort descending Modifiers Type Description Overrides
TranslationWrapper::$arguments protected property The translation arguments.
TranslationWrapper::$options protected property The translation options.
TranslationWrapper::$string protected property The string to be translated.
TranslationWrapper::getOption public function Gets a specific option from this translation wrapper.
TranslationWrapper::getOptions public function Gets all options from this translation wrapper.
TranslationWrapper::getUntranslatedString public function Gets the untranslated string value stored in this translation wrapper.
TranslationWrapper::render public function Renders the object as a string.
TranslationWrapper::__construct public function Constructs a new class instance.
TranslationWrapper::__sleep public function Magic __sleep() method to avoid serializing the string translator.
TranslationWrapper::__toString public function Implements the magic __toString() method.