Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/Console/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
namespace KKomelin\TranslatableStringExporter\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use KKomelin\TranslatableStringExporter\Core\Exporter;
use KKomelin\TranslatableStringExporter\Core\StringExtractor;
use Symfony\Component\Console\Input\InputArgument;

class ExportCommand extends Command
{
Expand Down Expand Up @@ -61,9 +62,14 @@ public function fire()
public function handle()
{
$languages = explode(',', $this->argument('lang'));
$patterns = null;

if($this->option('patterns')) {
$patterns = explode(',', $this->option('patterns'));
}

foreach ($languages as $language) {
$this->exporter->export(base_path(), $language);
$this->exporter->export(base_path(), $language, $patterns);

$this->info('Translatable strings have been extracted and written to the ' . $language . '.json file.');
}
Expand All @@ -81,7 +87,24 @@ protected function getArguments()
'lang',
InputArgument::REQUIRED,
'A language code or a comma-separated list of language codes for which the translatable strings are extracted, e.g. "es" or "es,bg,de".'
],
]
];
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
[
'patterns',
'p',
InputOption::VALUE_OPTIONAL,
'Override the default patterns and use the given instead, e.g. --patterns="*.php,*.js".'
]
];
}
}
5 changes: 3 additions & 2 deletions src/Core/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ public function __construct()
*
* @param string $base_path
* @param string $language
* @param array $patterns
* @return array
*/
public function export($base_path, $language)
public function export($base_path, $language, array $patterns=null)
{
$language_path = IO::languageFilePath($base_path, $language);

// Extract source strings from the project directories.
$new_strings = $this->extractor->extract();
$new_strings = $this->extractor->extract($patterns);

// Read existing translation file for the chosen language.
$existing_strings = IO::readTranslationFile($language_path);
Expand Down
10 changes: 10 additions & 0 deletions src/Core/FileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public function __construct()
]);
}

/**
* Override the default patterns
*
* @param array $patterns
*/
public function setPatterns(array $patterns)
{
$this->patterns = $patterns;
}

/**
* Find all files that can contain translatable strings.
*
Expand Down
8 changes: 7 additions & 1 deletion src/Core/StringExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ public function __construct()

/**
* Extract translatable strings from the project files.
*
* @param array $patterns
*/
public function extract() {
public function extract(array $patterns=null) {

if($patterns) {
$this->finder->setPatterns($patterns);
}

$strings = [];

Expand Down
5 changes: 5 additions & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ protected function createTestView($content)
file_put_contents(resource_path('views/index.blade.php'), $content);
}

protected function createTestTypescript($content)
{
file_put_contents(resource_path('ts/index.ts'), $content);
}

protected function getTranslationFilePath($language)
{
return resource_path('lang/' . $language . '.json');
Expand Down
25 changes: 25 additions & 0 deletions tests/ExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,29 @@ public function testPersistentTranslations()

$this->assertEquals($expected, $actual);
}

public function testOverridePatterns()
{
$this->cleanLangsFolder();

$this->createTestView("{{ __('name') }}");
$this->createTestTypescript("__('name1')");

$this->artisan('translatable:export', [
'lang' => 'bg,es',
'--patterns' => '*.ts'
])
->expectsOutput('Translatable strings have been extracted and written to the bg.json file.')
->expectsOutput('Translatable strings have been extracted and written to the es.json file.')
->assertExitCode(0);

$this->assertFileExists($this->getTranslationFilePath('bg'));
$this->assertFileExists($this->getTranslationFilePath('es'));

$bg_content = $this->getTranslationFileContent('bg');
$es_content = $this->getTranslationFileContent('es');

$this->assertEquals(['name1' => 'name1'], $bg_content);
$this->assertEquals(['name1' => 'name1'], $es_content);
}
}
2 changes: 2 additions & 0 deletions tests/__fixtures/resources/ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore