diff --git a/core/lib/Drupal/Core/Datetime/Date.php b/core/lib/Drupal/Core/Datetime/Date.php index dbd3dcf..67d6519 100644 --- a/core/lib/Drupal/Core/Datetime/Date.php +++ b/core/lib/Drupal/Core/Datetime/Date.php @@ -110,13 +110,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 +127,35 @@ 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 interface language format pattern. + * @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) { + $langcode = $this->languageManager->getLanguage(Language::TYPE_INTERFACE)->id; + } + 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. + $needs_language_context = $needs_language_context && + \Drupal::moduleHandler()->moduleExists('language'); + if ($needs_language_context) { + $language_context = config_context_enter('Drupal\language\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/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