diff --git a/core/core.services.yml b/core/core.services.yml index e8b6464..31a3818 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -421,7 +421,7 @@ services: - { name: event_subscriber } controller.page: class: Drupal\Core\Controller\HtmlPageController - arguments: ['@controller_resolver', '@string_translation', '@title_resolver'] + arguments: ['@controller_resolver', '@title_resolver'] controller.ajax: class: Drupal\Core\Controller\AjaxController arguments: ['@controller_resolver', '@ajax_response_renderer'] @@ -549,7 +549,7 @@ services: arguments: ['@config.manager', '@config.storage', '@config.storage.snapshot'] exception_controller: class: Drupal\Core\Controller\ExceptionController - arguments: ['@content_negotiation', '@string_translation', '@title_resolver', '@html_page_renderer', '@html_fragment_renderer'] + arguments: ['@content_negotiation', '@title_resolver', '@html_page_renderer', '@html_fragment_renderer'] calls: - [setContainer, ['@service_container']] exception_listener: diff --git a/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php b/core/lib/Drupal/Core/Breadcrumb/BreadcrumbBuilderBase.php index 63fe199..8b7da7a 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\StringTranslationAwareTrait; + /** * Defines a common base class for breadcrumb builders adding a link generator. * @@ -14,6 +16,8 @@ */ abstract class BreadcrumbBuilderBase implements BreadcrumbBuilderInterface { + use StringTranslationAwareTrait; + /** * 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..0580f11 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\StringTranslationAwareTrait; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -32,6 +33,8 @@ */ abstract class ControllerBase implements ContainerInjectionInterface { + use StringTranslationAwareTrait; + /** * 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/ExceptionController.php b/core/lib/Drupal/Core/Controller/ExceptionController.php index d79567c..6e76617 100644 --- a/core/lib/Drupal/Core/Controller/ExceptionController.php +++ b/core/lib/Drupal/Core/Controller/ExceptionController.php @@ -8,7 +8,6 @@ namespace Drupal\Core\Controller; use Drupal\Core\Page\HtmlPageRendererInterface; -use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; @@ -59,8 +58,6 @@ class ExceptionController extends HtmlControllerBase implements ContainerAwareIn * @param \Drupal\Core\ContentNegotiation $negotiation * The content negotiation library to use to determine the correct response * format. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager - * The translation manager. * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver * The title resolver. * @param \Drupal\Core\Page\HtmlPageRendererInterface $renderer @@ -68,8 +65,8 @@ class ExceptionController extends HtmlControllerBase implements ContainerAwareIn * @param \Drupal\Core\Page\HtmlFragmentRendererInterface $fragment_renderer * The fragment rendering service. */ - public function __construct(ContentNegotiation $negotiation, TranslationInterface $translation_manager, TitleResolverInterface $title_resolver, HtmlPageRendererInterface $renderer, $fragment_renderer) { - parent::__construct($translation_manager, $title_resolver); + public function __construct(ContentNegotiation $negotiation, TitleResolverInterface $title_resolver, HtmlPageRendererInterface $renderer, $fragment_renderer) { + parent::__construct($title_resolver); $this->negotiation = $negotiation; $this->htmlPageRenderer = $renderer; $this->fragmentRenderer = $fragment_renderer; diff --git a/core/lib/Drupal/Core/Controller/HtmlControllerBase.php b/core/lib/Drupal/Core/Controller/HtmlControllerBase.php index 0204bc0..6d71b79 100644 --- a/core/lib/Drupal/Core/Controller/HtmlControllerBase.php +++ b/core/lib/Drupal/Core/Controller/HtmlControllerBase.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Controller; -use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\Page\HtmlFragment; use Drupal\Core\Utility\Title; use Symfony\Cmf\Component\Routing\RouteObjectInterface; @@ -20,13 +19,6 @@ class HtmlControllerBase { /** - * The translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * The title resolver. * * @var \Drupal\Core\Controller\TitleResolver @@ -36,13 +28,10 @@ class HtmlControllerBase { /** * Constructs a new HtmlControllerBase object. * - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager - * The translation manager. * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver * The title resolver. */ - public function __construct(TranslationInterface $translation_manager, TitleResolverInterface $title_resolver) { - $this->translationManager = $translation_manager; + public function __construct(TitleResolverInterface $title_resolver) { $this->titleResolver = $title_resolver; } diff --git a/core/lib/Drupal/Core/Controller/HtmlPageController.php b/core/lib/Drupal/Core/Controller/HtmlPageController.php index 1672939..6ed82f1 100644 --- a/core/lib/Drupal/Core/Controller/HtmlPageController.php +++ b/core/lib/Drupal/Core/Controller/HtmlPageController.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Controller; -use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\HttpFoundation\Request; /** @@ -27,13 +26,11 @@ class HtmlPageController extends HtmlControllerBase { * * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver * The controller resolver. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager - * The translation manager. * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver * The title resolver. */ - public function __construct(ControllerResolverInterface $controller_resolver, TranslationInterface $translation_manager, TitleResolverInterface $title_resolver) { - parent::__construct($translation_manager, $title_resolver); + public function __construct(ControllerResolverInterface $controller_resolver, TitleResolverInterface $title_resolver) { + parent::__construct($title_resolver); $this->controllerResolver = $controller_resolver; } diff --git a/core/lib/Drupal/Core/Controller/TitleResolver.php b/core/lib/Drupal/Core/Controller/TitleResolver.php index 359af1d..e102596 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\StringTranslationAwareTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Route; @@ -16,6 +17,8 @@ */ class TitleResolver implements TitleResolverInterface { + use StringTranslationAwareTrait; + /** * The controller resolver. * @@ -24,23 +27,16 @@ 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 * The controller resolver. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The translation manager. */ - public function __construct(ControllerResolverInterface $controller_resolver, TranslationInterface $translation_manager) { + public function __construct(ControllerResolverInterface $controller_resolver, TranslationInterface $string_translation) { $this->controllerResolver = $controller_resolver; - $this->translationManager = $translation_manager; + $this->stringTranslation = $string_translation; } /** @@ -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/Datetime/Date.php b/core/lib/Drupal/Core/Datetime/Date.php index 5c2b92b..6c2851b 100644 --- a/core/lib/Drupal/Core/Datetime/Date.php +++ b/core/lib/Drupal/Core/Datetime/Date.php @@ -14,12 +14,15 @@ use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\Core\StringTranslation\StringTranslationAwareTrait; /** * Provides a service to handler various date related functionality. */ class Date { + use StringTranslationAwareTrait; + /** * The list of loaded timezones. * @@ -181,7 +184,7 @@ public function formatInterval($interval, $granularity = 2, $langcode = NULL) { foreach ($this->units as $key => $value) { $key = explode('|', $key); if ($interval >= $value) { - $output .= ($output ? ' ' : '') . $this->stringTranslation->formatPlural(floor($interval / $value), $key[0], $key[1], array(), array('langcode' => $langcode)); + $output .= ($output ? ' ' : '') . $this->formatPlural(floor($interval / $value), $key[0], $key[1], array(), array('langcode' => $langcode)); $interval %= $value; $granularity--; } @@ -194,15 +197,6 @@ public function formatInterval($interval, $granularity = 2, $langcode = NULL) { } /** - * 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->stringTranslation->translate($string, $args, $options); - } - - /** * Loads the given format pattern for the given langcode. * * @param string $format diff --git a/core/lib/Drupal/Core/Entity/EntityControllerBase.php b/core/lib/Drupal/Core/Entity/EntityControllerBase.php index 7b60766..364559f 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\StringTranslationAwareTrait; 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 StringTranslationAwareTrait; /** * 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/EntityFormControllerInterface.php b/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php index e63679b..0e025e5 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php @@ -154,14 +154,14 @@ public function validate(array $form, array &$form_state); public function submit(array $form, array &$form_state); /** - * Sets the translation manager for this form. + * Sets the string translation service for this form. * - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The translation manager. * * @return $this */ - public function setTranslationManager(TranslationInterface $translation_manager); + public function setStringTranslation(TranslationInterface $string_translation); /** * Sets the module handler for this form. 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/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 5684a9d..f639517 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -213,7 +213,7 @@ public function getFormController($entity_type, $operation) { } $controller - ->setTranslationManager($this->translationManager) + ->setStringTranslation($this->translationManager) ->setModuleHandler($this->moduleHandler) ->setOperation($operation); $this->controllers['form'][$operation][$entity_type] = $controller; @@ -271,8 +271,8 @@ public function getController($entity_type, $controller_type, $controller_class_ if (method_exists($controller, 'setModuleHandler')) { $controller->setModuleHandler($this->moduleHandler); } - if (method_exists($controller, 'setTranslationManager')) { - $controller->setTranslationManager($this->translationManager); + if (method_exists($controller, 'setStringTranslation')) { + $controller->setStringTranslation($this->translationManager); } $this->controllers[$controller_type][$entity_type] = $controller; } diff --git a/core/lib/Drupal/Core/Form/FormBase.php b/core/lib/Drupal/Core/Form/FormBase.php index 93abb5b..cbd8c6b 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\StringTranslationAwareTrait; 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 StringTranslationAwareTrait; /** * 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..8a4336e 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\StringTranslationAwareTrait; use Drupal\Core\Url; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -32,6 +33,8 @@ */ class FormBuilder implements FormBuilderInterface { + use StringTranslationAwareTrait; + /** * 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 @@ -129,19 +125,19 @@ class FormBuilder implements FormBuilderInterface { * The event dispatcher. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator * The URL generator. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The translation manager. * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token * The CSRF token generator. * @param \Drupal\Core\HttpKernel $http_kernel * The HTTP kernel. */ - public function __construct(ModuleHandlerInterface $module_handler, KeyValueExpirableFactoryInterface $key_value_expirable_factory, EventDispatcherInterface $event_dispatcher, UrlGeneratorInterface $url_generator, TranslationInterface $translation_manager, CsrfTokenGenerator $csrf_token = NULL, HttpKernel $http_kernel = NULL) { + public function __construct(ModuleHandlerInterface $module_handler, KeyValueExpirableFactoryInterface $key_value_expirable_factory, EventDispatcherInterface $event_dispatcher, UrlGeneratorInterface $url_generator, TranslationInterface $string_translation, CsrfTokenGenerator $csrf_token = NULL, HttpKernel $http_kernel = NULL) { $this->moduleHandler = $module_handler; $this->keyValueExpirableFactory = $key_value_expirable_factory; $this->eventDispatcher = $event_dispatcher; $this->urlGenerator = $url_generator; - $this->translationManager = $translation_manager; + $this->stringTranslation = $string_translation; $this->csrfToken = $csrf_token; $this->httpKernel = $http_kernel; } @@ -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/ContextAwarePluginBase.php b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php index 667bebe..0df1cbb 100644 --- a/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php +++ b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php @@ -11,7 +11,7 @@ use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Core\Plugin\Context\Context; use Drupal\Component\Plugin\Discovery\DiscoveryInterface; -use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\Core\StringTranslation\StringTranslationAwareTrait; /** * Drupal specific class for plugins that use context. @@ -22,6 +22,8 @@ */ abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase { + use StringTranslationAwareTrait; + /** * Override of \Drupal\Component\Plugin\ContextAwarePluginBase::__construct(). */ @@ -55,47 +57,4 @@ public function setContextValue($name, $value) { return $this; } - /** - * 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::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; - } - } diff --git a/core/lib/Drupal/Core/Plugin/PluginBase.php b/core/lib/Drupal/Core/Plugin/PluginBase.php index f0bce24..86ae503 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\StringTranslationAwareTrait; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -16,6 +16,8 @@ */ abstract class PluginBase extends ComponentPluginBase { + use StringTranslationAwareTrait; + /** * 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/StringTranslationAwareTrait.php b/core/lib/Drupal/Core/StringTranslation/StringTranslationAwareTrait.php new file mode 100644 index 0000000..675d82c --- /dev/null +++ b/core/lib/Drupal/Core/StringTranslation/StringTranslationAwareTrait.php @@ -0,0 +1,76 @@ +getStringTranslation()->translate($string, $args, $options); + } + + /** + * Formats a string containing a count of items. + * + * See \Drupal\Core\StringTranslation::formatPlural() for details. + */ + protected function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()) { + return $this->getStringTranslation()->formatPlural($count, $singular, $plural, $args, $options); + } + + /** + * Gets the string translation service. + * + * @return \Drupal\Core\StringTranslation\TranslationInterface + * The string translation service. + */ + protected function getStringTranslation() { + if (!$this->stringTranslation) { + $this->stringTranslation = \Drupal::service('string_translation'); + } + + return $this->stringTranslation; + } + + /** + * Sets the string translation service to use. + * + * @param \Drupal\Core\StringTranslation\TranslationInterface $translation + * The string translation service. + * + * @return $this + */ + public function setStringTranslation(TranslationInterface $translation) { + $this->stringTranslation = $translation; + + return $this; + } + +} diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Form/SettingsForm.php b/core/modules/aggregator/lib/Drupal/aggregator/Form/SettingsForm.php index 29376bc..ea77c2b 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Form/SettingsForm.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Form/SettingsForm.php @@ -56,12 +56,12 @@ class SettingsForm extends ConfigFormBase { * The aggregator parser plugin manager. * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $processor_manager * The aggregator processor plugin manager. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The string translation manager. */ - public function __construct(ConfigFactoryInterface $config_factory, AggregatorPluginManager $fetcher_manager, AggregatorPluginManager $parser_manager, AggregatorPluginManager $processor_manager, TranslationInterface $translation_manager) { + public function __construct(ConfigFactoryInterface $config_factory, AggregatorPluginManager $fetcher_manager, AggregatorPluginManager $parser_manager, AggregatorPluginManager $processor_manager, TranslationInterface $string_translation) { parent::__construct($config_factory); - $this->translationManager = $translation_manager; + $this->stringTranslation = $string_translation; $this->managers = array( 'fetcher' => $fetcher_manager, 'parser' => $parser_manager, 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..3eecf80 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\StringTranslationAwareTrait; use Drupal\Core\StringTranslation\TranslationInterface; /** @@ -22,6 +23,8 @@ */ class BlockManager extends DefaultPluginManager { + use StringTranslationAwareTrait; + /** * 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 @@ -48,15 +44,15 @@ class BlockManager extends DefaultPluginManager { * The language manager. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler to invoke the alter hook with. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The translation manager. */ - public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler, TranslationInterface $translation_manager) { + public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler, TranslationInterface $string_translation) { parent::__construct('Plugin/Block', $namespaces, 'Drupal\block\Annotation\Block'); $this->alterInfo($module_handler, 'block'); $this->setCacheBackend($cache_backend, $language_manager, 'block_plugins'); - $this->translationManager = $translation_manager; + $this->stringTranslation = $string_translation; } /** @@ -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..a151061 100644 --- a/core/modules/book/lib/Drupal/book/BookManager.php +++ b/core/modules/book/lib/Drupal/book/BookManager.php @@ -12,6 +12,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Language\Language; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\StringTranslationAwareTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\node\NodeInterface; @@ -21,6 +22,8 @@ */ class BookManager { + use StringTranslationAwareTrait; + /** * Database Service Object. * @@ -36,13 +39,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 +58,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->stringTranslation= $translation; $this->configFactory = $config_factory; } @@ -357,15 +353,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/comment/lib/Drupal/comment/CommentManager.php b/core/modules/comment/lib/Drupal/comment/CommentManager.php index 2102abe..444abe4 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentManager.php +++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php @@ -13,6 +13,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\StringTranslationAwareTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\field\FieldInfo; @@ -21,6 +22,8 @@ */ class CommentManager implements CommentManagerInterface { + use StringTranslationAwareTrait; + /** * The field info service. * @@ -57,13 +60,6 @@ class CommentManager implements CommentManagerInterface { protected $userConfig; /** - * The string translation service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * The url generator service. * * @var \Drupal\Core\Routing\UrlGeneratorInterface @@ -81,17 +77,17 @@ class CommentManager implements CommentManagerInterface { * The current user. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The string translation service. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator * The url generator service. */ - public function __construct(FieldInfo $field_info, EntityManagerInterface $entity_manager, AccountInterface $current_user, ConfigFactoryInterface $config_factory, TranslationInterface $translation_manager, UrlGeneratorInterface $url_generator) { + public function __construct(FieldInfo $field_info, EntityManagerInterface $entity_manager, AccountInterface $current_user, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator) { $this->fieldInfo = $field_info; $this->entityManager = $entity_manager; $this->currentUser = $current_user; $this->userConfig = $config_factory->get('user.settings'); - $this->translationManager = $translation_manager; + $this->stringTranslation = $string_translation; $this->urlGenerator = $url_generator; } @@ -294,13 +290,4 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) { return ''; } - /** - * 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/modules/config_translation/lib/Drupal/config_translation/ConfigNamesMapper.php b/core/modules/config_translation/lib/Drupal/config_translation/ConfigNamesMapper.php index 8198a47..d02f7a9 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/ConfigNamesMapper.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/ConfigNamesMapper.php @@ -81,14 +81,14 @@ class ConfigNamesMapper extends PluginBase implements ConfigMapperInterface, Con * The mapper plugin discovery service. * @param \Drupal\Core\Routing\RouteProviderInterface * The route provider. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The string translation manager. * * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException * Throws an exception if the route specified by the 'base_route_name' in * the plugin definition could not be found by the route provider. */ - public function __construct($plugin_id, array $plugin_definition, ConfigFactoryInterface $config_factory, LocaleConfigManager $locale_config_manager, ConfigMapperManagerInterface $config_mapper_manager, RouteProviderInterface $route_provider, TranslationInterface $translation_manager) { + public function __construct($plugin_id, array $plugin_definition, ConfigFactoryInterface $config_factory, LocaleConfigManager $locale_config_manager, ConfigMapperManagerInterface $config_mapper_manager, RouteProviderInterface $route_provider, TranslationInterface $string_translation) { $this->pluginId = $plugin_id; $this->pluginDefinition = $plugin_definition; @@ -96,7 +96,7 @@ public function __construct($plugin_id, array $plugin_definition, ConfigFactoryI $this->localeConfigManager = $locale_config_manager; $this->configMapperManager = $config_mapper_manager; - $this->setTranslationManager($translation_manager); + $this->stringTranslation = $string_translation; $this->baseRoute = $route_provider->getRouteByName($this->getBaseRouteName()); } diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/DateFormat.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/DateFormat.php index 3cc9867..da0701f 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/DateFormat.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/DateFormat.php @@ -11,11 +11,14 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\ReplaceCommand; use Drupal\Core\Language\Language; +use Drupal\Core\StringTranslation\StringTranslationAwareTrait; /** * Defines the date format element for the configuration translation interface. */ -class DateFormat extends Element { +class DateFormat implements ElementInterface { + + use StringTranslationAwareTrait; /** * {@inheritdoc} diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Element.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Element.php deleted file mode 100644 index 40a43c2..0000000 --- a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Element.php +++ /dev/null @@ -1,44 +0,0 @@ -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 = \Drupal::translation(); - } - return $this->translationManager; - } - -} diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textarea.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textarea.php index e79107e..02d70de 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textarea.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textarea.php @@ -8,11 +8,14 @@ namespace Drupal\config_translation\FormElement; use Drupal\Core\Language\Language; +use Drupal\Core\StringTranslation\StringTranslationAwareTrait; /** * Defines the textarea element for the configuration translation interface. */ -class Textarea extends Element { +class Textarea implements ElementInterface { + + use StringTranslationAwareTrait; /** * {@inheritdoc} diff --git a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textfield.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textfield.php index 1791961..1e6794c 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textfield.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/Textfield.php @@ -8,11 +8,14 @@ namespace Drupal\config_translation\FormElement; use Drupal\Core\Language\Language; +use Drupal\Core\StringTranslation\StringTranslationAwareTrait; /** * Defines the textfield element for the configuration translation interface. */ -class Textfield extends Element { +class Textfield implements ElementInterface { + + use StringTranslationAwareTrait; /** * {@inheritdoc} diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Plugin/Derivative/FieldUiLocalTask.php b/core/modules/field_ui/lib/Drupal/field_ui/Plugin/Derivative/FieldUiLocalTask.php index e80bd9d..27a1aee 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Plugin/Derivative/FieldUiLocalTask.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Plugin/Derivative/FieldUiLocalTask.php @@ -11,6 +11,7 @@ use Drupal\Component\Plugin\Derivative\DerivativeBase; use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface; use Drupal\Core\Routing\RouteProviderInterface; +use Drupal\Core\StringTranslation\StringTranslationAwareTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -19,6 +20,8 @@ */ class FieldUiLocalTask extends DerivativeBase implements ContainerDerivativeInterface { + use StringTranslationAwareTrait; + /** * The route provider. * @@ -34,26 +37,19 @@ class FieldUiLocalTask extends DerivativeBase implements ContainerDerivativeInte protected $entityManager; /** - * The translation manager service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * Creates an FieldUiLocalTask object. * * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider * The route provider. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The translation manager. */ - public function __construct(RouteProviderInterface $route_provider, EntityManagerInterface $entity_manager, TranslationInterface $translation_manager) { + public function __construct(RouteProviderInterface $route_provider, EntityManagerInterface $entity_manager, TranslationInterface $string_translation) { $this->routeProvider = $route_provider; $this->entityManager = $entity_manager; - $this->translationManager = $translation_manager; + $this->stringTranslation = $string_translation; } /** @@ -194,13 +190,4 @@ public function alterLocalTasks(&$local_tasks) { } } - /** - * 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/modules/forum/lib/Drupal/forum/ForumManager.php b/core/modules/forum/lib/Drupal/forum/ForumManager.php index bcc7372..9f993b7 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\StringTranslationAwareTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\comment\CommentInterface; use Drupal\field\FieldInfo; @@ -20,6 +21,8 @@ */ class ForumManager implements ForumManagerInterface { + use StringTranslationAwareTrait; + /** * 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 @@ -121,15 +117,15 @@ class ForumManager implements ForumManagerInterface { * The current database connection. * @param \Drupal\field\FieldInfo $field_info * The field info service. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The translation manager service. */ - public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $translation_manager) { + public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $string_translation) { $this->configFactory = $config_factory; $this->entityManager = $entity_manager; $this->connection = $connection; $this->fieldInfo = $field_info; - $this->translationManager = $translation_manager; + $this->stringTranslation = $string_translation; } /** @@ -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/modules/migrate/lib/Drupal/migrate/MigrateExecutable.php b/core/modules/migrate/lib/Drupal/migrate/MigrateExecutable.php index 104f187..17a4cde 100644 --- a/core/modules/migrate/lib/Drupal/migrate/MigrateExecutable.php +++ b/core/modules/migrate/lib/Drupal/migrate/MigrateExecutable.php @@ -8,6 +8,7 @@ namespace Drupal\migrate; use Drupal\Core\Utility\Error; +use Drupal\Core\StringTranslation\StringTranslationAwareTrait; use Drupal\migrate\Entity\MigrationInterface; use Drupal\migrate\Plugin\MigrateIdMapInterface; @@ -16,6 +17,8 @@ */ class MigrateExecutable { + use StringTranslationAwareTrait; + /** * The configuration of the migration to do. * @@ -106,13 +109,6 @@ class MigrateExecutable { protected $memoryLimit; /** - * The translation manager. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * The rollback action to be saved for the current row. * * @var int @@ -602,26 +598,4 @@ public function handleException(\Exception $exception, $save = TRUE) { $this->message->display($message); } - /** - * 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::translation(); - } - return $this->translationManager; - } - } diff --git a/core/modules/migrate/tests/Drupal/migrate/Tests/MigrateExecutableTest.php b/core/modules/migrate/tests/Drupal/migrate/Tests/MigrateExecutableTest.php index cd54fd9..f825287 100644 --- a/core/modules/migrate/tests/Drupal/migrate/Tests/MigrateExecutableTest.php +++ b/core/modules/migrate/tests/Drupal/migrate/Tests/MigrateExecutableTest.php @@ -71,7 +71,7 @@ protected function setUp() { $this->message = $this->getMock('Drupal\migrate\MigrateMessageInterface'); $this->executable = new TestMigrateExecutable($this->migration, $this->message); - $this->executable->setTranslationManager($this->getStringTranslationStub()); + $this->executable->setStringTranslation($this->getStringTranslationStub()); } /** diff --git a/core/modules/migrate/tests/Drupal/migrate/Tests/MigrateExecuteableMemoryExceededTest.php b/core/modules/migrate/tests/Drupal/migrate/Tests/MigrateExecuteableMemoryExceededTest.php index d69873f..b14069a 100644 --- a/core/modules/migrate/tests/Drupal/migrate/Tests/MigrateExecuteableMemoryExceededTest.php +++ b/core/modules/migrate/tests/Drupal/migrate/Tests/MigrateExecuteableMemoryExceededTest.php @@ -77,7 +77,7 @@ protected function setUp() { $this->message = $this->getMock('Drupal\migrate\MigrateMessageInterface'); $this->executable = new TestMigrateExecutable($this->migration, $this->message); - $this->executable->setTranslationManager($this->getStringTranslationStub()); + $this->executable->setStringTranslation($this->getStringTranslationStub()); } /** diff --git a/core/modules/migrate/tests/Drupal/migrate/Tests/TestMigrateExecutable.php b/core/modules/migrate/tests/Drupal/migrate/Tests/TestMigrateExecutable.php index 1a222c7..2ceaff1 100644 --- a/core/modules/migrate/tests/Drupal/migrate/Tests/TestMigrateExecutable.php +++ b/core/modules/migrate/tests/Drupal/migrate/Tests/TestMigrateExecutable.php @@ -37,13 +37,13 @@ class TestMigrateExecutable extends MigrateExecutable { protected $clearedMemoryUsage; /** - * Sets the translation manager. + * Sets the string translation service. * - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The translation manager. */ - public function setTranslationManager(TranslationInterface $translation_manager) { - $this->translationManager = $translation_manager; + public function setStringTranslation(TranslationInterface $string_translation) { + $this->stringTranslation = $string_translation; } /** diff --git a/core/modules/system/lib/Drupal/system/Controller/SystemController.php b/core/modules/system/lib/Drupal/system/Controller/SystemController.php index 5f55d6e..d5c3617 100644 --- a/core/modules/system/lib/Drupal/system/Controller/SystemController.php +++ b/core/modules/system/lib/Drupal/system/Controller/SystemController.php @@ -301,10 +301,10 @@ public function themesPage() { // There are two possible theme groups. $theme_group_titles = array( - 'enabled' => $this->translationManager()->formatPlural(count($theme_groups['enabled']), 'Enabled theme', 'Enabled themes'), + 'enabled' => $this->formatPlural(count($theme_groups['enabled']), 'Enabled theme', 'Enabled themes'), ); if (!empty($theme_groups['disabled'])) { - $theme_group_titles['disabled'] = $this->translationManager()->formatPlural(count($theme_groups['disabled']), 'Disabled theme', 'Disabled themes'); + $theme_group_titles['disabled'] = $this->formatPlural(count($theme_groups['disabled']), 'Disabled theme', 'Disabled themes'); } uasort($theme_groups['enabled'], 'system_sort_themes'); diff --git a/core/modules/system/lib/Drupal/system/Plugin/views/field/BulkForm.php b/core/modules/system/lib/Drupal/system/Plugin/views/field/BulkForm.php index ad784e3..66ce4bb 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/views/field/BulkForm.php +++ b/core/modules/system/lib/Drupal/system/Plugin/views/field/BulkForm.php @@ -269,7 +269,7 @@ public function viewsFormSubmit(&$form, &$form_state) { $count = count(array_filter($form_state['values'][$this->options['id']])); $action = $this->actions[$form_state['values']['action']]; if ($count) { - drupal_set_message($this->translationManager()->formatPlural($count, '%action was applied to @count item.', '%action was applied to @count items.', array( + drupal_set_message($this->formatPlural($count, '%action was applied to @count item.', '%action was applied to @count items.', array( '%action' => $action->label(), ))); } diff --git a/core/modules/update/lib/Drupal/update/UpdateManager.php b/core/modules/update/lib/Drupal/update/UpdateManager.php index 8c7c48a..a97e87a 100644 --- a/core/modules/update/lib/Drupal/update/UpdateManager.php +++ b/core/modules/update/lib/Drupal/update/UpdateManager.php @@ -10,6 +10,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; use Drupal\Core\StringTranslation\TranslationInterface; +use Drupal\Core\StringTranslation\StringTranslationAwareTrait; use Drupal\Core\Utility\ProjectInfo; /** @@ -17,6 +18,8 @@ */ class UpdateManager implements UpdateManagerInterface { + use StringTranslationAwareTrait; + /** * The update settings * @@ -60,13 +63,6 @@ class UpdateManager implements UpdateManagerInterface { protected $availableReleasesTempStore; /** - * The translation service. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translation; - - /** * Constructs a UpdateManager. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory @@ -84,7 +80,7 @@ public function __construct(ConfigFactoryInterface $config_factory, ModuleHandle $this->updateSettings = $config_factory->get('update.settings'); $this->moduleHandler = $module_handler; $this->updateProcessor = $update_processor; - $this->translation = $translation; + $this->stringTranslation = $translation; $this->keyValueStore = $key_value_expirable_factory->get('update'); $this->availableReleasesTempStore = $key_value_expirable_factory->get('update_available_releases'); $this->projects = array(); @@ -215,13 +211,4 @@ public function fetchDataBatch(&$context) { } } - /** - * 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); - } - } diff --git a/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsEntityArgumentValidator.php b/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsEntityArgumentValidator.php index eb2f383..2fd7d6f 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsEntityArgumentValidator.php +++ b/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsEntityArgumentValidator.php @@ -10,6 +10,7 @@ use Drupal\Component\Plugin\Derivative\DerivativeBase; use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\StringTranslation\StringTranslationAwareTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -21,6 +22,9 @@ * @see \Drupal\views\Plugin\views\argument_validator\Entity */ class ViewsEntityArgumentValidator extends DerivativeBase implements ContainerDerivativeInterface { + + use StringTranslationAwareTrait; + /** * The base plugin ID this derivative is for. * @@ -36,13 +40,6 @@ class ViewsEntityArgumentValidator extends DerivativeBase implements ContainerDe protected $entityManager; /** - * The string translation. - * - * @var \Drupal\Core\StringTranslation\TranslationInterface - */ - protected $translationManager; - - /** * List of derivative definitions. * * @var array @@ -56,13 +53,13 @@ class ViewsEntityArgumentValidator extends DerivativeBase implements ContainerDe * The base plugin ID. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. - * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The string translation. */ - public function __construct($base_plugin_id, EntityManagerInterface $entity_manager, TranslationInterface $translation_manager) { + public function __construct($base_plugin_id, EntityManagerInterface $entity_manager, TranslationInterface $string_translation) { $this->basePluginId = $base_plugin_id; $this->entityManager = $entity_manager; - $this->translationManager = $translation_manager; + $this->stringTranslation = $string_translation; } /** @@ -96,13 +93,4 @@ public function getDerivativeDefinitions(array $base_plugin_definition) { return $this->derivatives; } - /** - * 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/modules/views_ui/tests/Drupal/views_ui/Tests/ViewListControllerTest.php b/core/modules/views_ui/tests/Drupal/views_ui/Tests/ViewListControllerTest.php index 534f824..43977c9 100644 --- a/core/modules/views_ui/tests/Drupal/views_ui/Tests/ViewListControllerTest.php +++ b/core/modules/views_ui/tests/Drupal/views_ui/Tests/ViewListControllerTest.php @@ -125,7 +125,7 @@ public function testBuildRowEntityList() { // because t() is called on there. $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface'); $view_list_controller = new TestViewListController($entity_type, $storage_controller, $display_manager); - $view_list_controller->setTranslationManager($this->getStringTranslationStub()); + $view_list_controller->setStringTranslation($this->getStringTranslationStub()); $view = new View($values, 'view'); diff --git a/core/tests/Drupal/Tests/Core/Controller/ExceptionControllerTest.php b/core/tests/Drupal/Tests/Core/Controller/ExceptionControllerTest.php index 0c5f4e7..7597dea 100644 --- a/core/tests/Drupal/Tests/Core/Controller/ExceptionControllerTest.php +++ b/core/tests/Drupal/Tests/Core/Controller/ExceptionControllerTest.php @@ -35,7 +35,6 @@ public static function getInfo() { public function test405HTML() { $exception = new \Exception('Test exception'); $flat_exception = FlattenException::create($exception, 405); - $translation_manager = $this->getStringTranslationStub(); $html_page_renderer = $this->getMock('Drupal\Core\Page\HtmlPageRendererInterface'); $html_fragment_renderer = $this->getMock('Drupal\Core\Page\HtmlFragmentRendererInterface'); $title_resolver = $this->getMock('Drupal\Core\Controller\TitleResolverInterface'); @@ -45,7 +44,7 @@ public function test405HTML() { ->method('getContentType') ->will($this->returnValue('html')); - $exception_controller = new ExceptionController($content_negotiation, $translation_manager, $title_resolver, $html_page_renderer, $html_fragment_renderer); + $exception_controller = new ExceptionController($content_negotiation, $title_resolver, $html_page_renderer, $html_fragment_renderer); $response = $exception_controller->execute($flat_exception, new Request()); $this->assertEquals($response->getStatusCode(), 405, 'HTTP status of response is correct.'); $this->assertEquals($response->getContent(), 'Method Not Allowed', 'HTTP response body is correct.'); diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php index eaae25a..aaabaa0 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php @@ -347,7 +347,7 @@ public function testGetFormController() { $apple_form = $this->entityManager->getFormController('apple', 'default'); $this->assertInstanceOf('Drupal\Tests\Core\Entity\TestEntityForm', $apple_form); $this->assertAttributeInstanceOf('Drupal\Core\Extension\ModuleHandlerInterface', 'moduleHandler', $apple_form); - $this->assertAttributeInstanceOf('Drupal\Core\StringTranslation\TranslationInterface', 'translationManager', $apple_form); + $this->assertAttributeInstanceOf('Drupal\Core\StringTranslation\TranslationInterface', 'stringTranslation', $apple_form); $banana_form = $this->entityManager->getFormController('banana', 'default'); $this->assertInstanceOf('Drupal\Tests\Core\Entity\TestEntityFormInjected', $banana_form); @@ -397,7 +397,7 @@ public function testGetController() { $apple_controller = $this->entityManager->getController('apple', 'storage'); $this->assertInstanceOf($class, $apple_controller); $this->assertAttributeInstanceOf('Drupal\Core\Extension\ModuleHandlerInterface', 'moduleHandler', $apple_controller); - $this->assertAttributeInstanceOf('Drupal\Core\StringTranslation\TranslationInterface', 'translationManager', $apple_controller); + $this->assertAttributeInstanceOf('Drupal\Core\StringTranslation\TranslationInterface', 'stringTranslation', $apple_controller); $banana_controller = $this->entityManager->getController('banana', 'storage', 'getStorageClass'); $this->assertInstanceOf('Drupal\Tests\Core\Entity\TestEntityControllerInjected', $banana_controller); diff --git a/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php index e93b127..4603bdc 100644 --- a/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php @@ -71,7 +71,7 @@ protected function setUp() { protected function setupContextualLinkDefault() { $this->contextualLinkDefault = new ContextualLinkDefault($this->config, $this->pluginId, $this->pluginDefinition); - $this->contextualLinkDefault->setTranslationManager($this->stringTranslation); + $this->contextualLinkDefault->setStringTranslation($this->stringTranslation); } /** diff --git a/core/tests/Drupal/Tests/Core/Menu/LocalActionDefaultTest.php b/core/tests/Drupal/Tests/Core/Menu/LocalActionDefaultTest.php index 2ce3cf5..8478faa 100644 --- a/core/tests/Drupal/Tests/Core/Menu/LocalActionDefaultTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/LocalActionDefaultTest.php @@ -81,7 +81,7 @@ protected function setUp() { */ protected function setupLocalActionDefault() { $this->localActionDefault = new LocalActionDefault($this->config, $this->pluginId, $this->pluginDefinition, $this->routeProvider); - $this->localActionDefault->setTranslationManager($this->stringTranslation); + $this->localActionDefault->setStringTranslation($this->stringTranslation); } /** diff --git a/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php b/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php index 26ea046..c338878 100644 --- a/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php @@ -87,7 +87,7 @@ protected function setupLocalTaskDefault() { $this->localTaskBase = new TestLocalTaskDefault($this->config, $this->pluginId, $this->pluginDefinition); $this->localTaskBase ->setRouteProvider($this->routeProvider) - ->setTranslationManager($this->stringTranslation); + ->setStringTranslation($this->stringTranslation); } diff --git a/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationAwareTraitTest.php b/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationAwareTraitTest.php new file mode 100644 index 0000000..5d9b1b7 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/StringTranslation/StringTranslationAwareTraitTest.php @@ -0,0 +1,82 @@ + 'String translation aware trait', + 'description' => 'Tests the string translation aware trait.', + 'group' => 'StringTranslation', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + $this->translation = $this->getObjectForTrait('\Drupal\Core\StringTranslation\StringTranslationAwareTrait'); + $stub = $this->getStringTranslationStub(); + $stub->expects($this->any()) + ->method('formatPlural') + ->will($this->returnArgument(2)); + $this->translation->setStringTranslation($stub); + $this->reflection = new \ReflectionClass(get_class($this->translation)); + } + + /** + * @covers ::t + */ + public function testT() { + $method = $this->reflection->getMethod('t'); + $method->setAccessible(TRUE); + + $this->assertEquals('something', $method->invoke($this->translation, 'something')); + } + + /** + * @covers ::formatPlural + */ + public function testFormatPlural() { + $method = $this->reflection->getMethod('formatPlural'); + $method->setAccessible(TRUE); + + $this->assertEquals('apples', $method->invoke($this->translation, 2, 'apple', 'apples')); + } + +}