diff --git a/core/modules/language/lib/Drupal/language/LanguageConfigContext.php b/core/lib/Drupal/Core/Config/Context/LanguageConfigContext.php similarity index 78% rename from core/modules/language/lib/Drupal/language/LanguageConfigContext.php rename to core/lib/Drupal/Core/Config/Context/LanguageConfigContext.php index 58b83a6df9b104305ec781012d0f075c2d9f8122..ab65df113eb434b0d1dce8c7d4ba8d7b53f63b92 100644 --- a/core/modules/language/lib/Drupal/language/LanguageConfigContext.php +++ b/core/lib/Drupal/Core/Config/Context/LanguageConfigContext.php @@ -2,21 +2,19 @@ /** * @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; - /** * Defines a configuration context object for a language. * * This should be used when configuration objects need a context for a language * other than the current language. - * */ class LanguageConfigContext extends ConfigContext { @@ -29,14 +27,15 @@ class LanguageConfigContext extends ConfigContext { * Creates the configuration context for language. * * @param \Drupal\Core\Language\Language $language - * The language to add to the config context. + * The language object to add to the config context. * * @return \Drupal\Core\Language\Language - * The language config context object. + * The config context language object. */ public function setLanguage(Language $language) { $this->set(self::LANGUAGE_KEY, $language); - // Re-initialize since the language change changes the context fundamentally. + // Re-initialize since the language change changes the context + // fundamentally. $this->init(); return $this; } diff --git a/core/lib/Drupal/Core/Datetime/Date.php b/core/lib/Drupal/Core/Datetime/Date.php index dbd3dcfd40e9b7e2c100f0de717a57d0b14d9c1d..ed6db12c4c684315afca30185bed4072d3d1426e 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', $langcode)->getPattern($key); } // Call $date->format(). @@ -127,11 +127,27 @@ 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. + * + * @return string + * The pattern for the date format in the given language. + */ + protected function dateFormat($format, $langcode) { + if (!isset($this->dateFormats[$format][$langcode])) { + // Enter a language specific context so the right date format is loaded. + $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); + 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 f3f3ea6ff4314984a0aedfae35e901627689764c..24edb76443d1589ecce2a05a8e5e8ecd7d9b252e 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 72998261ea0416101166ae322557fe22d9e430be..3c219ec0dc39706138473ca5f83cba5ad1094dbe 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. + $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); + $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 7c5a420c9e8c68445f2d07aead86ca5f587525d1..ab375b6ebae14a3c1e245b2c565de8361de8d2f8 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 82fcec695248de06ed488d3080e2da593ff3c43f..020e9d300505482813952189ab9e769d7cdccd0c 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