diff --git a/core/modules/locale/config/locale.languages.yml b/core/modules/locale/config/locale.languages.yml new file mode 100644 index 0000000..8a4e564 --- /dev/null +++ b/core/modules/locale/config/locale.languages.yml @@ -0,0 +1 @@ +en: '0' \ No newline at end of file diff --git a/core/modules/locale/lib/Drupal/locale/LocaleTranslation.php b/core/modules/locale/lib/Drupal/locale/LocaleTranslation.php index ac2b630..54cc60d 100644 --- a/core/modules/locale/lib/Drupal/locale/LocaleTranslation.php +++ b/core/modules/locale/lib/Drupal/locale/LocaleTranslation.php @@ -8,6 +8,7 @@ namespace Drupal\locale; use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Config\ConfigFactory; use Drupal\Core\DestructableInterface; use Drupal\Core\Language\Language; use Drupal\Core\Lock\LockBackendAbstract; @@ -32,6 +33,13 @@ class LocaleTranslation implements TranslatorInterface, DestructableInterface { protected $storage; /** + * The configuration factory. + * + * @var \Drupal\Core\Config\ConfigFactory + */ + protected $configFactory; + + /** * Cached translations * * @var array @@ -63,11 +71,14 @@ class LocaleTranslation implements TranslatorInterface, DestructableInterface { * The cache backend. * @param \Drupal\Core\Lock\LockBackendInterface $lock * The lock backend. + * @param \Drupal\Core\Config\ConfigFactory $config_factory + * The config factory. */ - public function __construct(StringStorageInterface $storage, CacheBackendInterface $cache, LockBackendInterface $lock) { + public function __construct(StringStorageInterface $storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactory $config_factory) { $this->storage = $storage; $this->cache = $cache; $this->lock = $lock; + $this->configFactory = $config_factory; } /** @@ -75,7 +86,7 @@ public function __construct(StringStorageInterface $storage, CacheBackendInterfa */ public function getStringTranslation($langcode, $string, $context) { // If the language is not suitable for locale module, just return. - if ($langcode == Language::LANGCODE_SYSTEM || ($langcode == 'en' && !variable_get('locale_translate_english', FALSE))) { + if (!$this->configFactory->get('locale.languages')->get($langcode)) { return FALSE; } // Strings are cached by langcode, context and roles, using instances of the diff --git a/core/modules/locale/locale.bulk.inc b/core/modules/locale/locale.bulk.inc index 8a15656..8a35c14 100644 --- a/core/modules/locale/locale.bulk.inc +++ b/core/modules/locale/locale.bulk.inc @@ -26,7 +26,7 @@ function locale_translate_import_form($form, &$form_state) { // are to translate Drupal to English as well. $existing_languages = array(); foreach ($languages as $langcode => $language) { - if ($langcode != 'en' || locale_translate_english()) { + if (config('locale.languages')->get($langcode)) { $existing_languages[$langcode] = $language->name; } } @@ -149,7 +149,7 @@ function locale_translate_export_form($form, &$form_state) { $languages = language_list(); $language_options = array(); foreach ($languages as $langcode => $language) { - if ($langcode != 'en' || locale_translate_english()) { + if (config('locale.languages')->get($langcode)) { $language_options[$langcode] = $language->name; } } diff --git a/core/modules/locale/locale.install b/core/modules/locale/locale.install index b781421..db86fce 100644 --- a/core/modules/locale/locale.install +++ b/core/modules/locale/locale.install @@ -20,6 +20,31 @@ function locale_install() { } /** + * Implements hook_enable(). + */ +function locale_enable() { + // Update translatable language list. + $language_list = language_list(); + $config = config('locale.languages'); + $current = $config->get(); + // Remove deleted languages. + foreach (array_keys($current) as $langcode) { + if (!isset($language_list[$langcode])) { + $config->clear($langcode); + } + else { + unset($language_list[$langcode]); + } + } + // Add setting for remaining languages in the list. + foreach (array_keys($language_list) as $langcode) { + // All languages translatable by default but English. + $config->set($langcode, (int)($langcode != 'en')); + } + $config->save(); +} + +/** * Implements hook_uninstall(). */ function locale_uninstall() { diff --git a/core/modules/locale/locale.module b/core/modules/locale/locale.module index 932b2a1..279330a 100644 --- a/core/modules/locale/locale.module +++ b/core/modules/locale/locale.module @@ -272,6 +272,8 @@ function locale_stream_wrappers() { * Implements hook_language_insert(). */ function locale_language_insert($language) { + // All new languages will be translatable by default. + config('locale.languages')->set($language->id, 1)->save(); // @todo move these two cache clears out. See http://drupal.org/node/1293252 // Changing the language settings impacts the interface. cache('page')->deleteAll(); @@ -311,6 +313,9 @@ function locale_language_delete($language) { // Clear locale translation caches. locale_translation_status_delete_languages(array($language->id)); cache()->delete('locale:' . $language->id); + + // Delete from list of translatable languages. + config('locale.languages')->clear($language->id)->save(); } /** @@ -322,8 +327,10 @@ function locale_language_delete($language) { */ function locale_translatable_language_list() { $languages = language_list(); - if (!locale_translate_english()) { - unset($languages['en']); + foreach (array_keys($languages) as $langcode) { + if (!config('locale.languages')->get($langcode)) { + unset($languages[$langcode]); + } } return $languages; } @@ -736,7 +743,7 @@ function locale_form_language_admin_overview_form_alter(&$form, &$form_state) { 'translated' => 0, 'ratio' => 0, ); - if (!$language->locked && ($langcode != 'en' || locale_translate_english())) { + if (!$language->locked && (config('locale.languages')->get($langcode))) { $form['languages'][$langcode]['locale_statistics'] = array( '#markup' => l( t('@translated/@total (@ratio%)', array( @@ -806,7 +813,7 @@ function locale_form_language_admin_edit_form_alter(&$form, &$form_state) { $form['locale_translate_english'] = array( '#title' => t('Enable interface translation to English'), '#type' => 'checkbox', - '#default_value' => locale_translate_english(), + '#default_value' => config('locale.languages')->get('en'), ); $form['#submit'][] = 'locale_form_language_admin_edit_form_alter_submit'; } @@ -816,17 +823,7 @@ function locale_form_language_admin_edit_form_alter(&$form, &$form_state) { * Form submission handler for language_admin_edit_form(). */ function locale_form_language_admin_edit_form_alter_submit($form, $form_state) { - variable_set('locale_translate_english', $form_state['values']['locale_translate_english']); -} - -/** - * Checks whether locale translates to English. - * - * @return bool - * Returns TRUE if content should be translated to English, FALSE otherwise. - */ -function locale_translate_english() { - return variable_get('locale_translate_english', FALSE); + Drupal::config('locale.languages')->set('en', intval($form_state['values']['locale_translate_english']))->save(); } /** diff --git a/core/modules/locale/locale.pages.inc b/core/modules/locale/locale.pages.inc index 83777b6..2ed7a74 100644 --- a/core/modules/locale/locale.pages.inc +++ b/core/modules/locale/locale.pages.inc @@ -103,7 +103,7 @@ function locale_translate_filters() { $languages = language_list(); $language_options = array(); foreach ($languages as $langcode => $language) { - if ($langcode != 'en' || locale_translate_english()) { + if (config('locale.languages')->get($langcode)) { $language_options[$langcode] = $language->name; } } diff --git a/core/modules/locale/locale.services.yml b/core/modules/locale/locale.services.yml index f632704..776cb3b 100644 --- a/core/modules/locale/locale.services.yml +++ b/core/modules/locale/locale.services.yml @@ -12,7 +12,7 @@ services: arguments: ['@database'] string_translator.locale.lookup: class: Drupal\locale\LocaleTranslation - arguments: ['@locale.storage', '@cache.cache', '@lock'] + arguments: ['@locale.storage', '@cache.cache', '@lock', '@config.factory'] tags: - { name: string_translator } - { name: needs_destruction } diff --git a/core/modules/locale/tests/Drupal/locale/Tests/LocaleTranslationTest.php b/core/modules/locale/tests/Drupal/locale/Tests/LocaleTranslationTest.php index f66024e..602ccdd 100644 --- a/core/modules/locale/tests/Drupal/locale/Tests/LocaleTranslationTest.php +++ b/core/modules/locale/tests/Drupal/locale/Tests/LocaleTranslationTest.php @@ -46,7 +46,7 @@ protected function setUp() { * Tests for \Drupal\locale\LocaleTranslation::destruct() */ public function testDestruct() { - $translation = new LocaleTranslation($this->storage, $this->cache, $this->lock); + $translation = new LocaleTranslation($this->storage, $this->cache, $this->lock, $this->getConfigFactoryStub(array())); // Prove that destruction works without errors when translations are empty. $this->assertAttributeEmpty('translations', $translation); $translation->destruct(); diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php index 727976f..4fc3886 100644 --- a/core/tests/Drupal/Tests/UnitTestCase.php +++ b/core/tests/Drupal/Tests/UnitTestCase.php @@ -58,12 +58,12 @@ public function randomName($length = 8) { * @param array $configs * An associative array of configuration settings whose keys are configuration * object names and whose values are key => value arrays for the configuration - * object in question. + * object in question. Defaults to an empty array. * * @return \PHPUnit_Framework_MockObject_MockBuilder * A MockBuilder object for the ConfigFactory with the desired return values. */ - public function getConfigFactoryStub($configs) { + public function getConfigFactoryStub(array $configs = array()) { $config_map = array(); // Construct the desired configuration object stubs, each with its own // desired return map.