Skip to content

Commit

Permalink
cascade options
Browse files Browse the repository at this point in the history
  • Loading branch information
datashaman committed Aug 17, 2024
1 parent b1cb539 commit 38024a4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
6 changes: 4 additions & 2 deletions config/translators.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
],

'google-v2' => [
'project' => env('TRANSLATORS_GOOGLE_KEY'),
'construct' => [
'keyFilePath' => env('TRANSLATORS_GOOGLE_KEY_FILE', base_path('keys/google.json')),
],
'options' => [
'format' => 'text',
],
],

'google-v3' => [
'project' => env('TRANSLATORS_GOOGLE_PROJECT'),
'project' => env('TRANSLATORS_GOOGLE_PROJECT', env('GOOGLE_CLOUD_PROJECT')),
'options' => [],
],

Expand Down
5 changes: 1 addition & 4 deletions src/LaravelTranslatorsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ public function register(): void
$this->app->singleton(Client::class, fn () => OpenAI::client(config('translators.services.openai.api_key')));
}

$this->app->singleton(TranslateClient::class, fn () => new TranslateClient([
'key' => config('translators.services.google-v2.key'),
]));

$this->app->singleton(TranslateClient::class, fn () => new TranslateClient(config('translators.services.google-v2.construct')));
$this->app->singleton(Translator::class, fn () => new Translator(config('translators.services.deepl.api_key')));
$this->app->singleton('translator-manager', fn ($app) => new TranslatorManager($app));
}
Expand Down
10 changes: 5 additions & 5 deletions src/TranslatorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public function getDefaultDriver()
return $this->config->get('translators.default');
}

public function createAzureDriver(): TranslatorInterface
{
return resolve(AzureTranslator::class);
}

public function createDeeplDriver(): TranslatorInterface
{
return resolve(DeepLTranslator::class);
Expand All @@ -32,11 +37,6 @@ public function createGoogleV3Driver(): TranslatorInterface
return resolve(GoogleV3Translator::class);
}

public function createAzureaiDriver(): TranslatorInterface
{
return resolve(AzureTranslator::class);
}

public function createOpenaiDriver(): TranslatorInterface
{
return resolve(OpenAITranslator::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Translators/DeepLTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function translate(
sourceLang: null,
targetLang: $locale,
options: array_merge_recursive(
config('translators.service.deepl.options'),
config('translators.services.deepl.options'),
$options,
)
);
Expand Down
7 changes: 4 additions & 3 deletions src/Translators/GoogleV2Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Datashaman\LaravelTranslators\Translators;

use Datashaman\LaravelTranslators\Contracts\TranslatorInterface;
use Google\Cloud\Translate\V2\TranslateClient;

class GoogleV2Translator implements TranslatorInterface
{
Expand All @@ -15,16 +16,16 @@ public function translate(
string $locale,
array $options = []
): array {
$translations = $this->client->translateBatch([
'strings' => $contents,
$translations = $this->client->translateBatch(
$contents,
array_merge_recursive(
config('translators.services.google-v2.options'),
$options,
[
'target' => $locale,
]
),
]);
);

return array_map(
fn ($translation) => $translation['text'],
Expand Down

0 comments on commit 38024a4

Please sign in to comment.