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

Commit 561a815

Browse files
committed
Merging develop to master in preparation for 2.10.0 release.
2 parents 4074e60 + dc9f122 commit 561a815

File tree

8 files changed

+150
-31
lines changed

8 files changed

+150
-31
lines changed

CHANGELOG.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

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

5-
## 2.9.3 - TBD
5+
## 2.10.0 - 2019-11-18
66

77
### Added
88

9-
- Nothing.
9+
- [#102](https://github.com/zendframework/zend-i18n/pull/102) adds `Zend\I18n\View\HelperTrait`, which provides annotations describing the various helpers zend-i18n provides to a zend-view renderer. The trait can be used in combination with `Zend\View\Renderer\PhpRenderer` in annotations on the `$this` variable within view scripts to provide IDE autocompletion for helper-provided methods.
1010

1111
### Changed
1212

13-
- Nothing.
13+
- [#110](https://github.com/zendframework/zend-i18n/pull/110) modifies how `translatePlural()` works when a msgid is present, but no translations are present. It now properly returns the source-code if unable to translate the message, instead of returning an empty string (which is the behavior under `translate()` as well).
14+
15+
- [#126](https://github.com/zendframework/zend-i18n/pull/126) modifies the package definition to put an explicit requirement on ext-intl, as it is required for the majority of functionality. Users have indicated multiple times confusion about why the component does not work after installation, when attempting to use intl functionality; requiring the extension resolves that issue.
1416

1517
### Deprecated
1618

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"require": {
1919
"php": "^5.6 || ^7.0",
20+
"ext-intl": "*",
2021
"zendframework/zend-stdlib": "^2.7 || ^3.0"
2122
},
2223
"require-dev": {
@@ -31,7 +32,6 @@
3132
"zendframework/zend-view": "^2.6.3"
3233
},
3334
"suggest": {
34-
"ext-intl": "Required for most features of Zend\\I18n; included in default builds of PHP",
3535
"zendframework/zend-cache": "Zend\\Cache component",
3636
"zendframework/zend-config": "Zend\\Config component",
3737
"zendframework/zend-eventmanager": "You should install this package to use the events in the translator",
@@ -56,8 +56,8 @@
5656
},
5757
"extra": {
5858
"branch-alias": {
59-
"dev-master": "2.9.x-dev",
60-
"dev-develop": "2.10.x-dev"
59+
"dev-master": "2.10.x-dev",
60+
"dev-develop": "2.11.x-dev"
6161
},
6262
"zf": {
6363
"component": "Zend\\I18n",

composer.lock

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/book/view-helpers.md

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ displaying translated content.
77
See the [zend-view helpers documentation](https://docs.zendframework.com/zend-view/helpers/intro/)
88
for more information.
99

10+
> ### IDE auto-completion in templates
11+
>
12+
> The `Zend\I18n\View\HelperTrait` trait can be used to provide auto-completion
13+
> for modern IDEs. It defines the aliases of the view helpers in a DocBlock as
14+
> `@method` tags.
15+
>
16+
> #### Usage
17+
>
18+
> In order to allow auto-completion in templates, `$this` variable should be
19+
> type-hinted via a DocBlock at the top of your template. It is recommended that
20+
> you always add the `Zend\View\Renderer\PhpRenderer` as the first type, so that
21+
> the IDE can auto-suggest the default view helpers from `zend-view`. Next,
22+
> chain the `HelperTrait` from `zend-i18n` with a pipe symbol (a.k.a. vertical
23+
> bar) `|`:
24+
> ```php
25+
> /**
26+
> * @var Zend\View\Renderer\PhpRenderer|Zend\I18n\View\HelperTrait $this
27+
> */
28+
> ```
29+
>
30+
> You may chain as many `HelperTrait` traits as you like, depending on view
31+
> helpers from which Zend Framework component you are using and would like to
32+
> provide auto-completion for.
33+
1034
## CurrencyFormat Helper
1135
1236
The `CurrencyFormat` view helper can be used to simplify rendering of localized

src/Translator/Translator.php

+20-23
Original file line numberDiff line numberDiff line change
@@ -387,37 +387,34 @@ public function translatePlural(
387387
$locale = $locale ?: $this->getLocale();
388388
$translation = $this->getTranslatedMessage($singular, $locale, $textDomain);
389389

390-
if ($translation === null || $translation === '') {
391-
if (null !== ($fallbackLocale = $this->getFallbackLocale())
392-
&& $locale !== $fallbackLocale
393-
) {
394-
return $this->translatePlural(
395-
$singular,
396-
$plural,
397-
$number,
398-
$textDomain,
399-
$fallbackLocale
400-
);
401-
}
402-
403-
return ($number == 1 ? $singular : $plural);
404-
}
405-
406390
if (is_string($translation)) {
407391
$translation = [$translation];
408392
}
409393

410-
$index = $this->messages[$textDomain][$locale]
411-
->getPluralRule()
412-
->evaluate($number);
394+
$index = ($number === 1) ? 0 : 1; // en_EN Plural rule
395+
if ($this->messages[$textDomain][$locale] instanceof TextDomain) {
396+
$index = $this->messages[$textDomain][$locale]
397+
->getPluralRule()
398+
->evaluate($number);
399+
}
413400

414-
if (! isset($translation[$index])) {
415-
throw new Exception\OutOfBoundsException(
416-
sprintf('Provided index %d does not exist in plural array', $index)
401+
if (isset($translation[$index]) && $translation[$index] !== '' && $translation[$index] !== null) {
402+
return $translation[$index];
403+
}
404+
405+
if (null !== ($fallbackLocale = $this->getFallbackLocale())
406+
&& $locale !== $fallbackLocale
407+
) {
408+
return $this->translatePlural(
409+
$singular,
410+
$plural,
411+
$number,
412+
$textDomain,
413+
$fallbackLocale
417414
);
418415
}
419416

420-
return $translation[$index];
417+
return $index === 0 ? $singular : $plural;
421418
}
422419

423420
/**

src/View/HelperTrait.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-i18n for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-i18n/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace Zend\I18n\View;
9+
10+
use IntlDateFormatter;
11+
12+
// @codingStandardsIgnoreStart
13+
14+
/**
15+
* Helper trait for auto-completion of code in modern IDEs.
16+
*
17+
* The trait provides convenience methods for view helpers,
18+
* defined by the zend-i18n component. It is designed to be used
19+
* for type-hinting $this variable inside zend-view templates via doc blocks.
20+
*
21+
* The base class is PhpRenderer, followed by the helper trait from
22+
* the zend-i18n component. However, multiple helper traits from different
23+
* Zend Framework components can be chained afterwards.
24+
*
25+
* @example @var \Zend\View\Renderer\PhpRenderer|\Zend\I18n\View\HelperTrait $this
26+
*
27+
* @method string currencyFormat(float $number, string|null $currencyCode = null, bool|null $showDecimals = null, string|null $locale = null, string|null $pattern = null)
28+
* @method string dateFormat(\DateTime|int|array $date, int $dateType = IntlDateFormatter::NONE, int $timeType = IntlDateFormatter::NONE, string|null $locale = null, string|null $pattern = null)
29+
* @method string numberFormat(int|float $number, int|null $formatStyle = null, int|null $formatType = null, string|null $locale = null, int|null $decimals = null, array|null $textAttributes = null)
30+
* @method string plural(array|string $strings, int $number)
31+
* @method string translate(string $message, string|null $textDomain = null, string|null $locale = null)
32+
* @method string translatePlural(string $singular, string $plural, int $number, string|null $textDomain = null, string|null $locale = null)
33+
*/
34+
trait HelperTrait
35+
{
36+
}
37+
// @codingStandardsIgnoreEnd

test/Translator/TranslatorTest.php

+53
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,44 @@ public function testTranslatePlurals()
255255
$this->assertEquals('Message 5 (en) Plural 2', $pl2);
256256
}
257257

258+
public function testTranslatePluralsNonExistantLocale()
259+
{
260+
$this->translator->addTranslationFilePattern(
261+
'phparray',
262+
$this->testFilesDir . '/testarray',
263+
'translation-%s.php'
264+
);
265+
266+
$this->translator->setLocale('es_ES');
267+
268+
$pl0 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 1);
269+
$pl1 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 2);
270+
$pl2 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 10);
271+
272+
$this->assertEquals('Message 5', $pl0);
273+
$this->assertEquals('Message 5 Plural', $pl1);
274+
$this->assertEquals('Message 5 Plural', $pl2);
275+
}
276+
277+
public function testTranslatePluralsNonExistantTranslation()
278+
{
279+
$this->translator->addTranslationFilePattern(
280+
'phparray',
281+
$this->testFilesDir . '/testarray',
282+
'translation-%s.php'
283+
);
284+
285+
$this->translator->setLocale('de_DE');
286+
287+
$pl0 = $this->translator->translatePlural('Message 12', 'Message 12 Plural', 1);
288+
$pl1 = $this->translator->translatePlural('Message 12', 'Message 12 Plural', 2);
289+
$pl2 = $this->translator->translatePlural('Message 12', 'Message 12 Plural', 10);
290+
291+
$this->assertEquals('Message 12', $pl0);
292+
$this->assertEquals('Message 12 Plural', $pl1);
293+
$this->assertEquals('Message 12 Plural', $pl2);
294+
}
295+
258296
public function testTranslateNoPlurals()
259297
{
260298
// Some languages such as Japanese and Chinese does not have plural forms
@@ -296,6 +334,21 @@ public function testTranslateNonExistantLocale()
296334
$this->assertEquals('Message 9', $this->translator->translate('Message 9'));
297335
}
298336

337+
public function testTranslateNonExistantTranslation()
338+
{
339+
$this->translator->addTranslationFilePattern(
340+
'phparray',
341+
$this->testFilesDir . '/testarray',
342+
'translation-%s.php'
343+
);
344+
345+
// Test that a locale without translations does not cause warnings
346+
347+
$this->translator->setLocale('de_DE');
348+
349+
$this->assertEquals('Message 13', $this->translator->translate('Message 13'));
350+
}
351+
299352
public function testEnableDisableEventManger()
300353
{
301354
$this->assertFalse($this->translator->isEventManagerEnabled(), 'Default value');

test/Translator/_files/testarray/translation-de_DE.php

+5
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@
1515
'Nachricht 10 - 0',
1616
'Nachricht 10 - 1',
1717
],
18+
'Message 12' => [
19+
'',
20+
'',
21+
],
22+
'Message 13' => '',
1823
];

0 commit comments

Comments
 (0)