Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663

This commit is contained in:
Greg Anderson 2015-10-08 11:40:12 -07:00
parent eb34d130a8
commit f32e58e4b1
8476 changed files with 211648 additions and 170042 deletions

View file

@ -0,0 +1,169 @@
<?php
/**
* @file
* Contains \Drupal\Core\StringTranslation\PluralTranslatableMarkup.
*/
namespace Drupal\Core\StringTranslation;
/**
* A class to hold plural translatable markup.
*/
class PluralTranslatableMarkup extends TranslatableMarkup {
/**
* The delimiter used to split plural strings.
*
* This is the ETX (End of text) character and is used as a minimal means to
* separate singular and plural variants in source and translation text. It
* was found to be the most compatible delimiter for the supported databases.
*/
const DELIMITER = "\03";
/**
* The item count to display.
*
* @var int
*/
protected $count;
/**
* The already translated string.
*
* @var string
*/
protected $translatedString;
/**
* A bool that statically caches whether locale_get_plural() exists.
*
* @var bool
*/
protected static $localeEnabled;
/**
* Constructs a new PluralTranslatableMarkup object.
*
* Parses values passed into this class through the format_plural() function
* in Drupal and handles an optional context for the string.
*
* @param int $count
* The item count to display.
* @param string $singular
* The string for the singular case. Make sure it is clear this is singular,
* to ease translation (e.g. use "1 new comment" instead of "1 new"). Do not
* use @count in the singular string.
* @param string $plural
* The string for the plural case. Make sure it is clear this is plural, to
* ease translation. Use @count in place of the item count, as in
* "@count new comments".
* @param array $args
* (optional) An array with placeholder replacements, keyed by placeholder.
* See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
* additional information about placeholders. Note that you do not need to
* include @count in this array; this replacement is done automatically
* for the plural cases.
* @param array $options
* (optional) An associative array of additional options. See t() for
* allowed keys.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* (optional) The string translation service.
*
* @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
*/
public function __construct($count, $singular, $plural, array $args = [], array $options = [], TranslationInterface $string_translation = NULL) {
$this->count = $count;
$translatable_string = implode(static::DELIMITER, array($singular, $plural));
parent::__construct($translatable_string, $args, $options, $string_translation);
}
/**
* Constructs a new class instance from already translated markup.
*
* This method ensures that the string is pluralized correctly. As opposed
* to the __construct() method, this method is designed to be invoked with
* a string already translated (such as with configuration translation).
*
* @param int $count
* The item count to display.
* @param string $translated_string
* The already translated string.
* @param array $args
* An associative array of replacements to make after translation. Instances
* of any key in this array are replaced with the corresponding value.
* Based on the first character of the key, the value is escaped and/or
* themed. See \Drupal\Component\Utility\SafeMarkup::format(). Note that you
* do not need to include @count in this array; this replacement is done
* automatically for the plural cases.
* @param array $options
* An associative array of additional options. See t() for allowed keys.
*
* @return \Drupal\Core\StringTranslation\PluralTranslatableMarkup
* A PluralTranslatableMarkup object.
*/
public static function createFromTranslatedString($count, $translated_string, array $args = [], array $options = []) {
$plural = new static($count, '', '', $args, $options);
$plural->translatedString = $translated_string;
return $plural;
}
/**
* Renders the object as a string.
*
* @return string
* The translated string.
*/
public function render() {
if (!$this->translatedString) {
$this->translatedString = $this->getStringTranslation()->translateString($this);
}
if ($this->translatedString === '') {
return '';
}
$arguments = $this->getArguments();
$arguments['@count'] = $this->count;
$translated_array = explode(static::DELIMITER, $this->translatedString);
if ($this->count == 1) {
return $this->placeholderFormat($translated_array[0], $arguments);
}
$index = $this->getPluralIndex();
if ($index == 0) {
// Singular form.
$return = $translated_array[0];
}
else {
if (isset($translated_array[$index])) {
// N-th plural form.
$return = $translated_array[$index];
}
else {
// If the index cannot be computed or there's no translation, use the
// second plural form as a fallback (which allows for most flexibility
// with the replaceable @count value).
$return = $translated_array[1];
}
}
return $this->placeholderFormat($return, $arguments);
}
/**
* Gets the plural index through the gettext formula.
*
* @return int
*/
protected function getPluralIndex() {
if (!isset(static::$localeEnabled)) {
static::$localeEnabled = function_exists('locale_get_plural');
}
if (function_exists('locale_get_plural')) {
return locale_get_plural($this->count, $this->getOption('langcode'));
}
return -1;
}
}

View file

@ -70,12 +70,17 @@ trait StringTranslationTrait {
/**
* Returns the number of plurals supported by a given language.
*
* See the
* \Drupal\Core\StringTranslation\TranslationInterface::getNumberOfPlurals()
* See the \Drupal\locale\PluralFormulaInterface::getNumberOfPlurals()
* documentation for details.
*
* @see \Drupal\locale\PluralFormulaInterface::getNumberOfPlurals()
*/
protected function getNumberOfPlurals($langcode = NULL) {
return $this->getStringTranslation()->getNumberOfPlurals($langcode);
if (\Drupal::hasService('locale.plural.formula')) {
return \Drupal::service('locale.plural.formula')->getNumberOfPlurals($langcode);
}
// We assume 2 plurals if Locale's services are not available.
return 2;
}
/**

View file

@ -0,0 +1,173 @@
<?php
/**
* @file
* Contains \Drupal\Core\StringTranslation\TranslatableMarkup.
*/
namespace Drupal\Core\StringTranslation;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\ToStringTrait;
/**
* Provides translatable markup class.
*
* This class delays translation until rendering.
*
* This is useful for using translation in very low level subsystems like entity
* definition and stream wrappers.
*
* @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
* @see \Drupal\Core\StringTranslation\TranslationManager::translate()
* @see \Drupal\Core\StringTranslation\TranslationManager::translateString()
* @see \Drupal\Core\Annotation\Translation
*/
class TranslatableMarkup extends FormattableMarkup {
use ToStringTrait;
/**
* The string to be translated.
*
* @var string
*/
protected $string;
/**
* The translated markup without placeholder replacements.
*
* @var string
*/
protected $translatedMarkup;
/**
* 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.
* See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
* additional information about placeholders.
* @param array $options
* (optional) An array of additional options.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* (optional) The string translation service.
*
* @throws \InvalidArgumentException
* Exception thrown when $string is not a string.
*
* @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
*/
public function __construct($string, array $arguments = array(), array $options = array(), TranslationInterface $string_translation = NULL) {
if (!is_string($string)) {
$message = $string instanceof TranslatableMarkup ? '$string ("' . $string->getUntranslatedString() . '") must be a string.' : '$string ("' . (string) $string . '") must be a string.';
throw new \InvalidArgumentException($message);
}
$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->translatedMarkup)) {
$this->translatedMarkup = $this->getStringTranslation()->translateString($this);
}
// Handle any replacements.
if ($args = $this->getArguments()) {
return $this->placeholderFormat($this->translatedMarkup, $args);
}
return $this->translatedMarkup;
}
/**
* Magic __sleep() method to avoid serializing the string translator.
*/
public function __sleep() {
return array('string', 'arguments', '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;
}
}

View file

@ -33,19 +33,31 @@ interface TranslationInterface {
* what is used to display the page.
* - 'context': The context the source string belongs to.
*
* @return string
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The translated string.
*
* @see \Drupal\Component\Utility\SafeMarkup::format()
*/
public function translate($string, array $args = array(), array $options = array());
/**
* Translates a TranslatableMarkup object to a string.
*
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $translated_string
* A TranslatableMarkup object.
*
* @return string
* The translated string.
*/
public function translateString(TranslatableMarkup $translated_string);
/**
* Formats a string containing a count of items.
*
* This function ensures that the string is pluralized correctly. Since t() is
* called by this function, make sure not to pass already-localized strings to
* it. See formatPluralTranslated() for that.
* This function ensures that the string is pluralized correctly. Since
* TranslationInterface::translate() is called by this function, make sure not
* to pass already-localized strings to it. See
* PluralTranslatableMarkup::createFromTranslatedString() for that.
*
* For example:
* @code
@ -80,60 +92,14 @@ interface TranslationInterface {
* @param array $options
* An associative array of additional options. See t() for allowed keys.
*
* @return string
* @return \Drupal\Core\StringTranslation\PluralTranslatableMarkup
* A translated string.
*
* @see self::translate()
* @see \Drupal\Core\StringTranslation\TranslationInterface::translate()
* @see t()
* @see \Drupal\Component\Utility\SafeMarkup::format()
* @see self::formatPluralTranslated
* @see \Drupal\Core\StringTranslation\PluralTranslatableMarkup::createFromTranslatedString()
*/
public function formatPlural($count, $singular, $plural, array $args = array(), array $options = array());
/**
* Formats an already translated string containing a count of items.
*
* This function ensures that the string is pluralized correctly. As opposed
* to the formatPlural() method, this method is designed to be invoked with
* a string already translated (such as with configuration translation).
*
* @param int $count
* The item count to display.
* @param string $translation
* The string containing the translation of a singular/plural pair. It may
* contain any number of possible variants (depending on the language
* translated to) separated by the value of the LOCALE_PLURAL_DELIMITER
* constant.
* @param array $args
* Associative array of replacements to make in the translation. Instances
* of any key in this array are replaced with the corresponding value.
* Based on the first character of the key, the value is escaped and/or
* themed. See \Drupal\Component\Utility\SafeMarkup::format(). Note that you do
* not need to include @count in this array; this replacement is done
* automatically for the plural cases.
* @param array $options
* An associative array of additional options. The 'context' key is not
* supported because the passed string is already translated. Use the
* 'langcode' key to ensure the proper plural logic is used.
*
* @return string
* The correct substring for the given $count with $args replaced.
*
* @see self::formatPlural()
* @see \Drupal\Component\Utility\SafeMarkup::format()
*/
public function formatPluralTranslated($count, $translation, array $args = array(), array $options = array());
/**
* Returns the number of plurals supported by a given language.
*
* @param null|string $langcode
* (optional) The language code. If not provided, the current language
* will be used.
*
* @return int
* Number of plural variants supported by the given language.
*/
public function getNumberOfPlurals($langcode = NULL);
}

View file

@ -8,8 +8,7 @@
namespace Drupal\Core\StringTranslation;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Language\LanguageDefault;
use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
/**
@ -17,13 +16,6 @@ use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
*/
class TranslationManager implements TranslationInterface, TranslatorInterface {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* An array of active translators keyed by priority.
*
@ -53,36 +45,14 @@ class TranslationManager implements TranslationInterface, TranslatorInterface {
*/
protected $defaultLangcode;
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Constructs a TranslationManager object.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\State\StateInterface $state
* (optional) The state service.
* @param \Drupal\Core\Language\LanguageDefault $default_language
* The default language.
*/
public function __construct(LanguageManagerInterface $language_manager, StateInterface $state = NULL) {
$this->languageManager = $language_manager;
$this->defaultLangcode = $language_manager->getDefaultLanguage()->getId();
$this->state = $state;
}
/**
* Initializes the injected language manager with the translation manager.
*
* This should be called right after instantiating the translation manager to
* make it available to the language manager without introducing a circular
* dependency.
*/
public function initLanguageManager() {
$this->languageManager->setTranslation($this);
public function __construct(LanguageDefault $default_language) {
$this->defaultLangcode = $default_language->get()->getId();
}
/**
@ -140,21 +110,14 @@ class TranslationManager implements TranslationInterface, TranslatorInterface {
* {@inheritdoc}
*/
public function translate($string, array $args = array(), array $options = array()) {
$string = $this->doTranslate($string, $options);
if (empty($args)) {
// We add the string to the safe list as opposed to making it an object
// implementing SafeStringInterface as we may need to call __toString()
// on the object before render time, at which point the string ceases to
// be safe, and working around this would require significant rework.
// Adding this string to the safe list is assumed to be safe because
// translate() should only be called with strings defined in code.
// @see \Drupal\Core\StringTranslation\TranslationInterface::translate()
SafeMarkup::setMultiple([$string => ['html' => TRUE]]);
return $string;
}
else {
return SafeMarkup::format($string, $args);
}
return new TranslatableMarkup($string, $args, $options, $this);
}
/**
* {@inheritdoc}
*/
public function translateString(TranslatableMarkup $translated_string) {
return $this->doTranslate($translated_string->getUntranslatedString(), $translated_string->getOptions());
}
/**
@ -172,13 +135,11 @@ class TranslationManager implements TranslationInterface, TranslatorInterface {
* The translated string.
*/
protected function doTranslate($string, array $options = array()) {
// Merge in defaults.
if (empty($options['langcode'])) {
$options['langcode'] = $this->defaultLangcode;
}
if (empty($options['context'])) {
$options['context'] = '';
}
// Merge in options defaults.
$options = $options + [
'langcode' => $this->defaultLangcode,
'context' => '',
];
$translation = $this->getStringTranslation($options['langcode'], $string, $options['context']);
return $translation === FALSE ? $string : $translation;
}
@ -187,43 +148,7 @@ class TranslationManager implements TranslationInterface, TranslatorInterface {
* {@inheritdoc}
*/
public function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()) {
$translatable_string = implode(LOCALE_PLURAL_DELIMITER, array($singular, $plural));
$translated_strings = $this->doTranslate($translatable_string, $options);
return $this->formatPluralTranslated($count, $translated_strings, $args, $options);
}
/**
* {@inheritdoc}
*/
public function formatPluralTranslated($count, $translation, array $args = array(), array $options = array()) {
$args['@count'] = $count;
$translated_array = explode(LOCALE_PLURAL_DELIMITER, $translation);
if ($count == 1) {
return SafeMarkup::format($translated_array[0], $args);
}
// Get the plural index through the gettext formula.
// @todo implement static variable to minimize function_exists() usage.
$index = (function_exists('locale_get_plural')) ? locale_get_plural($count, isset($options['langcode']) ? $options['langcode'] : NULL) : -1;
if ($index == 0) {
// Singular form.
$return = $translated_array[0];
}
else {
if (isset($translated_array[$index])) {
// N-th plural form.
$return = $translated_array[$index];
}
else {
// If the index cannot be computed or there's no translation, use
// the second plural form as a fallback (which allows for most flexibility
// with the replaceable @count value).
$return = $translated_array[1];
}
}
return SafeMarkup::format($return, $args);
return new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $this);
}
/**
@ -248,21 +173,4 @@ class TranslationManager implements TranslationInterface, TranslatorInterface {
}
}
/**
* @inheritdoc.
*/
public function getNumberOfPlurals($langcode = NULL) {
// If the state service is not injected, we assume 2 plural variants are
// allowed. This may happen in the installer for simplicity. We also assume
// 2 plurals if there is no explicit information yet.
if (isset($this->state)) {
$langcode = $langcode ?: $this->languageManager->getCurrentLanguage()->getId();
$plural_formulas = $this->state->get('locale.translation.plurals') ?: array();
if (isset($plural_formulas[$langcode]['plurals'])) {
return $plural_formulas[$langcode]['plurals'];
}
}
return 2;
}
}

View file

@ -7,125 +7,10 @@
namespace Drupal\Core\StringTranslation;
use Drupal\Component\Utility\SafeStringInterface;
/**
* Provides a class to wrap a translatable string.
* Provides translatable string class.
*
* This class can be used to delay translating strings until the translation
* system is ready. This is useful for using translation in very low level
* subsystems like entity definition and stream wrappers.
*
* @see \Drupal\Core\Annotation\Translation
* @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
* Use the \Drupal\Core\StringTranslation\TranslatableMarkup class instead.
*/
class TranslationWrapper implements SafeStringInterface {
use StringTranslationTrait;
/**
* The string to be translated.
*
* @var string
*/
protected $string;
/**
* The translation arguments.
*
* @var array
*/
protected $arguments;
/**
* The translation options.
*
* @var array
*/
protected $options;
/**
* 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.
*/
public function __construct($string, array $arguments = array(), array $options = array()) {
$this->string = $string;
$this->arguments = $arguments;
$this->options = $options;
}
/**
* Gets the untranslated string value stored in this translation wrapper.
*
* @return string
* The string stored in this wrapper.
*/
public function getUntranslatedString() {
return $this->string;
}
/**
* Gets a specific option from this translation wrapper.
*
* @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 translation wrapper.
*
* @return mixed[]
* The array of options.
*/
public function getOptions() {
return $this->options;
}
/**
* Implements the magic __toString() method.
*/
public function __toString() {
return $this->render();
}
/**
* Renders the object as a string.
*
* @return string
* The translated string.
*/
public function render() {
return $this->t($this->string, $this->arguments, $this->options);
}
/**
* 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();
}
}
class TranslationWrapper extends TranslatableMarkup {}