diff --git a/core/core.services.yml b/core/core.services.yml index a827835..e383fde 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -555,7 +555,7 @@ services: arguments: ['@module_handler'] date: class: Drupal\Core\Datetime\Date - arguments: ['@entity.manager', '@language_manager'] + arguments: ['@entity.manager', '@language_manager', '@config.factory'] feed.bridge.reader: class: Drupal\Component\Bridge\ZfExtensionManagerSfContainer calls: diff --git a/core/modules/language/lib/Drupal/language/LanguageConfigContext.php b/core/lib/Drupal/Core/Config/Context/LanguageConfigContext.php similarity index 90% rename from core/modules/language/lib/Drupal/language/LanguageConfigContext.php rename to core/lib/Drupal/Core/Config/Context/LanguageConfigContext.php index 58b83a6..2c9acaa 100644 --- a/core/modules/language/lib/Drupal/language/LanguageConfigContext.php +++ b/core/lib/Drupal/Core/Config/Context/LanguageConfigContext.php @@ -2,10 +2,10 @@ /** * @file - * Contains \Drupal\language\LanguageConfigContext. + * Contains \Drupal\Core\Config\Context\LanguageConfigContext. */ -namespace Drupal\language; +namespace Drupal\Core\Config\Context; use Drupal\Core\Config\Context\ConfigContext; use Drupal\Core\Language\Language; diff --git a/core/lib/Drupal/Core/Datetime/Date.php b/core/lib/Drupal/Core/Datetime/Date.php index dbd3dcf..72c68f0 100644 --- a/core/lib/Drupal/Core/Datetime/Date.php +++ b/core/lib/Drupal/Core/Datetime/Date.php @@ -8,6 +8,8 @@ namespace Drupal\Core\Datetime; use Drupal\Component\Utility\Xss; +use Drupal\Core\Config\ConfigFactory; +use Drupal\Core\Config\Context\LanguageConfigContext; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Entity\EntityManager; use Drupal\Core\Language\Language; @@ -39,6 +41,13 @@ class Date { */ protected $languageManager; + /** + * Config factory for handling language contexts for date formats. + * + * @var \Drupal\Core\Config\ConfigFactory + */ + protected $configFactory; + protected $country = NULL; protected $dateFormats = array(); @@ -50,9 +59,10 @@ class Date { * @param \Drupal\Core\Language\LanguageManager $language_manager * The language manager. */ - public function __construct(EntityManager $entity_manager, LanguageManager $language_manager) { + public function __construct(EntityManager $entity_manager, LanguageManager $language_manager, ConfigFactory $config_factory) { $this->dateFormatStorage = $entity_manager->getStorageController('date_format'); $this->languageManager = $language_manager; + $this->configFactory = $config_factory; } /** @@ -110,13 +120,13 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N $key = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP; // If we have a non-custom date format use the provided date format pattern. - if ($date_format = $this->dateFormat($type)) { + if ($date_format = $this->dateFormat($type, $langcode)) { $format = $date_format->getPattern($key); } // Fall back to medium if a format was not found. if (empty($format)) { - $format = $this->dateFormat('fallback')->getPattern($key); + $format = $this->dateFormat('fallback')->getPattern($key, $langcode); } // Call $date->format(). @@ -127,11 +137,44 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N return Xss::filter($date->format($format, $settings)); } - protected function dateFormat($format) { - if (!isset($this->dateFormats[$format])) { - $this->dateFormats[$format] = $this->dateFormatStorage->load($format); + /** + * Loads the given format pattern for the given langcode. + * + * @param string $format + * The machine name of the date format. + * @param string $langcode + * The langcode of the language to use. If NULL, we load the current context + * language if any or the interface language format pattern as the last fallback. + * @return string + * The pattern for the date format in the given language. + */ + protected function dateFormat($format, $langcode = NULL) { + // If a langcode is not given, we use the interface language. + $needs_language_context = !empty($langcode); + if (!$needs_language_context) { + // If we are at any context we try to get the language from there. + $context = $this->configFactory->getContext(); + if ($context !== NULL && $contextLanguage = $context->get(LanguageConfigContext::LANGUAGE_KEY)) { + $langcode = $contextLanguage; + } + else { + $langcode = $this->languageManager->getLanguage(Language::TYPE_INTERFACE)->id; + $needs_language_context = TRUE; + } + } + if (!isset($this->dateFormats[$format][$langcode])) { + // Enter a language specific context for the language if some language is + // given, so the right date format is loaded. + if ($needs_language_context) { + $language_context = config_context_enter('Drupal\Core\Config\Context\LanguageConfigContext'); + $language_context->setLanguage(new Language(array('id' => $langcode))); + } + $this->dateFormats[$format][$langcode] = $this->dateFormatStorage->load($format); + if ($needs_language_context) { + config_context_leave(); + } } - return $this->dateFormats[$format]; + return $this->dateFormats[$format][$langcode]; } /** diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php b/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php index f3f3ea6..24edb76 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php @@ -170,7 +170,7 @@ function testConfigLocaleLanguageOverride() { ))); $language = language_load('fr'); - $language_config_context = config_context_enter('Drupal\language\LanguageConfigContext'); + $language_config_context = config_context_enter('Drupal\Core\Config\Context\LanguageConfigContext'); $language_config_context->setLanguage($language); $config = \Drupal::config('config_test.system'); $this->assertIdentical($config->get('foo'), 'fr bar'); @@ -196,7 +196,7 @@ function testConfigLocaleLanguageOverride() { // Enter an english context on top of the german context. $language = language_load('en'); // Create a new language config context to stack on top of the existing one. - $en_language_config_context = config_context_enter('Drupal\language\LanguageConfigContext'); + $en_language_config_context = config_context_enter('Drupal\Core\Config\Context\LanguageConfigContext'); $en_language_config_context->setLanguage($language); $config = \Drupal::config('config_test.system'); $this->assertIdentical($config->get('foo'), 'en bar'); diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigTranslationTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigTranslationTest.php index 7299826..a783826 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigTranslationTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigTranslationTest.php @@ -89,6 +89,37 @@ function testConfigTranslation() { $this->drupalGet($langcode); $this->assertText($site_name, 'The translated site name is displayed after translations refreshed.'); + // Check default medium date format exists and create a translation for it. + $string = $this->storage->findString(array('source' => 'D, m/d/Y - H:i', 'context' => '', 'type' => 'configuration')); + $this->assertTrue($string, 'Configuration date formats have been created upon installation.'); + + // Translate using the UI so configuration is refreshed. + $site_name = $this->randomName(20); + $search = array( + 'string' => $string->source, + 'langcode' => $langcode, + 'translation' => 'all', + ); + $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); + $textareas = $this->xpath('//textarea'); + $textarea = current($textareas); + $lid = (string) $textarea[0]['name']; + $edit = array( + $lid => 'D', + ); + $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations')); + + $wrapper = $this->container->get('locale.config.typed')->get('system.date_format.medium'); + + // Get translation and check we've only got the site name. + $translation = $wrapper->getTranslation($langcode); + $format = $translation->get('pattern')->get('php')->getValue(); + $this->assertEqual($format, 'D', 'Got the right date format pattern after translation.'); + + // Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should display "Tue". + $formatted_date = format_date(494015820, $type = 'medium', NULL, NULL, $langcode = $langcode); + $this->assertEqual($formatted_date, 'Tue', 'Got the right formatted date using the date format translation pattern.'); + // Assert strings from image module config are not available. $string = $this->storage->findString(array('source' => 'Medium (220x220)', 'context' => '', 'type' => 'configuration')); $this->assertFalse($string, 'Configuration strings have been created upon installation.'); diff --git a/core/modules/system/config/schema/system.data_types.schema.yml b/core/modules/system/config/schema/system.data_types.schema.yml index 7c5a420..ab375b6 100644 --- a/core/modules/system/config/schema/system.data_types.schema.yml +++ b/core/modules/system/config/schema/system.data_types.schema.yml @@ -53,6 +53,12 @@ text: label: 'Text' translatable: true +# PHP Date format string that is translatable. +date_format: + type: string + label: 'PHP date format' + translatable: true + # Complex extended data types: # Mail text with subject and body parts. diff --git a/core/modules/system/config/schema/system.schema.yml b/core/modules/system/config/schema/system.schema.yml index 82fcec6..020e9d3 100644 --- a/core/modules/system/config/schema/system.schema.yml +++ b/core/modules/system/config/schema/system.schema.yml @@ -141,7 +141,7 @@ system.date_format.*: label: 'Format string' mapping: php: - type: string + type: date_format label: 'PHP date format' intl: type: string