Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
302
Merge Requests
302
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
drupal
Commits
a96ce198
Commit
a96ce198
authored
Mar 12, 2014
by
webchick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2175821
by Xano, dawehner: Add placeholder support to the @Translation annotation.
parent
81fbebac
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
135 additions
and
4 deletions
+135
-4
core/lib/Drupal/Core/Annotation/Translation.php
core/lib/Drupal/Core/Annotation/Translation.php
+37
-3
core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php
core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php
+97
-0
core/tests/Drupal/Tests/UnitTestCase.php
core/tests/Drupal/Tests/UnitTestCase.php
+1
-1
No files found.
core/lib/Drupal/Core/Annotation/Translation.php
View file @
a96ce198
...
...
@@ -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
;
}
}
core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php
0 → 100644
View file @
a96ce198
<?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
.
' &'
.
$random
.
' <em class="placeholder">&'
.
$random
.
'</em>'
,
);
return
$data
;
}
}
core/tests/Drupal/Tests/UnitTestCase.php
View file @
a96ce198
...
...
@@ -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
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment