diff --git a/core/modules/locale/locale.install b/core/modules/locale/locale.install index 3f837f0f5d..225e952a7d 100644 --- a/core/modules/locale/locale.install +++ b/core/modules/locale/locale.install @@ -303,20 +303,3 @@ function locale_update_8300() { // the new key value collection. \Drupal::state()->delete('locale.translation_status'); } - -/** - * Fix old plural style. - */ -function locale_update_8400() { - $batch = [ - 'operations' => [ - ['_locale_fix_old_plural_style_batch_proccess', []] - ], - 'finished' => '_locale_fix_old_plural_style_batch_finished', - 'title' => t('Processing'), - 'progress_message' => '', - 'error_message' => t('The update has encountered an error.'), - 'file' => drupal_get_path('module', 'locale') . '/locale.bulk.inc', - ]; - batch_set($batch); -} diff --git a/core/modules/locale/locale.post_update.php b/core/modules/locale/locale.post_update.php new file mode 100644 index 0000000000..122e9c1c75 --- /dev/null +++ b/core/modules/locale/locale.post_update.php @@ -0,0 +1,18 @@ + [ + ['_locale_fix_old_plural_style_batch_proccess', []] + ], + 'finished' => '_locale_fix_old_plural_style_batch_finished', + 'title' => t('Processing'), + 'progress_message' => '', + 'error_message' => t('The update has encountered an error.'), + 'file' => drupal_get_path('module', 'locale') . '/locale.bulk.inc', + ]; + batch_set($batch); +} diff --git a/core/modules/locale/src/LocaleLookup.php b/core/modules/locale/src/LocaleLookup.php index 9290efc305..213ba1e486 100644 --- a/core/modules/locale/src/LocaleLookup.php +++ b/core/modules/locale/src/LocaleLookup.php @@ -171,6 +171,10 @@ protected function resolveCacheMiss($offset) { } } } + else { + // Fix old style plural problem, when value contains @count[0-9]. + $value = preg_replace('!@count\[\d+\]!', '@count', $value); + } $this->storage[$offset] = $value; // Disabling the usage of string caching allows a module to watch for diff --git a/core/modules/locale/tests/src/Functional/LocaleLocaleLookupTest.php b/core/modules/locale/tests/src/Functional/LocaleLocaleLookupTest.php index 5f3e5f7e80..3d2cca417e 100644 --- a/core/modules/locale/tests/src/Functional/LocaleLocaleLookupTest.php +++ b/core/modules/locale/tests/src/Functional/LocaleLocaleLookupTest.php @@ -4,6 +4,7 @@ use Drupal\language\Entity\ConfigurableLanguage; use Drupal\Tests\BrowserTestBase; +use Drupal\Tests\Core\Cache\CacheCollectorHelper; /** * Tests LocaleLookup. @@ -55,4 +56,38 @@ public function testLanguageFallbackDefaults() { $this->assertEqual($context['operation'], 'locale_lookup'); } + /** + * Test old plural style @count[number] fix. + */ + public function testFixOldPluralStyle() { + /** @var \Drupal\locale\StringDatabaseStorage $string_storage */ + $string_storage = \Drupal::service('locale.storage'); + + $string = $string_storage->findString(['source' => 'Member for', 'context' => '']); + + // Create translation with old @count[2] plural style. + $string_storage->createTranslation([ + 'lid' => $string->getId(), + 'language' => 'fr', + 'translation' => '@count[2] old-plural-test', + ])->save(); + + // Assert that @count[2] was fixed with caching. + drupal_flush_all_caches(); + $this->drupalGet(''); + $this->assertSession()->pageTextContains('@count old-plural-test'); + + // Assert that @count[2] wasn't fixed after caching. + drupal_flush_all_caches(); + $collector = new CacheCollectorHelper('locale:fr::authenticated', $this->container->get('cache.default'), $this->container->get('lock')); + $collector->set('Member for', '@count[2] old-plural back'); + $collector->destruct(); + $this->drupalGet(''); + $this->assertSession()->pageTextContains('@count[2] old-plural back'); + + // Assert that source not changed. + $translation = $string_storage->findTranslation(['language' => 'fr', 'lid' => $string->getId()])->translation; + $this->assertSame('@count[2] old-plural-test', $translation); + } + }