Commit d50a62fc authored by catch's avatar catch

Issue #1975490 by ParisLiakos: Convert locale_custom_strings_* to settings.

parent 66239988
......@@ -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:
......
......@@ -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')));
......
......@@ -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());
}
}
......@@ -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().
*/
......
......@@ -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();
}
/**
......
......@@ -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();
}
}
......
......@@ -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.
......
......@@ -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');
......
......@@ -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:
*
......
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