-
Notifications
You must be signed in to change notification settings - Fork 1
/
pobot.pl
executable file
·60 lines (46 loc) · 1.2 KB
/
pobot.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env perl
#@author: Giap Tran <[email protected]>
use Locale::PO;
use Getopt::Std;
our $lang="en:vi";
our $output="/dev/stdout";
sub do_help{
print "Usage:\tpobot.pl -i <input-file> [-o output-file] [-l srclang:dstlang]\n";
print "Ex:\tpobot.pl -l en:vi -i 01_the-debian-project.po -o 01_the-debian-project.po.vi";
exit 1
}
my %options=();
getopts("i:o:l:", \%options);
if (defined $options{l}){
$lang=$options{l};
}
if (!defined $options{i}){
do_help();
}
our $input = $options{i};
if (defined $options{o}){
$output=$options{o};
}
my $po = Locale::PO->load_file_asarray($input);
my $size=@$po;
my $count=1;
foreach my $entry (@$po){
print "\rTranslate $count/$size";
$count++;
my $msgid=$entry->msgid;
# skip if msgid empty
if ( (not defined $msgid) || ($msgid eq '""') ) { next }
# push <tag>
my @tags = $msgid =~ /<\/?.*?>/g;
my $source = $msgid =~ s/<\/?.*?>/<###>/rg;
# translate
my $msgstr=`trans $lang -b $source`;
# pop <tag>
for my $tag (@tags) {
$msgstr = $msgstr =~ s/<###>/$tag/r;
$msgstr = $msgstr =~ s/\n$//rg;
}
$entry->msgstr($msgstr);
}
print "::Done\n";
Locale::PO->save_file_fromarray($output,$po);