diff --git a/README.md b/README.md index 8b0081f..0c518e4 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,9 @@ You can also provide an associate array, if you want to map POEditor locales to // ... Or provide associative array with POEditor locales mapped to internal locales 'locales' => ['en-gb' => 'en', 'nl-be' => 'nl'], + +// ... Or you can map multiple internal locales to the same POEditor locale +'locales' => ['nl' => ['nl_BE', 'nl_NL']], ``` ## Usage diff --git a/src/Commands/DownloadCommand.php b/src/Commands/DownloadCommand.php index eb27802..d74c657 100644 --- a/src/Commands/DownloadCommand.php +++ b/src/Commands/DownloadCommand.php @@ -32,7 +32,9 @@ public function handle() $this->getLocales()->each(function ($locale, $key) { $translations = app(Poeditor::class)->download(is_string($key) ? $key : $locale); - app(TranslationManager::class)->createTranslationFiles($translations, $locale); + collect($locale)->each(function ($internalLocale) use ($translations) { + app(TranslationManager::class)->createTranslationFiles($translations, $internalLocale); + }); }); $this->info('All translations have been downloaded!'); diff --git a/src/Commands/UploadCommand.php b/src/Commands/UploadCommand.php index ede74ff..f0fbc33 100644 --- a/src/Commands/UploadCommand.php +++ b/src/Commands/UploadCommand.php @@ -63,7 +63,7 @@ protected function getLocale() { $locale = $this->argument('locale') ?? app()->getLocale(); - if (! in_array($locale, config('poeditor-sync.locales'))) { + if (! collect(config('poeditor-sync.locales'))->flatten()->contains($locale)) { return; } @@ -80,7 +80,9 @@ protected function getPoeditorLocale() $locales = config('poeditor-sync.locales'); if (Arr::isAssoc($locales)) { - return array_flip($locales)[$this->getLocale()]; + return collect($locales)->filter(function ($internalLocales) { + return collect($internalLocales)->contains($this->getLocale()); + })->keys()->first(); } return $this->getLocale(); diff --git a/tests/DownloadCommandTest.php b/tests/DownloadCommandTest.php index 2fa9917..9665629 100644 --- a/tests/DownloadCommandTest.php +++ b/tests/DownloadCommandTest.php @@ -334,6 +334,35 @@ public function it_maps_poeditor_locales_on_internal_locales() $this->assertJsonTranslationFile($this->getLangPath('nl.json'), ['bar foo' => 'foo bar']); } + /** @test */ + public function it_maps_poeditor_locales_on_multiple_internal_locales() + { + config()->set('poeditor-sync.locales', ['en' => 'en_GB', 'nl' => ['nl_BE', 'nl_NL']]); + + $this->mockPoeditorDownload('en', [ + 'en-php-file' => [ + 'foo' => 'bar', + ], + 'foo bar' => 'bar foo', + ]); + + $this->mockPoeditorDownload('nl', [ + 'nl-php-file' => [ + 'bar' => 'foo', + ], + 'bar foo' => 'foo bar', + ]); + + $this->artisan('poeditor:download')->assertExitCode(0); + + $this->assertPhpTranslationFile($this->getLangPath('en_GB/en-php-file.php'), ['foo' => 'bar']); + $this->assertJsonTranslationFile($this->getLangPath('en_GB.json'), ['foo bar' => 'bar foo']); + $this->assertPhpTranslationFile($this->getLangPath('nl_BE/nl-php-file.php'), ['bar' => 'foo']); + $this->assertJsonTranslationFile($this->getLangPath('nl_BE.json'), ['bar foo' => 'foo bar']); + $this->assertPhpTranslationFile($this->getLangPath('nl_NL/nl-php-file.php'), ['bar' => 'foo']); + $this->assertJsonTranslationFile($this->getLangPath('nl_NL.json'), ['bar foo' => 'foo bar']); + } + /** * Mock the POEditor "download" method. * diff --git a/tests/UploadCommandTest.php b/tests/UploadCommandTest.php index 7b44792..4c8c070 100644 --- a/tests/UploadCommandTest.php +++ b/tests/UploadCommandTest.php @@ -236,6 +236,36 @@ public function it_maps_internal_locale_on_poeditor_locale() $this->artisan('poeditor:upload en')->assertExitCode(0); } + /** @test */ + public function it_maps_one_of_multiple_internal_locale_on_the_poeditor_locale() + { + config()->set('poeditor-sync.locales', ['nl' => ['nl_BE', 'nl_NL']]); + + $this->createPhpTranslationFile($this->getLangPath('nl_NL/nl-php-file.php'), ['bar' => 'foo NL']); + $this->createJsonTranslationFile($this->getLangPath('nl_NL.json'), ['foo_bar' => 'bar foo NL']); + + $this->createPhpTranslationFile($this->getLangPath('nl_BE/nl-php-file.php'), ['bar' => 'foo BE']); + $this->createJsonTranslationFile($this->getLangPath('nl_BE.json'), ['foo_bar' => 'bar foo BE']); + + $this->mockPoeditorUpload('nl', [ + 'nl-php-file' => [ + 'bar' => 'foo NL', + ], + 'foo_bar' => 'bar foo NL', + ]); + + $this->artisan('poeditor:upload nl_NL')->assertExitCode(0); + + $this->mockPoeditorUpload('nl', [ + 'nl-php-file' => [ + 'bar' => 'foo BE', + ], + 'foo_bar' => 'bar foo BE', + ]); + + $this->artisan('poeditor:upload nl_BE')->assertExitCode(0); + } + /** @test */ public function it_throws_error_if_provided_locale_is_not_present_in_config_locales_array() {