diff --git a/.gitignore b/.gitignore index 19adabd..424add2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ composer.lock .DS_Store Thumbs.db phpunit.xml -/.idea \ No newline at end of file +/.idea +.phpunit.result.cache diff --git a/config/googletranslate.php b/config/googletranslate.php index 6b88f9d..0639db1 100644 --- a/config/googletranslate.php +++ b/config/googletranslate.php @@ -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'), ]; diff --git a/google.md b/google.md index 87717a9..44eb219 100644 --- a/google.md +++ b/google.md @@ -6,12 +6,11 @@ screen shot 2018-10-11 at 12 11 19 am -- 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). \ No newline at end of file +- Now, you can use this key to set a package-specific environment variable: +``` +GOOGLE_TRANSLATE_API_KEY=AIzaS..... +``` \ No newline at end of file diff --git a/src/GoogleTranslateClient.php b/src/GoogleTranslateClient.php index 0c2d63b..a18d425 100644 --- a/src/GoogleTranslateClient.php +++ b/src/GoogleTranslateClient.php @@ -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 @@ -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'], ]); } @@ -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']; @@ -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'