The sample code can be found:
The purpose this package is to detect languages of a given string. If you need other functionality you may call that function by the google client.
These samples show how to use the Google Cloud Translate API from PHP.
Add following code to the application's composer.json
file
"repositories": [
....
{
"type": "vcs",
"url": "https://github.com/virtunus/translator.git"
}
],
Now run the follwing command to add this package to your project.
comoser require virtunus/translator
- Enable APIs - Enable the Translate API and create a new project or select an existing project.
- Download The Credentials - Configure your project using Application Default Credentials.
Click Go to credentials after enabling the APIs. Click Create Credentials
and select Service Account Credentials and download the credentials file. Then set the path of
this file to the environment variable
GOOGLE_APPLICATION_CREDENTIALS
using one of the following two ways:
- Set environment variable to
.env
file in your project
GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
- Set glabal environment variable in OS
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
- Detect languages
$client = new \Virtunus\Translator\TranslationClient();
$langs = $client->detectLanguages('May Allah bless you', 'project-id');
If you do not want to pass the
project-id
again and again then you should set theGOOGLE_PROJECT_ID
environment varable in.env
file of your project.
- Get original google client
$client = new \Virtunus\Translator\TranslationClient();
$googleClent = $client->getClient();
Now can call any function of the google API client with $googleClient
.
composer test
You should set the following environment variable to disable language detection.
ENABLE_LANGUAGE_DETECTION=false