Skip to content

Commit

Permalink
chore: clean up for readme and stack translator
Browse files Browse the repository at this point in the history
  • Loading branch information
a-drew committed Aug 2, 2024
1 parent 9d609a7 commit 2b4bc3f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<p align="center"><a href="https://plank.co"><img src="art/polyglot.png" width="100%"></a></p>

<p align="center">
<a href="https://packagist.org/packages/plank/polyglot"><img src="https://img.shields.io/packagist/php-v/plank/snapshots?color=%23fae370&label=php&logo=php&logoColor=%23fff" alt="PHP Version Support"></a>
<a href="https://packagist.org/packages/plank/polyglot"><img src="https://img.shields.io/packagist/php-v/plank/polyglot?color=%23fae370&label=php&logo=php&logoColor=%23fff" alt="PHP Version Support"></a>
<a href="https://laravel.com/docs/11.x/releases#support-policy"><img src="https://img.shields.io/badge/laravel-9.x,%2010.x,%2011.x-%2343d399?color=%23f1ede9&logo=laravel&logoColor=%23ffffff" alt="PHP Version Support"></a>
<a href="https://github.com/plank/polyglot/actions?query=workflow%3Arun-tests"><img src="https://img.shields.io/github/actions/workflow/status/plank/snapshots/run-tests.yml?branch=main&&color=%23bfc9bd&label=run-tests&logo=github&logoColor=%23fff" alt="GitHub Workflow Tests Status"></a>
<a href="https://codeclimate.com/github/plank/polyglot/test_coverage"><img src="https://img.shields.io/codeclimate/coverage/plank/snapshots?color=%23ff9376&label=test%20coverage&logo=code-climate&logoColor=%23fff" /></a>
<a href="https://codeclimate.com/github/plank/polyglot/maintainability"><img src="https://img.shields.io/codeclimate/maintainability/plank/snapshots?color=%23528cff&label=maintainablility&logo=code-climate&logoColor=%23fff" /></a>
<a href="https://github.com/plank/polyglot/actions?query=workflow%3Arun-tests"><img src="https://img.shields.io/github/actions/workflow/status/plank/polyglot/run-tests.yml?branch=main&&color=%23bfc9bd&label=run-tests&logo=github&logoColor=%23fff" alt="GitHub Workflow Tests Status"></a>
<a href="https://codeclimate.com/github/plank/polyglot/test_coverage"><img src="https://img.shields.io/codeclimate/coverage/plank/polyglot?color=%23ff9376&label=test%20coverage&logo=code-climate&logoColor=%23fff" /></a>
<a href="https://codeclimate.com/github/plank/polyglot/maintainability"><img src="https://img.shields.io/codeclimate/maintainability/plank/polyglot?color=%23528cff&label=maintainablility&logo=code-climate&logoColor=%23fff" /></a>
</p>

# Laravel Polyglot
Expand Down Expand Up @@ -68,24 +68,23 @@ If you simply need to translate a piece of text, this package provides a few con
'translators' => [
'stack' => [
'driver' => 'stack',
'translators' => ['amazon', 'google', 'gpt35'],
'translators' => ['amazon', 'google', 'gpt'],
'retries' => 3,
'sleep' => 100,
'target' => 'it',
],
...
]
```
```php
> Polyglot::translator('stack')->from('auto')->translate('Hello!');
> Polyglot::translator('stack')->from('auto')->to('it')->translate('Hello!');
= "Ciao!"
```

### Other Methods

- `Translator::translateBatch` - Translate an array of strings, set target language with `to` method
- `Translator::translateBatchTo` - Translate an array of strings to a specific language
- `AbstractTranslator::to`, `from`, `format`, `setSource`, `setTarget`, `setFormat` - Fluent API for setting translator options
- `AbstractTranslator::to`, `from`, `format` - Fluent API for setting translator options
- `AbstractTranslator::languages` - Get a list of supported languages, if locale is passed it will return the supported languages for that locale
- `sendTranslateRequest` - Some drivers allow you to access the underlying response from the translation service

Expand All @@ -98,7 +97,7 @@ You can extend polyglot and provide your own translator drivers. To do so, follo
2. Then, in a service provider, call the `Plank\Polyglot\Polyglot::extend` method to register your new driver.

```php
Polyglot::extend('bing', static function (Container $app, array $config) {
Polyglot::extend('azure', static function (Container $app, array $config) {
$key = $config['key'];

return new AzureTranslator($key);
Expand All @@ -108,7 +107,7 @@ You can extend polyglot and provide your own translator drivers. To do so, follo

```php
return [
'default' => env('POLYGLOT_DEFAULT', 'microsoft'),
'default' => env('POLYGLOT_DEFAULT', 'bing'),
'translators' => [
'microsoft' => [
'driver' => 'azure',
Expand All @@ -121,7 +120,7 @@ You can extend polyglot and provide your own translator drivers. To do so, follo
4. And finally use it in your code:

```php
Polyglot::translator('microsoft')->from('en')->to('fr')->translate('Hello!');
Polyglot::translator('bing')->from('en')->to('fr')->translate('Hello!');
```

## Testing
Expand Down Expand Up @@ -154,4 +153,4 @@ All security vulnerabilities will be promptly addressed.

Plank focuses on impactful solutions that deliver engaging experiences to our clients and their users.
We're committed to innovation, inclusivity, and sustainability in the digital space.
[Learn more](https://plank.co/open-source/learn-more-link) about our mission to improve the web.
[Learn more](https://plank.co/open-source/learn-more-link) about our mission to improve the web.
4 changes: 2 additions & 2 deletions src/Drivers/StackTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function translateBatch(array $strings): array
return $this->tryAllClients(fn (Translator $client) => $client->translateBatch($strings));
}

public function translateBatchTo(array $text, string $target, ?string $source = null): array
public function translateBatchTo(array $strings, string $target, ?string $source = null): array
{
return $this->tryAllClients(fn (Translator $client) => $client->translateBatchTo($text, $target, $source));
return $this->tryAllClients(fn (Translator $client) => $client->translateBatchTo($strings, $target, $source));
}

protected function tryAllClients($callback): mixed
Expand Down
4 changes: 2 additions & 2 deletions src/TranslatorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function stack(...$translators): StackTranslate
$config = $this->config->get('polyglot.translators.stack') ?? [];
$config['translators'] = $translators;

return $this->drivers[$key] = $this->createStackDriver($config)->to($config['target'] ?? $this->container->getLocale());
return $this->drivers[$key] = $this->createStackDriver($config);
}

public function createStackDriver(array $config): StackTranslate
Expand All @@ -117,7 +117,7 @@ public function createStackDriver(array $config): StackTranslate

$translators = array_map(fn ($translator) => $this->translator($translator), $translators);

return new StackTranslate($translators, $config['retries'] ?? null);
return new StackTranslate($translators, $config['retries'] ?? 1, $config['sleep'] ?? 100);
}

public function createStichozaDriver(array $config): StichozaTranslate
Expand Down

0 comments on commit 2b4bc3f

Please sign in to comment.