Skip to content

Commit

Permalink
Merge pull request #103 from samybaxy/v1.0
Browse files Browse the repository at this point in the history
Fixed indentation and spaces
  • Loading branch information
amostajo authored May 27, 2024
2 parents 58ba12f + 38b5647 commit f64dcba
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/Traits/GeneratePotTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @copyright 10Quality <http://www.10quality.com>
* @license MIT
* @package WPMVC\Commands
* @version 1.1.17
* @version 1.1.18
*/
trait GeneratePotTrait
{
Expand Down Expand Up @@ -48,18 +48,28 @@ protected function generatePot($lang = 'en')
Translations::create($domain)
);
foreach (glob($this->rootPath.'*.php') as $file) {
if ($this->isFileToLocalizeExcluded($file))
continue;
$scanner->scanFile($file);
}
foreach (glob($this->getAppPath().'*.php') as $file) {
if ($this->isFileToLocalizeExcluded($file))
continue;
$scanner->scanFile($file);
}
foreach (glob($this->getAppPath().'**/*.php') as $file) {
if ($this->isFileToLocalizeExcluded($file))
continue;
$scanner->scanFile($file);
}
foreach (glob($this->getViewsPath().'*.php') as $file) {
if ($this->isFileToLocalizeExcluded($file))
continue;
$scanner->scanFile($file);
}
foreach (glob($this->getViewsPath().'**/*.php') as $file) {
if ($this->isFileToLocalizeExcluded($file))
continue;
$scanner->scanFile($file);
}
$scannedTranslations = $scanner->getTranslations();
Expand All @@ -70,9 +80,13 @@ protected function generatePot($lang = 'en')
Translations::create($domain)
);
foreach (glob($this->getAssetsPath().'js/*.js') as $file) {
if ($this->isFileToLocalizeExcluded($file))
continue;
$scanner->scanFile($file);
}
foreach (glob($this->getAssetsPath().'js/**/*.js') as $file) {
if ($this->isFileToLocalizeExcluded($file))
continue;
$scanner->scanFile($file);
}
$scannedTranslations = $scanner->getTranslations();
Expand All @@ -98,6 +112,31 @@ protected function generatePot($lang = 'en')
}
}

/**
* Exclude files from being scanned.
* @since 1.1.18
*
* @param string $file
*
* @return bool
*/
protected function isFileToLocalizeExcluded($file)
{
$exclusions = array_key_exists('localize',$this->config)
&& array_key_exists('translations',$this->config['localize'])
&& array_key_exists('file_exclusions',$this->config['localize']['translations'])
&& !empty($this->config['localize']['translations']['file_exclusions'])
&& is_array($this->config['localize']['translations']['file_exclusions'])
? $this->config['localize']['translations']['file_exclusions']
: null;
if (!empty($exclusions))
foreach($exclusions as $exclusion) {
if (strpos($file,$exclusion) !== false)
return true;
}
return false;
}

/**
* Generate PO file.
* @since 1.1.0
Expand Down
115 changes: 115 additions & 0 deletions tests/cases/GenerateLangExclusionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

use Gettext\Loader\PoLoader;

/**
* Tests generate pot command.
*
* @author Alejandro Mostajo <http://about.me/amostajo>
* @copyright 10Quality <http://www.10quality.com>
* @license MIT
* @package WPMVC\Commands
* @version 1.1.18
*/
class GenerateLangExclusionsTest extends WpmvcAyucoTestCase
{
/**
* Temporary hold for bootstrap config.
*/
protected $configBackup;
/**
* paths to be unlinked during Teardown.
*/
protected $path = [
FRAMEWORK_PATH.'/environment/assets/lang/',
FRAMEWORK_PATH.'/environment/assets/views/',
FRAMEWORK_PATH.'/environment/assets/js/',
FRAMEWORK_PATH.'/environment/assets/',
];
/**
* Run before tests.
*/
public function setUp(): void
{
// Make backup of current config
$config = TESTING_PATH.'/app/Config/app.php';
$this->configBackup = file_get_contents($config);
// Make other files
if (!is_dir(TESTING_PATH.'/assets/views/'))
mkdir(TESTING_PATH.'/assets/views/', 0777, true);
if (!is_file(TESTING_PATH.'/assets/views/localize.php'))
file_put_contents(TESTING_PATH.'/assets/views/localize.php', '<?php echo _e( \'View text\', \'my-app\' ) ?>');
if (!is_file(TESTING_PATH.'/assets/views/excluded.php'))
file_put_contents(TESTING_PATH.'/assets/views/excluded.php', '<?php echo _e( \'Excluded view\', \'my-app\' ) ?>');
if (!is_dir(TESTING_PATH.'/assets/js/'))
mkdir(TESTING_PATH.'/assets/js/', 0777, true);
if (!is_file(TESTING_PATH.'/assets/js/excluded.js'))
file_put_contents(TESTING_PATH.'/assets/js/excluded.js', '__( \'Excluded text\', \'my-app\' );');
// Override config
file_put_contents($config, '<?php
return [
\'namespace\' => \'MyApp\',
\'type\' => \'theme\',
\'version\' => \'1.0.0\',
\'author\' => \'Developer <developer@wpmvc>\',
\'paths\' => [
\'base\' => __DIR__ . \'/../\',
\'controllers\' => __DIR__ . \'/../Controllers/\',
\'views\' => __DIR__ . \'/../../assets/views/\',
],
\'localize\' => [
\'textdomain\' => \'my-app\',
\'path\' => __DIR__ . \'/../../assets/lang/\',
\'translations\' => [
\'file_exclusions\' => [\'excluded.php\', \'excluded.js\'],
],
],
];'
);
}
/**
* Restore bootstrap config.
* @since
*/
public function tearDown(): void
{
parent::tearDown();
file_put_contents(TESTING_PATH.'/app/Config/app.php', $this->configBackup);
}
/**
* Test when configuration is set.
* @group pot
* @group localization
*/
public function testPOTExclusion()
{
// Prepare
$loader = new PoLoader;
$filename = TESTING_PATH.'/assets/lang/my-app.pot';
// Execute
$execution = exec('php '.WPMVC_AYUCO.' generate pot');
$translations = $loader->loadFile($filename);
// Assert
$this->assertEquals('POT file generated!', $execution);
$this->assertFileExists($filename);
$this->assertCount(1, $translations);
}
/**
* Test when configuration is set.
* @group po
* @group localization
*/
public function testPOExclusion()
{
// Prepare
$loader = new PoLoader;
$filename = TESTING_PATH.'/assets/lang/my-app-en_US.po';
// Execute
$execution = exec('php '.WPMVC_AYUCO.' generate po:en_US');
$translations = $loader->loadFile($filename);
// Assert
$this->assertEquals('PO:en_US file generated!', $execution);
$this->assertFileExists($filename);
$this->assertCount(1, $translations);
}
}

0 comments on commit f64dcba

Please sign in to comment.