Skip to content

Commit

Permalink
Update to use latest Google Translate client
Browse files Browse the repository at this point in the history
  • Loading branch information
Flears committed Dec 28, 2019
1 parent 3f638e6 commit 621539e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 62 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ composer.lock
.DS_Store
Thumbs.db
phpunit.xml
/.idea
/.idea
.phpunit.result.cache
10 changes: 5 additions & 5 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',
'default_target_translation' => 'en',

/*
/*
|-------------------------------------------------------------------------------
| Path to the json file containing the authentication credentials.
| 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
|-------------------------------------------------------------------------------
*/
'key_file_path' => base_path('composer.json'),
'api_key' => env('GOOGLE_TRANSLATE_API_KEY'),
];
13 changes: 6 additions & 7 deletions google.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

<img width="576" alt="screen shot 2018-10-11 at 12 11 19 am" src="https://user-images.githubusercontent.com/11228182/46759117-29c62600-ccec-11e8-99a2-b23ee035a75d.png">

- Select the project you just created, and go the "Create Service Account Key" page and in the 'Service Account' section click on 'New Service Account'.
- Select the project you just created, and go the "APIs & Services" -> "Credentials" using the navigation.

- Enter the name & select the Role as 'Owner' for the project.
- Click at the "Create credentials" dropdown button and choose "API key". To prevent unauthorized use and quota theft, restrict your key to limit how it can be used.

- Then click on create to have the JSON credentials file downloaded automatically.

- Add that json file in your laravel project root & add it to `.gitignore`.

- Set the path to that file as the value for the key `key_file_path` in the `config/googletranslate.php` (config file published by this package).
- Now, you can use this key to set a package-specific environment variable:
```
GOOGLE_TRANSLATE_API_KEY=AIzaS.....
```
98 changes: 49 additions & 49 deletions src/GoogleTranslateClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,71 @@
namespace JoggApp\GoogleTranslate;

use Exception;
use Google\Cloud\Translate\TranslateClient;
use Google\Cloud\Translate\V2\TranslateClient;
use JoggApp\GoogleTranslate\Traits\SupportedLanguages;

class GoogleTranslateClient
{
use SupportedLanguages;
use SupportedLanguages;

private $translate;
private $translate;

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

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

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

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

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

public function getAvaliableTranslationsFor(string $languageCode)
{
return $this->translate
->localizedLanguages(['target' => $languageCode]);
}
public function getAvaliableTranslationsFor(string $languageCode)
{
return $this->translate
->localizedLanguages(['target' => $languageCode]);
}

private function checkForInvalidConfiguration(array $config)
{
if (!file_exists($config['key_file_path'])) {
throw new Exception('The json file does not exist at the given path');
}
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'];
$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 621539e

Please sign in to comment.