Commit b0821d88 authored by alexpott's avatar alexpott
Browse files

Issue #2079797 by ParisLiakos, Xano, amateescu, tim.plunkett: Provide a trait...

Issue #2079797 by ParisLiakos, Xano, amateescu, tim.plunkett: Provide a trait for $this->t() and $this->formatPlural().
parent ec5b4345
......@@ -478,7 +478,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']
......@@ -604,7 +604,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:
......
......@@ -8,6 +8,7 @@
namespace Drupal\Core\Annotation;
use Drupal\Component\Annotation\AnnotationBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* @defgroup plugin_translatable Translatable plugin metadata
......@@ -51,6 +52,7 @@
* @ingroup plugin_translatable
*/
class Translation extends AnnotationBase {
use StringTranslationTrait;
/**
* The translation of the value passed to the constructor of the class.
......@@ -59,13 +61,6 @@ class Translation extends AnnotationBase {
*/
protected $translation;
/**
* The translation manager.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* Constructs a new class instance.
*
......@@ -88,7 +83,7 @@ public function __construct(array $values) {
'context' => $values['context'],
);
}
$this->translation = $this->getTranslationManager()->translate($string, $arguments, $options);
$this->translation = $this->t($string, $arguments, $options);
}
/**
......@@ -98,18 +93,4 @@ public function get() {
return $this->translation;
}
/**
* Returns the translation manager.
*
* @return \Drupal\Core\StringTranslation\TranslationInterface
* The translation manager.
*/
protected function getTranslationManager() {
if (!$this->translationManager) {
$this->translationManager = \Drupal::translation();
}
return $this->translationManager;
}
}
......@@ -7,12 +7,15 @@
namespace Drupal\Core\Breadcrumb;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Defines a common base class for breadcrumb builders adding a link generator.
*
* @todo Use traits once we have a PHP 5.4 requirement.
*/
abstract class BreadcrumbBuilderBase implements BreadcrumbBuilderInterface {
use StringTranslationTrait;
/**
* The link generator.
......@@ -21,13 +24,6 @@ abstract class BreadcrumbBuilderBase implements BreadcrumbBuilderInterface {
*/
protected $linkGenerator;
/**
* The translation manager.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* Returns the service container.
*
......@@ -64,26 +60,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;
}
}
......@@ -7,13 +7,14 @@
namespace Drupal\Core\Config;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Defines a base event listener implementation for config sync validation.
*/
abstract class ConfigImportValidateEventSubscriberBase implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* Checks that the configuration synchronization is valid.
......@@ -31,27 +32,4 @@ static function getSubscribedEvents() {
return $events;
}
/**
* Translates a string to the current language or to a given language.
*
* @param string $string
* A string containing the English string to translate.
* @param array $args
* An associative array of replacements to make after translation. Based
* on the first character of the key, the value is escaped and/or themed.
* See \Drupal\Component\Utility\String::format() for details.
* @param array $options
* An associative array of additional options, with the following elements:
* - 'langcode': The language code to translate to a language other than
* what is used to display the page.
* - 'context': The context the source string belongs to.
*
* @return string
* The translated string.
*
* @see \Drupal\Core\StringTranslation\TranslationInterface::translate()
*/
protected function t($string, array $args = array(), array $options = array()) {
return \Drupal::translation()->translate($string, $args, $options);
}
}
......@@ -15,7 +15,8 @@
use Drupal\Core\DependencyInjection\DependencySerialization;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
......@@ -38,6 +39,7 @@
* @see \Drupal\Core\Config\ConfigImporterEvent
*/
class ConfigImporter extends DependencySerialization {
use StringTranslationTrait;
/**
* The name used to identify the lock.
......@@ -121,13 +123,6 @@ class ConfigImporter extends DependencySerialization {
*/
protected $themeHandler;
/**
* The string translation service.
*
* @var \Drupal\Core\StringTranslation\TranslationManager
*/
protected $translationManager;
/**
* Flag set to import system.theme during processing theme enable and disables.
*
......@@ -178,10 +173,10 @@ class ConfigImporter extends DependencySerialization {
* The module handler
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler
* @param \Drupal\Core\StringTranslation\TranslationManager $translation_manager
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
*/
public function __construct(StorageComparerInterface $storage_comparer, EventDispatcherInterface $event_dispatcher, ConfigManagerInterface $config_manager, LockBackendInterface $lock, TypedConfigManagerInterface $typed_config, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, TranslationManager $translation_manager) {
public function __construct(StorageComparerInterface $storage_comparer, EventDispatcherInterface $event_dispatcher, ConfigManagerInterface $config_manager, LockBackendInterface $lock, TypedConfigManagerInterface $typed_config, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, TranslationInterface $string_translation) {
$this->storageComparer = $storage_comparer;
$this->eventDispatcher = $event_dispatcher;
$this->configManager = $config_manager;
......@@ -189,7 +184,7 @@ public function __construct(StorageComparerInterface $storage_comparer, EventDis
$this->typedConfigManager = $typed_config;
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
$this->translationManager = $translation_manager;
$this->stringTranslation = $string_translation;
$this->processedConfiguration = $this->storageComparer->getEmptyChangelist();
$this->processedExtensions = $this->getEmptyExtensionsProcessedList();
}
......@@ -835,7 +830,7 @@ protected function checkOp($op, $name) {
$entity_type = $this->configManager->getEntityManager()->getDefinition($entity_type_id);
$entity = $entity_storage->load($entity_storage->getIDFromConfigName($name, $entity_type->getConfigPrefix()));
$entity->delete();
$this->logError($this->translationManager->translate('Deleted and replaced configuration entity "@name"', array('@name' => $name)));
$this->logError($this->t('Deleted and replaced configuration entity "@name"', array('@name' => $name)));
}
else {
$this->storageComparer->getTargetStorage()->delete($name);
......@@ -1008,31 +1003,7 @@ protected function reInjectMe() {
$this->typedConfigManager = \Drupal::service('config.typed');
$this->moduleHandler = \Drupal::moduleHandler();
$this->themeHandler = \Drupal::service('theme_handler');
$this->translationManager = \Drupal::service('string_translation');
}
/**
* Translates a string to the current language or to a given language.
*
* @param string $string
* A string containing the English string to translate.
* @param array $args
* An associative array of replacements to make after translation. Based
* on the first character of the key, the value is escaped and/or themed.
* See \Drupal\Component\Utility\String::format() for details.
* @param array $options
* An associative array of additional options, with the following elements:
* - 'langcode': The language code to translate to a language other than
* what is used to display the page.
* - 'context': The context the source string belongs to.
*
* @return string
* The translated string.
*
* @see \Drupal\Core\StringTranslation\TranslationManager::translate()
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->translationManager->translate($string, $args, $options);
$this->stringTranslation = \Drupal::service('string_translation');
}
}
......@@ -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;
......@@ -31,6 +32,7 @@
* @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
*/
abstract class ControllerBase implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The entity manager.
......@@ -53,13 +55,6 @@ abstract class ControllerBase implements ContainerInjectionInterface {
*/
protected $languageManager;
/**
* The translation manager.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* The configuration factory.
*
......@@ -275,28 +270,6 @@ protected function currentUser() {
return $this->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.
*
......
......@@ -9,7 +9,6 @@
use Drupal\Core\Page\DefaultHtmlPageRenderer;
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;
......@@ -60,8 +59,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
......@@ -69,8 +66,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;
......
......@@ -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;
......@@ -19,13 +18,6 @@
*/
class HtmlControllerBase {
/**
* The translation manager service.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* The title resolver.
*
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
......@@ -15,6 +16,7 @@
* Provides the default implementation of the title resolver interface.
*/
class TitleResolver implements TitleResolverInterface {
use StringTranslationTrait;
/**
* The controller resolver.
......@@ -23,24 +25,17 @@ 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;
}
/**
......@@ -74,7 +69,7 @@ public function getTitle(Request $request, Route $route) {
}
// Fall back to a static string from the route.
$route_title = $this->translationManager->translate($title, $args, $options);
$route_title = $this->t($title, $args, $options);
}
return $route_title;
}
......
......@@ -14,11 +14,13 @@
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Provides a service to handler various date related functionality.
*/
class Date {
use StringTranslationTrait;
/**
* The list of loaded timezones.
......@@ -181,7 +183,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--;
}
......@@ -193,15 +195,6 @@ public function formatInterval($interval, $granularity = 2, $langcode = NULL) {
return $output ? $output : $this->t('0 sec', array(), array('langcode' => $langcode));
}
/**
* 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.
*
......
......@@ -9,6 +9,7 @@
use Drupal\Core\DependencyInjection\DependencySerialization;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
/**
......@@ -17,13 +18,7 @@
* @todo Convert this to a trait.
*/
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.
......@@ -32,32 +27,6 @@ abstract class EntityControllerBase extends DependencySerialization {
*/
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.
*
......
......@@ -129,14 +129,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.
......
......@@ -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;
......@@ -201,15 +200,6 @@ public function render() {
return $build;
}
/**
* 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.
*
......
......@@ -230,7 +230,7 @@ public function getFormObject($entity_type, $operation) {
}
$controller
->setTranslationManager($this->translationManager)
->setStringTranslation($this->translationManager)
->setModuleHandler($this->moduleHandler)
->setOperation($operation);
$this->controllers['form'][$operation][$entity_type] = $controller;
......@@ -288,8 +288,8 @@ public function getController($entity_type, $controller_type, $controller_class_
if (