#!/usr/bin/perl -w use 5.011; use WWW::Google::Translate; use Data::Dumper; use open OUT => ':utf8'; use Path::Tiny; use lib "."; use translate; binmode STDOUT, 'utf8'; use POSIX qw(strftime); ### values to initialize (customize these to suit) my $ini_path = qw( /home/bob/Documents/html_template_data/3.values.ini ); my $sub_hash = "google"; my ( $from, $to ) = ( 'en', 'ru' ); #put defaults here my $input_directory = qw( /home/bob/Documents/meditations/castaways/translate/data ); my $output_appendage = "output"; ## get values for google from an .ini file my $key = get_config( $ini_path, $sub_hash ); say "Would you like to see the possibilities?"; my $prompt1 = ; chomp $prompt1; if ( $prompt1 eq ( "y" | "Y" ) ) { show_lang_codes(); } say "Would you like to change the from language?"; $prompt1 = ; chomp $prompt1; if ( $prompt1 eq ( "y" | "Y" ) ) { $from = get_lang($from); } say "Would you like to change the to language?"; $prompt1 = ; chomp $prompt1; if ( $prompt1 eq ( "y" | "Y" ) ) { $to = get_lang($to); } # create output directory say "Creating output directory as nephew of input"; say "using localtime for uniqueness"; my $munge = strftime( "%d-%m-%Y-%H-%M-%S\.txt", localtime ); my $parent = path($input_directory)->parent; my $out_dir = path( $parent, $output_appendage, $munge ); my $wgt = WWW::Google::Translate->new( { key => $key, default_source => $from, default_target => $to, } ); my @texts = path("$input_directory")->children(qr/\.txt$/); say "texts are @texts"; for my $file (@texts) { local $/ = ""; open my $fh, '<', $file; my $base_name = path("$file")->basename; my $out_file = path( $out_dir, $base_name )->touchpath; say "out_file is $out_file"; while (<$fh>) { print "New Paragraph: $_"; my $r = get_trans( $wgt, $_ ); for my $trans_rh ( @{ $r->{data}->{translations} } ) { #print $trans_rh->{translatedText}, "\n"; my $result = $trans_rh->{translatedText}; say "result is $result "; my @lines = split /\n/, $result; push @lines, "\n"; path("$out_file")->append_utf8(@lines); } } close $fh; }