diff --git a/src/Loader.php b/src/Loader.php index a4ecb3d..661da72 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -23,12 +23,13 @@ namespace MoTranslator; -class Loader { +class Loader +{ /** - * Loader instance + * Loader instance. * - * @access private * @static + * * @var Loader */ private static $_instance; @@ -48,36 +49,35 @@ class Loader { private $locale = ''; /** - * Loaded domains + * Loaded domains. * * @var array */ private $domains = array(); /** - * Bound paths for domains + * Bound paths for domains. * * @var array */ private $paths = array('' => './'); /** - * Returns the singleton Loader object + * Returns the singleton Loader object. * * @return Loader object */ public static function getInstance() { if (empty(self::$_instance)) { - self::$_instance = new Loader(); + self::$_instance = new self(); } + return self::$_instance; } /** * Loads global localizaton functions. - * - * @return void */ public static function loadFunctions() { @@ -91,23 +91,23 @@ public static function loadFunctions() * * @param string $locale Locale code * - * @return array list of locales to try for any POSIX-style locale specification. + * @return array list of locales to try for any POSIX-style locale specification */ - public static function listLocales($locale) { + public static function listLocales($locale) + { $locale_names = array(); - $lang = NULL; - $country = NULL; - $charset = NULL; - $modifier = NULL; + $lang = null; + $country = null; + $charset = null; + $modifier = null; if ($locale) { if (preg_match('/^(?P[a-z]{2,3})' // language code - .'(?:_(?P[A-Z]{2}))?' // country code - .'(?:\\.(?P[-A-Za-z0-9_]+))?' // charset - .'(?:@(?P[-A-Za-z0-9_]+))?$/', // @ modifier + . '(?:_(?P[A-Z]{2}))?' // country code + . '(?:\\.(?P[-A-Za-z0-9_]+))?' // charset + . '(?:@(?P[-A-Za-z0-9_]+))?$/', // @ modifier $locale, $matches)) { - extract($matches); if ($modifier) { @@ -137,11 +137,12 @@ public static function listLocales($locale) { array_push($locale_names, $locale); } } + return $locale_names; } /** - * Returns Translator object for domain or for default domain + * Returns Translator object for domain or for default domain. * * @param string $domain Translation domain * @@ -154,7 +155,6 @@ public function getTranslator($domain = '') } if (!isset($this->domains[$domain])) { - if (isset($this->paths[$domain])) { $base = $this->paths[$domain]; } else { @@ -184,8 +184,6 @@ public function getTranslator($domain = '') * * @param string $domain Domain name * @param string $path Path where to find locales - * - * @return void */ public function bindtextdomain($domain, $path) { @@ -196,8 +194,6 @@ public function bindtextdomain($domain, $path) * Sets the default domain. * * @param string $domain Domain name - * - * @return void */ public function textdomain($domain) { @@ -205,7 +201,7 @@ public function textdomain($domain) } /** - * Sets a requested locale + * Sets a requested locale. * * @param string $locale Locale name * @@ -220,11 +216,12 @@ public function setlocale($locale) setlocale(0, $locale); } } + return $this->locale; } /** - * Detects currently configured locale + * Detects currently configured locale. * * It checks: * @@ -244,6 +241,7 @@ public function detectlocale() } elseif (getenv('LANG')) { return getenv('LANG'); } + return 'en'; } } diff --git a/src/ReaderException.php b/src/ReaderException.php index c3e53f9..33cbeb7 100644 --- a/src/ReaderException.php +++ b/src/ReaderException.php @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + namespace MoTranslator; /** diff --git a/src/StringReader.php b/src/StringReader.php index c9ae16f..847ef8f 100644 --- a/src/StringReader.php +++ b/src/StringReader.php @@ -19,57 +19,63 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + namespace MoTranslator; /** * Simple wrapper around string buffer for * random access and values parsing. */ -class StringReader { - +class StringReader +{ private $_str; private $_len; /** - * Constructor + * Constructor. * * @param string $filename Name of file to load */ - public function __construct($filename) { + public function __construct($filename) + { $this->_str = file_get_contents($filename); $this->_len = strlen($this->_str); } /** - * Read number of bytes from given offset + * Read number of bytes from given offset. * * @param int $pos Offset * @param int $bytes Number of bytes to read * * @return string */ - public function read($pos, $bytes) { + public function read($pos, $bytes) + { if ($pos + $bytes > $this->_len) { throw new ReaderException('Not enough bytes!'); } + return substr($this->_str, $pos, $bytes); } /** - * Reads a 32bit integer from the stream + * Reads a 32bit integer from the stream. * * @param string $unpack Unpack string * @param int $pos Position * * @return int Ingerer from the stream */ - public function readint($unpack, $pos) { + public function readint($unpack, $pos) + { $data = unpack($unpack, $this->read($pos, 4)); + return $data[1]; } /** - * Reads an array of integers from the stream + * Reads an array of integers from the stream. * * @param string $unpack Unpack string * @param int $pos Position @@ -77,8 +83,8 @@ public function readint($unpack, $pos) { * * @return array Array of Integers */ - public function readintarray($unpack, $pos, $count) { + public function readintarray($unpack, $pos, $count) + { return unpack($unpack . $count, $this->read($pos, 4 * $count)); } - } diff --git a/src/Translator.php b/src/Translator.php index bd92e9c..458e280 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -32,22 +32,22 @@ * * It caches ll strings and translations to speed up the string lookup. */ -class Translator { - +class Translator +{ /** - * None error + * None error. */ const ERROR_NONE = 0; /** - * File does not exist + * File does not exist. */ const ERROR_DOES_NOT_EXIST = 1; /** - * File has bad magic number + * File has bad magic number. */ const ERROR_BAD_MAGIC = 2; /** - * Error while reading file, probably too short + * Error while reading file, probably too short. */ const ERROR_READING = 3; @@ -61,46 +61,43 @@ class Translator { const MAGIC_LE = "\xde\x12\x04\x95"; /** - * Parse error code (0 if no error) + * Parse error code (0 if no error). * * @var int */ - public $error = Translator::ERROR_NONE; + public $error = self::ERROR_NONE; /** - * Cache header field for plural forms + * Cache header field for plural forms. * * @var string|null */ - private $pluralequation = NULL; + private $pluralequation = null; /** - * - * * @var ExpressionLanguage|null Evaluator for plurals */ - private $pluralexpression = NULL; + private $pluralexpression = null; /** - * - * * @var int|null number of plurals */ - private $pluralcount = NULL; + private $pluralcount = null; /** - * Array with original -> translation mapping + * Array with original -> translation mapping. * * @var array */ private $cache_translations = array(); /** - * Constructor + * Constructor. * * @param string $filename Name of mo file to load */ public function __construct($filename) { if (!is_readable($filename)) { - $this->error = Translator::ERROR_DOES_NOT_EXIST; + $this->error = self::ERROR_DOES_NOT_EXIST; + return; } @@ -108,12 +105,13 @@ public function __construct($filename) try { $magic = $stream->read(0, 4); - if (strcmp($magic, Translator::MAGIC_LE) == 0) { + if (strcmp($magic, self::MAGIC_LE) == 0) { $unpack = 'V'; - } elseif (strcmp($magic, Translator::MAGIC_BE) == 0) { + } elseif (strcmp($magic, self::MAGIC_BE) == 0) { $unpack = 'N'; } else { - $this->error = Translator::ERROR_BAD_MAGIC; + $this->error = self::ERROR_BAD_MAGIC; + return; } @@ -127,19 +125,20 @@ public function __construct($filename) $table_translations = $stream->readintarray($unpack, $translations, $total * 2); /* read all strings to the cache */ - for ($i = 0; $i < $total; $i++) { + for ($i = 0; $i < $total; ++$i) { $original = $stream->read($table_originals[$i * 2 + 2], $table_originals[$i * 2 + 1]); $translation = $stream->read($table_translations[$i * 2 + 2], $table_translations[$i * 2 + 1]); $this->cache_translations[$original] = $translation; } } catch (ReaderException $e) { - $this->error = Translator::ERROR_READING; + $this->error = self::ERROR_READING; + return; } } /** - * Translates a string + * Translates a string. * * @param string $msgid String to be translated * @@ -155,7 +154,7 @@ public function gettext($msgid) } /** - * Sanitize plural form expression for use in ExpressionLanguage + * Sanitize plural form expression for use in ExpressionLanguage. * * @param string $expr Expression to sanitize * @@ -179,11 +178,12 @@ public static function sanitizePluralExpression($expr) if (substr($expr, 0, 1) === '=') { $expr = ltrim(substr($expr, 1)); } + return $expr; } /** - * Extracts number of plurals from plurals form expression + * Extracts number of plurals from plurals form expression. * * @param string $expr Expression to process * @@ -196,6 +196,7 @@ public static function extractPluralCount($expr) if (strtolower(rtrim($nplurals[0])) != 'nplurals') { return 1; } + return intval($nplurals[1]); } @@ -215,11 +216,12 @@ public static function extractPluralsForms($header) $expr = substr($header, 13); } } + return $expr; } /** - * Get possible plural forms from MO header + * Get possible plural forms from MO header. * * @return string plural form header */ @@ -235,11 +237,12 @@ private function getPluralForms() $this->pluralequation = $this->sanitizePluralExpression($expr); $this->pluralcount = $this->extractPluralCount($expr); } + return $this->pluralequation; } /** - * Detects which plural form to take + * Detects which plural form to take. * * @param int $n count of objects * @@ -257,11 +260,12 @@ private function selectString($n) if ($plural >= $this->pluralcount) { $plural = $this->pluralcount - 1; } + return $plural; } /** - * Plural version of gettext + * Plural version of gettext. * * @param string $msgid Single form * @param string $msgidPlural Plural form @@ -282,14 +286,15 @@ public function ngettext($msgid, $msgidPlural, $number) $result = $this->cache_translations[$key]; $list = explode(chr(0), $result); + return $list[$select]; } /** - * Translate with context + * Translate with context. * - * @param string $msgctxt Context - * @param string $msgid String to be translated + * @param string $msgctxt Context + * @param string $msgid String to be translated * * @return string translated plural form */ @@ -305,7 +310,7 @@ public function pgettext($msgctxt, $msgid) } /** - * Plural version of pgettext + * Plural version of pgettext. * * @param string $msgctxt Context * @param string $msgid Single form diff --git a/src/functions.php b/src/functions.php index 64fed85..6295373 100644 --- a/src/functions.php +++ b/src/functions.php @@ -24,7 +24,7 @@ use MoTranslator\Loader; /** - * Sets a requested locale + * Sets a requested locale. * * @param int $category Locale category, ignored * @param string $locale Locale name @@ -41,8 +41,6 @@ function _setlocale($category, $locale) * * @param string $domain Domain name * @param string $path Path where to find locales - * - * @return void */ function _bindtextdomain($domain, $path) { @@ -51,11 +49,10 @@ function _bindtextdomain($domain, $path) /** * Dummy compatibility function, MoTranslator assumes - * everything is UTF-8 - * - * @return void + * everything is UTF-8. */ -function _bind_textdomain_codeset($domain, $codeset) { +function _bind_textdomain_codeset($domain, $codeset) +{ return; } @@ -63,8 +60,6 @@ function _bind_textdomain_codeset($domain, $codeset) { * Sets the default domain. * * @param string $domain Domain name - * - * @return void */ function _textdomain($domain) { @@ -72,7 +67,7 @@ function _textdomain($domain) } /** - * Translates a string + * Translates a string. * * @param string $msgid String to be translated * @@ -86,7 +81,7 @@ function _gettext($msgid) } /** - * Translates a string, alias for _gettext + * Translates a string, alias for _gettext. * * @param string $msgid String to be translated * @@ -100,7 +95,7 @@ function __($msgid) } /** - * Plural version of gettext + * Plural version of gettext. * * @param string $msgid Single form * @param string $msgidPlural Plural form @@ -116,10 +111,10 @@ function _ngettext($msgid, $msgidPlural, $number) } /** - * Translate with context + * Translate with context. * - * @param string $msgctxt Context - * @param string $msgid String to be translated + * @param string $msgctxt Context + * @param string $msgid String to be translated * * @return string translated plural form */ @@ -131,7 +126,7 @@ function _pgettext($msgctxt, $msgid) } /** - * Plural version of pgettext + * Plural version of pgettext. * * @param string $msgctxt Context * @param string $msgid Single form @@ -148,7 +143,7 @@ function _npgettext($msgctxt, $msgid, $msgidPlural, $number) } /** - * Translates a string + * Translates a string. * * @param string $domain Domain to use * @param string $msgid String to be translated @@ -163,7 +158,7 @@ function _dgettext($domain, $msgid) } /** - * Plural version of gettext + * Plural version of gettext. * * @param string $domain Domain to use * @param string $msgid Single form @@ -180,7 +175,7 @@ function _dngettext($domain, $msgid, $msgidPlural, $number) } /** - * Translate with context + * Translate with context. * * @param string $domain Domain to use * @param string $msgctxt Context @@ -196,7 +191,7 @@ function _dpgettext($domain, $msgctxt, $msgid) } /** - * Plural version of pgettext + * Plural version of pgettext. * * @param string $domain Domain to use * @param string $msgctxt Context diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index e1b7ae1..396ab30 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -1,12 +1,9 @@ setlocale($locale); $loader->textdomain($domain); $loader->bindtextdomain($domain, __DIR__ . '/data/locale/'); + return $loader; } diff --git a/tests/MoFilesTest.php b/tests/MoFilesTest.php index 4c58e9e..8c22b90 100644 --- a/tests/MoFilesTest.php +++ b/tests/MoFilesTest.php @@ -1,11 +1,9 @@ =3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5;\n", - 'n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5' + 'n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5', ), array( ' nplurals=1; plural=baz(n);', @@ -112,5 +109,4 @@ public function pluralExpressions() ), ); } - } diff --git a/tests/PluralTest.php b/tests/PluralTest.php index eddb0ee..b1905fb 100644 --- a/tests/PluralTest.php +++ b/tests/PluralTest.php @@ -1,20 +1,17 @@ npgettext( - "context", + 'context', "%d pig went to the market\n", "%d pigs went to the market\n", $number @@ -31,7 +28,7 @@ public function testNpgettext($number, $expected) } /** - * Data provider for test_npgettext + * Data provider for test_npgettext. * * @return array */ diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 0422d78..4485057 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,9 +2,5 @@ /** * Bootstrap for tests. - * - * @package MoTranslator - * @subpackage Tests */ - require_once 'vendor/autoload.php';