diff --git a/core/lib/Drupal/Core/Datetime/Date.php b/core/lib/Drupal/Core/Datetime/Date.php index 5c2b92b..a98986d 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\StringTranslationTrait; /** * Provides a service to handler various date related functionality. */ class Date { + use StringTranslationTrait; + /** * The list of loaded timezones. * @@ -85,7 +88,7 @@ class Date { public function __construct(EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory) { $this->dateFormatStorage = $entity_manager->getStorageController('date_format'); $this->languageManager = $language_manager; - $this->stringTranslation = $translation; + $this->translationManager = $translation; $this->configFactory = $config_factory; } @@ -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/Plugin/ContextAwarePluginBase.php b/core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php index 667bebe..4efa42b 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\StringTranslationTrait; /** * Drupal specific class for plugins that use context. @@ -22,6 +22,8 @@ */ abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase { + use StringTranslationTrait; + /** * 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/StringTranslation/StringTranslationTrait.php b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php index 5b46054..6fa75b9 100644 --- a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php +++ b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php @@ -29,6 +29,15 @@ protected function t($string, array $args = array(), array $options = array()) { } /** + * Formats a string containing a count of items. + * + * See the \Drupal\Core\StringTranslation::formatPlural() for details. + */ + protected function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()) { + return $this->getTranslationManager()->formatPlural($count, $singular, $plural, $args, $options); + } + + /** * Gets the translation manager. * * @return \Drupal\Core\StringTranslation\TranslationInterface diff --git a/core/modules/comment/lib/Drupal/comment/CommentManager.php b/core/modules/comment/lib/Drupal/comment/CommentManager.php index 2102abe..3cec56f 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\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\field\FieldInfo; @@ -21,6 +22,8 @@ */ class CommentManager implements CommentManagerInterface { + use StringTranslationTrait; + /** * 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 @@ -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/FormElement/DateFormat.php b/core/modules/config_translation/lib/Drupal/config_translation/FormElement/DateFormat.php index 3cc9867..5bf650a 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\StringTranslationTrait; /** * Defines the date format element for the configuration translation interface. */ -class DateFormat extends Element { +class DateFormat implements ElementInterface { + + use StringTranslationTrait; /** * {@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..aa6b2e6 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\StringTranslationTrait; /** * Defines the textarea element for the configuration translation interface. */ -class Textarea extends Element { +class Textarea implements ElementInterface { + + use StringTranslationTrait; /** * {@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..ded36b1 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\StringTranslationTrait; /** * Defines the textfield element for the configuration translation interface. */ -class Textfield extends Element { +class Textfield implements ElementInterface { + + use StringTranslationTrait; /** * {@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..7322daf 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\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -19,6 +20,8 @@ */ class FieldUiLocalTask extends DerivativeBase implements ContainerDerivativeInterface { + use StringTranslationTrait; + /** * The route provider. * @@ -34,13 +37,6 @@ 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 @@ -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/migrate/lib/Drupal/migrate/MigrateExecutable.php b/core/modules/migrate/lib/Drupal/migrate/MigrateExecutable.php index 104f187..7ac3187 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\StringTranslationTrait; use Drupal\migrate\Entity\MigrationInterface; use Drupal\migrate\Plugin\MigrateIdMapInterface; @@ -16,6 +17,8 @@ */ class MigrateExecutable { + use StringTranslationTrait; + /** * 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/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 2139fdc..daa7816 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\StringTranslationTrait; use Drupal\Core\Utility\ProjectInfo; /** @@ -17,6 +18,8 @@ */ class UpdateManager implements UpdateManagerInterface { + use StringTranslationTrait; + /** * 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->translationManager = $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..5c17ee2 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\StringTranslationTrait; 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 StringTranslationTrait; + /** * 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 @@ -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); - } - }