Skip to content

Commit

Permalink
Added sorting option for Translator package
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 28, 2024
1 parent 5af9c62 commit f85dbb9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions config/public.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,14 @@
'translator' => '\LaravelLang\Translator\Integrations\Google',

'enabled' => (bool) env('TRANSLATION_GOOGLE_ENABLED', true),
'order' => (int) env('TRANSLATION_GOOGLE_ORDER', 1),
],

'deepl' => [
'translator' => '\LaravelLang\Translator\Integrations\Deepl',

'enabled' => (bool) env('TRANSLATION_DEEPL_ENABLED', false),
'order' => (int) env('TRANSLATION_DEEPL_ORDER', 2),

'credentials' => [
'key' => env('TRANSLATION_DEEPL_KEY'),
Expand All @@ -202,6 +204,7 @@
'translator' => '\LaravelLang\Translator\Integrations\Yandex',

'enabled' => (bool) env('TRANSLATION_YANDEX_ENABLED', false),
'order' => (int) env('TRANSLATION_YANDEX_ORDER', 3),

'credentials' => [
'key' => env('TRANSLATION_YANDEX_KEY'),
Expand Down
1 change: 1 addition & 0 deletions src/Data/Shared/Translators/TranslatorData.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public function __construct(
public bool $enabled,
public string $translator,
public array $credentials = [],
public int $order = 0,
) {}
}
14 changes: 9 additions & 5 deletions src/Services/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace LaravelLang\Config\Services;

use Illuminate\Config\Repository;
use Illuminate\Support\Collection;
use LaravelLang\Config\Constants\RouteName;
use LaravelLang\Config\Data\Common\NonPushableData;
use LaravelLang\Config\Data\Common\PushableData;
Expand Down Expand Up @@ -126,11 +127,14 @@ protected function translatorOptions(): TranslatorOptionsData

protected function getTranslators(): array
{
return array_map(fn (array $item) => new TranslatorData(
enabled : $item['enabled'] ?? true,
translator : $item['translator'],
credentials: $item['credentials'] ?? []
), $this->value(Name::Shared, 'translators.channels'));
return (new Collection($this->value(Name::Shared, 'translators.channels')))->map(
fn (array $item) => new TranslatorData(
enabled : $item['enabled'] ?? true,
translator : $item['translator'],
credentials: $item['credentials'] ?? [],
order : $item['order'] ?? 0
)
)->sortBy(fn (TranslatorData $item) => $item->order)->all();
}

protected function value(
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/SharedWithEnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
putenv('LOCALIZATION_SMART_ENABLED=true');

config()->set(Name::Shared() . '.translators.channels.google.enabled', false);
config()->set(Name::Shared() . '.translators.channels.google.order', 5);

config()->set(Name::Shared() . '.translators.channels.deepl.enabled', true);
config()->set(Name::Shared() . '.translators.channels.deepl.order', 6);
config()->set(Name::Shared() . '.translators.channels.deepl.credentials.key', 'qwerty123');

config()->set(Name::Shared() . '.translators.channels.yandex.enabled', true);
config()->set(Name::Shared() . '.translators.channels.yandex.order', 7);
config()->set(Name::Shared() . '.translators.channels.yandex.credentials.key', 'qwerty456');
config()->set(Name::Shared() . '.translators.channels.yandex.credentials.folder', '123');
});
Expand Down Expand Up @@ -126,12 +129,14 @@
expect(Config::shared()->translators->channels->all['google'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeFalse()
->order->toBe(5)
->translator->toBe('\LaravelLang\Translator\Integrations\Google')
->credentials->toBeEmpty();

expect(Config::shared()->translators->channels->all['deepl'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeTrue()
->order->toBe(6)
->translator->toBe('\LaravelLang\Translator\Integrations\Deepl')
->credentials->toBe([
'key' => 'qwerty123',
Expand All @@ -140,6 +145,7 @@
expect(Config::shared()->translators->channels->all['yandex'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeTrue()
->order->toBe(7)
->translator->toBe('\LaravelLang\Translator\Integrations\Yandex')
->credentials->toBe([
'key' => 'qwerty456',
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/SharedWithoutEnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@
expect(Config::shared()->translators->channels->all['google'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeTrue()
->order->toBe(1)
->translator->toBe('\LaravelLang\Translator\Integrations\Google')
->credentials->toBeEmpty();

expect(Config::shared()->translators->channels->all['deepl'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeFalse()
->order->toBe(2)
->translator->toBe('\LaravelLang\Translator\Integrations\Deepl')
->credentials->toBe([
'key' => null,
Expand All @@ -131,6 +133,7 @@
expect(Config::shared()->translators->channels->all['yandex'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeFalse()
->order->toBe(3)
->translator->toBe('\LaravelLang\Translator\Integrations\Yandex')
->credentials->toBe([
'key' => null,
Expand Down

0 comments on commit f85dbb9

Please sign in to comment.