Commit 5c943b5c authored by alexpott's avatar alexpott

Issue #2569069 by stefan.r, mr.baileys, alexpott, lauriii: Replace...

Issue #2569069 by stefan.r, mr.baileys, alexpott, lauriii: Replace TranslationWrapper with TranslatableString and deprecate TranslationWrapper
parent 13ae3e14
......@@ -25,7 +25,7 @@
use Drupal\Core\Render\SafeString;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Site\Settings;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
......@@ -261,7 +261,7 @@ function check_url($uri) {
* Optional language code to translate to a language other than what is used
* to display the page.
*
* @return \Drupal\Core\StringTranslation\TranslationWrapper
* @return \Drupal\Core\StringTranslation\TranslatableString
* A translated string representation of the size.
*/
function format_size($size, $langcode = NULL) {
......@@ -283,21 +283,21 @@ function format_size($size, $langcode = NULL) {
$options = ['langcode' => $langcode];
switch ($unit) {
case 'KB':
return new TranslationWrapper('@size KB', $args, $options);
return new TranslatableString('@size KB', $args, $options);
case 'MB':
return new TranslationWrapper('@size MB', $args, $options);
return new TranslatableString('@size MB', $args, $options);
case 'GB':
return new TranslationWrapper('@size GB', $args, $options);
return new TranslatableString('@size GB', $args, $options);
case 'TB':
return new TranslationWrapper('@size TB', $args, $options);
return new TranslatableString('@size TB', $args, $options);
case 'PB':
return new TranslationWrapper('@size PB', $args, $options);
return new TranslatableString('@size PB', $args, $options);
case 'EB':
return new TranslationWrapper('@size EB', $args, $options);
return new TranslatableString('@size EB', $args, $options);
case 'ZB':
return new TranslationWrapper('@size ZB', $args, $options);
return new TranslatableString('@size ZB', $args, $options);
case 'YB':
return new TranslationWrapper('@size YB', $args, $options);
return new TranslatableString('@size YB', $args, $options);
}
}
}
......
......@@ -26,7 +26,7 @@ trait PlaceholderTrait {
* The string with the placeholders replaced.
*
* @see \Drupal\Component\Utility\SafeMarkup::format()
* @see \Drupal\Core\StringTranslation\TranslationWrapper::render()
* @see \Drupal\Core\StringTranslation\TranslatableString::render()
*/
protected static function placeholderFormat($string, array $args, &$safe = TRUE) {
// Transform arguments before inserting them.
......
......@@ -8,7 +8,7 @@
namespace Drupal\Core\Annotation;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
/**
* @defgroup plugin_context Annotation for context definition
......@@ -113,7 +113,7 @@ public function __construct(array $values) {
// used in the classes they pass to.
foreach (['label', 'description'] as $key) {
// @todo Remove this workaround in https://www.drupal.org/node/2362727.
if (isset($values[$key]) && $values[$key] instanceof TranslationWrapper) {
if (isset($values[$key]) && $values[$key] instanceof TranslatableString) {
$values[$key] = (string) $values[$key]->get();
}
else {
......
......@@ -8,7 +8,7 @@
namespace Drupal\Core\Annotation;
use Drupal\Component\Annotation\AnnotationBase;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
/**
* @defgroup plugin_translatable Annotation for translatable text
......@@ -60,7 +60,7 @@ class Translation extends AnnotationBase {
/**
* The string translation object.
*
* @var \Drupal\Core\StringTranslation\TranslationWrapper
* @var \Drupal\Core\StringTranslation\TranslatableString
*/
protected $translation;
......@@ -86,7 +86,7 @@ public function __construct(array $values) {
'context' => $values['context'],
);
}
$this->translation = new TranslationWrapper($string, $arguments, $options);
$this->translation = new TranslatableString($string, $arguments, $options);
}
/**
......
......@@ -47,7 +47,7 @@ public function label() {
$definition = $this->getPluginDefinition();
// Cast the admin label to a string since it is an object.
// @see \Drupal\Core\StringTranslation\TranslationWrapper
// @see \Drupal\Core\StringTranslation\TranslatableString
return (string) $definition['admin_label'];
}
......
......@@ -6,7 +6,7 @@
*/
namespace Drupal\Core\Entity\Annotation;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
/**
* Defines a config entity type annotation object.
......@@ -37,7 +37,7 @@ class ConfigEntityType extends EntityType {
* {@inheritdoc}
*/
public function get() {
$this->definition['group_label'] = new TranslationWrapper('Configuration', array(), array('context' => 'Entity type group'));
$this->definition['group_label'] = new TranslatableString('Configuration', array(), array('context' => 'Entity type group'));
return parent::get();
}
......
......@@ -6,7 +6,7 @@
*/
namespace Drupal\Core\Entity\Annotation;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
/**
* Defines a content entity type annotation object.
......@@ -37,7 +37,7 @@ class ContentEntityType extends EntityType {
* {@inheritdoc}
*/
public function get() {
$this->definition['group_label'] = new TranslationWrapper('Content', array(), array('context' => 'Entity type group'));
$this->definition['group_label'] = new TranslatableString('Content', array(), array('context' => 'Entity type group'));
return parent::get();
}
......
......@@ -12,7 +12,7 @@
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
use Drupal\Core\TypedData\OptionsProviderInterface;
use Drupal\Core\TypedData\DataDefinition;
......@@ -34,8 +34,8 @@ class BooleanItem extends FieldItemBase implements OptionsProviderInterface {
*/
public static function defaultFieldSettings() {
return array(
'on_label' => new TranslationWrapper('On'),
'off_label' => new TranslationWrapper('Off'),
'on_label' => new TranslatableString('On'),
'off_label' => new TranslatableString('Off'),
) + parent::defaultFieldSettings();
}
......
......@@ -13,7 +13,7 @@
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\DataReferenceDefinition;
......@@ -83,19 +83,19 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
if ($target_id_data_type === 'integer') {
$target_id_definition = DataDefinition::create('integer')
->setLabel(new TranslationWrapper('@label ID', ['@label' => $target_type_info->getLabel()]))
->setLabel(new TranslatableString('@label ID', ['@label' => $target_type_info->getLabel()]))
->setSetting('unsigned', TRUE);
}
else {
$target_id_definition = DataDefinition::create('string')
->setLabel(new TranslationWrapper('@label ID', ['@label' => $target_type_info->getLabel()]));
->setLabel(new TranslatableString('@label ID', ['@label' => $target_type_info->getLabel()]));
}
$target_id_definition->setRequired(TRUE);
$properties['target_id'] = $target_id_definition;
$properties['entity'] = DataReferenceDefinition::create('entity')
->setLabel($target_type_info->getLabel())
->setDescription(new TranslationWrapper('The referenced entity'))
->setDescription(new TranslatableString('The referenced entity'))
// The entity object is computed out of the entity ID.
->setComputed(TRUE)
->setReadOnly(FALSE)
......
......@@ -9,7 +9,7 @@
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
use Drupal\Core\TypedData\DataDefinition;
/**
......@@ -29,10 +29,10 @@ class PasswordItem extends StringItem {
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(new TranslationWrapper('The hashed password'))
->setLabel(new TranslatableString('The hashed password'))
->setSetting('case_sensitive', TRUE);
$properties['existing'] = DataDefinition::create('string')
->setLabel(new TranslationWrapper('Existing password'));
->setLabel(new TranslatableString('Existing password'));
return $properties;
}
......
......@@ -9,7 +9,7 @@
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
use Drupal\Core\TypedData\DataDefinition;
/**
......@@ -31,9 +31,9 @@ public static function defaultStorageSettings() {
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
// This is called very early by the user entity roles field. Prevent
// early t() calls by using the TranslationWrapper.
// early t() calls by using the TranslatableString.
$properties['value'] = DataDefinition::create('string')
->setLabel(new TranslationWrapper('Text value'))
->setLabel(new TranslatableString('Text value'))
->setSetting('case_sensitive', $field_definition->getSetting('case_sensitive'))
->setRequired(TRUE);
......
......@@ -10,7 +10,7 @@
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
use Drupal\Core\Url;
/**
......@@ -109,13 +109,13 @@ public function getLanguageTypes() {
public function getDefinedLanguageTypesInfo() {
$this->definedLanguageTypesInfo = array(
LanguageInterface::TYPE_INTERFACE => array(
'name' => new TranslationWrapper('Interface text'),
'description' => new TranslationWrapper('Order of language detection methods for interface text. If a translation of interface text is available in the detected language, it will be displayed.'),
'name' => new TranslatableString('Interface text'),
'description' => new TranslatableString('Order of language detection methods for interface text. If a translation of interface text is available in the detected language, it will be displayed.'),
'locked' => TRUE,
),
LanguageInterface::TYPE_CONTENT => array(
'name' => new TranslationWrapper('Content'),
'description' => new TranslationWrapper('Order of language detection methods for content. If a version of content is available in the detected language, it will be displayed.'),
'name' => new TranslatableString('Content'),
'description' => new TranslatableString('Order of language detection methods for content. If a version of content is available in the detected language, it will be displayed.'),
'locked' => TRUE,
),
LanguageInterface::TYPE_URL => array(
......@@ -210,16 +210,16 @@ public function getDefaultLockedLanguages($weight = 0) {
'direction' => LanguageInterface::DIRECTION_LTR,
);
// This is called very early while initializing the language system. Prevent
// early t() calls by using the TranslationWrapper.
// early t() calls by using the TranslatableString.
$languages[LanguageInterface::LANGCODE_NOT_SPECIFIED] = new Language(array(
'id' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'name' => new TranslationWrapper('Not specified'),
'name' => new TranslatableString('Not specified'),
'weight' => ++$weight,
) + $locked_language);
$languages[LanguageInterface::LANGCODE_NOT_APPLICABLE] = new Language(array(
'id' => LanguageInterface::LANGCODE_NOT_APPLICABLE,
'name' => new TranslationWrapper('Not applicable'),
'name' => new TranslatableString('Not applicable'),
'weight' => ++$weight,
) + $locked_language);
......
......@@ -7,7 +7,7 @@
namespace Drupal\Core\Logger;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
/**
* @defgroup logging_severity_levels Logging severity levels
......@@ -96,14 +96,14 @@ class RfcLogLevel {
public static function getLevels() {
if (!static::$levels) {
static::$levels = [
static::EMERGENCY => new TranslationWrapper('Emergency'),
static::ALERT => new TranslationWrapper('Alert'),
static::CRITICAL => new TranslationWrapper('Critical'),
static::ERROR => new TranslationWrapper('Error'),
static::WARNING => new TranslationWrapper('Warning'),
static::NOTICE => new TranslationWrapper('Notice'),
static::INFO => new TranslationWrapper('Info'),
static::DEBUG => new TranslationWrapper('Debug'),
static::EMERGENCY => new TranslatableString('Emergency'),
static::ALERT => new TranslatableString('Alert'),
static::CRITICAL => new TranslatableString('Critical'),
static::ERROR => new TranslatableString('Error'),
static::WARNING => new TranslatableString('Warning'),
static::NOTICE => new TranslatableString('Notice'),
static::INFO => new TranslatableString('Info'),
static::DEBUG => new TranslatableString('Debug'),
];
}
......
......@@ -19,7 +19,7 @@ class ContextualLinkDefault extends PluginBase implements ContextualLinkInterfac
* {@inheritdoc}
*/
public function getTitle(Request $request = NULL) {
// The title from YAML file discovery may be a TranslationWrapper object.
// The title from YAML file discovery may be a TranslatableString object.
return (string) $this->pluginDefinition['title'];
}
......
......@@ -71,7 +71,7 @@ public function getRouteName() {
*/
public function getTitle(Request $request = NULL) {
// Subclasses may pull in the request or specific attributes as parameters.
// The title from YAML file discovery may be a TranslationWrapper object.
// The title from YAML file discovery may be a TranslatableString object.
return (string) $this->pluginDefinition['title'];
}
......
......@@ -80,7 +80,7 @@ public function getRouteParameters(RouteMatchInterface $route_match) {
* {@inheritdoc}
*/
public function getTitle(Request $request = NULL) {
// The title from YAML file discovery may be a TranslationWrapper object.
// The title from YAML file discovery may be a TranslatableString object.
return (string) $this->pluginDefinition['title'];
}
......
......@@ -41,10 +41,10 @@ class MenuLinkManager implements MenuLinkManagerInterface {
// The external URL if this link has one (required if route_name is empty).
'url' => '',
// The static title for the menu link. If this came from a YAML definition
// or other safe source this may be a TranslationWrapper object.
// or other safe source this may be a TranslatableString object.
'title' => '',
// The description. If this came from a YAML definition or other safe source
// this may be be a TranslationWrapper object.
// this may be be a TranslatableString object.
'description' => '',
// The plugin ID of the parent link (or NULL for a top-level link).
'parent' => '',
......
......@@ -1262,14 +1262,14 @@ protected static function schemaDefinition() {
'default' => '',
),
'title' => array(
'description' => 'The serialized title for the link. May be a TranslationWrapper.',
'description' => 'The serialized title for the link. May be a TranslatableString.',
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'serialize' => TRUE,
),
'description' => array(
'description' => 'The serialized description of this link - used for admin pages and title attribute. May be a TranslationWrapper.',
'description' => 'The serialized description of this link - used for admin pages and title attribute. May be a TranslatableString.',
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
......
......@@ -235,10 +235,10 @@
* The value corresponding to each machine name key is an associative array
* that may contain the following key-value pairs:
* - title: (required) The title of the menu link. If this should be
* translated, create a \Drupal\Core\StringTranslation\TranslationWrapper
* translated, create a \Drupal\Core\StringTranslation\TranslatableString
* object.
* - description: The description of the link. If this should be
* translated, create a \Drupal\Core\StringTranslation\TranslationWrapper
* translated, create a \Drupal\Core\StringTranslation\TranslatableString
* object.
* - route_name: (optional) The route name to be used to build the path.
* Either the route_name or url element must be provided.
......@@ -264,13 +264,13 @@
function hook_menu_links_discovered_alter(&$links) {
// Change the weight and title of the user.logout link.
$links['user.logout']['weight'] = -10;
$links['user.logout']['title'] = new \Drupal\Core\StringTranslation\TranslationWrapper('Logout');
$links['user.logout']['title'] = new \Drupal\Core\StringTranslation\TranslatableString('Logout');
// Conditionally add an additional link with a title that's not translated.
if (\Drupal::moduleHandler()->moduleExists('search')) {
$links['menu.api.search'] = array(
'title' => \Drupal::config('system.site')->get('name'),
'route_name' => 'menu.api.search',
'description' => new \Drupal\Core\StringTranslation\TranslationWrapper('View popular search phrases for this site.'),
'description' => new \Drupal\Core\StringTranslation\TranslatableString('View popular search phrases for this site.'),
'parent' => 'system.admin_reports',
);
}
......
......@@ -10,7 +10,7 @@
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Discovery\YamlDiscovery as ComponentYamlDiscovery;
use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\StringTranslation\TranslatableString;
/**
* Allows YAML files to define plugin definitions.
......@@ -18,7 +18,7 @@
* If the value of a key (like title) in the definition is translatable then
* the addTranslatableProperty() method can be used to mark it as such and also
* to add translation context. Then
* \Drupal\Core\StringTranslation\TranslationWrapper will be used to translate
* \Drupal\Core\StringTranslation\TranslatableString will be used to translate
* the string and also to mark it safe. Only strings written in the YAML files
* should be marked as safe, strings coming from dynamic plugin definitions
* potentially containing user input should not.
......@@ -83,7 +83,7 @@ public function getDefinitions() {
$definitions = array();
foreach ($plugins as $provider => $list) {
foreach ($list as $id => $definition) {
// Add translation wrappers.
// Add TranslatableStrings.
foreach ($this->translatableProperties as $property => $context_key) {
if (isset($definition[$property])) {
$options = [];
......@@ -93,7 +93,7 @@ public function getDefinitions() {
$options['context'] = $definition[$context_key];
unset($definition[$context_key]);
}
$definition[$property] = new TranslationWrapper($definition[$property], [], $options);
$definition[$property] = new TranslatableString($definition[$property], [], $options);
}
}
// Add ID and provider.
......
<?php
/**
* @file
* Contains \Drupal\Core\StringTranslation\TranslatableString.
*/
namespace Drupal\Core\StringTranslation;
use Drupal\Component\Utility\PlaceholderTrait;
use Drupal\Component\Utility\SafeStringInterface;
use Drupal\Component\Utility\ToStringTrait;
/**
* Provides translatable string class.
*
* This class delays translating strings until rendering them.
*
* This is useful for using translation in very low level subsystems like entity
* definition and stream wrappers.
*
* @see \Drupal\Core\StringTranslation\TranslationManager::translate()
* @see \Drupal\Core\StringTranslation\TranslationManager::translateString()
* @see \Drupal\Core\Annotation\Translation
*/
class TranslatableString implements SafeStringInterface {
use PlaceholderTrait;
use ToStringTrait;
/**
* The string to be translated.
*
* @var string
*/
protected $string;
/**
* The translated string without placeholder replacements.
*
* @var string
*/
protected $translatableString;
/**
* The translation arguments.
*
* @var array
*/
protected $arguments;
/**
* The translation options.
*
* @var array
*/
protected $options;
/**
* The string translation service.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $stringTranslation;
/**
* Constructs a new class instance.
*
* Parses values passed into this class through the t() function in Drupal and
* handles an optional context for the string.
*
* @param string $string
* The string that is to be translated.
* @param array $arguments
* (optional) An array with placeholder replacements, keyed by placeholder.
* @param array $options
* (optional) An array of additional options.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* (optional) The string translation service.
*/
public function __construct($string, array $arguments = array(), array $options = array(), TranslationInterface $string_translation = NULL) {
$this->string = $string;
$this->arguments = $arguments;
$this->options = $options;
$this->stringTranslation = $string_translation;
}
/**
* Gets the untranslated string value stored in this translated string.
*
* @return string
* The string stored in this wrapper.
*/
public function getUntranslatedString() {
return $this->string;
}
/**
* Gets a specific option from this translated string.
*
* @param $name
* Option name.
*
* @return mixed
* The value of this option or empty string of option is not set.
*/
public function getOption($name) {
return isset($this->options[$name]) ? $this->options[$name] : '';
}
/**
* Gets all options from this translated string.
*
* @return mixed[]
* The array of options.
*/
public function getOptions() {
return $this->options;
}
/**
* Gets all argments from this translated string.
*
* @return mixed[]
* The array of arguments.
*/
public function getArguments() {
return $this->arguments;
}
/**
* Renders the object as a string.
*
* @return string
* The translated string.
*/
public function render() {
if (!isset($this->translatableString)) {
$this->translatableString = $this->getStringTranslation()->translateString($this);
}
// Handle any replacements.
// @todo https://www.drupal.org/node/2509218 Note that the argument
// replacement is not stored so that different sanitization strategies can
// be used in different contexts.
if ($args = $this->getArguments()) {
return $this->placeholderFormat($this->translatableString, $args);
}
return $this->translatableString;
}
/**
* Magic __sleep() method to avoid serializing the string translator.
*/
public function __sleep() {
return array('string', 'arguments', 'options');
}
/**
* Returns a representation of the object for use in JSON serialization.
*
* @return string
* The safe string content.
*/
public function jsonSerialize() {
return $this->__toString();
}
/**
* 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;
}
}
......@@ -33,7 +33,7 @@ interface TranslationInterface {
* what is used to display the page.
* - 'context': The context the source string belongs to.
*
* @return string|\Drupal\Core\StringTranslation\TranslationWrapper
* @return string|\Drupal\Core\StringTranslation\TranslatableString
* The translated string.
*
* @see \Drupal\Component\Utility\SafeMarkup::format()
......@@ -41,15 +41,15 @@ interface TranslationInterface {
public function translate($string, array $args = array(), array $options = array());
/**
* Translates a TranslationWrapper object to a string.
* Translates a TranslatableString object to a string.
*
* @param \Drupal\Core\StringTranslation\TranslationWrapper $translated_string
* A TranslationWrapper object.
* @param \Drupal\Core\StringTranslation\TranslatableString $translated_string
* A TranslatableString object.
*
* @return string
* The translated string.
*/
public function translateString(TranslationWrapper $translated_string);
public function translateString(TranslatableString $translated_string);
/**
* Formats a string containing a count of items.
......
......@@ -152,14 +152,14 @@ public function translate($string, array $args = array(), array $options = array
break;
}
}
$wrapper = new TranslationWrapper($string, $args, $options, $this);
$wrapper = new TranslatableString($string, $args, $options, $this);
return $safe ? $wrapper : (string) $wrapper;
}
/**
* {@inheritdoc}
*/
public function translateString(TranslationWrapper $translated_string) {
public function translateString(TranslatableString $translated_string) {
return $this->doTranslate($translated_string->getUntranslatedString(), $translated_string->getOptions());
}
......
......@@ -7,177 +7,10 @@
namespace Drupal\Core\StringTranslation;
use Drupal\Component\Utility\PlaceholderTrait;
use Drupal\Component\Utility\SafeStringInterface;
use Drupal\Component\Utility\ToStringTrait;
/**
* Provides translatable string class.
*
* This class delays translating strings until rendering them.
*
* This is useful for using translation in very low level subsystems like entity