Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
introwit committed Oct 23, 2018
1 parent e6bc6cd commit ca053dc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ return [

- After setting up the config file values you are all set to use the following methods :smile:

- Detect the language:
- Detecting the language. You can pass both, a single string or an array of multiple strings to it:

```php
GoogleTranslate::detectLanguage('Hello world'): array
Expand All @@ -64,7 +64,7 @@ GoogleTranslate::detectLanguage(['Hello world', 'Laravel is the best']);
// Returns multi-dimensional array containing result set for all the array elements.
```

- Translate the string(s): The `translate` method accepts a second optional argument which can be the code of the language you want the string to be translated in. You can specify the default option in the config file:
- Translating the string(s): The `translate` method accepts a second optional argument which can be the code of the language you want the string to be translated in. You can specify the default option in the config file:

```php
GoogleTranslate::translate('Hello world'): array
Expand All @@ -80,13 +80,23 @@ GoogleTranslate::translate(['Hello world', 'Laravel is the best']);
GoogleTranslate::getAvaliableTranslationsFor('en'): array
```

- Translate unless the language is same as the first argument. This method is a clean way to ask the package to detect the language of the given string, if it is same as the first argument, translation isn't performed. It accepts an optional third argument which is the language code you want the string to be translated in. You can specify the default option in the config file:
- Translate unless the language is same as the first argument. This method is a clean way to ask the package to detect the language of the given string, if it is same as the first argument, translation isn't performed. It accepts an optional third argument which is the language code you want the string to be translated in. You can specify the default option in the config file. If the languages are same, the input string is returned as it is, else an array is returned containing the translation results:

```php
GoogleTranslate::unlessLanguageIs('en', string $text)
GoogleTranslate::unlessLanguageIs('en', string $text);
```

- If the languages are same, the input string is returned as it is, else an array is returned containing the translation results.
- Translating and just returning back the translated string. It accepts an optional second argument which is the language code you want the string to be translated in. You can specify the default option in the config file.

```php
GoogleTranslate::justTranslate(string $text): string
```

- There is also a nice blade helper called `@translate` that comes with the package to make its use more neat in the view files. It accepts an optional second argument which is the language code you want the string to be translated in. You can specify the default option in the config file.

```
@translate('Hello World')
```

## Testing

Expand Down
15 changes: 14 additions & 1 deletion src/GoogleTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ public function translate($input, $to = null): array
];
}

public function justTranslate(string $input, $to = null): string
{
$translateTo = $to ?? config('googletranslate.default_target_translation');

$translateTo = $this->sanitizeLanguageCode($translateTo);

$response = $this
->translateClient
->translate($input, $translateTo);

return $response['text'];
}

public function translateBatch(array $input, string $translateTo): array
{
$translateTo = $this->sanitizeLanguageCode($translateTo);
Expand Down Expand Up @@ -121,7 +134,7 @@ public function unlessLanguageIs(string $languageCode, string $input, $to = null

public function sanitizeLanguageCode(string $languageCode)
{
$languageCode = strtolower($languageCode);
$languageCode = trim(strtolower($languageCode));

if (in_array($languageCode, $this->languages())) {
return $languageCode;
Expand Down
12 changes: 12 additions & 0 deletions src/GoogleTranslateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace JoggApp\GoogleTranslate;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class GoogleTranslateServiceProvider extends ServiceProvider
Expand All @@ -11,6 +12,17 @@ public function boot()
$this->publishes([
__DIR__ . '/../config/googletranslate.php' => config_path('googletranslate.php'),
]);

$defaultLanguage = config('googletranslate.default_target_translation');

Blade::directive('translate', function ($expression) use ($defaultLanguage) {
$expression = explode(',', $expression);

$input = $expression[0];
$languageCode = isset($expression[1]) ? str_replace("'", '', $expression[1]) : $defaultLanguage;

return "<?php echo GoogleTranslate::justTranslate($input, '$languageCode'); ?>";
});
}

public function register()
Expand Down
17 changes: 17 additions & 0 deletions tests/GoogleTranslateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ public function it_can_translate_an_array_of_strings_passed_to_it()
$this->assertArrayHasKey('translated_language_code', $response[1]);
}

/** @test */
public function test_the_just_translate_method_returns_just_the_translated_string()
{
$this->translateClient
->shouldReceive('translate')->with($this->testString, 'en')
->once()
->andReturn(['text' => 'A test string']);

$response = $this->translate->justTranslate($this->testString, 'en');

$this->assertEquals('A test string', $response);
}

/** @test */
public function test_the_unless_language_is_method_does_not_translate_the_language_of_given_text_if_it_is_same_as_defined_in_that_method()
{
Expand Down Expand Up @@ -155,6 +168,10 @@ public function it_sanitizes_the_language_codes()

$this->assertEquals('en', $response);

$response = $this->translate->sanitizeLanguageCode(' en');

$this->assertEquals('en', $response);

$response = $this->translate->sanitizeLanguageCode('EN');

$this->assertEquals('en', $response);
Expand Down

0 comments on commit ca053dc

Please sign in to comment.