trait TranslatableTrait in Backup and Migrate 5.0.x
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).
@package Drupal\backup_migrate\Core\Translation
Hierarchy
- trait \Drupal\backup_migrate\Core\Translation\TranslatableTrait
1 file declares its use of TranslatableTrait
- ConfigurableTrait.php in src/
Core/ Config/ ConfigurableTrait.php
File
- src/
Core/ Translation/ TranslatableTrait.php, line 14
Namespace
Drupal\backup_migrate\Core\TranslationView source
trait TranslatableTrait {
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* @param TranslatorInterface $translator
*/
public function setTranslator(TranslatorInterface $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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TranslatableTrait:: |
protected | property | ||
TranslatableTrait:: |
public | function | ||
TranslatableTrait:: |
public | function | Translate the given string if there is a translator service available. |