Skip to content

Commit 12753df

Browse files
committed
Merge branch 'eusonlito-master'
2 parents 21cd12d + 6562413 commit 12753df

File tree

10 files changed

+51
-34
lines changed

10 files changed

+51
-34
lines changed

src/Extractors/Extractor.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Gettext\Extractors;
33

4+
use Exception, InvalidArgumentException;
45
use Gettext\Translations;
56

67
abstract class Extractor
@@ -36,16 +37,16 @@ public static function fromFile($file, Translations $translations = null)
3637
protected static function getFiles($file)
3738
{
3839
if (empty($file)) {
39-
throw new \InvalidArgumentException('There is not any file defined');
40+
throw new InvalidArgumentException('There is not any file defined');
4041
}
4142

4243
if (is_string($file)) {
4344
if (!is_file($file)) {
44-
throw new \InvalidArgumentException("'$file' is not a valid file");
45+
throw new InvalidArgumentException("'$file' is not a valid file");
4546
}
4647

4748
if (!is_readable($file)) {
48-
throw new \InvalidArgumentException("'$file' is not a readable file");
49+
throw new InvalidArgumentException("'$file' is not a readable file");
4950
}
5051

5152
return array($file);
@@ -61,7 +62,7 @@ protected static function getFiles($file)
6162
return $files;
6263
}
6364

64-
throw new \InvalidArgumentException('The first argumet must be string or array');
65+
throw new InvalidArgumentException('The first argumet must be string or array');
6566
}
6667

6768
/**
@@ -76,7 +77,7 @@ protected static function readFile($file)
7677
$length = filesize($file);
7778

7879
if (!($fd = fopen($file, 'rb'))) {
79-
throw new \Exception("Cannot read the file '$file', probably permissions");
80+
throw new Exception("Cannot read the file '$file', probably permissions");
8081
}
8182

8283
$content = fread($fd, $length);

src/Extractors/Mo.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,22 @@ public static function fromString($string, Translations $translations = null, $f
4949
$stream->seekto($table_originals[$i * 2 + 2]);
5050
$original = $stream->read($table_originals[$i * 2 + 1]);
5151

52-
if ($original) {
53-
$stream->seekto($table_translations[$i * 2 + 2]);
54-
$original = explode("\000", $original, 2);
55-
$translated = explode("\000", $stream->read($table_translations[$i * 2 + 1]), 2);
52+
if (empty($original)) {
53+
continue;
54+
}
55+
56+
$stream->seekto($table_translations[$i * 2 + 2]);
57+
$original = explode("\000", $original, 2);
58+
$translated = explode("\000", $stream->read($table_translations[$i * 2 + 1]), 2);
5659

57-
$plural = isset($original[1]) ? $original[1] : '';
58-
$pluralTranslation = isset($translated[1]) ? $translated[1] : '';
60+
$plural = isset($original[1]) ? $original[1] : '';
61+
$pluralTranslation = isset($translated[1]) ? $translated[1] : '';
5962

60-
$translation = $translations->insert(null, $original[0], $plural);
61-
$translation->setTranslation($translated[0]);
63+
$translation = $translations->insert(null, $original[0], $plural);
64+
$translation->setTranslation($translated[0]);
6265

63-
if ($plural && $pluralTranslation) {
64-
$translation->setPluralTranslation($pluralTranslation);
65-
}
66+
if ($plural && $pluralTranslation) {
67+
$translation->setPluralTranslation($pluralTranslation);
6668
}
6769
}
6870
}

src/Extractors/PhpArray.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Gettext\Extractors;
33

4+
use Exception;
45
use Gettext\Translations;
56

67
/**
@@ -34,7 +35,7 @@ public static function fromFile($file, Translations $translations = null)
3435
*/
3536
public static function fromString($string, Translations $translations = null, $file = '')
3637
{
37-
throw new \Exception("PhpArray::fromString() cannot be called. Use PhpArray::fromFile()");
38+
throw new Exception("PhpArray::fromString() cannot be called. Use PhpArray::fromFile()");
3839
}
3940

4041
/**

src/Extractors/Po.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private static function clean($str)
181181
*
182182
* @return string
183183
*/
184-
private static function fixMultiLines($line, Array $lines, &$i)
184+
private static function fixMultiLines($line, array $lines, &$i)
185185
{
186186
for ($j = $i; $j<count($lines); $j++) {
187187
if (substr($line, -1, 1) == '"'

src/Translator.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Gettext;
33

4-
use Gettext\Translations;
4+
use Exception;
55
use Gettext\Generators\PhpArray;
66

77
class Translator
@@ -22,13 +22,15 @@ public function loadTranslations($translations)
2222
{
2323
if ($translations instanceof Translations) {
2424
$this->loadArray(PhpArray::toArray($translations));
25-
} else if (is_string($translations) && is_file($translations)) {
25+
} elseif (is_string($translations) && is_file($translations)) {
2626
$this->loadArray(include $translations);
27-
} else if (is_array($translations)) {
27+
} elseif (is_array($translations)) {
2828
$this->loadArray($translations);
2929
} else {
3030
throw new \InvalidArgumentException('Invalid Translator: only arrays, files or instance of Translations are allowed');
3131
}
32+
33+
return $this;
3234
}
3335

3436
/**
@@ -60,7 +62,7 @@ protected function loadArray(array $translations)
6062
* @param array $translations
6163
* @param null|string $domain
6264
*/
63-
protected function addTranslations(array $translations, $domain = null)
65+
public function addTranslations(array $translations, $domain = null)
6466
{
6567
if ($domain === null) {
6668
$domain = $this->domain;
@@ -73,6 +75,14 @@ protected function addTranslations(array $translations, $domain = null)
7375
}
7476
}
7577

78+
/**
79+
* Clear all translations
80+
*/
81+
public function clearTranslations()
82+
{
83+
$this->dictionary = array();
84+
}
85+
7686
/**
7787
* Search and returns a translation
7888
*
@@ -82,7 +92,7 @@ protected function addTranslations(array $translations, $domain = null)
8292
*
8393
* @return array
8494
*/
85-
protected function getTranslation($domain, $context, $original)
95+
public function getTranslation($domain, $context, $original)
8696
{
8797
$key = isset($context) ? $context.$this->context_glue.$original : $original;
8898

@@ -219,7 +229,7 @@ public function dnpgettext($domain, $context, $original, $plural, $value)
219229
* @param string $n
220230
* @return int
221231
*/
222-
protected function isPlural($n)
232+
public function isPlural($n)
223233
{
224234
if (!$this->pluralFunction) {
225235
$this->pluralFunction = create_function('$n', self::fixTerseIfs($this->pluralCode));

src/Utils/FunctionsScanner.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Gettext\Utils;
33

4+
use Exception;
45
use Gettext\Translations;
56

67
abstract class FunctionsScanner
@@ -56,7 +57,7 @@ public function saveGettextFunctions(array $functions, Translations $translation
5657
break;
5758

5859
default:
59-
throw new \Exception('Not valid functions');
60+
throw new Exception('Not valid functions');
6061
}
6162

6263
$translation->addReference($file, $line);

tests/GettextTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
include_once dirname(__DIR__).'/src/autoloader.php';
33

44
if (!function_exists('n__')) {
5-
require_once(dirname(__DIR__).'/src/translator_functions.php');
5+
require_once dirname(__DIR__).'/src/translator_functions.php';
66
}
77

88
class GettextTest extends PHPUnit_Framework_TestCase
@@ -29,7 +29,7 @@ public function testPoFileExtractor()
2929
return $translations;
3030
}
3131

32-
public function testAutomaticHeaders()
32+
public function testAutomaticHeaders()
3333
{
3434
$translations = Gettext\Extractors\Po::fromFile(__DIR__.'/files/gettext_multiple_headers.po');
3535
$language = 'bs';
@@ -45,11 +45,12 @@ public function testAutomaticHeaders()
4545
return $translations;
4646
}
4747

48-
public function testSplitHeader()
48+
public function testSplitHeader()
4949
{
5050
$translations = Gettext\Extractors\Po::fromFile(__DIR__.'/files/gettext_multiple_headers.po');
5151
$pluralHeader = "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);";
5252
$this->assertEquals($pluralHeader, $translations->getHeader('Plural-Forms'), 'header split over 2 lines not extracted correctly');
53+
5354
return $translations;
5455
}
5556

@@ -173,7 +174,7 @@ public function testPhpArrayGenerator($translations)
173174
*/
174175
public function testMultiPlural($translations)
175176
{
176-
$translator = new \Gettext\Translator;
177+
$translator = new \Gettext\Translator();
177178
$translator->loadTranslations($translations);
178179

179180
//Set the current translator before execute the functions

tests/JedTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
include_once dirname(__DIR__).'/src/autoloader.php';
33

44
if (!function_exists('n__')) {
5-
require_once(dirname(__DIR__).'/src/translator_functions.php');
5+
require_once dirname(__DIR__).'/src/translator_functions.php';
66
}
77

88
class JedTest extends PHPUnit_Framework_TestCase
99
{
10-
public function testScript() {
10+
public function testScript()
11+
{
1112
$translations = Gettext\Extractors\JsCode::fromFile(__DIR__.'/files/script.js');
1213

1314
$string1 = $translations->toJedString();
@@ -16,7 +17,8 @@ public function testScript() {
1617
$this->assertEquals($string1, $string2);
1718
}
1819

19-
public function testDynamicHeaders() {
20+
public function testDynamicHeaders()
21+
{
2022
$translations = Gettext\Extractors\Po::fromFile(__DIR__.'/files/gettext_multiple_headers.po');
2123
$jsonString = Gettext\Generators\Jed::toString($translations, __DIR__.'/files/gettext_multiple_headers.json', true);
2224

tests/MagicMethodsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
include_once dirname(__DIR__).'/src/autoloader.php';
33

44
if (!function_exists('n__')) {
5-
require_once(dirname(__DIR__).'/src/translator_functions.php');
5+
require_once dirname(__DIR__).'/src/translator_functions.php';
66
}
77

88
class MagicMethodsTest extends PHPUnit_Framework_TestCase

tests/files/wordpress-template.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@
2424
</div>
2525
</div>
2626
</div>");
27-
?>

0 commit comments

Comments
 (0)