5 * Contains \Drupal\Tests\Core\StringTranslation\TranslationManagerTest.
8 namespace Drupal\Tests\Core\StringTranslation;
10 use Drupal\Component\Render\MarkupInterface;
11 use Drupal\Core\StringTranslation\TranslationManager;
12 use Drupal\Tests\UnitTestCase;
15 * @coversDefaultClass \Drupal\Core\StringTranslation\TranslationManager
16 * @group StringTranslation
18 class TranslationManagerTest extends UnitTestCase {
21 * The tested translation manager.
23 * @var \Drupal\Core\StringTranslation\TranslationManager
25 protected $translationManager;
30 protected function setUp() {
31 $this->translationManager = new TestTranslationManager();
35 * Provides some test data for formatPlural()
38 public function providerTestFormatPlural() {
40 [1, 'Singular', '@count plural', [], [], 'Singular'],
41 [2, 'Singular', '@count plural', [], [], '2 plural'],
42 // @todo support locale_get_plural
43 [2, 'Singular', '@count @arg', ['@arg' => '<script>'], [], '2 <script>'],
44 [2, 'Singular', '@count %arg', ['%arg' => '<script>'], [], '2 <em class="placeholder"><script></em>'],
45 [1, 'Singular', '@count plural', [], ['langcode' => NULL], 'Singular'],
46 [1, 'Singular', '@count plural', [], ['langcode' => 'es'], 'Singular'],
51 * @dataProvider providerTestFormatPlural
53 public function testFormatPlural($count, $singular, $plural, array $args = [], array $options = [], $expected) {
54 $langcode = empty($options['langcode']) ? 'fr' : $options['langcode'];
55 $translator = $this->getMock('\Drupal\Core\StringTranslation\Translator\TranslatorInterface');
56 $translator->expects($this->once())
57 ->method('getStringTranslation')
58 ->with($langcode, $this->anything(), $this->anything())
59 ->will($this->returnCallback(function ($langcode, $string, $context) {
62 $this->translationManager->setDefaultLangcode('fr');
63 $this->translationManager->addTranslator($translator);
64 $result = $this->translationManager->formatPlural($count, $singular, $plural, $args, $options);
65 $this->assertEquals($expected, $result);
66 $this->assertInstanceOf(MarkupInterface::class, $result);
70 * Tests translation using placeholders.
72 * @param string $string
73 * A string containing the English text to translate.
75 * An associative array of replacements to make after translation.
76 * @param string $expected_string
77 * The expected translated string value.
79 * @dataProvider providerTestTranslatePlaceholder
81 public function testTranslatePlaceholder($string, array $args = [], $expected_string) {
82 $actual = $this->translationManager->translate($string, $args);
83 $this->assertInstanceOf(MarkupInterface::class, $actual);
84 $this->assertEquals($expected_string, (string) $actual);
88 * Provides test data for translate().
92 public function providerTestTranslatePlaceholder() {
94 ['foo @bar', ['@bar' => 'bar'], 'foo bar'],
95 ['bar %baz', ['%baz' => 'baz'], 'bar <em class="placeholder">baz</em>'],
96 ['bar @bar %baz', ['@bar' => 'bar', '%baz' => 'baz'], 'bar bar <em class="placeholder">baz</em>'],
97 ['bar %baz @bar', ['%baz' => 'baz', '@bar' => 'bar'], 'bar <em class="placeholder">baz</em> bar'],
103 class TestTranslationManager extends TranslationManager {
105 public function __construct() {