Skip to content

Commit d229f59

Browse files
committed
Merge branch 'master' of github.com:JoggApp/laravel-google-translate
2 parents abf9447 + 1c837e5 commit d229f59

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ composer.lock
33
.DS_Store
44
Thumbs.db
55
phpunit.xml
6-
/.idea
6+
/.idea
7+
.phpunit.result.cache

config/googletranslate.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
return [
44
/*
5-
|----------------------------------------------------------------------------------------------------
6-
| The ISO 639-1 code of the language in lowercase to which the text will be translated to by default.
7-
|----------------------------------------------------------------------------------------------------
8-
*/
5+
|----------------------------------------------------------------------------------------------------
6+
| The ISO 639-1 code of the language in lowercase to which the text will be translated to by default.
7+
|----------------------------------------------------------------------------------------------------
8+
*/
99
'default_target_translation' => 'en',
1010

1111
/*
12-
|-------------------------------------------------------------------------------
13-
| Path to the json file containing the authentication credentials.
14-
|
15-
| The process to get this file is documented in a step by step detailed manner
16-
| over here:
17-
| https://github.com/JoggApp/laravel-google-translate/blob/master/google.md
18-
|-------------------------------------------------------------------------------
19-
*/
20-
'key_file_path' => base_path('composer.json'),
12+
|-------------------------------------------------------------------------------
13+
| Api Key generated within Google Cloud Dashboard.
14+
|
15+
| The process to get this file is documented in a step by step detailed manner
16+
| over here:
17+
| https://github.com/JoggApp/laravel-google-translate/blob/master/google.md
18+
|-------------------------------------------------------------------------------
19+
*/
20+
'api_key' => env('GOOGLE_TRANSLATE_API_KEY'),
2121
];

google.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66

77
<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">
88

9-
- 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'.
9+
- Select the project you just created, and go the "APIs & Services" -> "Credentials" using the navigation.
1010

11-
- Enter the name & select the Role as 'Owner' for the project.
11+
- 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.
1212

13-
- Then click on create to have the JSON credentials file downloaded automatically.
14-
15-
- Add that json file in your laravel project root & add it to `.gitignore`.
16-
17-
- 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).
13+
- Now, you can use this key to set a package-specific environment variable:
14+
```
15+
GOOGLE_TRANSLATE_API_KEY=AIzaS.....
16+
```

src/GoogleTranslateClient.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace JoggApp\GoogleTranslate;
44

55
use Exception;
6-
use Google\Cloud\Translate\TranslateClient;
6+
use Google\Cloud\Translate\V2\TranslateClient;
77
use JoggApp\GoogleTranslate\Traits\SupportedLanguages;
88

99
class GoogleTranslateClient
@@ -15,9 +15,8 @@ class GoogleTranslateClient
1515
public function __construct(array $config)
1616
{
1717
$this->checkForInvalidConfiguration($config);
18-
1918
$this->translate = new TranslateClient([
20-
'keyFilePath' => $config['key_file_path']
19+
'key' => $config['api_key'],
2120
]);
2221
}
2322

@@ -53,8 +52,8 @@ public function getAvaliableTranslationsFor(string $languageCode)
5352

5453
private function checkForInvalidConfiguration(array $config)
5554
{
56-
if (!file_exists($config['key_file_path'])) {
57-
throw new Exception('The json file does not exist at the given path');
55+
if ( ! isset($config['api_key']) || $config['api_key'] === null) {
56+
throw new Exception('Google Api Key is required.');
5857
}
5958

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

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

0 commit comments

Comments
 (0)