From 621539ed5f96e46be297e0fcfc9f964bf38b59f6 Mon Sep 17 00:00:00 2001 From: Flears <49255915+flearsnetwork@users.noreply.github.com> Date: Sat, 28 Dec 2019 22:47:11 +0100 Subject: [PATCH 1/2] Update to use latest Google Translate client --- .gitignore | 3 +- config/googletranslate.php | 10 ++-- google.md | 13 +++-- src/GoogleTranslateClient.php | 98 +++++++++++++++++------------------ 4 files changed, 62 insertions(+), 62 deletions(-) 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..b07327e 100644 --- a/config/googletranslate.php +++ b/config/googletranslate.php @@ -1,21 +1,21 @@ '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'), ]; 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..86b2f7b 100644 --- a/src/GoogleTranslateClient.php +++ b/src/GoogleTranslateClient.php @@ -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' - ); - } + ); } + } } From 7d2422ab6f8399de843e0cc921774df4a271ed4c Mon Sep 17 00:00:00 2001 From: Flears <49255915+flearsnetwork@users.noreply.github.com> Date: Sat, 28 Dec 2019 23:16:04 +0100 Subject: [PATCH 2/2] Formatting --- config/googletranslate.php | 32 ++++++------ src/GoogleTranslateClient.php | 97 +++++++++++++++++------------------ 2 files changed, 64 insertions(+), 65 deletions(-) diff --git a/config/googletranslate.php b/config/googletranslate.php index b07327e..0639db1 100644 --- a/config/googletranslate.php +++ b/config/googletranslate.php @@ -1,21 +1,21 @@ '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'), ]; diff --git a/src/GoogleTranslateClient.php b/src/GoogleTranslateClient.php index 86b2f7b..a18d425 100644 --- a/src/GoogleTranslateClient.php +++ b/src/GoogleTranslateClient.php @@ -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' - ); + ); + } } - } }