From d50a62fc3dc630b1ed6b24a42e2fe06c976f0447 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Fri, 11 Oct 2013 14:09:25 +0100 Subject: [PATCH] Issue #1975490 by ParisLiakos: Convert locale_custom_strings_* to settings. --- core/core.services.yml | 1 + core/includes/install.core.inc | 3 +- .../Translator/CustomStrings.php | 22 ++++++++- .../lib/Drupal/simpletest/WebTestBase.php | 45 +++++++++++++++++++ .../system/Tests/Common/FormatDateTest.php | 6 +-- .../system/Tests/Menu/MenuRouterTest.php | 7 ++- .../Plugin/CacheDecoratorLanguageTest.php | 7 ++- .../system/Tests/System/PageTitleTest.php | 3 +- sites/default/default.settings.php | 31 +++++++------ 9 files changed, 101 insertions(+), 24 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index 608c439c415f..465d1458c0b0 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -202,6 +202,7 @@ services: arguments: ['@state'] string_translator.custom_strings: class: Drupal\Core\StringTranslation\Translator\CustomStrings + arguments: ['@settings'] tags: - { name: string_translator, priority: 30 } string_translation: diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index d547daf4242f..9b0ef4ca008d 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -1505,7 +1505,8 @@ function install_translations_directory() { function install_register_translation_service(ContainerBuilder $container) { $container->register('string_translator.file_translation', 'Drupal\Core\StringTranslation\Translator\FileTranslation') ->addArgument(install_translations_directory()); - $container->register('string_translator.custom_strings', 'Drupal\Core\StringTranslation\Translator\CustomStrings'); + $container->register('string_translator.custom_strings', 'Drupal\Core\StringTranslation\Translator\CustomStrings') + ->addArgument(settings()); $container->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager') ->addMethodCall('addTranslator', array(new Reference('string_translator.file_translation'))) ->addMethodCall('addTranslator', array(new Reference('string_translator.custom_strings'))); diff --git a/core/lib/Drupal/Core/StringTranslation/Translator/CustomStrings.php b/core/lib/Drupal/Core/StringTranslation/Translator/CustomStrings.php index a1ed337fc426..3debb1f9a914 100644 --- a/core/lib/Drupal/Core/StringTranslation/Translator/CustomStrings.php +++ b/core/lib/Drupal/Core/StringTranslation/Translator/CustomStrings.php @@ -7,6 +7,8 @@ namespace Drupal\Core\StringTranslation\Translator; +use Drupal\Component\Utility\Settings; + /** * String translator using overrides from variables. * @@ -15,11 +17,29 @@ */ class CustomStrings extends StaticTranslation { + /** + * The settings read only object. + * + * @var \Drupal\Component\Utility\Settings + */ + protected $settings; + + /** + * Constructs a CustomStrings object. + * + * @param \Drupal\Component\Utility\Settings $settings + * The settings read only object. + */ + public function __construct(Settings $settings) { + parent::__construct(); + $this->settings = $settings; + } + /** * {@inheritdoc} */ protected function loadLanguage($langcode) { - return variable_get('locale_custom_strings_' . $langcode, array()); + return $this->settings->get('locale_custom_strings_' . $langcode, array()); } } diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index a064d01a9f0c..a307e32af876 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -177,6 +177,13 @@ abstract class WebTestBase extends TestBase { */ protected $curlCookies = array(); + /** + * An array of custom translations suitable for drupal_rewrite_settings(). + * + * @var array + */ + protected $customTranslations; + /** * Constructor for \Drupal\simpletest\WebTestBase. */ @@ -917,6 +924,44 @@ protected function writeSettings($settings) { } } + /** + * Sets custom translations to the settings object and queues them to writing. + * + * In order for those custom translations to persist (being written in test + * site's settings.php) make sure to also call self::writeCustomTranslations() + * + * @param string $langcode + * The langcode to add translations for. + * @param array $values + * Array of values containing the untranslated string and its translation. + * For example: + * @code + * array( + * '' => array('Sunday' => 'domingo'), + * 'Long month name' => array('March' => 'marzo'), + * ); + * @endcode + */ + protected function addCustomTranslations($langcode, array $values) { + $this->settingsSet('locale_custom_strings_' . $langcode, $values); + foreach ($values as $key => $translations) { + foreach ($translations as $label => $value) { + $this->customTranslations['locale_custom_strings_' . $langcode][$key][$label] = (object) array( + 'value' => $value, + 'required' => TRUE, + ); + } + } + } + + /** + * Writes custom translations to test site's settings.php. + */ + protected function writeCustomTranslations() { + $this->writeSettings(array('settings' => $this->customTranslations)); + $this->customTranslations = array(); + } + /** * Overrides \Drupal\simpletest\TestBase::rebuildContainer(). */ diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/FormatDateTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/FormatDateTest.php index 0fb2212c2470..4511346494a4 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Common/FormatDateTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Common/FormatDateTest.php @@ -47,16 +47,16 @@ function setUp() { $formats['long']->setPattern('l, j. F Y - G:i')->save(); $formats['medium']->setPattern('j. F Y - G:i')->save(); $formats['short']->setPattern('Y M j - g:ia')->save(); + $this->refreshVariables(); - variable_set('locale_custom_strings_' . self::LANGCODE, array( + $this->settingsSet('locale_custom_strings_' . self::LANGCODE, array( '' => array('Sunday' => 'domingo'), 'Long month name' => array('March' => 'marzo'), )); $language = new Language(array('id' => static::LANGCODE)); language_save($language); - - $this->refreshVariables(); + $this->resetAll(); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Menu/MenuRouterTest.php b/core/modules/system/lib/Drupal/system/Tests/Menu/MenuRouterTest.php index 1a4f746aff79..f3416811f6a6 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Menu/MenuRouterTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Menu/MenuRouterTest.php @@ -387,9 +387,12 @@ protected function doTestMenuItemTitlesCases() { foreach ($test_data as $case_no => $override) { $this->menuItemTitlesCasesHelper($case_no); - variable_set('locale_custom_strings_en', array('' => $override)); + $this->addCustomTranslations('en', array('' => $override)); + $this->writeCustomTranslations(); + $this->menuItemTitlesCasesHelper($case_no, TRUE); - variable_set('locale_custom_strings_en', array()); + $this->addCustomTranslations('en', array()); + $this->writeCustomTranslations(); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorLanguageTest.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorLanguageTest.php index 4aa627005045..34e388967086 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorLanguageTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorLanguageTest.php @@ -68,8 +68,10 @@ public function setUp() { foreach ($this->mockBlockExpectedDefinitions as $plugin_id => $definition) { $custom_strings[$definition['label']] = $langcode . ' ' . $definition['label']; } - variable_set('locale_custom_strings_' . $langcode, array('' => $custom_strings)); + $this->addCustomTranslations($langcode, array('' => $custom_strings)); } + // Write test settings.php with new translations. + $this->writeCustomTranslations(); } /** @@ -111,7 +113,8 @@ public function testCacheDecoratorLanguage() { foreach ($this->mockBlockExpectedDefinitions as $plugin_id => $definition) { $custom_strings[$definition['label']] = $definition['label'] . ' de'; } - variable_set('locale_custom_strings_de', array('' => $custom_strings)); + $this->addCustomTranslations('de', array('' => $custom_strings)); + $this->writeCustomTranslations(); $this->drupalGet('de/plugin_definition_test'); foreach ($this->mockBlockExpectedDefinitions as $plugin_id => $definition) { // Find our provided translations. diff --git a/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php b/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php index b22fd903c67c..d08817d5f03e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php @@ -150,9 +150,10 @@ public function testRoutingTitle() { $this->assertEqual('Test dynamic title', (string) $result[0]); // Set some custom translated strings. - variable_set('locale_custom_strings_en', array('' => array( + $this->addCustomTranslations('en', array('' => array( 'Static title' => 'Static title translated' ))); + $this->writeCustomTranslations(); // Ensure that the title got translated. $this->drupalGet('test-page-static-title'); diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php index 731179fd8ce7..0574b23bea01 100644 --- a/sites/default/default.settings.php +++ b/sites/default/default.settings.php @@ -472,6 +472,23 @@ */ # $settings['session_write_interval'] = 180; +/** + * String overrides: + * + * To override specific strings on your site with or without enabling the Locale + * module, add an entry to this list. This functionality allows you to change + * a small number of your site's default English language interface strings. + * + * Remove the leading hash signs to enable. + * + * The "en" part of the variable name, is dynamic and can be any langcode of + * any enabled language. (eg locale_custom_strings_de for german). + */ +# $settings['locale_custom_strings_en'][''] = array( +# 'forum' => 'Discussion board', +# '@count min' => '@count minutes', +# ); + /** * A custom theme for the offline page: * @@ -600,20 +617,6 @@ # $conf['system.performance']['css']['gzip'] = FALSE; # $conf['system.performance']['js']['gzip'] = FALSE; -/** - * String overrides: - * - * To override specific strings on your site with or without enabling the Locale - * module, add an entry to this list. This functionality allows you to change - * a small number of your site's default English language interface strings. - * - * Remove the leading hash signs to enable. - */ -# $conf['locale_custom_strings_en'][''] = array( -# 'forum' => 'Discussion board', -# '@count min' => '@count minutes', -# ); - /** * Fast 404 pages: * -- GitLab