Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed updating entires with plural form #2

Merged
merged 2 commits into from Nov 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 56 additions & 10 deletions src/Parser.php
Expand Up @@ -439,22 +439,68 @@ public function setEntries(array $entries)
}

/**
* Allows modification a msgid.
* By default disabled fuzzy flag if defined.
* Helper for the update-functions by deleting the fuzzy flag
*
* @param $original
* @param $translation
* @param $msgid string msgid of entry
*
* @throws \Exception
*/
public function updateEntry($original, $translation)
protected function removeFuzzyFlagForMsgId($msgid)
{
$this->entriesAsArrays[$original]['fuzzy'] = false;
$this->entriesAsArrays[$original]['msgstr'] = array($translation);
if (!isset($this->entriesAsArrays[$msgid])) {
throw new \Exception('Entry does not exist');
}
if ($this->entriesAsArrays[$msgid]['fuzzy']) {
$flags = $this->entriesAsArrays[$msgid]['flags'];
unset($flags[array_search('fuzzy', $flags, true)]);
$this->entriesAsArrays[$msgid]['flags'] = $flags;
$this->entriesAsArrays[$msgid]['fuzzy'] = false;
}
}

$flags = $this->entriesAsArrays[$original]['flags'];
unset($flags[array_search('fuzzy', $flags, true)]);
$this->entriesAsArrays[$original]['flags'] = $flags;
/**
* Allows modification of all translations of an entry
*
* @param $msgid string msgid of the entry which should be updated
* @param $translation array of strings new Translation for all msgstr by msgid
*
* @throws \Exception
*/
public function updateEntries($msgid, $translation)
{
if (
!isset($this->entriesAsArrays[$msgid])
|| !is_array($translation)
|| sizeof($translation) != sizeof($this->entriesAsArrays[$msgid]['msgstr'])
) {
throw new \Exception('Cannot update entry translation');
}
$this->removeFuzzyFlagForMsgId($msgid);
$this->entriesAsArrays[$msgid]['msgstr'] = $translation;
}

/**
* Allows modification of a single translation of an entry
*
* @param $msgid string msgid of the entry which should be updated
* @param $translation string new translation for an msgstr by msgid
* @param $positionMsgstr integer spezification which of the msgstr
* should be changed
*
* @throws \Exception
*/
public function updateEntry($msgid, $translation, $positionMsgstr = 0)
{
if (
!isset($this->entriesAsArrays[$msgid])
|| !is_string($translation)
|| !isset($this->entriesAsArrays[$msgid]['msgstr'][$positionMsgstr])
) {
throw new \Exception('Cannot update entry translation');
}
$this->removeFuzzyFlagForMsgId($msgid);
$this->entriesAsArrays[$msgid]['msgstr'][$positionMsgstr] = $translation;
}

/**
* Write entries into the po file.
Expand Down
107 changes: 107 additions & 0 deletions tests/ParserTest.php
Expand Up @@ -238,6 +238,15 @@ protected function getGeneralCorrectData()
'plural' => false,
'flags' => array()
),
array(
'msgid' => 'cookie',
'msgid_plural' => 'cookies',
'msgstr' => array('biscuit', 'biscuits'),
'fuzzy' => true,
'obsolete' => false,
'plural' => true,
'flags' => array('fuzzy', 'other-flag')
),
);
}

Expand Down Expand Up @@ -340,5 +349,103 @@ public function testUpdateEntry()
$entries = $this->parser->getEntriesAsArrays();
$this->assertEquals('mapache2', $entries['racoon']['msgstr'][0]);
$this->assertFalse($entries['racoon']['fuzzy']);

// checking if plural msgstr still existing bevor changing the updateEntry-function
$pluralmsgstr = isset($entries['cookie']['msgstr'][1]);
$this->parser->updateEntry('cookie', 'Keks');
$entries = $this->parser->getEntriesAsArrays();
$this->assertEquals('Keks', $entries['cookie']['msgstr'][0]);
$this->assertEquals($pluralmsgstr, isset($entries['cookie']['msgstr'][1]));
}

/**
* Test if exception is thrown, when in the updateEntry-Function:
* if the given msgid is not an entry
*/
public function testFailUpdateEntryNotInArray()
{
$this->setExpectedException('\Exception', 'Cannot update entry translation');
$this->parser->read(TEST_DATA_PATH . '/general.po');

$this->parser->updateEntry('NOT IN PO', 'Ist nicht in Po');
}

/**
* Test if exception is thrown, when in the updateEntry-Function:
* when the given parameter for the translation isn't a string
*/
public function testFailUpdateEntryNotAString()
{
$this->setExpectedException('\Exception', 'Cannot update entry translation');
$this->parser->read(TEST_DATA_PATH . '/general.po');

$this->parser->updateEntry('cookie', array('Keks'));
}

/**
* Test if exception is thrown, when in the updateEntry-Function:
* when the given parameter for the translationPosition doesn't exist
*/
public function testFailUpdateEntryNotExistingTranslationPosition()
{
$this->setExpectedException('\Exception', 'Cannot update entry translation');
$this->parser->read(TEST_DATA_PATH . '/general.po');

$this->parser->updateEntry('cookie', 'Keks', 5);
}

/**
* Test if exception is thrown, when in the updateEntries-Function:
* if the given msgid is not an entry
*/
public function testFailUpdateEntriesNotInArray()
{
$this->setExpectedException('\Exception', 'Cannot update entry translation');
$this->parser->read(TEST_DATA_PATH . '/general.po');

$this->parser->updateEntries('NOT IN PO', array('Ist nicht in Po'));
}

/**
* Test if exception is thrown, when in the updateEntries-Function:
* when the given parameter for the translation isn't an array
*/
public function testFailUpdateEntriesNotAnArray()
{
$this->setExpectedException('\Exception', 'Cannot update entry translation');
$this->parser->read(TEST_DATA_PATH . '/general.po');

$this->parser->updateEntries('cookie', 'Keks');
}

/**
* Test if exception is thrown, when in the updateEntries-Function:
* in the given Array isn't the same amount of translation like before
*/
public function testFailUpdateEntriesNotEqualAmountOfTranslations()
{
$this->setExpectedException('\Exception', 'Cannot update entry translation');
$this->parser->read(TEST_DATA_PATH . '/general.po');

$this->parser->updateEntries('cookie', array('Keks'));
}

/**
*
*/
public function testUpdateEntries()
{
$this->parser->read(TEST_DATA_PATH . '/general.po');

$entries = $this->parser->getEntriesAsArrays();
$this->assertEquals('biscuit', $entries['cookie']['msgstr'][0]);
$this->assertEquals('biscuits', $entries['cookie']['msgstr'][1]);
$this->assertTrue($entries['cookie']['fuzzy']);

$this->parser->updateEntries('cookie', array('Keks','Kekse'));
$entries = $this->parser->getEntriesAsArrays();
$this->assertEquals('Keks', $entries['cookie']['msgstr'][0]);
$this->assertEquals('Kekse', $entries['cookie']['msgstr'][1]);
$this->assertFalse($entries['cookie']['fuzzy']);
}
}
8 changes: 7 additions & 1 deletion tests/data/general.po
Expand Up @@ -55,4 +55,10 @@ msgstr[1] "very long translation2"
#~ msgstr "liebre"

msgid "\"Stay in quotation\""
msgstr "Lass die \" am Ende stehen"
msgstr "Lass die \" am Ende stehen"

#, fuzzy, other-flag
msgid "cookie"
msgid_plural "cookies"
msgstr[0] "biscuit"
msgstr[1] "biscuits"