Skip to content
This package makes using the Google Translate API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API.
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config v1 Oct 23, 2018
src Allow format option to be overridden Nov 14, 2019
tests Update existing tests for new optional parameter Nov 14, 2019
.gitignore v1 Oct 23, 2018
.travis.yml
CHANGELOG.md Support the format param Nov 14, 2019
LICENSE.txt v1 Oct 23, 2018
README.md Update existing tests for new optional parameter Nov 14, 2019
composer.json Laravel v6.0 support Sep 4, 2019
google.md v1 Oct 23, 2018
phpunit.xml.dist

README.md

Laravel package for the Google Translate API

Latest Version Build Status Total Downloads

This package makes using the Google Translate API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API.

translate

Installation

  • You can install this package via composer using this command:
composer require joggapp/laravel-google-translate
  • The package will automatically register itself.

  • We have documented how to setup the project and get the necessary configurations from the Google Cloud Platform console in a step by step detailed manner over here.

  • You can publish the config file using the following command:

php artisan vendor:publish --provider="JoggApp\GoogleTranslate\GoogleTranslateServiceProvider"

This will create the package's config file called googletranslate.php in the config directory. These are the contents of the published config file:

return [
    /*
    |----------------------------------------------------------------------------------------------------
    | 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'),
];

How to use

  • After setting up the config file values you are all set to use the following methods 😄

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

GoogleTranslate::detectLanguage('Hello world'): array

GoogleTranslate::detectLanguage(['Hello world', 'Laravel is the best']);

// Returns multi-dimensional array containing result set for all the array elements.
  • 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:
GoogleTranslate::translate('Hello world'): array

GoogleTranslate::translate(['Hello world', 'Laravel is the best']);

// Returns multi-dimensional array containing result set for all the array elements.
  • Get all the available translations from 'Google Translation' for a particular language by passing its language code:
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. If the languages are same, the input string is returned as it is, else an array is returned containing the translation results:
GoogleTranslate::unlessLanguageIs('en', string $text);
  • 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.
GoogleTranslate::justTranslate(string $text): string
  • There is is an optional third parameter for format to take advantage for better html translation support. Google Translate API currently supports 'text' and 'html' as parameters. The default for this parameter is 'text' as it has the best use case for most translations. Google Translate API Docs
GoogleTranslate::unlessLanguageIs('en', string $text, string $format);
  • 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

You can run the tests with:

vendor/bin/phpunit

Changelog

Please see the CHANGELOG for more information about what has changed recently.

Security

If you discover any security related issues, please email them to harish@jogg.co instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see the License File for more information.

You can’t perform that action at this time.