Nowadays DeepL is considered to produce better results than Google Translate. It even has an HTTP API, for which you can register for free (for some value of free):
Thx for your reply, kikuchiyo, I think this is gonna work out for me. They do make you put a credit card on record, but I'm willing to offer that kind of skin in this game. DeepL seems more trustworthy than Google. I'm excited to see what capabilities this service can provide.
I don't know about this "trans" command of yours, but coding a wrapper script for a HTTP API is trivial in Perl.
Well, I don't know about "trivial." Maybe for corion and bliako, but I'm a garden-variety human who fumbles the ball and needs to consult. I was proud that I remembered corion's curl converter, from which I got this:
#!perl
use strict;
use warnings;
use HTTP::Tiny;
my $ua = HTTP::Tiny->new( 'verify_SSL' => '1' );
my $res = $ua->request(
'POST' => 'https://api-free.deepl.com/v2/translate',
{
headers => {
'Authorization' =>
'DeepL-Auth-Key redacted',
'Content-Length' => '37',
'Accept' => '*/*',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'curl/7.55.1'
},
content => "text=Hello\x252C\x2520world!&target_lang=DE"
},
);
__END__
Created from curl command line
curl -X POST 'https://api-free.deepl.com/v2/translate' -H 'Author
+ization: DeepL-Auth-Key redacted' -d 'text=Hello%2C%20world!'
+ -d 'target_lang=DE'
But I run into trouble decoding the json:
fritz@laptop:~/Documents$ ./3.trans.pl
Hello neighbor on Watercress lane,
{"translations":[{"detected_source_language":"EN","text":"Hola vecino
+de Watercress lane,"}]}content is {"translations":[{"detected_source_
+language":"EN","text":"Hola vecino de Watercress lane,"}]}
data is HASH(0x55fbf7cf4140)
...
Anyways, I start getting letters saying that I have not complied with
+this declaration, which had the bizarre predicate that we had to come
+ to their residence to prove that we had complied. One thing I can pr
+omise you: I will never cross their threshold, because I don't want t
+o know them at all based on what they stuffed into my mailbox.
{"translations":[{"detected_source_language":"EN","text":"De todos mod
+os, empiezo a recibir cartas diciendo que no he cumplido con esta dec
+laración, que tenía el extraño predicado de que teníamos que ir a su
+residencia para demostrar que habíamos cumplido. Una cosa puedo prome
+ter: Nunca cruzaré su umbral, porque no quiero conocerlos en absoluto
+ basándome en lo que me metieron en el buzón."}]}content is {"transla
+tions":[{"detected_source_language":"EN","text":"De todos modos, empi
+ezo a recibir cartas diciendo que no he cumplido con esta declaración
+, que tenía el extraño predicado de que teníamos que ir a su residenc
+ia para demostrar que habíamos cumplido. Una cosa puedo prometer: Nun
+ca cruzaré su umbral, porque no quiero conocerlos en absoluto basándo
+me en lo que me metieron en el buzón."}]}
data is HASH(0x55fbf8768858)
fritz@laptop:~/Documents$ ^C
Source:
#!/usr/bin/perl
use v5.030; # strictness implied
use warnings;
use Path::Tiny;
use HTTP::Tiny;
use JSON::MaybeXS;
my $file_in = path("/home/fritz/Desktop/1.enchanto.txt");
my $file_out = path('/home/fritz/Desktop/1.enc_trans.txt');
my $lang = 'es';
my $guts = $file_in->slurp_utf8;
my @spl = split( '\n', $guts );
my $ua = HTTP::Tiny->new( 'verify_SSL' => '1' );
for my $para (@spl) {
say $para;
my $payload = "text=$para&target_lang=$lang";
my $payloadlen = length($payload);
my $response = $ua->request(
'POST' => 'https://api-free.deepl.com/v2/translate',
{
headers => {
'Authorization' =>
'DeepL-Auth-Key redacted',
'Content-Length' => $payloadlen,
'Accept' => '*/*',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'curl/7.55.1'
},
content => $payload,
},
);
die "Failed!\n" unless $response->{success};
print $response->{content} if length $response->{content};
my $content = $response->{content};
say "content is $content";
my $data = decode_json($content);
say "data is $data";
$file_out->spew_utf8( $para, $data );
}
__END__
I typically use bliako's software for this, but I couldn't reconcile that with HTTP::Tiny:
use LWP::UserAgent;
use HTTP::Request;
use Data::Roundtrip;
...
my $req = HTTP::Request->new(
...
$response = $ua->request($req);
die "Error fetching: " . $response->status_line
unless $response->is_success;
my $content = $response->decoded_content;
my $data = Data::Roundtrip::json2perl($content);
die "failed to parse received data:\n$content\n"
unless exists $data->{'elevation'};
return $data->{'elevation'};
In particular I don't see how to do this without these modules:
my $content = $response->decoded_content;
my $data = Data::Roundtrip::json2perl($content);
Anyways, I'm elated that I have spanish that I don't understand already and hope that someone can help me over the finish line with the json.
Cheers from the Rocky Mountains,
|