GithubHelp home page GithubHelp logo

google-translate's Introduction

Laravel package for Google Translate REST API

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Package allows to work with Google Translate API

Installation

Package can be installed using composer by adding to "require" object

"require": {
    "aurawindsurfing/google-translate"
}

or from console:

composer require aurawindsurfing/google-translate

Configuration

After the installation, you should add "Dedicated\GoogleTranslate\GoogleTranslateProvider" to providers.

        'providers' => [
                /* 3rd Party Providers */
                Dedicated\GoogleTranslate\GoogleTranslateProvider::class,
         ],

Then you should publish config file to be able to add your Google API key. To publish config you should do:

php artisan vendor:publish \
--provider="Dedicated\GoogleTranslate\GoogleTranslateProvider" --tag=config

After config is published, you will have it in config\google-translate.php of your Laravel project directory

You should change only one line:

    ...
    
    /**
     * Google key for authentication
     */
    'api_key' => 'YOUR-GOOGLE-API-KEY-GOES-HERE',
    
    ...

Usage

To translate text with given source language and target language:

$translator = new Dedicated\GoogleTranslate\Translator;


$result = $translator->setSourceLang('en')
                     ->setTargetLang('ru')
                     ->translate('Hello World');
                           
dd($result); // "Привет мир"                           

By default language detection is turned on, so you can translate text without specifying source language.

This will make 2 requests to google API:

  • First request will go to /detect URL and get source language name
  • Second request will make actual translate request and give out result.
$translator = new Dedicated\GoogleTranslate\Translator;


$result = $translator->setTargetLang('ru')
                     ->translate('Hello World');
                           
dd($result); // "Привет мир"                           

You can also use function to only detect text's source language:


$result = $translator->detect('Hello World');

dd($result); // "en"

License

This repository code is open-sourced software licensed under the MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.