lib/Locale/Maketext/AutoTranslate.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 74 CC 6 SCAR 28.9
CC
6
Cov
0%
translate
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 18.0
1 package Locale::Maketext::AutoTranslate;
2  
3 use strict;
4 use warnings;
5  
6 use utf8;
7 use encoding 'utf8';
8 use Moose;
9 use REST::Google::Translate;
10 use Locale::Maketext::Extract;
11 use HTML::Entities;
12 use Locale::Maketext::Lexicon {
13 _allow_empty => 1,
14 };
15  
16 our $VERSION = '0.1';
17  
18 has 'from' => (is => 'rw');
19 has 'to' => (is => 'rw');
20  
21 sub translate {
22 my $self = shift;
23 my $source_file = shift or die "Please specify source file";
24 my $target_file = shift or die "Please specify target file";
25  
26 die "Please specify target language" if !$self->to;
27  
28 $self->from('en') if !$self->from;
29  
30 my $ext = Locale::Maketext::Extract->new;
31 $ext->read_po($source_file);
32  
33 REST::Google::Translate->http_referer('http://example.com');
34  
35 for my $i ($ext->msgids()) {
36 my $res = REST::Google::Translate->new(q => $i,
37 langpair => $self->from() . '|' . $self->to());
38  
39 print STDERR "[$i] => " if $ENV{AUTOTRANSLATE_DEBUG};
40  
41 if ($res->responseStatus == 200) {
42 my $translated = $res->responseData->translatedText;
43 $ext->set_msgstr($i, $translated);
44 }
45 else {
46 $ext->set_msgstr($i, undef);
47 }
48  
49 print STDERR "[" . $ext->msgstr($i) . "]\n" if $ENV{AUTOTRANSLATE_DEBUG};
50 }
51 $ext->write_po($target_file);
52 }
53  
54 1;
55  
56 =pod
57  
58 =head1 NAME
59  
60 Locale::Maketext::AutoTranslate - Translate L10N messages automatically
61  
62 =head1 SYNOPSIS
63  
64 use Locale::Maketext::AutoTranslate;
65  
66 my $t = Locale::Maketext::AutoTranslate->new();
67  
68 $t->from('en');
69 $t->to('zh_tw');
70  
71 $t->translate('en.po' => 'zh_tw.po'); # writes the translations
72 # into zh_tw.po.
73  
74 =head1 DESCRIPTION
75  
76 This module can help human translators to translate l10n messages with
77 less effort. It sends messages to Google Translate service and get
78 rough translations. No translation memories need to be set up locally,
79 and translation process would simply become just correcting,
80 correcting, and correcting.
81  
82 Setting environment variable B<AUTOTRANSLATE_DEBUG> can trace the
83 translation process.
84  
85 =head1 LICENSE
86  
87 This library is free software; you can redistribute it and/or modify
88 it under the same terms as Perl itself.
89  
90 =head1 AUTHOR
91  
92 Yung-chung Lin (henearkrxern@gmail.com)
93  
94 =cut