Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Translation] add gettext PO and MO Dumper
- Loading branch information
Showing
5 changed files
with
204 additions
and
0 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,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Dumper; | ||
|
||
use Symfony\Component\Translation\MessageCatalogue; | ||
use Symfony\Component\Translation\Loader\MoFileLoader; | ||
|
||
/** | ||
* MoFileDumper generates a gettext formated string representation of a message catalogue. | ||
* | ||
* @author Stealth35 | ||
*/ | ||
class MoFileDumper extends FileDumper | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function format(MessageCatalogue $messages, $domain = 'messages') | ||
{ | ||
$output = $sources = $targets = $source_offsets = $target_offsets = ''; | ||
$offsets = array(); | ||
$size = 0; | ||
|
||
foreach ($messages->all($domain) as $source => $target) { | ||
$offsets[] = array_map('strlen', array($sources, $source, $targets, $target)); | ||
$sources .= "\0".$source; | ||
$targets .= "\0".$target; | ||
++$size; | ||
} | ||
|
||
$header = array( | ||
'magicNumber' => MoFileLoader::MO_LITTLE_ENDIAN_MAGIC, | ||
'formatRevision' => 0, | ||
'count' => $size, | ||
'offsetId' => MoFileLoader::MO_HEADER_SIZE, | ||
'offsetTranslated' => MoFileLoader::MO_HEADER_SIZE + (8 * $size), | ||
'sizeHashes' => 0, | ||
'offsetHashes' => MoFileLoader::MO_HEADER_SIZE + (16 * $size), | ||
); | ||
|
||
$sources_size = strlen($sources); | ||
$sources_start = $header['offsetHashes'] + 1; | ||
|
||
foreach ($offsets as $offset) { | ||
$source_offsets .= $this->writeLong($offset[1]) | ||
. $this->writeLong($offset[0] + $sources_start); | ||
$target_offsets .= $this->writeLong($offset[3]) | ||
. $this->writeLong($offset[2] + $sources_start + $sources_size); | ||
} | ||
|
||
$output = implode(array_map(array($this, 'writeLong'), $header)) | ||
. $source_offsets | ||
. $target_offsets | ||
. $sources | ||
. $targets | ||
; | ||
|
||
return $output; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function getExtension() | ||
{ | ||
return 'mo'; | ||
} | ||
|
||
private function writeLong($str) | ||
{ | ||
return pack('V*', $str); | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Dumper; | ||
|
||
use Symfony\Component\Translation\MessageCatalogue; | ||
|
||
/** | ||
* PoFileDumper generates a gettext formated string representation of a message catalogue. | ||
* | ||
* @author Stealth35 | ||
*/ | ||
class PoFileDumper extends FileDumper | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function format(MessageCatalogue $messages, $domain = 'messages') | ||
{ | ||
$output = ''; | ||
|
||
foreach ($messages->all($domain) as $source => $target) { | ||
$output .= sprintf("msgid \"%s\"\n", $this->escape($source)); | ||
$output .= sprintf("msgstr \"%s\"\n\n", $this->escape($target)); | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function getExtension() | ||
{ | ||
return 'po'; | ||
} | ||
|
||
private function escape($str) | ||
{ | ||
return addcslashes($str, "\0..\37\42\134"); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/Symfony/Tests/Component/Translation/Dumper/MoFileDumperTest.php
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,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Tests\Component\Translation\Dumper; | ||
|
||
use Symfony\Component\Translation\MessageCatalogue; | ||
use Symfony\Component\Translation\Dumper\MoFileDumper; | ||
|
||
class MoFileDumperTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testDump() | ||
{ | ||
$catalogue = new MessageCatalogue('en'); | ||
$catalogue->add(array('foo' => 'bar')); | ||
|
||
$tempDir = sys_get_temp_dir(); | ||
$dumper = new MoFileDumper(); | ||
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir)); | ||
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.mo'), file_get_contents($tempDir.'/messages.en.mo')); | ||
|
||
unlink($tempDir.'/messages.en.mo'); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/Symfony/Tests/Component/Translation/Dumper/PoFileDumperTest.php
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,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Tests\Component\Translation\Dumper; | ||
|
||
use Symfony\Component\Translation\MessageCatalogue; | ||
use Symfony\Component\Translation\Dumper\PoFileDumper; | ||
|
||
class PoFileDumperTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testDump() | ||
{ | ||
$catalogue = new MessageCatalogue('en'); | ||
$catalogue->add(array('foo' => 'bar')); | ||
|
||
$tempDir = sys_get_temp_dir(); | ||
$dumper = new PoFileDumper(); | ||
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir)); | ||
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.po'), file_get_contents($tempDir.'/messages.en.po')); | ||
|
||
unlink($tempDir.'/messages.en.po'); | ||
} | ||
} |