Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Translate the names of the dates using perl

by corfuitl (Novice)
on Feb 17, 2014 at 09:47 UTC ( #1075150=perlquestion: print w/replies, xml ) Need Help??
corfuitl has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a perl code that recognises the name of the days and translates them into an other language. My code looks like:
next unless /(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday +)/g; #there is one file for evert language containing the translations open (SRC, "<db-dates/$src") or die "can't open file '$src' $!"; my @src = <SRC>; chomp(@src); for my $i (0 .. $#src){ $dayname =~ s/@src[$i]/@tgt[$i]/g } given($tgt){ when("fr_fr") { print $day" ; } when("it_it") { print $day" ; } default { print $day" ; } }
This script, recognises the days and translate them correctly, but I am not able to print the rest of the phrase. For instance, if the sentence is Today is Monday I want to print Today is lundi for french or Today is lunedi for italian. At the moment is translates only lundi or lunedi. Thank you in advance for your time.

Replies are listed 'Best First'.
Re: Translate the names of the dates using perl
by Eily (Parson) on Feb 17, 2014 at 10:04 UTC

    You didn't give us enough information. The code you gave us is far from complete (we don't see where half the variables come from), and you didn't provide a data sample. See How do I post a question effectively?.

    Unless you're writing in Perl 6, which you probably aren't, you should write $src[$i] instead of @src[$i]. $ is for single values (scalars), and @ for multiple values (lists or arrays), so when you get the single value $i out of @array, it's $array[$i].

    Reading a bit of documentation would be a good idea, and perlintro a good place to start.

Re: Translate the names of the dates using perl
by Tux (Abbot) on Feb 17, 2014 at 11:50 UTC

    There are quite a lot of tools available to do this in a generic and reliable way from CPAN, but from what you describe, I think this comes close:

    my $tgt = "fr_fr"; my %day = ( en_gb => [qw( sunday monday tuesday wednesday thursday friday satu +rday )], fr_fr => [qw( dimanche lundi mardi mercredi jeudi vendredi samedi +)], it_it => [qw( day0 day1 day2 day3 day4 day5 day6 )], ); my %tr = map { lc $day{en_gb}[$_] => $day{$tgt}[$_] } 0..6; my $tr = do { local $" = "|"; qr{\b (@{$day{en_gb}}) \b}i; }; open my $fh, "<:encoding(utf-8)", "db-dates/$src" or die "$src: $!"; while (<$fh>) { s/$tr/$tr{lc $1}/ge; print; }

    Enjoy, Have FUN! H.Merijn
Re: Translate the names of the dates using perl
by GotToBTru (Prior) on Feb 17, 2014 at 19:25 UTC

    It looks like $day contains the name of the day, and nothing else. You need to tell the program to print the "Today is".

    print "Today is "; given ($tgt) { when("fr_fr") { print $day" ; } when("it_it") { print $day" ; } default { print $day" ; } }

    If you want "Aujourd'hui est lundi" or "Oggi lunedi", you need to substitute those in the line for each language.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1075150]
Approved by hdb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2017-10-20 05:16 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (258 votes). Check out past polls.