Commit a1456528 authored by webchick's avatar webchick

Issue #2098089 by penyaskito, Schnitzel, tim.plunkett, YesCT, EllaTheHarpy,...

Issue #2098089 by penyaskito, Schnitzel, tim.plunkett, YesCT, EllaTheHarpy, Gábor Hojtsy: Fixed Date formats cannot be translated (the core UIs are useless).
parent 55a6dd8f
...@@ -2,21 +2,19 @@ ...@@ -2,21 +2,19 @@
/** /**
* @file * @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\Config\Context\ConfigContext;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
/** /**
* Defines a configuration context object for a language. * Defines a configuration context object for a language.
* *
* This should be used when configuration objects need a context for a language * This should be used when configuration objects need a context for a language
* other than the current language. * other than the current language.
*
*/ */
class LanguageConfigContext extends ConfigContext { class LanguageConfigContext extends ConfigContext {
...@@ -29,14 +27,15 @@ class LanguageConfigContext extends ConfigContext { ...@@ -29,14 +27,15 @@ class LanguageConfigContext extends ConfigContext {
* Creates the configuration context for language. * Creates the configuration context for language.
* *
* @param \Drupal\Core\Language\Language $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 * @return \Drupal\Core\Language\Language
* The language config context object. * The config context language object.
*/ */
public function setLanguage(Language $language) { public function setLanguage(Language $language) {
$this->set(self::LANGUAGE_KEY, $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(); $this->init();
return $this; return $this;
} }
......
...@@ -110,13 +110,13 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N ...@@ -110,13 +110,13 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N
$key = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP; $key = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP;
// If we have a non-custom date format use the provided date format pattern. // 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); $format = $date_format->getPattern($key);
} }
// Fall back to medium if a format was not found. // Fall back to medium if a format was not found.
if (empty($format)) { if (empty($format)) {
$format = $this->dateFormat('fallback')->getPattern($key); $format = $this->dateFormat('fallback', $langcode)->getPattern($key);
} }
// Call $date->format(). // Call $date->format().
...@@ -127,11 +127,27 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N ...@@ -127,11 +127,27 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N
return Xss::filter($date->format($format, $settings)); return Xss::filter($date->format($format, $settings));
} }
protected function dateFormat($format) { /**
if (!isset($this->dateFormats[$format])) { * Loads the given format pattern for the given langcode.
$this->dateFormats[$format] = $this->dateFormatStorage->load($format); *
* @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];
} }
/** /**
......
...@@ -170,7 +170,7 @@ function testConfigLocaleLanguageOverride() { ...@@ -170,7 +170,7 @@ function testConfigLocaleLanguageOverride() {
))); )));
$language = language_load('fr'); $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); $language_config_context->setLanguage($language);
$config = \Drupal::config('config_test.system'); $config = \Drupal::config('config_test.system');
$this->assertIdentical($config->get('foo'), 'fr bar'); $this->assertIdentical($config->get('foo'), 'fr bar');
...@@ -196,7 +196,7 @@ function testConfigLocaleLanguageOverride() { ...@@ -196,7 +196,7 @@ function testConfigLocaleLanguageOverride() {
// Enter an english context on top of the german context. // Enter an english context on top of the german context.
$language = language_load('en'); $language = language_load('en');
// Create a new language config context to stack on top of the existing one. // 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); $en_language_config_context->setLanguage($language);
$config = \Drupal::config('config_test.system'); $config = \Drupal::config('config_test.system');
$this->assertIdentical($config->get('foo'), 'en bar'); $this->assertIdentical($config->get('foo'), 'en bar');
......
...@@ -89,6 +89,37 @@ function testConfigTranslation() { ...@@ -89,6 +89,37 @@ function testConfigTranslation() {
$this->drupalGet($langcode); $this->drupalGet($langcode);
$this->assertText($site_name, 'The translated site name is displayed after translations refreshed.'); $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. // Assert strings from image module config are not available.
$string = $this->storage->findString(array('source' => 'Medium (220x220)', 'context' => '', 'type' => 'configuration')); $string = $this->storage->findString(array('source' => 'Medium (220x220)', 'context' => '', 'type' => 'configuration'));
$this->assertFalse($string, 'Configuration strings have been created upon installation.'); $this->assertFalse($string, 'Configuration strings have been created upon installation.');
......
...@@ -53,6 +53,12 @@ text: ...@@ -53,6 +53,12 @@ text:
label: 'Text' label: 'Text'
translatable: true translatable: true
# PHP Date format string that is translatable.
date_format:
type: string
label: 'PHP date format'
translatable: true
# Complex extended data types: # Complex extended data types:
# Mail text with subject and body parts. # Mail text with subject and body parts.
......
...@@ -141,7 +141,7 @@ system.date_format.*: ...@@ -141,7 +141,7 @@ system.date_format.*:
label: 'Format string' label: 'Format string'
mapping: mapping:
php: php:
type: string type: date_format
label: 'PHP date format' label: 'PHP date format'
intl: intl:
type: string type: string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment