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

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 17 changed files with 740 additions and 13 deletions.
86 changes: 86 additions & 0 deletions src/Translator/Loader/Ini.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

namespace Zend\I18n\Translator\Loader;

use Zend\Config\Reader\Ini as IniReader;
use Zend\I18n\Exception;
use Zend\I18n\Translator\Plural\Rule as PluralRule;
use Zend\I18n\Translator\TextDomain;

/**
* PHP INI format loader.
*
* @category Zend
* @package Zend_I18n
* @subpackage Translator
*/
class Ini implements FileLoaderInterface
{
/**
* load(): defined by FileLoaderInterface.
*
* @see FileLoaderInterface::load()
* @param string $locale
* @param string $filename
* @return TextDomain|null
* @throws Exception\InvalidArgumentException
*/
public function load($locale, $filename)
{
if (!is_file($filename) || !is_readable($filename)) {
throw new Exception\InvalidArgumentException(sprintf(
'Could not open file %s for reading',
$filename
));
}

$messages = array();
$iniReader = new IniReader();
$messagesNamespaced = $iniReader->fromFile($filename);

$list = $messagesNamespaced;
if (isset($messagesNamespaced['translation'])) {
$list = $messagesNamespaced['translation'];
}

foreach ($list as $message) {
if (!is_array($message) || count($message) < 2) {
throw new Exception\InvalidArgumentException(
'Each INI row must be an array with message and translation'
);
}
if (isset($message['message']) && isset($message['translation'])) {
$messages[$message['message']] = $message['translation'];
continue;
}
$messages[array_shift($message)] = array_shift($message);
}

if (!is_array($messages)) {
throw new Exception\InvalidArgumentException(sprintf(
'Expected an array, but received %s',
gettype($messages)
));
}

$textDomain = new TextDomain($messages);

if (array_key_exists('plural', $messagesNamespaced)
&& isset($messagesNamespaced['plural']['plural_forms'])
) {
$textDomain->setPluralRule(
PluralRule::fromString($messagesNamespaced['plural']['plural_forms'])
);
}

return $textDomain;
}
}
3 changes: 2 additions & 1 deletion src/Translator/LoaderPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class LoaderPluginManager extends AbstractPluginManager
* @var array
*/
protected $invokableClasses = array(
'phparray' => 'Zend\I18n\Translator\Loader\PhpArray',
'gettext' => 'Zend\I18n\Translator\Loader\Gettext',
'ini' => 'Zend\I18n\Translator\Loader\Ini',
'phparray' => 'Zend\I18n\Translator\Loader\PhpArray',
);

/**
Expand Down
120 changes: 120 additions & 0 deletions src/Translator/TranslatorAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

namespace Zend\I18n\Translator;

use Zend\I18n\Translator\Translator;

/**
* @category Zend
* @package Zend_I18n
* @subpackage Translator
*/
trait TranslatorAwareTrait
{
/**
* @var Translator
*/
protected $translator = null;

/**
* @var bool
*/
protected $translatorEnabled = true;

/**
* @var string
*/
protected $translatorTextDomain = 'default';

/**
* Sets translator to use in helper
*
* @param Translator $translator
* @param string $textDomain
* @return mixed
*/
public function setTranslator(Translator $translator = null, $textDomain = null)
{
$this->translator = $translator;

if (!is_null($textDomain)) {
$this->setTranslatorTextDomain($textDomain);
}

return $this;
}

/**
* Returns translator used in object
*
* @return Translator
*/
public function getTranslator()
{
return $this->translator;
}

/**
* Checks if the object has a translator
*
* @return bool
*/
public function hasTranslator()
{
return !is_null($this->translator);
}

/**
* Sets whether translator is enabled and should be used
*
* @param bool $enabled
* @return mixed
*/
public function setTranslatorEnabled($enabled = true)
{
$this->translatorEnabled = $enabled;

return $this;
}

/**
* Returns whether translator is enabled and should be used
*
* @return bool
*/
public function isTranslatorEnabled()
{
return $this->translatorEnabled;
}

/**
* Set translation text domain
*
* @param string $textDomain
* @return mixed
*/
public function setTranslatorTextDomain($textDomain = 'default')
{
$this->translatorTextDomain = $textDomain;

return $this;
}

/**
* Return the translation text domain
*
* @return string
*/
public function getTranslatorTextDomain()
{
return $this->translatorTextDomain;
}
}
19 changes: 11 additions & 8 deletions src/View/Helper/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,35 @@ public function getlocale()
/**
* Format a date.
*
* @param DateTime|integer|array $date
* @param integer $dateType
* @param integer $timeType
* @param string $locale
* @param DateTime|integer|array $date
* @param int $dateType
* @param int $timeType
* @param string $locale
* @param string|null $pattern
* @return string
* @throws Exception\RuntimeException
*/
public function __invoke(
$date,
$dateType = IntlDateFormatter::NONE,
$timeType = IntlDateFormatter::NONE,
$locale = null
$locale = null,
$pattern = null
) {
if ($locale === null) {
$locale = $this->getlocale();
}

$timezone = $this->getTimezone();
$formatterId = md5($dateType . "\0" . $timeType . "\0" . $locale);
$formatterId = md5($dateType . "\0" . $timeType . "\0" . $locale ."\0" . $pattern);

if (!isset($this->formatters[$formatterId])) {
$this->formatters[$formatterId] = new IntlDateFormatter(
$locale,
$dateType,
$timeType,
$timezone
$timezone,
IntlDateFormatter::GREGORIAN,
$pattern
);
}

Expand Down
84 changes: 84 additions & 0 deletions src/View/Helper/Plural.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_View
*/

namespace Zend\I18n\View\Helper;

use Zend\I18n\Exception;
use Zend\I18n\Translator\Plural\Rule as PluralRule;
use Zend\View\Helper\AbstractHelper;

/**
* Helper for rendering text based on a count number (like the I18n plural translation helper, but when translation
* is not needed).
*
* Please note that we did not write any hard-coded rules for languages, as languages can evolve, we prefered to
* let the developer define the rules himself, instead of potentially break applications if we change rules in the
* future.
*
* However, you can find most of the up-to-date plural rules for most languages in those links:
* - http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
* - https://developer.mozilla.org/en-US/docs/Localization_and_Plurals
*
* @category Zend
* @package Zend_I18n
* @subpackage View
*/
class Plural extends AbstractHelper
{
/**
* Rule to use
*
* @var PluralRule
*/
protected $rule;

/**
* Set the plural rule to use
*
* @param PluralRule|string $pluralRule
* @return Plural
*/
public function setPluralRule($pluralRule)
{
if (!$pluralRule instanceof PluralRule) {
$pluralRule = PluralRule::fromString($pluralRule);
}

$this->rule = $pluralRule;

return $this;
}

/**
* Given an array of strings, a number and, if wanted, an optional locale (the default one is used
* otherwise), this picks the right string according to plural rules of the locale
*
* @param array|string $strings
* @param int $number
* @throws Exception\InvalidArgumentException
* @return string
*/
public function __invoke($strings, $number)
{
if ($this->rule === null) {
throw new Exception\InvalidArgumentException(sprintf(
'No plural rule was set'
));
}

if (!is_array($strings)) {
$strings = (array) $strings;
}

$pluralIndex = $this->rule->evaluate($number);

return $strings[$pluralIndex];
}
}
1 change: 1 addition & 0 deletions src/View/HelperConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class HelperConfig implements ConfigInterface
'currencyformat' => 'Zend\I18n\View\Helper\CurrencyFormat',
'dateformat' => 'Zend\I18n\View\Helper\DateFormat',
'numberformat' => 'Zend\I18n\View\Helper\NumberFormat',
'plural' => 'Zend\I18n\View\Helper\Plural',
'translate' => 'Zend\I18n\View\Helper\Translate',
'translateplural' => 'Zend\I18n\View\Helper\TranslatePlural',
);
Expand Down
Loading

0 comments on commit efcc8e0

Please sign in to comment.