Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Flears committed Dec 28, 2019
1 parent 621539e commit 7d2422a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 65 deletions.
32 changes: 16 additions & 16 deletions config/googletranslate.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

return [
/*
|----------------------------------------------------------------------------------------------------
| The ISO 639-1 code of the language in lowercase to which the text will be translated to by default.
|----------------------------------------------------------------------------------------------------
*/
'default_target_translation' => 'en',
/*
|----------------------------------------------------------------------------------------------------
| The ISO 639-1 code of the language in lowercase to which the text will be translated to by default.
|----------------------------------------------------------------------------------------------------
*/
'default_target_translation' => 'en',

/*
|-------------------------------------------------------------------------------
| Api Key generated within Google Cloud Dashboard.
|
| The process to get this file is documented in a step by step detailed manner
| over here:
| https://github.com/JoggApp/laravel-google-translate/blob/master/google.md
|-------------------------------------------------------------------------------
*/
'api_key' => env('GOOGLE_TRANSLATE_API_KEY'),
/*
|-------------------------------------------------------------------------------
| Api Key generated within Google Cloud Dashboard.
|
| The process to get this file is documented in a step by step detailed manner
| over here:
| https://github.com/JoggApp/laravel-google-translate/blob/master/google.md
|-------------------------------------------------------------------------------
*/
'api_key' => env('GOOGLE_TRANSLATE_API_KEY'),
];
97 changes: 48 additions & 49 deletions src/GoogleTranslateClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,65 @@

class GoogleTranslateClient
{
use SupportedLanguages;
use SupportedLanguages;

private $translate;
private $translate;

public function __construct(array $config)
{
$this->checkForInvalidConfiguration($config);

$this->translate = new TranslateClient([
'key' => $config['api_key']
]);
}

public function detectLanguage(string $text)
{
return $this->translate
->detectLanguage($text);
}
public function __construct(array $config)
{
$this->checkForInvalidConfiguration($config);
$this->translate = new TranslateClient([
'key' => $config['api_key'],
]);
}

public function detectLanguageBatch(array $input)
{
return $this->translate
->detectLanguageBatch($input);
}
public function detectLanguage(string $text)
{
return $this->translate
->detectLanguage($text);
}

public function translate(string $text, string $translateTo, string $format = 'text')
{
return $this->translate
->translate($text, ['target' => $translateTo, 'format' => $format]);
}
public function detectLanguageBatch(array $input)
{
return $this->translate
->detectLanguageBatch($input);
}

public function translateBatch(array $input, string $translateTo, string $format = 'text')
{
return $this->translate
->translateBatch($input, ['target' => $translateTo, 'format' => $format]);
}
public function translate(string $text, string $translateTo, string $format = 'text')
{
return $this->translate
->translate($text, ['target' => $translateTo, 'format' => $format]);
}

public function getAvaliableTranslationsFor(string $languageCode)
{
return $this->translate
->localizedLanguages(['target' => $languageCode]);
}
public function translateBatch(array $input, string $translateTo, string $format = 'text')
{
return $this->translate
->translateBatch($input, ['target' => $translateTo, 'format' => $format]);
}

private function checkForInvalidConfiguration(array $config)
{
if (!isset($config['api_key']) || $config['api_key'] === null) {
throw new Exception('Google Api Key is required.');
public function getAvaliableTranslationsFor(string $languageCode)
{
return $this->translate
->localizedLanguages(['target' => $languageCode]);
}

$codeInConfig = $config['default_target_translation'];
private function checkForInvalidConfiguration(array $config)
{
if ( ! isset($config['api_key']) || $config['api_key'] === null) {
throw new Exception('Google Api Key is required.');
}

$codeInConfig = $config['default_target_translation'];

$languageCodeIsValid = is_string($codeInConfig)
&& ctype_lower($codeInConfig)
&& in_array($codeInConfig, $this->languages());
$languageCodeIsValid = is_string($codeInConfig)
&& ctype_lower($codeInConfig)
&& in_array($codeInConfig, $this->languages());

if (!$languageCodeIsValid) {
throw new Exception(
'The default_target_translation value in the config/googletranslate.php file should
if ( ! $languageCodeIsValid) {
throw new Exception(
'The default_target_translation value in the config/googletranslate.php file should
be a valid lowercase ISO 639-1 code of the language'
);
);
}
}
}
}

0 comments on commit 7d2422a

Please sign in to comment.