Skip to content

Commit

Permalink
Added function removeUntranslated and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zemistr committed Mar 28, 2015
1 parent f7ffe5b commit fe85e70
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
32 changes: 27 additions & 5 deletions src/l10n/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected function checkPlural($plural) {
public function setText($key, $text, $plural = 0) {
$this->checkPlural($plural);
$this->translated[$key][$plural] = $text;
$this->removeUntranslated($key, $plural);
}

/**
Expand All @@ -71,11 +72,15 @@ public function getText($key, $plural = 0) {

if (func_num_args() === 2) {
if (isset($this->translated[$key][$plural])) {
$this->removeUntranslated($key, $plural);

return $this->translated[$key][$plural];
}
}
else {
if (isset($this->translated[$key])) {
$this->removeUntranslated($key);

return $this->translated[$key];
}
}
Expand All @@ -85,17 +90,34 @@ public function getText($key, $plural = 0) {

/**
* @param string $key
* @param int $plural
* @param int $plural Nothing for all plurals
*/
public function removeText($key, $plural = 0) {
if (func_num_args() === 2) {
unset($this->translated[$key][$plural]);
$this->removeUntranslated($key, $plural);
}
else {
unset($this->translated[$key]);
$this->removeUntranslated($key);
}
}

/**
* @param $key
* @param int $plural Nothing for all plurals
*/
public function removeUntranslated($key, $plural = 0) {
if (func_num_args() === 2) {
unset($this->untranslated[$key][$plural]);

unset($this->untranslated[$key]);
if (empty($this->untranslated[$key])) {
unset($this->untranslated[$key]);
}
}
else {
unset($this->untranslated[$key]);
}
}

/**
Expand All @@ -114,12 +136,12 @@ public function translate($key, $n = 1, array $parameters = array()) {
$translated = $this->getText($key, $plural);

if ($translated === null) {
$this->untranslated[$key] = true;
$this->untranslated[$key][$plural] = true;

return $key;
}

unset($this->untranslated[$key]);
$this->removeUntranslated($key, $plural);

$parameters += array('%n%' => $n);
$translated = strtr($translated, $parameters);
Expand All @@ -138,6 +160,6 @@ public function getTranslated() {
* @return array
*/
public function getUntranslated() {
return array_keys($this->untranslated);
return $this->untranslated;
}
}
74 changes: 73 additions & 1 deletion tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,82 @@ public function testGetUntranslated() {
$plural = $this->createPluralMock();

$translator = new Translator($plural);
$translator->translate('key');
$translator->translate('key_2');
$translator->translate('key', 0);
$translator->translate('key_2', 0);

$expected = array("key", "key_2");
$expected = array(
"key" => array(true, true),
"key_2" => array(true, true)
);
$this->assertSame($expected, $translator->getUntranslated());

$translator->setText('key', 'value');
unset($expected['key'][0]);
$this->assertSame($expected, $translator->getUntranslated());

$translator->setText('key', 'value', 1);
unset($expected['key']);
$this->assertSame($expected, $translator->getUntranslated());

$translator->removeText('key_2', 0);
unset($expected['key_2'][0]);
$this->assertSame($expected, $translator->getUntranslated());

$translator->removeText('key_2', 1);
unset($expected['key_2']);
$this->assertSame($expected, $translator->getUntranslated());

$translator->translate('key_2');
$translator->translate('key_2', 0);

$expected = array(
"key_2" => array(true, true)
);
$this->assertSame($expected, $translator->getUntranslated());

$translator->setText('key_2', 'value', 0);
unset($expected['key_2'][0]);
$this->assertSame($expected, $translator->getUntranslated());

$translator->setText('key_2', 'value', 1);
unset($expected['key_2']);
$this->assertSame($expected, $translator->getUntranslated());
}

public function testRemoveUntranslated() {
$plural = $this->createPluralMock();

$translator = new Translator($plural);
$translator->translate('key');
$translator->translate('key_2');
$translator->translate('key', 0);
$translator->translate('key_2', 0);

$expected = array(
"key" => array(true, true),
"key_2" => array(true, true)
);
$this->assertSame($expected, $translator->getUntranslated());

$translator->removeUntranslated('key', 0);
$translator->removeUntranslated('key_2', 0);
$expected = array(
"key" => array(1 => true),
"key_2" => array(1 => true)
);
$this->assertSame($expected, $translator->getUntranslated());

$translator->removeUntranslated('key', 1);
$translator->removeUntranslated('key_2', 1);
$this->assertSame(array(), $translator->getUntranslated());

$translator = new Translator($plural);
$translator->translate('key');
$translator->translate('key_2');
$translator->removeUntranslated('key');
$translator->removeUntranslated('key_2');
$this->assertSame(array(), $translator->getUntranslated());
}
}

0 comments on commit fe85e70

Please sign in to comment.