Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 9766ae0

Browse files
committed
Merge branch 'hotfix/i18n'
Close #57
2 parents 3d3983f + 8ba6468 commit 9766ae0

6 files changed

+200
-255
lines changed

CHANGELOG.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.6.6 - TBD
5+
## 2.6.6 - 2016-04-18
66

77
### Added
88

9-
- Nothing.
9+
- [#57](https://github.com/zendframework/zend-view/pull/57) adds
10+
`Zend\View\Helper\TranslatorAwareTrait`, which provides implementation for
11+
`Zend\I18n\Translator\TranslatorAwareInterface`, and allowed removal of
12+
duplicated implementations in several helpers.
1013

1114
### Deprecated
1215

@@ -18,7 +21,15 @@ All notable changes to this project will be documented in this file, in reverse
1821

1922
### Fixed
2023

21-
- Nothing.
24+
- [#57](https://github.com/zendframework/zend-view/pull/57) removes the explicit
25+
dependency on `Zend\I18n\Translator\TranslatorAwareInterface` by allowing
26+
helpers to duck type the interface to receive a translator during
27+
instantiation; this allows such helpers to work even when zend-i18n is not
28+
installed. The following helpers were updated to duck type the interface
29+
instead of implement it explicitly:
30+
- `FlashMessenger`
31+
- `HeadTitle`
32+
- all `Navigation` helpers
2233

2334
## 2.6.5 - 2016-03-21
2435

src/Helper/FlashMessenger.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
<?php
22
/**
3-
* Zend Framework (http://framework.zend.com/)
4-
*
5-
* @link http://github.com/zendframework/zf2 for the canonical source repository
6-
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
3+
* @link http://github.com/zendframework/zend-view for the canonical source repository
4+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
75
* @license http://framework.zend.com/license/new-bsd New BSD License
86
*/
97

108
namespace Zend\View\Helper;
119

1210
use Zend\Mvc\Controller\Plugin\FlashMessenger as PluginFlashMessenger;
13-
use Zend\I18n\View\Helper\AbstractTranslatorHelper;
1411

1512
/**
1613
* Helper to proxy the plugin flash messenger
14+
*
15+
* Duck-types against Zend\I18n\Translator\TranslatorAwareInterface.
1716
*/
18-
class FlashMessenger extends AbstractTranslatorHelper
17+
class FlashMessenger extends AbstractHelper
1918
{
19+
use TranslatorAwareTrait;
20+
2021
/**
2122
* Default attributes for the open format tag
2223
*
@@ -149,9 +150,9 @@ protected function renderMessages(
149150
}
150151

151152
// Flatten message array
152-
$escapeHtml = $this->getEscapeHtmlHelper();
153-
$messagesToPrint = [];
154-
$translator = $this->getTranslator();
153+
$escapeHtml = $this->getEscapeHtmlHelper();
154+
$messagesToPrint = [];
155+
$translator = $this->getTranslator();
155156
$translatorTextDomain = $this->getTranslatorTextDomain();
156157
array_walk_recursive(
157158
$messages,

src/Helper/HeadTitle.php

+26-117
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
<?php
22
/**
3-
* Zend Framework (http://framework.zend.com/)
4-
*
5-
* @link http://github.com/zendframework/zf2 for the canonical source repository
6-
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
3+
* @link http://github.com/zendframework/zend-view for the canonical source repository
4+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
75
* @license http://framework.zend.com/license/new-bsd New BSD License
86
*/
97

108
namespace Zend\View\Helper;
119

12-
use Zend\I18n\Translator\TranslatorInterface as Translator;
13-
use Zend\I18n\Translator\TranslatorAwareInterface;
1410
use Zend\View\Exception;
1511

1612
/**
17-
* Helper for setting and retrieving title element for HTML head
13+
* Helper for setting and retrieving title element for HTML head.
14+
*
15+
* Duck-types against Zend\I18n\Translator\TranslatorAwareInterface.
1816
*/
19-
class HeadTitle extends Placeholder\Container\AbstractStandalone implements
20-
TranslatorAwareInterface
17+
class HeadTitle extends Placeholder\Container\AbstractStandalone
2118
{
19+
use TranslatorAwareTrait;
20+
2221
/**
2322
* Registry key for placeholder
2423
*
@@ -33,27 +32,6 @@ class HeadTitle extends Placeholder\Container\AbstractStandalone implements
3332
*/
3433
protected $defaultAttachOrder = null;
3534

36-
/**
37-
* Translator (optional)
38-
*
39-
* @var Translator
40-
*/
41-
protected $translator;
42-
43-
/**
44-
* Translator text domain (optional)
45-
*
46-
* @var string
47-
*/
48-
protected $translatorTextDomain = 'default';
49-
50-
/**
51-
* Whether translator should be used
52-
*
53-
* @var bool
54-
*/
55-
protected $translatorEnabled = true;
56-
5735
/**
5836
* Retrieve placeholder for title element and optionally set state
5937
*
@@ -109,14 +87,9 @@ public function renderTitle()
10987
{
11088
$items = [];
11189

112-
if (null !== ($translator = $this->getTranslator())) {
113-
foreach ($this as $item) {
114-
$items[] = $translator->translate($item, $this->getTranslatorTextDomain());
115-
}
116-
} else {
117-
foreach ($this as $item) {
118-
$items[] = $item;
119-
}
90+
$itemCallback = $this->getTitleItemCallback();
91+
foreach ($this as $item) {
92+
$items[] = $itemCallback($item);
12093
}
12194

12295
$separator = $this->getSeparator();
@@ -172,92 +145,28 @@ public function getDefaultAttachOrder()
172145
return $this->defaultAttachOrder;
173146
}
174147

175-
// Translator methods - Good candidate to refactor as a trait with PHP 5.4
176148

177149
/**
178-
* Sets translator to use in helper
150+
* Create and return a callback for normalizing title items.
179151
*
180-
* @param Translator $translator [optional] translator.
181-
* Default is null, which sets no translator.
182-
* @param string $textDomain [optional] text domain
183-
* Default is null, which skips setTranslatorTextDomain
184-
* @return HeadTitle
185-
*/
186-
public function setTranslator(Translator $translator = null, $textDomain = null)
187-
{
188-
$this->translator = $translator;
189-
if (null !== $textDomain) {
190-
$this->setTranslatorTextDomain($textDomain);
191-
}
192-
return $this;
193-
}
194-
195-
/**
196-
* Returns translator used in helper
152+
* If translation is not enabled, or no translator is present, returns a
153+
* callable that simply returns the provided item; otherwise, returns a
154+
* callable that returns a translation of the provided item.
197155
*
198-
* @return Translator|null
156+
* @return callable
199157
*/
200-
public function getTranslator()
158+
private function getTitleItemCallback()
201159
{
202-
if (! $this->isTranslatorEnabled()) {
203-
return;
160+
if (! $this->isTranslatorEnabled() || ! $this->hasTranslator()) {
161+
return function ($item) {
162+
return $item;
163+
};
204164
}
205165

206-
return $this->translator;
207-
}
208-
209-
/**
210-
* Checks if the helper has a translator
211-
*
212-
* @return bool
213-
*/
214-
public function hasTranslator()
215-
{
216-
return (bool) $this->getTranslator();
217-
}
218-
219-
/**
220-
* Sets whether translator is enabled and should be used
221-
*
222-
* @param bool $enabled [optional] whether translator should be used.
223-
* Default is true.
224-
* @return HeadTitle
225-
*/
226-
public function setTranslatorEnabled($enabled = true)
227-
{
228-
$this->translatorEnabled = (bool) $enabled;
229-
return $this;
230-
}
231-
232-
/**
233-
* Returns whether translator is enabled and should be used
234-
*
235-
* @return bool
236-
*/
237-
public function isTranslatorEnabled()
238-
{
239-
return $this->translatorEnabled;
240-
}
241-
242-
/**
243-
* Set translation text domain
244-
*
245-
* @param string $textDomain
246-
* @return HeadTitle
247-
*/
248-
public function setTranslatorTextDomain($textDomain = 'default')
249-
{
250-
$this->translatorTextDomain = $textDomain;
251-
return $this;
252-
}
253-
254-
/**
255-
* Return the translation text domain
256-
*
257-
* @return string
258-
*/
259-
public function getTranslatorTextDomain()
260-
{
261-
return $this->translatorTextDomain;
166+
$translator = $this->getTranslator();
167+
$textDomain = $this->getTranslatorTextDomain();
168+
return function ($item) use ($translator, $textDomain) {
169+
return $translator->translate($item, $textDomain);
170+
};
262171
}
263172
}

0 commit comments

Comments
 (0)