Commit a96ce198 authored by webchick's avatar webchick

Issue #2175821 by Xano, dawehner: Add placeholder support to the @Translation annotation.

parent 81fbebac
......@@ -24,6 +24,11 @@
* Remove spaces after @ in your actual plugin - these are put into this sample
* code so that it is not recognized as annotation.
*
* To provide replacement values for placeholders, use the "arguments" array:
* @code
* title = @ Translation("Bundle !title", arguments = {"!title" = "Foo"}),
* @endcode
*
* It is also possible to provide a context with the text, similar to t():
* @code
* title = @ Translation("Bundle", context = "Validation"),
......@@ -55,20 +60,35 @@ class Translation extends AnnotationBase {
protected $translation;
/**
* Constructs a Translation object.
* The translation manager.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* Constructs a new class instance.
*
* Parses values passed into this class through the t() function in Drupal and
* handles an optional context for the string.
*
* @param array $values
* Possible array keys:
* - value (required): the string that is to be translated.
* - arguments (optional): an array with placeholder replacements, keyed by
* placeholder.
* - context (optional): a string that describes the context of "value";
*/
public function __construct($values) {
public function __construct(array $values) {
$string = $values['value'];
$arguments = isset($values['arguments']) ? $values['arguments'] : array();
$options = array();
if (!empty($values['context'])) {
$options = array(
'context' => $values['context'],
);
}
$this->translation = t($string, array(), $options);
$this->translation = $this->getTranslationManager()->translate($string, $arguments, $options);
}
/**
......@@ -78,4 +98,18 @@ public function get() {
return $this->translation;
}
/**
* Returns the translation manager.
*
* @return \Drupal\Core\StringTranslation\TranslationInterface
* The translation manager.
*/
protected function getTranslationManager() {
if (!$this->translationManager) {
$this->translationManager = \Drupal::translation();
}
return $this->translationManager;
}
}
<?php
/**
* @file Contains \Drupal\Tests\Core\Annotation\TranslationTest.
*/
namespace Drupal\Tests\Core\Annotation;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
/**
* Tests the Translation annotation.
*
* @covers \Drupal\Core\Annotation\Translation.
*/
class TranslationTest extends UnitTestCase {
/**
* The translation manager used for testing.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $translationManager;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'description' => '',
'name' => '\Drupal\Core\Annotation\Translation unit test',
'group' => 'System',
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
$this->translationManager = $this->getStringTranslationStub();
}
/**
* @covers \Drupal\Core\Annotation\Translation::get()
*
* @dataProvider providerTestGet
*/
public function testGet(array $values, $expected) {
$container = new ContainerBuilder();
$container->set('string_translation', $this->translationManager);
\Drupal::setContainer($container);
$arguments = isset($values['arguments']) ? $values['arguments'] : array();
$options = isset($values['context']) ? array(
'context' => $values['context'],
) : array();
$this->translationManager->expects($this->once())
->method('translate')
->with($values['value'], $arguments, $options);
$annotation = new Translation($values);
$this->assertSame($expected, $annotation->get());
}
/**
* Provides data to self::testGet().
*/
public function providerTestGet() {
$data = array();
$data[] = array(
array(
'value' => 'Foo',
),
'Foo'
);
$random = $this->randomName();
$random_html_entity = '&' . $random;
$data[] = array(
array(
'value' => 'Foo !bar @baz %qux',
'arguments' => array(
'!bar' => $random,
'@baz' => $random_html_entity,
'%qux' => $random_html_entity,
),
'context' => $this->randomName(),
),
'Foo ' . $random . ' &amp;' . $random . ' <em class="placeholder">&amp;' . $random . '</em>',
);
return $data;
}
}
......@@ -190,7 +190,7 @@ public function getStringTranslationStub() {
$translation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
$translation->expects($this->any())
->method('translate')
->will($this->returnCallback(function ($string, array $args = array()) { return strtr($string, $args); }));
->will($this->returnCallback('Drupal\Component\Utility\String::format'));
return $translation;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment