Unverified Commit c274b2b8 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3255250 by murilohp, daffie, longwave, Gábor Hojtsy: [Symfony 5]...

Issue #3255250 by murilohp, daffie, longwave, Gábor Hojtsy: [Symfony 5] TranslatorInterface::transChoice() is deprecated
parent 8f2269dd
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -42,8 +42,14 @@ public function trans($id, array $parameters = [], $domain = NULL, $locale = NUL

  /**
   * {@inheritdoc}
   *
   * @deprecated in drupal:9.4.0 and is removed from drupal:10.0.0. Use
   *   \Drupal\Core\Validation\DrupalTranslator::trans() instead.
   *
   * @see https://www.drupal.org/node/3255250
   */
  public function transChoice($id, $number, array $parameters = [], $domain = NULL, $locale = NULL) {
    @trigger_error(__NAMESPACE__ . '\DrupalTranslator::transChoice() is deprecated in drupal:9.4.0 and is removed from drupal:10.0.0. Use DrupalTranslator::trans() instead. See https://www.drupal.org/node/3255250', E_USER_DEPRECATED);
    // Violation messages can separated singular and plural versions by "|".
    $ids = explode('|', $id);

+61 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\KernelTests\Core\Validation;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\Core\Validation\DrupalTranslator;
use Drupal\Tests\UnitTestCase;

/**
 * Tests the for translator.
 *
 * @coversDefaultClass \Drupal\Core\Validation\DrupalTranslator
 *
 * @group validation
 */
class DrupalTranslatorTest extends UnitTestCase {

  /**
   * {@inheritDoc}
   */
  public function setUp(): void {
    parent::setUp();

    // Set up a mock container as transChoice() will call for the
    // 'string_translation' service.
    $plural_translatable_markup = $this->getMockBuilder(PluralTranslatableMarkup::class)
      ->disableOriginalConstructor()
      ->getMock();

    $translation_manager = $this->getMockBuilder(TranslationManager::class)
      ->onlyMethods(['formatPlural'])
      ->disableOriginalConstructor()
      ->getMock();
    $translation_manager->expects($this->any())
      ->method('formatPlural')
      ->willReturn($plural_translatable_markup);

    $container = new ContainerBuilder();
    $container->set('string_translation', $translation_manager);

    \Drupal::setContainer($container);
  }

  /**
   * Test transChoice deprecation message.
   *
   * @covers ::transChoice
   * @group legacy
   */
  public function testDeprecation() {
    $this->expectDeprecation('Drupal\Core\Validation\DrupalTranslator::transChoice() is deprecated in drupal:9.4.0 and is removed from drupal:10.0.0. Use DrupalTranslator::trans() instead. See https://www.drupal.org/node/3255250');
    $translator = new DrupalTranslator();
    $this->assertInstanceOf(
      PluralTranslatableMarkup::class,
      $translator->transChoice('There is one apple | There are @count apples', 1)
    );
  }

}