From 66c9b5f6a0938893279e20c69c34dd9fa7cc6ca2 Mon Sep 17 00:00:00 2001 From: Dani Date: Wed, 17 Jun 2020 21:24:06 +0200 Subject: [PATCH 1/3] new method to upload local translation changes --- src/Commands/Download.php | 4 +-- src/Commands/Upload.php | 16 ++++++++--- src/Translation.php | 56 ++++++++++++++++++++++++++++++++++----- 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/Commands/Download.php b/src/Commands/Download.php index 56d9df0..ab02159 100755 --- a/src/Commands/Download.php +++ b/src/Commands/Download.php @@ -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()); } diff --git a/src/Commands/Upload.php b/src/Commands/Upload.php index cb1efd6..ed60cd8 100755 --- a/src/Commands/Upload.php +++ b/src/Commands/Upload.php @@ -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-for-language=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-for-language')) { + $language = $this->option('translations-for-language') === null ? null : explode(',', $this->option('translations-for-language')); + app(Translation::class)->syncTranslations($language); + } - $this->info('⬆ Finished uploading all translations'); + $this->info('⬆️ Finished uploading all translations'); } catch (Exception $e) { $this->error($e->getMessage()); } diff --git a/src/Translation.php b/src/Translation.php index 5023913..5dad715 100755 --- a/src/Translation.php +++ b/src/Translation.php @@ -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); }) ); } @@ -131,7 +131,7 @@ public function download(): Collection return $languages->pluck('code'); } - public function upload(): void + public function syncTerms(): void { try { $this->setupPoeditorCredentials(); @@ -153,6 +153,44 @@ public function upload(): void } } + public function syncTranslations(?array $languages = null): void + { + if ($languages === null) { + $translations = $this->getTranslations(); + } else { + $translations = $this->getTranslations($languages); + } + + $this->setupPoeditorCredentials(); + + foreach ($translations as $language => $entries) { + try { + $json = collect($entries) + ->mapToGroups(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')) { @@ -171,11 +209,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)]; From f8ed11db4dc78edb4aaadf5369297d9f37171b09 Mon Sep 17 00:00:00 2001 From: Dani Date: Wed, 17 Jun 2020 21:26:51 +0200 Subject: [PATCH 2/3] cleanup --- src/Translation.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Translation.php b/src/Translation.php index 5dad715..5f77834 100755 --- a/src/Translation.php +++ b/src/Translation.php @@ -155,11 +155,7 @@ public function syncTerms(): void public function syncTranslations(?array $languages = null): void { - if ($languages === null) { - $translations = $this->getTranslations(); - } else { - $translations = $this->getTranslations($languages); - } + $translations = $this->getTranslations($languages); $this->setupPoeditorCredentials(); From ccc061f0871a510ca383148c7c54ca4c3e2f85f4 Mon Sep 17 00:00:00 2001 From: Henrik B Hansen Date: Thu, 18 Jun 2020 09:22:03 +0200 Subject: [PATCH 3/3] Rename and README --- README.md | 6 ++++++ src/Commands/Upload.php | 6 +++--- src/Translation.php | 15 +++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e50349e..12adcc9 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/Commands/Upload.php b/src/Commands/Upload.php index ed60cd8..3055d7f 100755 --- a/src/Commands/Upload.php +++ b/src/Commands/Upload.php @@ -10,7 +10,7 @@ class Upload extends Command { protected $signature = 'translation:upload {--scan : Whether the job should scan before uploading} - {--translations-for-language=all : Upload translations for language sv,da,...} + {--translations=all : Upload translations for language sv,da,...} '; protected $description = 'Upload all translations to POEditor'; @@ -26,8 +26,8 @@ public function handle(): void app(Translation::class)->syncTerms(); - if ($this->hasOption('translations-for-language')) { - $language = $this->option('translations-for-language') === null ? null : explode(',', $this->option('translations-for-language')); + if ($this->hasOption('translations')) { + $language = in_array($this->option('translations'), [null, 'all'], true) ? null : explode(',', $this->option('translations')); app(Translation::class)->syncTranslations($language); } diff --git a/src/Translation.php b/src/Translation.php index 5f77834..1f8ff71 100755 --- a/src/Translation.php +++ b/src/Translation.php @@ -155,14 +155,13 @@ public function syncTerms(): void public function syncTranslations(?array $languages = null): void { - $translations = $this->getTranslations($languages); - - $this->setupPoeditorCredentials(); + try { + $this->setupPoeditorCredentials(); + $translations = $this->getTranslations($languages); - foreach ($translations as $language => $entries) { - try { + foreach ($translations as $language => $entries) { $json = collect($entries) - ->mapToGroups(function ($value, $key) { + ->mapToGroups(static function ($value, $key) { return [[ 'term' => $key, 'translation' => [ @@ -181,9 +180,9 @@ public function syncTranslations(?array $languages = null): void 'data' => $json, ] ], 'POST'); - } catch (Exception $e) { - throw $e; } + } catch (Exception $e) { + throw $e; } }