Skip to content

Commit

Permalink
#97 Added support for --clear
Browse files Browse the repository at this point in the history
  • Loading branch information
amostajo committed Feb 8, 2023
1 parent 39990b2 commit b461626
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Traits/GeneratePotTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ protected function generatePot($lang = 'en')
$output = $generator->generateString($translations, $filename);
$output = str_replace($this->rootPath, '', $output);
$output = str_replace('#: \assets', '#: /assets', $output);
if ($is_update)
unlink($filename);
file_put_contents($filename, $output);
// Print end
$this->_print($is_update ? 'POT file updated!' : 'POT file generated!');
Expand Down Expand Up @@ -114,7 +116,8 @@ protected function generatePo($locale, $lang = 'en')
$translations = null;
$to_update = false;
$loader = new PoLoader;
if (file_exists($po_filename)) {
$clear = array_key_exists('clear', $this->options);
if (!$clear && file_exists($po_filename)) {
$translations = $loader->loadFile($po_filename);
$translations = $translations->mergeWith($loader->loadFile($pot_filename));
$to_update = true;
Expand Down
24 changes: 24 additions & 0 deletions tests/cases/GeneratePoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,28 @@ public function testExistingGeneration()
$this->assertFileExists($filename);
$this->assertCount(3, $translations);
}
/**
* Test resulting message.
* @group po
* @group localization
*/
public function testExistingGenerationWithClearOption()
{
// Prepare
$loader = new PoLoader;
$filename = FRAMEWORK_PATH.'/environment/assets/lang/my-app-es_ES.po';
if (!is_file(TESTING_PATH.'/app/Localize/Test2.php'))
file_put_contents(TESTING_PATH.'/app/Localize/Test2.php', '<?php namespace MyApp\Localize;'
. ' class Test2 { public function __construct() { $assign = __( \'New string\', \'my-app\' );}}'
);
exec('php '.WPMVC_AYUCO.' generate po:es_ES');
unlink(TESTING_PATH.'/app/Localize/Test2.php');
// Execure
$execution = exec('php '.WPMVC_AYUCO.' generate po:es_ES --clear');
$translations = $loader->loadFile($filename);
// Assert
$this->assertEquals('PO:es_ES file generated!', $execution);
$this->assertFileExists($filename);
$this->assertCount(2, $translations);
}
}

0 comments on commit b461626

Please sign in to comment.