diff --git a/core/core.services.yml b/core/core.services.yml index 608c439..465d145 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 d547daf..9b0ef4c 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 a1ed337..3debb1f 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. * @@ -16,10 +18,28 @@ 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 a064d01..a307e32 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -178,6 +178,13 @@ protected $curlCookies = array(); /** + * An array of custom translations suitable for drupal_rewrite_settings(). + * + * @var array + */ + protected $customTranslations; + + /** * Constructor for \Drupal\simpletest\WebTestBase. */ function __construct($test_id = NULL) { @@ -918,6 +925,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(). */ protected function 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 0fb2212..4511346 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 1a4f746..f341681 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 4aa6270..34e3889 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 b22fd90..d08817d 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 731179f..0574b23 100644 --- a/sites/default/default.settings.php +++ b/sites/default/default.settings.php @@ -473,6 +473,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: * * This applies when the site is explicitly set to maintenance mode through the @@ -601,20 +618,6 @@ # $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: * * Drupal can generate fully themed 404 pages. However, some of these responses