Skip to content

Commit

Permalink
Merge pull request #11 from nextapps-be/allow_mapping_to_multiple_app…
Browse files Browse the repository at this point in the history
…_locales

allow mapping one POEditor locale to multiple internal locales
  • Loading branch information
Bart-Kuenen-nextapps authored Aug 25, 2023
2 parents 69de069 + 271eb4c commit 1e92b40
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/UploadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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();
Expand Down
29 changes: 29 additions & 0 deletions tests/DownloadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
30 changes: 30 additions & 0 deletions tests/UploadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 1e92b40

Please sign in to comment.