Skip to content

Commit

Permalink
Merge branch 'master' of github.com:JoggApp/laravel-google-translate
Browse files Browse the repository at this point in the history
  • Loading branch information
introwit committed Jan 14, 2020
2 parents abf9447 + 1c837e5 commit d229f59
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 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
26 changes: 13 additions & 13 deletions config/googletranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

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

/*
|-------------------------------------------------------------------------------
| Path to the json file containing the authentication credentials.
|
| 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 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'),
];
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.....
```
11 changes: 5 additions & 6 deletions src/GoogleTranslateClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JoggApp\GoogleTranslate;

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

class GoogleTranslateClient
Expand All @@ -15,9 +15,8 @@ class GoogleTranslateClient
public function __construct(array $config)
{
$this->checkForInvalidConfiguration($config);

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

Expand Down Expand Up @@ -53,8 +52,8 @@ public function getAvaliableTranslationsFor(string $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');
if ( ! isset($config['api_key']) || $config['api_key'] === null) {
throw new Exception('Google Api Key is required.');
}

$codeInConfig = $config['default_target_translation'];
Expand All @@ -63,7 +62,7 @@ private function checkForInvalidConfiguration(array $config)
&& ctype_lower($codeInConfig)
&& in_array($codeInConfig, $this->languages());

if (!$languageCodeIsValid) {
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'
Expand Down

0 comments on commit d229f59

Please sign in to comment.