Skip to content

Commit

Permalink
Merge pull request #7 from vemcogroup/feature/upload-translations
Browse files Browse the repository at this point in the history
new method to upload local translation changes
  • Loading branch information
Henrik B Hansen authored Jun 18, 2020
2 parents 4ebec51 + ccc061f commit 95dfc76
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ To upload your translation terms to poeditor run this command:
php artisan translation:upload {--scan : Whether the job should scan before uploading}
```

You are also able to upload your local translations if you have locale changes
```bash
php artisan translation:upload {--translations=all : Upload translations for language sv,da,...}
```


**Download translation languages**

To download languages from poeditor run this command:
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class Download extends Command
public function handle(): void
{
try {
$this->info('⬇️ Preparing to download languages');
$this->info('⬇️ Preparing to download languages');

$languages = app(Translation::class)->download();

$this->info('⬇️ Finished downloading languages: ' . $languages->implode(', '));
$this->info('⬇️ Finished downloading languages: ' . $languages->implode(', '));
} catch (Exception $e) {
$this->error($e->getMessage());
}
Expand Down
16 changes: 12 additions & 4 deletions src/Commands/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,29 @@
class Upload extends Command
{
protected $signature = 'translation:upload
{--scan : Whether the job should scan before uploading}';
{--scan : Whether the job should scan before uploading}
{--translations=all : Upload translations for language sv,da,...}
';

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

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

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

app(Translation::class)->upload();
app(Translation::class)->syncTerms();

if ($this->hasOption('translations')) {
$language = in_array($this->option('translations'), [null, 'all'], true) ? null : explode(',', $this->option('translations'));
app(Translation::class)->syncTranslations($language);
}

$this->info('⬆ Finished uploading all translations');
$this->info(' Finished uploading all translations');
} catch (Exception $e) {
$this->error($e->getMessage());
}
Expand Down
51 changes: 45 additions & 6 deletions src/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public function scan($mergeKeys = false): int
$collapsedKeys = collect($allMatches)->collapse();
$keys = $collapsedKeys->combine($collapsedKeys);

if($mergeKeys) {
if ($mergeKeys) {
$content = $this->getFileContent();
$keys = $content->union(
$keys->filter(function($key) use ($content) {
return ! $content->has($key);
$keys->filter(function ($key) use ($content) {
return !$content->has($key);
})
);
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public function download(): Collection
return $languages->pluck('code');
}

public function upload(): void
public function syncTerms(): void
{
try {
$this->setupPoeditorCredentials();
Expand All @@ -153,6 +153,39 @@ public function upload(): void
}
}

public function syncTranslations(?array $languages = null): void
{
try {
$this->setupPoeditorCredentials();
$translations = $this->getTranslations($languages);

foreach ($translations as $language => $entries) {
$json = collect($entries)
->mapToGroups(static function ($value, $key) {
return [[
'term' => $key,
'translation' => [
'content' => $value,
],
]];
})
->first()
->toJson();

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

protected function setupPoeditorCredentials(): void
{
if (!$this->apiKey = config('translation.api_key')) {
Expand All @@ -171,11 +204,17 @@ protected function getFileContent(): Collection
: collect();
}

protected function getTranslations(): Collection
protected function getTranslations(?array $languages = null): Collection
{
$namePattern = '*.json';

if ($languages !== null) {
$namePattern = '/(' . implode('|', $languages) . ').json/';
}

return collect(app(Finder::class)
->in(app()->langPath())
->name('*.json')
->name($namePattern)
->files())
->mapWithKeys(function (SplFileInfo $file) {
return [$file->getBaseName('.json') => json_decode($file->getContents(), true)];
Expand Down

0 comments on commit 95dfc76

Please sign in to comment.