-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
260 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
namespace l10n\Translator; | ||
|
||
use l10n\Plural\IPlural; | ||
|
||
class Translator { | ||
/** @var \l10n\Plural\IPlural */ | ||
private $plural; | ||
|
||
/** @var array */ | ||
private $translated = array(); | ||
|
||
/** @var array */ | ||
private $untranslated = array(); | ||
|
||
/** | ||
* @param \l10n\Plural\IPlural $plural | ||
*/ | ||
public function __construct(IPlural $plural) { | ||
$this->plural = $plural; | ||
} | ||
|
||
/** | ||
* @return \l10n\Plural\IPlural | ||
*/ | ||
public function getPlural() { | ||
return $this->plural; | ||
} | ||
|
||
/** | ||
* @param $plural | ||
*/ | ||
protected function checkPlural($plural) { | ||
if ($plural > $this->plural->getPluralsCount() - 1) { | ||
throw new \RangeException('The plural is bigger than is allowed'); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param string $text | ||
* @param int $plural | ||
*/ | ||
public function setText($key, $text, $plural = 0) { | ||
$this->checkPlural($plural); | ||
$this->translated[$key][$plural] = $text; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param int $plural | ||
* @return null|string | ||
*/ | ||
public function getText($key, $plural = 0) { | ||
$this->checkPlural($plural); | ||
|
||
if (func_num_args() === 2) { | ||
if (isset($this->translated[$key][$plural])) { | ||
return $this->translated[$key][$plural]; | ||
} | ||
} | ||
else { | ||
if (isset($this->translated[$key])) { | ||
return $this->translated[$key]; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param int $plural | ||
*/ | ||
public function removeText($key, $plural = 0) { | ||
if (func_num_args() === 2) { | ||
unset($this->translated[$key][$plural]); | ||
} | ||
else { | ||
unset($this->translated[$key]); | ||
} | ||
|
||
unset($this->untranslated[$key]); | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param int|array $n When $n is an array, it's used as $parameters. | ||
* @param array $parameters | ||
* @return null|string | ||
*/ | ||
public function translate($key, $n = 1, array $parameters = array()) { | ||
if (is_array($n)) { | ||
$parameters = $n; | ||
$n = 1; | ||
} | ||
|
||
$plural = $this->plural->getPlural($n); | ||
$translated = $this->getText($key, $plural); | ||
|
||
if ($translated === null) { | ||
$this->untranslated[$key] = true; | ||
|
||
return $key; | ||
} | ||
|
||
unset($this->untranslated[$key]); | ||
|
||
$parameters += array('%n%' => $n); | ||
$translated = strtr($translated, $parameters); | ||
|
||
return $translated; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getTranslated() { | ||
return $this->translated; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getUntranslated() { | ||
return array_keys($this->untranslated); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
use l10n\Translator\Translator; | ||
|
||
class TranslatorTest extends PHPUnit_Framework_TestCase { | ||
private function createPluralMock() { | ||
/** @var \l10n\Plural\IPlural|PHPUnit_Framework_MockObject_MockObject $mock */ | ||
$mock = $this->getMock('l10n\Plural\IPlural'); | ||
|
||
$mock->expects($this->any()) | ||
->method("getPluralsCount") | ||
->willReturn(2); | ||
|
||
$mock->expects($this->any()) | ||
->method("getPlural") | ||
->will( | ||
$this->returnCallback( | ||
function ($n) { | ||
return ($n != 1) ? 1 : 0; | ||
} | ||
) | ||
); | ||
|
||
return $mock; | ||
} | ||
|
||
public function testConstructor() { | ||
$plural = $this->createPluralMock(); | ||
|
||
$translator = new Translator($plural); | ||
|
||
$this->assertInstanceOf('l10n\Plural\IPlural', $plural); | ||
$this->assertSame($plural, $translator->getPlural()); | ||
} | ||
|
||
public function testSetGetRemoveText() { | ||
$plural = $this->createPluralMock(); | ||
|
||
$translator = new Translator($plural); | ||
|
||
$translator->setText('key', 'text %n%', 0); | ||
$translator->setText('key', 'text %n%', 1); | ||
|
||
$this->assertSame('text %n%', $translator->getText('key', 0)); | ||
$this->assertSame('text %n%', $translator->getText('key', 1)); | ||
$this->assertSame(array('text %n%', 'text %n%'), $translator->getText('key')); | ||
|
||
$translator->removeText('key', 0); | ||
$this->assertSame(null, $translator->getText('key', 0)); | ||
|
||
$translator->removeText('key', 1); | ||
$this->assertSame(null, $translator->getText('key', 1)); | ||
|
||
$translator->setText('foo', 'bar', 0); | ||
$translator->setText('foo', 'baz', 1); | ||
$this->assertSame('bar', $translator->getText('foo', 0)); | ||
$this->assertSame('baz', $translator->getText('foo', 1)); | ||
|
||
$translator->removeText('foo'); | ||
$this->assertSame(null, $translator->getText('foo', 0)); | ||
$this->assertSame(null, $translator->getText('foo', 1)); | ||
} | ||
|
||
public function testPluralsRange() { | ||
$plural = $this->createPluralMock(); | ||
|
||
$translator = new Translator($plural); | ||
|
||
$translator->setText('key', 'text %n%', 0); | ||
$translator->setText('key', 'text %n%', 1); | ||
|
||
$this->setExpectedException('RangeException', 'The plural is bigger than is allowed'); | ||
$translator->setText('key', 'text %n%', 2); | ||
} | ||
|
||
public function testTranslate() { | ||
$plural = $this->createPluralMock(); | ||
|
||
$translator = new Translator($plural); | ||
$translator->setText('key', '%n% person', 0); | ||
$translator->setText('key', '%n% people', 1); | ||
$translator->setText('foo', 'bar %var%'); | ||
|
||
$this->assertSame('0 people', $translator->translate('key', 0)); | ||
$this->assertSame('1 person', $translator->translate('key', 1)); | ||
$this->assertSame('50 people', $translator->translate('key', 50)); | ||
$this->assertSame('100 people', $translator->translate('key', 100)); | ||
$this->assertSame('bar baz', $translator->translate('foo', array('%var%' => 'baz'))); | ||
} | ||
|
||
public function testGetTranslated() { | ||
$plural = $this->createPluralMock(); | ||
|
||
$translator = new Translator($plural); | ||
$translator->setText('key', 'text'); | ||
$translator->setText('key_2', 'text_2'); | ||
|
||
$expected = array("key" => array('text'), "key_2" => array('text_2')); | ||
$this->assertSame($expected, $translator->getTranslated()); | ||
} | ||
|
||
public function testGetUntranslated() { | ||
$plural = $this->createPluralMock(); | ||
|
||
$translator = new Translator($plural); | ||
$translator->translate('key', 0); | ||
$translator->translate('key_2', 0); | ||
|
||
$expected = array("key", "key_2"); | ||
$this->assertSame($expected, $translator->getUntranslated()); | ||
} | ||
} |