I'm currently refining WebService::GoogleAPI::Client module that is on CPAN and can help you do this if
you wish to use Google Translate API.
Here's an example of translating from English to French.
#!/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 authoris
+e
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.go
+ogleapis.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.co
+m/images/branding/product/1x/googleg_32dp.png',
'x16' => 'https://www.gstatic.co
+m/images/branding/product/1x/googleg_16dp.png'
},
'name' => 'translate',
'documentationLink' => 'https://code.google.c
+om/apis/language/translate/v2/getting_started.html'
},
=cut
## assumes gapi.json configuration in working directory with scoped pr
+oject and user authorization
## manunally sets the client user email to be the first in the gapi.js
+on 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.langu
+ages.list');
#say Dumper $r->json;
## https://cloud.google.com/translate/docs/reference/translate
my $r = $gapi_client->api_query( api_endpoint_id => 'translate.transl
+ations.translate',
options => {
q=>'this is some text to transla
+te into french',
target => 'fr',
format => 'text',
} );
say Dumper $r->json;
say $r->json->{data}{translations}[0]{translatedText};
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.