Skip to content

Commit

Permalink
Use camel caps for variable names
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Feb 27, 2020
1 parent abfaf2f commit 189f6da
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 62 deletions.
42 changes: 21 additions & 21 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class Loader
* @static
* @var Loader
*/
private static $_instance;
private static $instance;

/**
* Default gettext domain to use.
*
* @var string
*/
private $default_domain = '';
private $defaultDomain = '';

/**
* Configured locale.
Expand Down Expand Up @@ -76,11 +76,11 @@ class Loader
*/
public static function getInstance()
{
if (empty(self::$_instance)) {
self::$_instance = new self();
if (empty(self::$instance)) {
self::$instance = new self();
}

return self::$_instance;
return self::$instance;
}

/**
Expand All @@ -102,7 +102,7 @@ public static function loadFunctions()
*/
public static function listLocales($locale)
{
$locale_names = [];
$localeNames = [];

if ($locale) {
if (preg_match(
Expand All @@ -122,57 +122,57 @@ public static function listLocales($locale)
if ($country) {
if ($charset) {
array_push(
$locale_names,
$localeNames,
sprintf('%s_%s.%s@%s', $lang, $country, $charset, $modifier)
);
}

array_push(
$locale_names,
$localeNames,
sprintf('%s_%s@%s', $lang, $country, $modifier)
);
} elseif ($charset) {
array_push(
$locale_names,
$localeNames,
sprintf('%s.%s@%s', $lang, $charset, $modifier)
);
}

array_push(
$locale_names,
$localeNames,
sprintf('%s@%s', $lang, $modifier)
);
}

if ($country) {
if ($charset) {
array_push(
$locale_names,
$localeNames,
sprintf('%s_%s.%s', $lang, $country, $charset)
);
}

array_push(
$locale_names,
$localeNames,
sprintf('%s_%s', $lang, $country)
);
} elseif ($charset) {
array_push(
$locale_names,
$localeNames,
sprintf('%s.%s', $lang, $charset)
);
}

array_push($locale_names, $lang);
array_push($localeNames, $lang);
}

// If the locale name doesn't match POSIX style, just include it as-is.
if (! in_array($locale, $locale_names)) {
array_push($locale_names, $locale);
if (! in_array($locale, $localeNames)) {
array_push($localeNames, $locale);
}
}

return $locale_names;
return $localeNames;
}

/**
Expand All @@ -185,7 +185,7 @@ public static function listLocales($locale)
public function getTranslator($domain = '')
{
if (empty($domain)) {
$domain = $this->default_domain;
$domain = $this->defaultDomain;
}

if (! isset($this->domains[$this->locale])) {
Expand All @@ -199,10 +199,10 @@ public function getTranslator($domain = '')
$base = './';
}

$locale_names = $this->listLocales($this->locale);
$localeNames = $this->listLocales($this->locale);

$filename = '';
foreach ($locale_names as $locale) {
foreach ($localeNames as $locale) {
$filename = $base . '/' . $locale . '/LC_MESSAGES/' . $domain . '.mo';
if (file_exists($filename)) {
break;
Expand Down Expand Up @@ -235,7 +235,7 @@ public function bindtextdomain($domain, $path)
*/
public function textdomain($domain)
{
$this->default_domain = $domain;
$this->defaultDomain = $domain;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/StringReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@
*/
class StringReader
{
private $_str;
private $_len;
private $string;
private $length;

/**
* @param string $filename Name of file to load
*/
public function __construct($filename)
{
$this->_str = (string) file_get_contents($filename);
$this->_len = strlen($this->_str);
$this->string = (string) file_get_contents($filename);
$this->length = strlen($this->string);
}

/**
Expand All @@ -57,11 +57,11 @@ public function __construct($filename)
*/
public function read($pos, $bytes)
{
if ($pos + $bytes > $this->_len) {
if ($pos + $bytes > $this->length) {
throw new ReaderException('Not enough bytes!');
}

return substr($this->_str, $pos, $bytes);
return substr($this->string, $pos, $bytes);
}

/**
Expand Down
55 changes: 29 additions & 26 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,20 @@ class Translator
*
* @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.
*
* @var array
*/
private $cache_translations = [];
private $cacheTranslations = [];

/**
* @param string $filename Name of mo file to load
Expand Down Expand Up @@ -133,14 +136,14 @@ public function __construct(string $filename)
$translations = $stream->readint($unpack, 16);

/* get original and translations tables */
$table_originals = $stream->readintarray($unpack, $originals, $total * 2);
$table_translations = $stream->readintarray($unpack, $translations, $total * 2);
$tableOriginals = $stream->readintarray($unpack, $originals, $total * 2);
$tableTranslations = $stream->readintarray($unpack, $translations, $total * 2);

/* read all strings to the cache */
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;
$original = $stream->read($tableOriginals[$i * 2 + 2], $tableOriginals[$i * 2 + 1]);
$translation = $stream->read($tableTranslations[$i * 2 + 2], $tableTranslations[$i * 2 + 1]);
$this->cacheTranslations[$original] = $translation;
}
} catch (ReaderException $e) {
$this->error = self::ERROR_READING;
Expand All @@ -158,8 +161,8 @@ public function __construct(string $filename)
*/
public function gettext($msgid)
{
if (array_key_exists($msgid, $this->cache_translations)) {
return $this->cache_translations[$msgid];
if (array_key_exists($msgid, $this->cacheTranslations)) {
return $this->cacheTranslations[$msgid];
}

return $msgid;
Expand All @@ -174,7 +177,7 @@ public function gettext($msgid)
*/
public function exists($msgid)
{
return array_key_exists($msgid, $this->cache_translations);
return array_key_exists($msgid, $this->cacheTranslations);
}

/**
Expand Down Expand Up @@ -264,19 +267,19 @@ private function getPluralForms()
// this is true, right?
// cache header field for plural forms
if ($this->pluralequation === null) {
if (isset($this->cache_translations[''])) {
$header = $this->cache_translations[''];
if ($this->pluralEquation === null) {
if (isset($this->cacheTranslations[''])) {
$header = $this->cacheTranslations[''];
} else {
$header = '';
}

$expr = $this->extractPluralsForms($header);
$this->pluralequation = $this->sanitizePluralExpression($expr);
$this->pluralcount = $this->extractPluralCount($expr);
$this->pluralEquation = $this->sanitizePluralExpression($expr);
$this->pluralCount = $this->extractPluralCount($expr);
}

return $this->pluralequation;
return $this->pluralEquation;
}

/**
Expand All @@ -288,21 +291,21 @@ private function getPluralForms()
*/
private function selectString($n)
{
if ($this->pluralexpression === null) {
$this->pluralexpression = new ExpressionLanguage();
if ($this->pluralExpression === null) {
$this->pluralExpression = new ExpressionLanguage();
}

try {
$plural = $this->pluralexpression->evaluate(
$plural = $this->pluralExpression->evaluate(
$this->getPluralForms(),
['n' => $n]
);
} catch (Throwable $e) {
$plural = 0;
}

if ($plural >= $this->pluralcount) {
$plural = $this->pluralcount - 1;
if ($plural >= $this->pluralCount) {
$plural = $this->pluralCount - 1;
}

return $plural;
Expand All @@ -321,14 +324,14 @@ public function ngettext($msgid, $msgidPlural, $number)
{
// this should contains all strings separated by NULLs
$key = implode(chr(0), [$msgid, $msgidPlural]);
if (! array_key_exists($key, $this->cache_translations)) {
if (! array_key_exists($key, $this->cacheTranslations)) {
return ($number != 1) ? $msgidPlural : $msgid;
}

// find out the appropriate form
$select = $this->selectString($number);

$result = $this->cache_translations[$key];
$result = $this->cacheTranslations[$key];
$list = explode(chr(0), $result);
if ($list === false) {
return '';
Expand Down Expand Up @@ -391,6 +394,6 @@ public function npgettext($msgctxt, $msgid, $msgidPlural, $number)
*/
public function setTranslation($msgid, $msgstr)
{
$this->cache_translations[$msgid] = $msgstr;
$this->cacheTranslations[$msgid] = $msgstr;
}
}
18 changes: 9 additions & 9 deletions tests/MoFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ public function testMoFileTranslate($filename)
public function testMoFilePlurals($filename)
{
$parser = new Translator($filename);
$expected_2 = '%d sekundy';
$expected2 = '%d sekundy';
if (strpos($filename, 'invalid-formula.mo') !== false || strpos($filename, 'lessplurals.mo') !== false) {
$expected_0 = '%d sekunda';
$expected_2 = '%d sekunda';
$expected0 = '%d sekunda';
$expected2 = '%d sekunda';
} elseif (strpos($filename, 'plurals.mo') !== false || strpos($filename, 'noheader.mo') !== false) {
$expected_0 = '%d sekundy';
$expected0 = '%d sekundy';
} else {
$expected_0 = '%d sekund';
$expected0 = '%d sekund';
}

$this->assertEquals(
$expected_0,
$expected0,
$parser->ngettext(
'%d second',
'%d seconds',
Expand All @@ -69,23 +69,23 @@ public function testMoFilePlurals($filename)
)
);
$this->assertEquals(
$expected_2,
$expected2,
$parser->ngettext(
'%d second',
'%d seconds',
2
)
);
$this->assertEquals(
$expected_0,
$expected0,
$parser->ngettext(
'%d second',
'%d seconds',
5
)
);
$this->assertEquals(
$expected_0,
$expected0,
$parser->ngettext(
'%d second',
'%d seconds',
Expand Down

0 comments on commit 189f6da

Please sign in to comment.