You are here

trait TranslatableTrait in Backup and Migrate 8.4

This trait can be used to implement the TranslatableInterface.

A class with this trait will be able to use $this->t(...); to translate a string (if a translator is available).

Class TranslatableTrait

@package BackupMigrate\Core\Translation

Hierarchy

4 files declare their use of TranslatableTrait
ConfigurableTrait.php in lib/backup_migrate_core/src/Config/ConfigurableTrait.php
DrupalSiteArchiveSource.php in src/Source/DrupalSiteArchiveSource.php
DrupalUtils.php in src/Filter/DrupalUtils.php
FileNamer.php in lib/backup_migrate_core/src/Filter/FileNamer.php

File

lib/backup_migrate_core/src/Translation/TranslatableTrait.php, line 16

Namespace

BackupMigrate\Core\Translation
View source
trait TranslatableTrait {

  /**
   * @var TranslatorInterface;
   */
  protected $translator;

  /**
   * @param TranslatorInterface $translator
   */
  public function setTranslator($translator) {
    $this->translator = $translator;
  }

  /**
   * Translate the given string if there is a translator service available.
   *
   * @param $string
   * @param $replacements
   * @param $context
   *
   * @return mixed
   */
  public function t($string, $replacements = [], $context = []) {

    // If there is no translation service available use a passthrough to send
    // back the original (en-us) string.
    if (empty($this->translator)) {
      $this->translator = new PassthroughTranslator();
    }
    return $this->translator
      ->translate($string, $replacements, $context);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TranslatableTrait::$translator protected property
TranslatableTrait::setTranslator public function
TranslatableTrait::t public function Translate the given string if there is a translator service available.