Issue #3422915 by magaki, karanpagare, joachim, alexpott, Keshav Patel, smustgrave, Aguillet: Deprecate Drupal\locale\Locale and its one method

merge-requests/7514/merge
Alex Pott 2024-04-29 08:35:43 +01:00
parent 7cfb8bbd48
commit dc9a4c537e
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
7 changed files with 74 additions and 24 deletions

View File

@ -6,12 +6,11 @@
*/ */
use Drupal\Core\Batch\BatchBuilder; use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Url;
use Drupal\Core\File\Exception\FileException; use Drupal\Core\File\Exception\FileException;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\file\FileInterface; use Drupal\file\FileInterface;
use Drupal\locale\Gettext; use Drupal\locale\Gettext;
use Drupal\locale\Locale;
/** /**
* Prepare a batch to import all translations. * Prepare a batch to import all translations.
@ -328,7 +327,8 @@ function locale_translate_batch_refresh(&$context) {
} }
elseif ($name = array_shift($context['sandbox']['refresh']['names'])) { elseif ($name = array_shift($context['sandbox']['refresh']['names'])) {
// Refresh all languages for one object at a time. // Refresh all languages for one object at a time.
$count = Locale::config()->updateConfigTranslations([$name], $context['sandbox']['refresh']['languages']); $count = \Drupal::service('locale.config_manager')
->updateConfigTranslations([$name], $context['sandbox']['refresh']['languages']);
$context['results']['stats']['config'] += $count; $context['results']['stats']['config'] += $count;
// Inherit finished information from the "parent" string lookup step so // Inherit finished information from the "parent" string lookup step so
// visual display of status will make sense. // visual display of status will make sense.
@ -344,7 +344,8 @@ function locale_translate_batch_refresh(&$context) {
// Clear cache and force refresh of JavaScript translations. // Clear cache and force refresh of JavaScript translations.
_locale_refresh_translations($context['sandbox']['refresh']['languages'], $next); _locale_refresh_translations($context['sandbox']['refresh']['languages'], $next);
// Check whether we need to refresh configuration objects. // Check whether we need to refresh configuration objects.
if ($names = Locale::config()->getStringNames($next)) { if ($names = \Drupal::service('locale.config_manager')
->getStringNames($next)) {
$context['sandbox']['refresh']['names_finished'] = $context['finished']; $context['sandbox']['refresh']['names_finished'] = $context['finished'];
$context['sandbox']['refresh']['names'] = $names; $context['sandbox']['refresh']['names'] = $names;
} }
@ -547,7 +548,7 @@ function locale_translate_delete_translation_files(array $projects = [], array $
*/ */
function locale_config_batch_update_components(array $options, array $langcodes = [], array $components = [], bool $update_default_config_langcodes = FALSE) { function locale_config_batch_update_components(array $options, array $langcodes = [], array $components = [], bool $update_default_config_langcodes = FALSE) {
$langcodes = $langcodes ? $langcodes : array_keys(\Drupal::languageManager()->getLanguages()); $langcodes = $langcodes ? $langcodes : array_keys(\Drupal::languageManager()->getLanguages());
if ($langcodes && $names = Locale::config()->getComponentNames($components)) { if ($langcodes && $names = \Drupal::service('locale.config_manager')->getComponentNames($components)) {
// If the component list is empty we need to ensure that all configuration // If the component list is empty we need to ensure that all configuration
// in the default collection is using the site's default langcode. // in the default collection is using the site's default langcode.
return locale_config_batch_build($names, $langcodes, $options, $update_default_config_langcodes); return locale_config_batch_build($names, $langcodes, $options, $update_default_config_langcodes);
@ -612,7 +613,7 @@ function locale_config_batch_build(array $names, array $langcodes, array $option
* The batch context. * The batch context.
*/ */
function locale_config_batch_set_config_langcodes(&$context) { function locale_config_batch_set_config_langcodes(&$context) {
Locale::config()->updateDefaultConfigLangcodes(); \Drupal::service('locale.config_manager')->updateDefaultConfigLangcodes();
$context['finished'] = 1; $context['finished'] = 1;
$context['message'] = t('Updated default configuration to %langcode', ['%langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId()]); $context['message'] = t('Updated default configuration to %langcode', ['%langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId()]);
} }
@ -635,7 +636,8 @@ function locale_config_batch_refresh_name(array $names, array $langcodes, &$cont
if (!isset($context['results']['stats']['config'])) { if (!isset($context['results']['stats']['config'])) {
$context['results']['stats']['config'] = 0; $context['results']['stats']['config'] = 0;
} }
$context['results']['stats']['config'] += Locale::config()->updateConfigTranslations($names, $langcodes); $context['results']['stats']['config'] += \Drupal::service('locale.config_manager')
->updateConfigTranslations($names, $langcodes);
foreach ($names as $name) { foreach ($names as $name) {
$context['results']['names'][] = $name; $context['results']['names'][] = $name;
} }

View File

@ -27,7 +27,6 @@ use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\language\ConfigurableLanguageInterface; use Drupal\language\ConfigurableLanguageInterface;
use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Crypt;
use Drupal\locale\Locale;
use Drupal\locale\LocaleEvent; use Drupal\locale\LocaleEvent;
use Drupal\locale\LocaleEvents; use Drupal\locale\LocaleEvents;
@ -238,7 +237,7 @@ function locale_configurable_language_delete(ConfigurableLanguageInterface $lang
locale_translate_delete_translation_files([], [$language->id()]); locale_translate_delete_translation_files([], [$language->id()]);
// Remove translated configuration objects. // Remove translated configuration objects.
Locale::config()->deleteLanguageTranslations($language->id()); \Drupal::service('locale.config_manager')->deleteLanguageTranslations($language->id());
// Changing the language settings impacts the interface: // Changing the language settings impacts the interface:
_locale_invalidate_js($language->id()); _locale_invalidate_js($language->id());
@ -367,7 +366,7 @@ function locale_cron() {
* Updates default configuration when new modules or themes are installed. * Updates default configuration when new modules or themes are installed.
*/ */
function locale_system_set_config_langcodes() { function locale_system_set_config_langcodes() {
Locale::config()->updateDefaultConfigLangcodes(); \Drupal::service('locale.config_manager')->updateDefaultConfigLangcodes();
} }
/** /**
@ -1053,8 +1052,9 @@ function _locale_refresh_translations($langcodes, $lids = []) {
* List of string identifiers that have been updated / created. * List of string identifiers that have been updated / created.
*/ */
function _locale_refresh_configuration(array $langcodes, array $lids) { function _locale_refresh_configuration(array $langcodes, array $lids) {
if ($lids && $langcodes && $names = Locale::config()->getStringNames($lids)) { $locale_config_manager = \Drupal::service('locale.config_manager');
Locale::config()->updateConfigTranslations($names, $langcodes); if ($lids && $langcodes && $names = $locale_config_manager->getStringNames($lids)) {
$locale_config_manager->updateConfigTranslations($names, $langcodes);
} }
} }

View File

@ -2,8 +2,15 @@
namespace Drupal\locale; namespace Drupal\locale;
@trigger_error('The ' . __NAMESPACE__ . '\Locale is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3437110', E_USER_DEPRECATED);
/** /**
* Static service container wrapper for locale. * Static service container wrapper for locale.
*
* @deprecated in drupal:10.3.0 and is removed from drupal:11.0.0.
* There is no replacement.
*
* @see https://www.drupal.org/node/3437110
*/ */
class Locale { class Locale {
@ -13,11 +20,16 @@ class Locale {
* Use the locale config manager service for creating locale-wrapped typed * Use the locale config manager service for creating locale-wrapped typed
* configuration objects. * configuration objects.
* *
* @see \Drupal\Core\TypedData\TypedDataManager::create()
*
* @return \Drupal\locale\LocaleConfigManager * @return \Drupal\locale\LocaleConfigManager
* The locale configuration manager.
*
* @deprecated in drupal:10.3.0 and is removed from drupal:11.0.0.
* Use \Drupal::service('locale.config_manager') instead.
*
* @see https://www.drupal.org/node/3437110
*/ */
public static function config() { public static function config() {
@trigger_error(__METHOD__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal::service(\'locale.config_manager\') instead. See https://www.drupal.org/node/3437110', E_USER_DEPRECATED);
return \Drupal::service('locale.config_manager'); return \Drupal::service('locale.config_manager');
} }

View File

@ -4,9 +4,8 @@ declare(strict_types=1);
namespace Drupal\Tests\locale\Functional; namespace Drupal\Tests\locale\Functional;
use Drupal\locale\Locale;
use Drupal\Tests\BrowserTestBase;
use Drupal\language\Entity\ConfigurableLanguage; use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
/** /**
* Tests translation update's effects on configuration translations. * Tests translation update's effects on configuration translations.
@ -233,7 +232,7 @@ class LocaleConfigTranslationImportTest extends BrowserTestBase {
$string = $locale_storage->findString(['source' => 'Locale can translate']); $string = $locale_storage->findString(['source' => 'Locale can translate']);
\Drupal::service('locale.storage')->delete($string); \Drupal::service('locale.storage')->delete($string);
// Force a rebuild of config translations. // Force a rebuild of config translations.
$count = Locale::config()->updateConfigTranslations(['locale_test_translate.settings'], ['af']); $count = \Drupal::service('locale.config_manager')->updateConfigTranslations(['locale_test_translate.settings'], ['af']);
$this->assertEquals(1, $count, 'Correct count of updated translations'); $this->assertEquals(1, $count, 'Correct count of updated translations');
$override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings'); $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');

View File

@ -4,11 +4,10 @@ declare(strict_types=1);
namespace Drupal\Tests\locale\Kernel; namespace Drupal\Tests\locale\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage; use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\locale\Locale;
use Drupal\locale\StringInterface; use Drupal\locale\StringInterface;
use Drupal\locale\TranslationString; use Drupal\locale\TranslationString;
use Drupal\KernelTests\KernelTestBase;
/** /**
* Tests that shipped configuration translations are updated correctly. * Tests that shipped configuration translations are updated correctly.
@ -69,8 +68,9 @@ class LocaleConfigSubscriberTest extends KernelTestBase {
// @see locale_system_update() // @see locale_system_update()
locale_system_set_config_langcodes(); locale_system_set_config_langcodes();
$langcodes = array_keys(\Drupal::languageManager()->getLanguages()); $langcodes = array_keys(\Drupal::languageManager()->getLanguages());
$names = Locale::config()->getComponentNames(); $locale_config_manager = \Drupal::service('locale.config_manager');
Locale::config()->updateConfigTranslations($names, $langcodes); $names = $locale_config_manager->getComponentNames();
$locale_config_manager->updateConfigTranslations($names, $langcodes);
$this->configFactory = $this->container->get('config.factory'); $this->configFactory = $this->container->get('config.factory');
$this->stringStorage = $this->container->get('locale.storage'); $this->stringStorage = $this->container->get('locale.storage');

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\locale\Unit;
use Drupal\Core\DependencyInjection\Container;
use Drupal\locale\Locale;
use Drupal\locale\LocaleConfigManager;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\locale\Locale
* @group Cache
*/
class LocaleTest extends UnitTestCase {
/**
* Tests deprecation of config() method.
*
* @covers ::config
* @group legacy
*/
public function testConfig() {
$config_manager = $this->prophesize(LocaleConfigManager::class);
$container = $this->prophesize(Container::class);
$container->get('locale.config_manager')
->willReturn($config_manager->reveal());
\Drupal::setContainer($container->reveal());
$this->expectDeprecation('The Drupal\locale\Locale is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3437110');
$this->expectDeprecation('Drupal\locale\Locale::config() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal::service(\'locale.config_manager\') instead. See https://www.drupal.org/node/3437110');
$this->assertInstanceOf(LocaleConfigManager::class, Locale::config());
}
}

View File

@ -7,7 +7,6 @@ namespace Drupal\Tests\user\Kernel;
use Drupal\Core\Test\AssertMailTrait; use Drupal\Core\Test\AssertMailTrait;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage; use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\locale\Locale;
/** /**
* Tests _user_mail_notify() use of user.settings.notify.*. * Tests _user_mail_notify() use of user.settings.notify.*.
@ -131,8 +130,9 @@ class UserMailNotifyTest extends EntityKernelTestBase {
locale_system_set_config_langcodes(); locale_system_set_config_langcodes();
$langcodes = array_keys(\Drupal::languageManager()->getLanguages()); $langcodes = array_keys(\Drupal::languageManager()->getLanguages());
$names = Locale::config()->getComponentNames(); $locale_config_manager = \Drupal::service('locale.config_manager');
Locale::config()->updateConfigTranslations($names, $langcodes); $names = $locale_config_manager->getComponentNames();
$locale_config_manager->updateConfigTranslations($names, $langcodes);
$this->config('user.settings')->set('notify.password_reset', TRUE)->save(); $this->config('user.settings')->set('notify.password_reset', TRUE)->save();