diff --git a/README.md b/README.md index 509c7f3..8f3ba1b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/src/GoogleTranslate.php b/src/GoogleTranslate.php index a30df7f..38b2c2b 100644 --- a/src/GoogleTranslate.php +++ b/src/GoogleTranslate.php @@ -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); @@ -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; diff --git a/src/GoogleTranslateServiceProvider.php b/src/GoogleTranslateServiceProvider.php index 0669a84..0ac1365 100644 --- a/src/GoogleTranslateServiceProvider.php +++ b/src/GoogleTranslateServiceProvider.php @@ -2,6 +2,7 @@ namespace JoggApp\GoogleTranslate; +use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; class GoogleTranslateServiceProvider extends ServiceProvider @@ -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 ""; + }); } public function register() diff --git a/tests/GoogleTranslateTest.php b/tests/GoogleTranslateTest.php index 7f05344..dda1f10 100644 --- a/tests/GoogleTranslateTest.php +++ b/tests/GoogleTranslateTest.php @@ -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() { @@ -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);