summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2013-10-29 21:29:39 (GMT)
committerwebchick2013-10-29 21:29:39 (GMT)
commita14565288672aa65ce8898a398e711ba216498ee (patch)
tree759dab377ea3fb6dcab7cdd0d0754fc639733539
parent55a6dd8f1f316be4119f21e4511834a8edf41605 (diff)
Issue #2098089 by penyaskito, Schnitzel, tim.plunkett, YesCT, EllaTheHarpy, Gábor Hojtsy: Fixed Date formats cannot be translated (the core UIs are useless).
-rw-r--r--core/lib/Drupal/Core/Config/Context/LanguageConfigContext.php (renamed from core/modules/language/lib/Drupal/language/LanguageConfigContext.php)13
-rw-r--r--core/lib/Drupal/Core/Datetime/Date.php28
-rw-r--r--core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php4
-rw-r--r--core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigTranslationTest.php31
-rw-r--r--core/modules/system/config/schema/system.data_types.schema.yml6
-rw-r--r--core/modules/system/config/schema/system.schema.yml2
6 files changed, 68 insertions, 16 deletions
diff --git a/core/modules/language/lib/Drupal/language/LanguageConfigContext.php b/core/lib/Drupal/Core/Config/Context/LanguageConfigContext.php
index 58b83a6..ab65df1 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 dbd3dcf..ed6db12 100644
--- a/core/lib/Drupal/Core/Datetime/Date.php
+++ b/core/lib/Drupal/Core/Datetime/Date.php
@@ -110,13 +110,13 @@ class Date {
$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 @@ class Date {
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 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 @@ class ConfigLocaleOverride extends DrupalUnitTestBase {
)));
$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 @@ class ConfigLocaleOverride extends DrupalUnitTestBase {
// 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..3c219ec 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 @@ class LocaleConfigTranslationTest extends WebTestBase {
$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 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