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

Commit

Permalink
Merge branch 'hotfix/i18n-translator' of https://github.com/denixport…
Browse files Browse the repository at this point in the history
…/zf2 into hotfix/i18n-translator
  • Loading branch information
Show file tree
Hide file tree
Showing 31 changed files with 652 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidArgumentException extends \InvalidArgumentException implements
class InvalidArgumentException extends \InvalidArgumentException implements
ExceptionInterface
{}
{}
2 changes: 1 addition & 1 deletion src/Exception/OutOfBoundsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class OutOfBoundsException extends \OutOfBoundsException implements
class OutOfBoundsException extends \OutOfBoundsException implements
ExceptionInterface
{}
File renamed without changes.
57 changes: 30 additions & 27 deletions src/Translator/Loader/Gettext.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,41 +56,46 @@ class Gettext implements LoaderInterface
* @see LoaderInterface::load()
* @param string $filename
* @param string $locale
* @return TextDomain|null
* @return TextDomain
* @throws Exception\InvalidArgumentException
*/
public function load($filename, $locale)
{
if (!is_file($filename) || !is_readable($filename)) {
throw new Exception\InvalidArgumentException(sprintf(
'Could not open file %s for reading',
$filename
));
}

$textDomain = new TextDomain();
$this->file = @fopen($filename, 'rb');

if (!$this->file) {
throw new Exception\InvalidArgumentException(
sprintf('Could not open file %s for reading', $filename)
);
}
$this->file = @fopen($filename, 'rb');

// Verify magic number
$magic = fread($this->file, 4);

if ($magic === "\x95\x04\x12\xde") {
$this->littleEndian = true;
} elseif ($magic === "\xde\x12\x04\x95") {
if ($magic == "\x95\x04\x12\xde") {
$this->littleEndian = false;
} elseif ($magic == "\xde\x12\x04\x95") {
$this->littleEndian = true;
} else {
fclose($this->file);
throw new Exception\InvalidArgumentException(
sprintf('%s is not a valid gettext file', $filename)
);
throw new Exception\InvalidArgumentException(sprintf(
'%s is not a valid gettext file',
$filename
));
}

// Verify major revision (only 0 and 1 supported)
$majorRevision = ($this->readInteger() >> 16);

if ($majorRevision !== 0 && $majorRevision !== 1) {
fclose($this->file);
throw new Exception\InvalidArgumentException(
sprintf('%s has an unknown major revision', $filename)
);
throw new Exception\InvalidArgumentException(sprintf(
'%s has an unknown major revision',
$filename
));
}

// Gather main information
Expand All @@ -100,10 +105,10 @@ public function load($filename, $locale)

// Usually there follow size and offset of the hash table, but we have
// no need for it, so we skip them.
fseek($originalStringTableOffset);
fseek($this->file, $originalStringTableOffset);
$originalStringTable = $this->readIntegerList(2 * $numStrings);

fseek($translationStringTableOffset);
fseek($this->file, $translationStringTableOffset);
$translationStringTable = $this->readIntegerList(2 * $numStrings);

// Read in all translations
Expand All @@ -117,39 +122,37 @@ public function load($filename, $locale)

$originalString = array('');
if ($originalStringSize > 0) {
fseek($originalStringOffset);
fseek($this->file, $originalStringOffset);
$originalString = explode("\0", fread($this->file, $originalStringSize));
}

if ($translationStringSize > 0) {
fseek($translationStringOffset);
fseek($this->file, $translationStringOffset);
$translationString = explode("\0", fread($this->file, $translationStringSize));

if (count($originalString) > 1 && count($translationString) > 1) {
$textDomain[$original[0]] = $translationString;
$textDomain[$originalString[0]] = $translationString;

array_shift($originalString);

foreach ($originalString as $string) {
$textDomain[$string] = '';
}
} else {
$textDomain[$original[0]] = $translationString[0];
$textDomain[$originalString[0]] = $translationString[0];
}
}
}

// Read header entries
if (array_key_exists('', $textDomain)) {
$rawHeaders = explode("\n", $textDomain['']);
$rawHeaders = explode("\n", trim($textDomain['']));

foreach ($rawHeaders as $rawHeader) {
list($header, $content) = explode(':', $rawHeader, 1);
list($header, $content) = explode(':', $rawHeader, 2);

if (trim(strtolower($header)) === 'plural-forms') {
$textDomain->pluralRule(
PluralRule::fromString($content)
);
$textDomain->setPluralRule(PluralRule::fromString($content));
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/Translator/Loader/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

namespace Zend\I18n\Translator\Loader;

use Zend\I18n\Translator\TextDomain;

/**
* Loader interface.
*
Expand All @@ -39,7 +37,7 @@ interface LoaderInterface
*
* @param string $filename
* @param string $locale
* @return TextDomain|null
* @return \Zend\I18n\Translator\TextDomain|null
*/
public function load($filename, $locale);
}
17 changes: 10 additions & 7 deletions src/Translator/Loader/PhpArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,31 @@ class PhpArray implements LoaderInterface
* @param string $filename
* @param string $locale
* @return TextDomain|null
* @throws Exception\InvalidArgumentException
*/
public function load($filename, $locale)
{
if (!is_file($filename) || !is_readable($filename)) {
throw new Exception\InvalidArgumentException(
sprintf('Could not open file %s for reading', $filename)
);
throw new Exception\InvalidArgumentException(sprintf(
'Could not open file %s for reading',
$filename
));
}

$messages = include $filename;

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

$textDomain = new TextDomain($messages);

if (array_key_exists('', $textDomain)) {
if (isset($textDomain['']['plural_forms'])) {
$textDomain->pluralRule(
$textDomain->setPluralRule(
PluralRule::fromString($textDomain['']['plural_forms'])
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/LoaderPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class LoaderPluginManager extends AbstractPluginManager
* @var array
*/
protected $invokableClasses = array(
'phpArray' => 'Zend\I18n\Translator\Loader\PhpArray',
'phparray' => 'Zend\I18n\Translator\Loader\PhpArray',
'gettext' => 'Zend\I18n\Translator\Loader\Gettext',
);

Expand Down
2 changes: 2 additions & 0 deletions src/Translator/Plural/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ public function expression($rightBindingPower = 0)
*
* @param string $id
* @return void
* @throws Exception\ParseException
*/
public function advance($id = null)
{
Expand All @@ -295,6 +296,7 @@ public function advance($id = null)
* Get the next token.
*
* @return array
* @throws Exception\ParseException
*/
protected function getNextToken()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Translator/Plural/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ protected function __construct($numPlurals, array $ast)
*
* @param integer $number
* @return integer
* @throws Exception\RangeException
*/
public function evaluate($number)
{
Expand All @@ -94,6 +95,7 @@ public function evaluate($number)
* @param array $ast
* @param integer $number
* @return integer
* @throws Exception\ParseException
*/
protected function evaluateAstPart(array $ast, $number)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Translator/Plural/Symbol.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace Zend\I18n\Translator\Plural;

use Closure;
use Zend\I18n\Exception;

/**
* Parser symbol.
Expand Down Expand Up @@ -154,6 +155,7 @@ public function getNullDenotation()
));
}

/** @var callable $function */
$function = $this->nullDenotationGetter;
return $function($this);
}
Expand All @@ -172,6 +174,7 @@ public function getLeftDenotation($left)
));
}

/** @var callable $function */
$function = $this->leftDenotationGetter;
return $function($this, $left);
}
Expand Down
Loading

0 comments on commit e56ce9b

Please sign in to comment.