#!/usr/bin/env perl use WebService::GoogleAPI::Client; use Data::Dumper qw (Dumper); use strict; use warnings; use feature 'say'; =head1 translation_example.pl =head2 PRE-REQUISITES Setup a Google Project in the Google Console and add the Translate API Library. You may need to enable billing to access Google Cloud Services. Setup an OAUTH Credential set and feed this into the CLI goauth included in WebService::GoogleAPI::Client and use the tool to authorise your user to access the project which will also create the local gapi.json config. =over 2 =item https://www.googleapis.com/auth/cloud-translation =item https://www.googleapis.com/auth/cloud-platform =item assumes gapi.json configuration in working directory with scoped project and user authorization =back Translation Service Specification: 'discoveryRestUrl' => 'https://translation.googleapis.com/$discovery/rest?version=v2', 'description' => 'Integrates text translation into your website or application.', 'preferred' => $VAR1->{'items'}[0]{'preferred'}, 'kind' => 'discovery#directoryItem', 'id' => 'translate:v2', 'version' => 'v2', 'title' => 'Cloud Translation API', 'icons' => { 'x32' => 'https://www.gstatic.com/images/branding/product/1x/googleg_32dp.png', 'x16' => 'https://www.gstatic.com/images/branding/product/1x/googleg_16dp.png' }, 'name' => 'translate', 'documentationLink' => 'https://code.google.com/apis/language/translate/v2/getting_started.html' }, =cut ## assumes gapi.json configuration in working directory with scoped project and user authorization ## manunally sets the client user email to be the first in the gapi.json file my $gapi_client = WebService::GoogleAPI::Client->new( debug => 0, gapi_json => 'gapi.json', debug=>1 ); my $aref_token_emails = $gapi_client->auth_storage->storage->get_token_emails_from_storage; my $user = $aref_token_emails->[0]; $gapi_client->user( $user ); ## set to the first authorised user email # my $list = $gapi_client->discover_all(); # say Dumper $list; # list available api endpoints #my $r = $gapi_client->api_query( api_endpoint_id => 'translate.languages.list'); #say Dumper $r->json; ## https://cloud.google.com/translate/docs/reference/translate my $r = $gapi_client->api_query( api_endpoint_id => 'translate.translations.translate', options => { q=>'this is some text to translate into french', target => 'fr', format => 'text', } ); say Dumper $r->json; say $r->json->{data}{translations}[0]{translatedText};