Skip to content

Commit

Permalink
Added add-terms command to only upload and not sync terms
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik B Hansen committed Sep 21, 2020
1 parent 65f77ec commit 3a9d0ed
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ php artisan translation:scan {--merge : Whether the job should overwrite or merg

The command creates your `base_language` .json file in `/resources/lang`

**Add terms**

To only add your terms run this command:
```bash
php artisan translation:add-terms {--scan : Whether the job should scan before uploading}
```
This command doesn't remove unsused terms, so remember *NOT* to run `upload` command afterward.


**Upload translations**

To upload your translation terms to poeditor run this command:
Expand Down
33 changes: 33 additions & 0 deletions src/Commands/AddTerms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Vemcogroup\Translation\Commands;

use Exception;
use Illuminate\Console\Command;
use Vemcogroup\Translation\Translation;

class AddTerms extends Command
{
protected $signature = 'translation:add-terms
{--scan : Whether the job should scan before uploading}
';

protected $description = 'Upload all terms to POEditor';

public function handle(): void
{
try {
$this->info('⬆️ Preparing to upload terms');

if ($this->option('scan')) {
$this->call('translation:scan');
}

app(Translation::class)->addTerms();

$this->info('⬆️ Finished uploading all terms');
} catch (Exception $e) {
$this->error($e->getMessage());
}
}
}
22 changes: 22 additions & 0 deletions src/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ public function syncTerms(): void
}
}

public function addTerms(): void
{
try {
$this->setupPoeditorCredentials();
$entries = $this->getFileContent()
->map(function ($value, $key) {
return ['term' => $key];
})
->toJson();

$this->query('https://api.poeditor.com/v2/terms/add', [
'form_params' => [
'api_token' => $this->apiKey,
'id' => $this->projectId,
'data' => $entries,
]
], 'POST');
} catch (Exception $e) {
throw $e;
}
}

public function syncTranslations(?array $languages = null): void
{
try {
Expand Down
2 changes: 2 additions & 0 deletions src/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Vemcogroup\Translation\Commands\Upload;
use Vemcogroup\Translation\Commands\Download;
use Vemcogroup\Translation\Commands\CreateJs;
use Vemcogroup\Translation\Commands\AddTerms;

class TranslationServiceProvider extends ServiceProvider
{
Expand All @@ -22,6 +23,7 @@ public function boot(): void
Upload::class,
Download::class,
CreateJs::class,
AddTerms::class,
]);
}
}
Expand Down

0 comments on commit 3a9d0ed

Please sign in to comment.