Commit f347360a authored by alexpott's avatar alexpott

Issue #2428837 by swentel, borisson_: Adding/updating interface translations...

Issue #2428837 by swentel, borisson_: Adding/updating interface translations should invalidate page & render caches
parent 0bcac2f0
......@@ -25,6 +25,8 @@
use Drupal\language\ConfigurableLanguageInterface;
use Drupal\Component\Utility\Crypt;
use Drupal\locale\Locale;
use Drupal\locale\LocaleEvent;
use Drupal\locale\LocaleEvents;
/**
* Regular expression pattern used to localize JavaScript strings.
......@@ -1037,6 +1039,7 @@ function locale_string_is_safe($string) {
* The information that will be refreshed includes:
* - JavaScript translations.
* - Locale cache.
* - Render cache.
*
* @param array $langcodes
* Language codes for updated translations.
......@@ -1052,8 +1055,9 @@ function _locale_refresh_translations($langcodes, $lids = array()) {
array_map('_locale_invalidate_js', $langcodes);
}
}
// Clear locale cache.
Cache::invalidateTags(array('locale'));
// Throw locale.save_translation event.
\Drupal::service('event_dispatcher')->dispatch(LocaleEvents::SAVE_TRANSLATION, new LocaleEvent($langcodes, $lids));
}
/**
......
......@@ -25,3 +25,8 @@ services:
arguments: ['@config.factory', '@locale.config_manager']
tags:
- { name: event_subscriber }
locale.locale_translation_cache_tag:
class: Drupal\locale\EventSubscriber\LocaleTranslationCacheTag
arguments: ['@cache_tags.invalidator']
tags:
- { name: event_subscriber }
<?php
/**
* @file
* Contains \Drupal\locale\EventSubscriber\LocaleTranslationCacheTag.
*/
namespace Drupal\locale\EventSubscriber;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\locale\LocaleEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* A subscriber invalidating cache tags when translating a string.
*/
class LocaleTranslationCacheTag implements EventSubscriberInterface {
/**
* The cache tags invalidator.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
*/
protected $cacheTagsInvalidator;
/**
* Constructs a LocaleTranslationCacheTag object.
*
* @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
* The cache tags invalidator.
*/
public function __construct(CacheTagsInvalidatorInterface $cache_tags_invalidator) {
$this->cacheTagsInvalidator = $cache_tags_invalidator;
}
/**
* Invalidate cache tags whenever a string is translated.
*/
public function saveTranslation() {
$this->cacheTagsInvalidator->invalidateTags(['rendered', 'locale']);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[LocaleEvents::SAVE_TRANSLATION][] = ['saveTranslation'];
return $events;
}
}
<?php
/**
* @file
* Contains \Drupal\locale\LocaleEvent.
*/
namespace Drupal\locale;
use Symfony\Component\EventDispatcher\Event;
/**
* Defines a Locale event.
*/
class LocaleEvent extends Event {
/**
* The list of Language codes for updated translations.
*
* @var string[]
*/
protected $langCodes;
/**
* List of string identifiers that have been updated / created.
*
* @var string[]
*/
protected $original;
/**
* Constructs a new LocaleEvent.
*
* @param array $lang_codes
* Language codes for updated translations.
* @param array $lids
* (optional) List of string identifiers that have been updated / created.
*/
public function __construct(array $lang_codes, array $lids = array()) {
$this->langCodes = $lang_codes;
$this->lids = $lids;
}
/**
* Returns the language codes.
*
* @return string[] $langCodes
*/
public function getLangCodes() {
return $this->langCodes;
}
/**
* Returns the string identifiers.
*
* @return array $lids
*/
public function getLids() {
return $this->lids;
}
}
<?php
/**
* @file
* Contains \Drupal\locale\LocaleEvents.
*/
namespace Drupal\locale;
/**
* Defines events for locale translation.
*
* @see \Drupal\Core\Config\ConfigCrudEvent
*/
final class LocaleEvents {
/**
* The name of the event fired when saving a translated string.
*
* This event allows you to perform custom actions whenever a translated
* string is saved.
*
* @Event
*
* @see \Drupal\locale\EventSubscriber\LocaleTranslationCacheTag
*/
const SAVE_TRANSLATION = 'locale.save_translation';
}
......@@ -81,11 +81,6 @@ public function testStringTranslation() {
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this->assertText($name, 'Search found the string as untranslated.');
// Assume this is the only result, given the random name.
// We save the lid from the path.
$textarea = current($this->xpath('//textarea'));
$lid = (string) $textarea[0]['name'];
// No t() here, it's surely not translated yet.
$this->assertText($name, 'name found on edit screen.');
$this->assertNoOption('edit-langcode', 'en', 'No way to translate the string to English.');
......@@ -135,10 +130,6 @@ public function testStringTranslation() {
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this->assertRaw($translation_to_en, 'English translation properly saved.');
// Reset the tag cache on the tester side in order to pick up the call to
// Cache::invalidateTags() on the tested side.
\Drupal::service('cache_tags.invalidator.checksum')->reset();
$this->assertTrue($name != $translation && t($name, array(), array('langcode' => $langcode)) == $translation, 't() works for non-English.');
// Refresh the locale() cache to get fresh data from t() below. We are in
// the same HTTP request and therefore t() is not refreshed by saving the
......@@ -156,7 +147,28 @@ public function testStringTranslation() {
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this->assertText(t('No strings available.'), 'String is translated.');
// Test invalidation of 'rendered' cache tag after string translation.
$this->drupalLogout();
$this->drupalGet('xx/user/login');
$this->assertText('Enter the password that accompanies your username.');
$this->drupalLogin($translate_user);
$search = array(
'string' => 'accompanies your username',
'langcode' => $langcode,
'translation' => 'untranslated',
);
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$textarea = current($this->xpath('//textarea'));
$lid = (string) $textarea[0]['name'];
$edit = array(
$lid => 'Please enter your Llama username.',
);
$this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$this->drupalLogout();
$this->drupalGet('xx/user/login');
$this->assertText('Please enter your Llama username.');
// Delete the language.
$this->drupalLogin($admin_user);
......
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