diff --git a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php index 63fe199..cd41d51 100644 --- a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php +++ b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Breadcrumb; +use Drupal\Core\StringTranslation\StringTranslationTrait; + /** * Defines a common base class for breadcrumb builders adding a link generator. * @@ -14,6 +16,8 @@ */ abstract class BreadcrumbBuilderBase implements BreadcrumbBuilderInterface { + use StringTranslationTrait; + /** * The link generator. * @@ -22,13 +26,6 @@ protected $linkGenerator; /** - * The translation manager. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * Returns the service container. * * @return \Symfony\Component\DependencyInjection\ContainerInterface $container @@ -64,26 +61,4 @@ protected function linkGenerator() { return $this->linkGenerator; } - /** - * Translates a string to the current language or to a given language. - * - * See the t() documentation for details. - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager()->translate($string, $args, $options); - } - - /** - * Returns the translation manager. - * - * @return \Drupal\Core\StringTranslation\TranslationInterface - * The translation manager. - */ - protected function translationManager() { - if (!$this->translationManager) { - $this->translationManager = $this->container()->get('string_translation'); - } - return $this->translationManager; - } - } diff --git a/core/lib/Drupal/Core/Controller/ControllerBase.php b/core/lib/Drupal/Core/Controller/ControllerBase.php index 7eb368a..56fc3e9 100644 --- a/core/lib/Drupal/Core/Controller/ControllerBase.php +++ b/core/lib/Drupal/Core/Controller/ControllerBase.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Controller; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -32,6 +33,8 @@ */ abstract class ControllerBase implements ContainerInjectionInterface { + use StringTranslationTrait; + /** * The entity manager. * @@ -54,13 +57,6 @@ protected $languageManager; /** - * The translation manager. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * The configuration factory. * * @var \Drupal\Core\Config\Config @@ -276,28 +272,6 @@ protected function currentUser() { } /** - * Translates a string to the current language or to a given language. - * - * See the t() documentation for details. - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager()->translate($string, $args, $options); - } - - /** - * Returns the translation manager. - * - * @return \Drupal\Core\StringTranslation\TranslationInterface - * The translation manager. - */ - protected function translationManager() { - if (!$this->translationManager) { - $this->translationManager = $this->container()->get('string_translation'); - } - return $this->translationManager; - } - - /** * Returns the language manager service. * * @return \Drupal\Core\Language\LanguageManager diff --git a/core/lib/Drupal/Core/Controller/TitleResolver.php b/core/lib/Drupal/Core/Controller/TitleResolver.php index 359af1d..39b82ec 100644 --- a/core/lib/Drupal/Core/Controller/TitleResolver.php +++ b/core/lib/Drupal/Core/Controller/TitleResolver.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Controller; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Route; @@ -16,6 +17,8 @@ */ class TitleResolver implements TitleResolverInterface { + use StringTranslationTrait; + /** * The controller resolver. * @@ -24,13 +27,6 @@ class TitleResolver implements TitleResolverInterface { protected $controllerResolver; /** - * The translation manager. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * Constructs a TitleResolver instance. * * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver @@ -62,7 +58,7 @@ public function getTitle(Request $request, Route $route) { $options['context'] = $context; } // Fall back to a static string from the route. - $route_title = $this->translationManager->translate($title, array(), $options); + $route_title = $this->t($title, array(), $options); } return $route_title; } diff --git a/core/lib/Drupal/Core/Entity/EntityControllerBase.php b/core/lib/Drupal/Core/Entity/EntityControllerBase.php index 7b60766..8fb39b1 100644 --- a/core/lib/Drupal/Core/Entity/EntityControllerBase.php +++ b/core/lib/Drupal/Core/Entity/EntityControllerBase.php @@ -9,6 +9,7 @@ use Drupal\Core\DependencyInjection\DependencySerialization; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; /** @@ -18,12 +19,7 @@ */ abstract class EntityControllerBase extends DependencySerialization { - /** - * The translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; + use StringTranslationTrait; /** * The module handler to invoke hooks on. @@ -33,32 +29,6 @@ protected $moduleHandler; /** - * Gets the translation manager. - * - * @return \Drupal\Core\StringTranslation\TranslationInterface - * The translation manager. - */ - protected function translationManager() { - if (!$this->translationManager) { - $this->translationManager = \Drupal::translation(); - } - return $this->translationManager; - } - - /** - * Sets the translation manager for this controller. - * - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager - * The translation manager. - * - * @return $this - */ - public function setTranslationManager(TranslationInterface $translation_manager) { - $this->translationManager = $translation_manager; - return $this; - } - - /** * Returns the module handler. * * @return \Drupal\Core\Extension\ModuleHandlerInterface diff --git a/core/lib/Drupal/Core/Entity/EntityListController.php b/core/lib/Drupal/Core/Entity/EntityListController.php index 0703f50..f1951bb 100644 --- a/core/lib/Drupal/Core/Entity/EntityListController.php +++ b/core/lib/Drupal/Core/Entity/EntityListController.php @@ -8,7 +8,6 @@ namespace Drupal\Core\Entity; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Component\Utility\String; @@ -186,15 +185,6 @@ public function render() { } /** - * Translates a string to the current language or to a given language. - * - * See the t() documentation for details. - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager()->translate($string, $args, $options); - } - - /** * Returns the title of the page. * * @return string diff --git a/core/lib/Drupal/Core/Form/FormBase.php b/core/lib/Drupal/Core/Form/FormBase.php index 93abb5b..fa5d2df 100644 --- a/core/lib/Drupal/Core/Form/FormBase.php +++ b/core/lib/Drupal/Core/Form/FormBase.php @@ -11,7 +11,7 @@ use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\DependencySerialization; use Drupal\Core\Routing\UrlGeneratorInterface; -use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; @@ -20,12 +20,7 @@ */ abstract class FormBase extends DependencySerialization implements FormInterface, ContainerInjectionInterface { - /** - * The translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; + use StringTranslationTrait; /** * The current request. @@ -70,15 +65,6 @@ public function validateForm(array &$form, array &$form_state) { } /** - * Translates a string to the current language or to a given language. - * - * See the t() documentation for details. - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager()->translate($string, $args, $options); - } - - /** * Generates a URL or path for a specific route based on the given parameters. * * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for @@ -92,19 +78,6 @@ public function url($route_name, $route_parameters = array(), $options = array() } /** - * Gets the translation manager. - * - * @return \Drupal\Core\StringTranslation\TranslationInterface - * The translation manager. - */ - protected function translationManager() { - if (!$this->translationManager) { - $this->translationManager = $this->container()->get('string_translation'); - } - return $this->translationManager; - } - - /** * Retrieves a configuration object. * * This is the main entry point to the configuration API. Calling @@ -128,19 +101,6 @@ protected function config($name) { } /** - * Sets the translation manager for this form. - * - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager - * The translation manager. - * - * @return $this - */ - public function setTranslationManager(TranslationInterface $translation_manager) { - $this->translationManager = $translation_manager; - return $this; - } - - /** * Sets the config factory for this form. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index 49692d9..49edc7c 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -18,6 +18,7 @@ use Drupal\Core\Render\Element; use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -32,6 +33,8 @@ */ class FormBuilder implements FormBuilderInterface { + use StringTranslationTrait; + /** * The module handler. * @@ -61,13 +64,6 @@ class FormBuilder implements FormBuilderInterface { protected $urlGenerator; /** - * The translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * The current request. * * @var \Symfony\Component\HttpFoundation\Request @@ -1779,15 +1775,6 @@ protected function currentUser() { } /** - * Translates a string to the current language or to a given language. - * - * See the t() documentation for details. - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager->translate($string, $args, $options); - } - - /** * {@inheritdoc} */ public function setRequest(Request $request) { diff --git a/core/lib/Drupal/Core/Plugin/PluginBase.php b/core/lib/Drupal/Core/Plugin/PluginBase.php index f0bce24..d672042 100644 --- a/core/lib/Drupal/Core/Plugin/PluginBase.php +++ b/core/lib/Drupal/Core/Plugin/PluginBase.php @@ -8,7 +8,7 @@ namespace Drupal\Core\Plugin; use Drupal\Component\Plugin\PluginBase as ComponentPluginBase; -use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -16,6 +16,8 @@ */ abstract class PluginBase extends ComponentPluginBase { + use StringTranslationTrait; + /** * An array of service IDs keyed by property name used for serialization. * @@ -27,49 +29,6 @@ protected $_serviceIds = array(); /** - * The translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** - * Translates a string to the current language or to a given language. - * - * See the t() documentation for details. - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager()->translate($string, $args, $options); - } - - /** - * Gets the translation manager. - * - * @return \Drupal\Core\StringTranslation\TranslationInterface - * The translation manager. - */ - protected function translationManager() { - if (!$this->translationManager) { - $this->translationManager = \Drupal::getContainer()->get('string_translation'); - } - return $this->translationManager; - } - - /** - * Sets the translation manager for this plugin. - * - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager - * The translation manager. - * - * @return self - * The plugin object. - */ - public function setTranslationManager(TranslationInterface $translation_manager) { - $this->translationManager = $translation_manager; - return $this; - } - - /** * {@inheritdoc} * * @todo Remove when Drupal\Core\DependencyInjection\DependencySerialization diff --git a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php new file mode 100644 index 0000000..7a53c68 --- /dev/null +++ b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php @@ -0,0 +1,54 @@ +getTranslationManager()->translate($string, $args, $options); + } + + /** + * Gets the translation manager. + * + * @return \Drupal\Core\StringTranslation\TranslationInterface + * The translation manager. + */ + protected function getTranslationManager() { + if (!$this->translationManager) { + $this->translationManager = \Drupal::service('string_translation'); + } + return $this->translationManager; + } + + /** + * Sets the string translation service to use. + * + * @param \Drupal\Core\StringTranslation\TranslationInterface $translation + * The string translation service. + */ + public function setTranslationManager(TranslationInterface $translation) { + $this->translationManager = $translation; + } + +} diff --git a/core/modules/block/lib/Drupal/block/Plugin/Type/BlockManager.php b/core/modules/block/lib/Drupal/block/Plugin/Type/BlockManager.php index a1187a6..5b793df 100644 --- a/core/modules/block/lib/Drupal/block/Plugin/Type/BlockManager.php +++ b/core/modules/block/lib/Drupal/block/Plugin/Type/BlockManager.php @@ -11,6 +11,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\LanguageManager; use Drupal\Core\Plugin\DefaultPluginManager; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; /** @@ -22,6 +23,8 @@ */ class BlockManager extends DefaultPluginManager { + use StringTranslationTrait; + /** * An array of all available modules and their data. * @@ -30,13 +33,6 @@ class BlockManager extends DefaultPluginManager { protected $moduleData; /** - * The translation manager. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * Constructs a new \Drupal\block\Plugin\Type\BlockManager object. * * @param \Traversable $namespaces @@ -99,15 +95,6 @@ protected function getModuleName($module) { } /** - * Translates a string to the current language or to a given language. - * - * See the t() documentation for details. - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager->translate($string, $args, $options); - } - - /** * Gets the names of all block categories. * * @return array diff --git a/core/modules/book/lib/Drupal/book/BookManager.php b/core/modules/book/lib/Drupal/book/BookManager.php index 8edd4d7..33ff7af 100644 --- a/core/modules/book/lib/Drupal/book/BookManager.php +++ b/core/modules/book/lib/Drupal/book/BookManager.php @@ -21,6 +21,8 @@ */ class BookManager { + use StringTranslationTrait; + /** * Database Service Object. * @@ -36,13 +38,6 @@ class BookManager { protected $entityManager; /** - * The translation service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translation; - - /** * Config Factory Service Object. * * @var \Drupal\Core\Config\ConfigFactoryInterface @@ -62,7 +57,7 @@ class BookManager { public function __construct(Connection $connection, EntityManagerInterface $entity_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory) { $this->connection = $connection; $this->entityManager = $entity_manager; - $this->translation = $translation; + $this->translationManager= $translation; $this->configFactory = $config_factory; } @@ -357,15 +352,6 @@ public function updateOutline(NodeInterface $node) { return FALSE; } -/** - * Translates a string to the current language or to a given language. - * - * See the t() documentation for details. - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translation->translate($string, $args, $options); - } - /** * Generates the corresponding menu name from a book ID. * diff --git a/core/modules/forum/lib/Drupal/forum/ForumManager.php b/core/modules/forum/lib/Drupal/forum/ForumManager.php index bcc7372..4182693 100644 --- a/core/modules/forum/lib/Drupal/forum/ForumManager.php +++ b/core/modules/forum/lib/Drupal/forum/ForumManager.php @@ -10,6 +10,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\comment\CommentInterface; use Drupal\field\FieldInfo; @@ -20,6 +21,8 @@ */ class ForumManager implements ForumManagerInterface { + use StringTranslationTrait; + /** * Forum sort order, newest first. */ @@ -104,13 +107,6 @@ class ForumManager implements ForumManagerInterface { protected $fieldInfo; /** - * Translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * Constructs the forum manager service. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory @@ -551,13 +547,4 @@ public function updateIndex($nid) { } } - /** - * Translates a string to the current language or to a given language. - * - * See the t() documentation for details. - */ - protected function t($string, array $args = array(), array $options = array()) { - return $this->translationManager->translate($string, $args, $options); - } - } diff --git a/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitMock.php b/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitMock.php new file mode 100644 index 0000000..1cfb280 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationTraitMock.php @@ -0,0 +1,19 @@ + 'String translation trait', + 'description' => 'Tests the string translation trait.', + 'group' => 'StringTranslation', + ); + } + + public function testT() { + $method = (new \ReflectionClass('\Drupal\Tests\Core\StringTranslation\StringTranslationTraitMock'))->getMethod('t'); + $method->setAccessible(TRUE); + + $translation = new StringTranslationTraitMock(); + $translation->setTranslationManager($this->getStringTranslationStub()); + $this->assertEquals('something', $method->invoke($translation, 'something')); + } + +}