TranslationStringTest.php in Drupal 8
File
core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php
View source
<?php
namespace Drupal\KernelTests\Core\StringTranslation;
use Drupal\Core\Site\Settings;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
class TranslationStringTest extends KernelTestBase {
public static $modules = [
'language',
];
protected function setUp() {
parent::setUp();
ConfigurableLanguage::createFromLangcode('de')
->save();
}
public function testComparison() {
$this
->rebootAndPrepareSettings();
$a = \Drupal::service('string_translation')
->translate('Example @number', [
'@number' => 42,
], [
'langcode' => 'de',
]);
$this
->rebootAndPrepareSettings();
$b = \Drupal::service('string_translation')
->translate('Example @number', [
'@number' => 42,
], [
'langcode' => 'de',
]);
$c = \Drupal::service('string_translation')
->translate('Example @number', [
'@number' => 43,
], [
'langcode' => 'de',
]);
$d = \Drupal::service('string_translation')
->translate('Example @number', [
'@number' => 42,
], [
'langcode' => 'en',
]);
$this
->assertEquals($a, $b);
$this
->assertNotSame($a, $b);
$this
->assertNotEquals($a, $c);
$this
->assertNotEquals($a, $d);
}
protected function rebootAndPrepareSettings() {
$kernel = $this->container
->get('kernel');
$kernel
->shutdown();
$kernel
->boot();
$settings = Settings::getAll();
$settings['locale_custom_strings_de'] = [
'' => [
'Example @number' => 'Example @number translated',
],
];
new Settings($settings);
}
}