Skip to content

Commit

Permalink
Merge pull request #29 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Added settings for translators
  • Loading branch information
andrey-helldar authored Jun 27, 2024
2 parents ecec07c + 5af9c62 commit 65d5b15
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 1 deletion.
68 changes: 68 additions & 0 deletions config/public.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,72 @@

'helpers' => env('VENDOR_PATH', base_path('vendor/_laravel_lang')),
],

/*
* This option contains a list of translators that the Laravel Lang Translator project works with.
*
* Google Translate is enabled by default.
*
* @see https://laravel-lang.com/configuration.html#translators
*/

'translators' => [
/*
* List of channels used for translations.
*
* By default,
*
* Google is enabled
* Deepl is disabled
* Yandex is disabled
*/

'channels' => [
'google' => [
'translator' => '\LaravelLang\Translator\Integrations\Google',

'enabled' => (bool) env('TRANSLATION_GOOGLE_ENABLED', true),
],

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

'enabled' => (bool) env('TRANSLATION_DEEPL_ENABLED', false),

'credentials' => [
'key' => env('TRANSLATION_DEEPL_KEY'),
],
],

'yandex' => [
'translator' => '\LaravelLang\Translator\Integrations\Yandex',

'enabled' => (bool) env('TRANSLATION_YANDEX_ENABLED', false),

'credentials' => [
'key' => env('TRANSLATION_YANDEX_KEY'),
'folder' => env('TRANSLATION_FOLDER_ID'),
],
],
],

'options' => [
/*
* Set a custom pattern for extracting replaceable keywords from the string,
* default to extracting words prefixed with a colon.
*
* Available options:
*
* `true` is a `/:(\w+)/`
* `false` will disable regular expression processing
* `/any regex/` - any regular expression you specify
*
* By default, `true`
*
* @example (e.g. "Hello :name" will extract "name")
*/

'preserve_parameters' => true,
],
],
];
17 changes: 17 additions & 0 deletions src/Data/Shared/Translators/TranslatorChannelsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Config\Data\Shared\Translators;

/**
* @property array<TranslatorData> $all
* @property array<TranslatorData> $enabled
*/
class TranslatorChannelsData
{
public function __construct(
public array $all,
public array $enabled,
) {}
}
14 changes: 14 additions & 0 deletions src/Data/Shared/Translators/TranslatorData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Config\Data\Shared\Translators;

class TranslatorData
{
public function __construct(
public bool $enabled,
public string $translator,
public array $credentials = [],
) {}
}
12 changes: 12 additions & 0 deletions src/Data/Shared/Translators/TranslatorOptionsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Config\Data\Shared\Translators;

class TranslatorOptionsData
{
public function __construct(
public bool|string $preserveParameters
) {}
}
16 changes: 16 additions & 0 deletions src/Data/Shared/TranslatorsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace LaravelLang\Config\Data\Shared;

use LaravelLang\Config\Data\Shared\Translators\TranslatorChannelsData;
use LaravelLang\Config\Data\Shared\Translators\TranslatorOptionsData;

class TranslatorsData
{
public function __construct(
public TranslatorChannelsData $channels,
public TranslatorOptionsData $options,
) {}
}
2 changes: 2 additions & 0 deletions src/Data/SharedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use LaravelLang\Config\Data\Shared\ModelsData;
use LaravelLang\Config\Data\Shared\RoutesData;
use LaravelLang\Config\Data\Shared\SmartPunctuationData;
use LaravelLang\Config\Data\Shared\TranslatorsData;

class SharedData
{
Expand All @@ -18,5 +19,6 @@ public function __construct(
public SmartPunctuationData $punctuation,
public RoutesData $routes,
public ModelsData $models,
public TranslatorsData $translators,
) {}
}
40 changes: 40 additions & 0 deletions src/Services/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use LaravelLang\Config\Data\Shared\RouteNameData;
use LaravelLang\Config\Data\Shared\RoutesData;
use LaravelLang\Config\Data\Shared\SmartPunctuationData;
use LaravelLang\Config\Data\Shared\Translators\TranslatorChannelsData;
use LaravelLang\Config\Data\Shared\Translators\TranslatorData;
use LaravelLang\Config\Data\Shared\Translators\TranslatorOptionsData;
use LaravelLang\Config\Data\Shared\TranslatorsData;
use LaravelLang\Config\Data\SharedData;
use LaravelLang\Config\Enums\Name;
use LaravelLang\Config\Helpers\Path;
Expand All @@ -35,6 +39,7 @@ public function shared(): SharedData
punctuation: $this->smartPunctuation(),
routes : $this->routes(),
models : $this->models(),
translators: $this->translators(),
);
}

Expand Down Expand Up @@ -93,6 +98,41 @@ protected function models(): ModelsData
);
}

protected function translators(): TranslatorsData
{
return new TranslatorsData(
channels: $this->translatorChannels(),
options : $this->translatorOptions()
);
}

protected function translatorChannels(): TranslatorChannelsData
{
$items = $this->getTranslators();

return new TranslatorChannelsData($items, array_filter($items, fn (TranslatorData $item) => $item->enabled));
}

protected function translatorOptions(): TranslatorOptionsData
{
return new TranslatorOptionsData(
preserveParameters: $this->value(
Name::Shared,
'translators.options.preserve_parameters',
fallback: true
)
);
}

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'));
}

protected function value(
Name $name,
string $key,
Expand Down
62 changes: 61 additions & 1 deletion tests/Unit/SharedWithEnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

declare(strict_types=1);

use LaravelLang\Config\Data\Shared\Translators\TranslatorData;
use LaravelLang\Config\Enums\Name;
use LaravelLang\Config\Facades\Config;
use LaravelLang\LocaleList\Locale;

beforeAll(function () {
beforeEach(function () {
putenv('LOCALIZATION_INLINE=true');
putenv('LOCALIZATION_ALIGN=false');
putenv('LOCALIZATION_SMART_ENABLED=true');

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

config()->set(Name::Shared() . '.translators.channels.deepl.enabled', true);
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.credentials.key', 'qwerty456');
config()->set(Name::Shared() . '.translators.channels.yandex.credentials.folder', '123');
});

test('inline', function () {
Expand Down Expand Up @@ -111,3 +121,53 @@
->toBe(realpath(dirname(__DIR__)))
->toBe(config('localization.models.helpers'));
});

test('translators: all', function () {
expect(Config::shared()->translators->channels->all['google'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeFalse()
->translator->toBe('\LaravelLang\Translator\Integrations\Google')
->credentials->toBeEmpty();

expect(Config::shared()->translators->channels->all['deepl'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeTrue()
->translator->toBe('\LaravelLang\Translator\Integrations\Deepl')
->credentials->toBe([
'key' => 'qwerty123',
]);

expect(Config::shared()->translators->channels->all['yandex'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeTrue()
->translator->toBe('\LaravelLang\Translator\Integrations\Yandex')
->credentials->toBe([
'key' => 'qwerty456',
'folder' => '123',
]);
});

test('translators: enabled', function () {
expect(Config::shared()->translators->channels->enabled)
->not->toHaveKey('google');

expect(Config::shared()->translators->channels->enabled)
->toHaveKey('deepl');

expect(Config::shared()->translators->channels->enabled)
->toHaveKey('yandex');
});

test('translators: options', function () {
config()->set(Name::Shared() . '.translators.options.preserve_parameters', true);
expect(Config::shared()->translators->options->preserveParameters)->toBeTrue();

config()->set(Name::Shared() . '.translators.options.preserve_parameters', false);
expect(Config::shared()->translators->options->preserveParameters)->toBeFalse();

config()->set(Name::Shared() . '.translators.options.preserve_parameters', '/foo/');
expect(Config::shared()->translators->options->preserveParameters)->toBe('/foo/');

config()->set(Name::Shared() . '.translators.options.preserve_parameters', ' / foo / ');
expect(Config::shared()->translators->options->preserveParameters)->toBe(' / foo / ');
});
51 changes: 51 additions & 0 deletions tests/Unit/SharedWithoutEnvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use LaravelLang\Config\Data\Shared\Translators\TranslatorData;
use LaravelLang\Config\Enums\Name;
use LaravelLang\Config\Facades\Config;
use LaravelLang\LocaleList\Locale;
Expand Down Expand Up @@ -111,3 +112,53 @@
->toBe(realpath(dirname(__DIR__)))
->toBe(config('localization.models.helpers'));
});

test('translators: all', function () {
expect(Config::shared()->translators->channels->all['google'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeTrue()
->translator->toBe('\LaravelLang\Translator\Integrations\Google')
->credentials->toBeEmpty();

expect(Config::shared()->translators->channels->all['deepl'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeFalse()
->translator->toBe('\LaravelLang\Translator\Integrations\Deepl')
->credentials->toBe([
'key' => null,
]);

expect(Config::shared()->translators->channels->all['yandex'])
->toBeInstanceOf(TranslatorData::class)
->enabled->toBeFalse()
->translator->toBe('\LaravelLang\Translator\Integrations\Yandex')
->credentials->toBe([
'key' => null,
'folder' => null,
]);
});

test('translators: enabled', function () {
expect(Config::shared()->translators->channels->enabled)
->toHaveKey('google');

expect(Config::shared()->translators->channels->enabled)
->not->toHaveKey('deepl');

expect(Config::shared()->translators->channels->enabled)
->not->toHaveKey('yandex');
});

test('translators: options', function () {
config()->set(Name::Shared() . '.translators.options.preserve_parameters', true);
expect(Config::shared()->translators->options->preserveParameters)->toBeTrue();

config()->set(Name::Shared() . '.translators.options.preserve_parameters', false);
expect(Config::shared()->translators->options->preserveParameters)->toBeFalse();

config()->set(Name::Shared() . '.translators.options.preserve_parameters', '/foo/');
expect(Config::shared()->translators->options->preserveParameters)->toBe('/foo/');

config()->set(Name::Shared() . '.translators.options.preserve_parameters', ' / foo / ');
expect(Config::shared()->translators->options->preserveParameters)->toBe(' / foo / ');
});

0 comments on commit 65d5b15

Please sign in to comment.