#!/usr/bin/perl

#(9/8/2022): Accept a list of words, one per line, and translate using Google Translate into all available languages.
#USAGE: ./step1_translatewordlist.pl ./WORDLIST.TXT

$PROJECTID = '';
$WORDLIST = $ARGV[0];

use JSON::XS;

#########################################################
#get our word list...
open(FILE, $WORDLIST); binmode(FILE, ":utf8");
while(<FILE>) {
    $_=~s/\s+$//;
    $WORDS{$_} = 1;
}
close(FILE);
@WORDS = (keys %WORDS); if (scalar(@WORDS) < 1) { print "FATAL: No Word List!\n"; exit; };
#########################################################

#########################################################################################
#get our bearer token...-H \"Authorization: Bearer \$(gcloud auth application-default print-access-token)\"
$bearertoken = `gcloud auth application-default print-access-token`; $bearertoken=~s/\s+$//s;
print "GOT TOKEN: $bearertoken\n";
#########################################################################################

#########################################################################################
#get the available list of all language codes supported by Google Translate...
$JSON_LANGS = `curl -s -X GET -H "X-Goog-User-Project: $PROJECTID" -H "Authorization: Bearer $bearertoken" https://translation.googleapis.com/language/translate/v2/languages`;
#and cache locally for review...
open(OUT, ">./LANGS.json"); binmode(OUT, ":utf8"); print OUT $JSON_LANGS; close(OUT);

#and parse them...
my $ref; $ref = decode_json $JSON_LANGS;
if (!defined($ref)) { print "FATAL: UNABLE TO LIST LANGUAGES!\n"; exit; }
foreach (@{$ref->{'data'}->{'languages'}}) { push(@LANGCODES, $_->{'language'}); };
$LANGCOUNT = scalar(@LANGCODES); print "Found $LANGCOUNT Languages...\n";
#########################################################################################

#########################################################################################
#now, translate each of our words into each of the languages serially in sequence...
open(TRANS, ">./TRANSLATIONS.json");
foreach $LANG (@LANGCODES) {
    if ($LANG eq 'en') { next; };
    print "Lang: $LANG\n";
    
    #create the request...
    my %ref;
    $ref{'sourceLanguageCode'} = 'en';
    $ref{'targetLanguageCode'} = $LANG;
    $ref{'mimeType'} = 'text/plain';
    $ref{'contents'} = [@WORDS];
    my $json; $json = JSON::XS->new->allow_nonref(1)->utf8->encode(\%ref);
    
    #submit the request...
    print "\tSubmitting Request to API...\n";
    open(OUT, ">./TMP.JSON"); print OUT $json; close(OUT); unlink("./TMP.JSON.RET");
    system("curl -s -X POST -H \"X-Goog-User-Project: $PROJECTID\" -H \"Authorization: Bearer $bearertoken\" -H \"Content-Type: application/json; charset=utf-8\" -d \@./TMP.JSON -o ./TMP.JSON.RET https://translation.googleapis.com/v3/projects/$PROJECTID:translateText");
    
    #parse the results...
    my $json; open(FILE, "./TMP.JSON.RET"); read(FILE, $json, (-s FILE)); close(FILE);
    my $ref; $ref = decode_json($json);
    foreach (@{$ref->{'translations'}}) {
    	my $write; $write{'lang'} = $LANG; $write{'trans'} = $_->{'translatedText'};
	print TRANS JSON::XS->new->allow_nonref(1)->utf8->encode(\%write) . "\n";
    }
    
    #and clean up...
    unlink("./TMP.JSON");
    unlink("./TMP.JSON.RET");

}

#and add in the english words...
foreach (@WORDS) {
    my $write; $write{'lang'} = 'en'; $write{'trans'} = $_;
    print TRANS JSON::XS->new->allow_nonref(1)->utf8->encode(\%write) . "\n";
}
close(TRANS);
#########################################################################################

print "Done...\n";
