| 1 | package Yandex::Translate; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use HTML::Entities qw{encode_entities}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use POSIX qw{:locale_h}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use URI::Escape qw{uri_escape_utf8}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | # The “my” keyword is on a separate line so that the VERSION_FROM attribute | ||||||||||||||||||||||||||||
| 15 | # of ExtUtils::MakeMaker->WriteMakefile() will accurately detect $VERSION. | ||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||
| 17 | my | ||||||||||||||||||||||||||||
| 18 | 5 | $VERSION = '1.002'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | # See https://tech.yandex.ru/translate/doc/dg/concepts/api-overview-docpage/ | ||||||||||||||||||||||||||||
| 22 | # for the supported language codes. | ||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||
| 24 | 5 | my %valid_lang = map { $_ => 1 } qw{ | |||||||||||||||||||||||||||
| 25 | az sq am en ar hy af eu ba be bn my bg bs cy hu vi ht gl nl | ||||||||||||||||||||||||||||
| 26 | mrj el ka gu da he yi id ga it is es kk kn ca ky zh ko xh km | ||||||||||||||||||||||||||||
| 27 | lo la lv lt lb mg ms | ||||||||||||||||||||||||||||
| 28 | ml mt mk mi mr mhr mn de ne no pa pap fa pl pt ro ru ceb sr si | ||||||||||||||||||||||||||||
| 29 | sk sl sw su tg th tl ta tt te tr udm uz uk ur fi fr hi hr cs | ||||||||||||||||||||||||||||
| 30 | sv gd et eo jv ja | ||||||||||||||||||||||||||||
| 31 | }; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 5 | my %valid_format = map { $_ => 1 } qw { plain html }; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 5 | my %valid_options = map { $_ => 1 } qw { 1 }; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 5 | my %valid_default_ui = map { $_ => 1 } qw{ en ru tr }; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # | ||||||||||||||||||||||||||||
| 40 | # Set the default UI to Russian if the locale is Russian; | ||||||||||||||||||||||||||||
| 41 | # Set the default UI to Turkish if the locale is Turkish; | ||||||||||||||||||||||||||||
| 42 | # otherwise, set it to English. | ||||||||||||||||||||||||||||
| 43 | # | ||||||||||||||||||||||||||||
| 44 | 5 | ▶ | (my $default_ui = setlocale(LC_CTYPE) || 'en') =~ s/_.*$//; | ||||||||||||||||||||||||||
|
Condition50%1/2
setlocale(0) || 'en'
MC/DC0%0/1
setlocale(0) || 'en'
setlocale(0)
Truth table50%1/2
A
A= setlocale(0)
| |||||||||||||||||||||||||||||
| 45 | 5 | ▶ | $default_ui = 'en' if (!exists $valid_default_ui{$default_ui}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub new | ||||||||||||||||||||||||||||
| 48 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 50 | 1 | ▶ | my $self = { | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $default_ui
MC/DC0%0/2
shift() || $default_ui
shift()
$default_ui
Truth table33%1/3
A || B
A= shift()B= $default_ui
| |||||||||||||||||||||||||||||
| 51 | _key_ => shift, | ||||||||||||||||||||||||||||
| 52 | _text_ => shift, | ||||||||||||||||||||||||||||
| 53 | _from_lang_ => shift, | ||||||||||||||||||||||||||||
| 54 | _to_lang_ => shift, | ||||||||||||||||||||||||||||
| 55 | _ui_ => shift || $default_ui, | ||||||||||||||||||||||||||||
| 56 | _hint_ => shift, | ||||||||||||||||||||||||||||
| 57 | _format_ => shift, | ||||||||||||||||||||||||||||
| 58 | _options_ => shift, | ||||||||||||||||||||||||||||
| 59 | _base_ => 'https://translate.yandex.net/api/v1.5/tr.json', | ||||||||||||||||||||||||||||
| 60 | _post_ => undef, | ||||||||||||||||||||||||||||
| 61 | _http_ => HTTP::Tiny->new | ||||||||||||||||||||||||||||
| 62 | }; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | $self->{_text_} = uri_escape_utf8($self->{_text_}) if (defined $self->{_text_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | return bless $self, $class; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub set_key | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_key: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 0 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | $self->{_key_} = $key if (defined $key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub set_ui | ||||||||||||||||||||||||||||
| 76 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_ui: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 3 | my ( $self, $ui ) = @_; | |||||||||||||||||||||||||||
| 78 | 3 | ▶ | $self->{_ui_} = (defined $ui && exists $valid_lang{$ui}) ? $ui : $default_ui; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ui and exists $valid_lang{$ui}
MC/DC0%0/2
defined $ui and exists $valid_lang{$ui}
defined $ui
exists $valid_lang{$ui}
Truth table33%1/3
A and B
A= defined $uiB= exists $valid_lang{$ui}
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub set_default_ui | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_default_ui: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 0 | my ( $self, $this_default_ui ) = @_; | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | $default_ui = $this_default_ui if (defined $this_default_ui && exists $valid_default_ui{$this_default_ui}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $this_default_ui and exists $valid_default_ui{$this_default_ui}
MC/DC0%0/2
defined $this_default_ui and exists $valid_default_ui{$this_default_ui}
defined $this_default_ui
exists $valid_default_ui{$this_default_ui}
Truth table0%0/3
A and B
A= defined $this_default_uiB= exists $valid_default_ui{$this_default_ui}
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # | ||||||||||||||||||||||||||||
| 88 | # Get a list of supported translation directions. | ||||||||||||||||||||||||||||
| 89 | # | ||||||||||||||||||||||||||||
| 90 | sub get_langs_list | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_langs_list: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my $query = '/getLangs?'; | |||||||||||||||||||||||||||
| 95 | 0 | $self->{_post_} = 'key='.$self->{_key_}.'&ui='.$self->{_ui_}; | |||||||||||||||||||||||||||
| 96 | 0 | my $response = $self->{_http_}->get($self->{_base_} . $query . $self->{_post_}); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | die "Invalid API key\n" if ($response->{status} eq '401'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | die "Blocked API key\n" if ($response->{status} eq '402'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | die "Failed to get list of supported languages! (response code $response->{status})\n" unless ($response->{success}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if (defined wantarray && length $response->{content}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined wantarray and length $response->{"content"}
MC/DC0%0/2
defined wantarray and length $response->{"content"}
defined wantarray
length $response->{"content"}
Truth table0%0/3
A and B
A= defined wantarrayB= length $response->{"content"}
| |||||||||||||||||||||||||||||
| 103 | 0 | my $json_respond = JSON->new->utf8->decode($response->{content}); | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | return (wantarray) ? @{ $json_respond->{dirs} } : scalar(@{ $json_respond->{dirs} }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub set_text | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_text: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 0 | my ( $self, $text ) = @_; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | $self->{_text_} = uri_escape_utf8($text) if (defined $text); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub set_hint | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_hint: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 0 | my ( $self, $hint ) = @_; | |||||||||||||||||||||||||||
| 117 | 0 | my @valid_hint_lang; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | if (defined $hint && ref($hint) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $hint and ref $hint eq "ARRAY"
MC/DC0%0/2
defined $hint and ref $hint eq "ARRAY"
defined $hint
ref $hint eq "ARRAY"
Truth table0%0/3
A and B
A= defined $hintB= ref $hint eq "ARRAY"
| |||||||||||||||||||||||||||||
| 119 | 0 | for (@{ $hint }) { | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | push @valid_hint_lang, $_ if (exists $valid_lang{$_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | $self->{_hint_} = (@valid_hint_lang) ? [ @valid_hint_lang ] : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub detect_lang | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
detect_lang: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $query = '/detect?'; | |||||||||||||||||||||||||||
| 131 | 0 | $self->{_post_} = 'key='.$self->{_key_}.'&text='.$self->{_text_}; | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | $self->{_post_} .= '&hint='.join(',', @{ $self->{_hint_} }) if (defined $self->{_hint_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | my $response = $self->{_http_}->get($self->{_base_} . $query . $self->{_post_}); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | die "Invalid API key\n" if ($response->{status} eq '401'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | die "Blocked API key\n" if ($response->{status} eq '402'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | die "Exceeded the daily limit on the amount of translated text\n" if ($response->{status} eq '404'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | die "Failed to detect the language! (response code $response->{status})\n" unless ($response->{success}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if (defined wantarray && length $response->{content}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined wantarray and length $response->{"content"}
MC/DC0%0/2
defined wantarray and length $response->{"content"}
defined wantarray
length $response->{"content"}
Truth table0%0/3
A and B
A= defined wantarrayB= length $response->{"content"}
| |||||||||||||||||||||||||||||
| 141 | 0 | my $json_respond = JSON->new->utf8->decode($response->{content}); | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | return (wantarray) ? ($json_respond->{lang}) : $json_respond->{lang}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub set_from_lang | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_from_lang: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 0 | my ( $self, $from_lang ) = @_; | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | $self->{_from_lang_} = $from_lang if (!defined $from_lang || exists $valid_lang{$from_lang}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $from_lang or exists $valid_lang{$from_lang}
MC/DC0%0/2
not defined $from_lang or exists $valid_lang{$from_lang}
not defined $from_lang
exists $valid_lang{$from_lang}
Truth table0%0/3
not(A) or B
A= not defined $from_langB= exists $valid_lang{$from_lang}
| |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub set_to_lang | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_to_lang: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 0 | my ( $self, $to_lang ) = @_; | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | $self->{_to_lang_} = $to_lang if (defined $to_lang && exists $valid_lang{$to_lang}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $to_lang and exists $valid_lang{$to_lang}
MC/DC0%0/2
defined $to_lang and exists $valid_lang{$to_lang}
defined $to_lang
exists $valid_lang{$to_lang}
Truth table0%0/3
A and B
A= defined $to_langB= exists $valid_lang{$to_lang}
| |||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | sub set_format | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_format: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 0 | my ( $self, $format ) = @_; | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | $self->{_format_} = $format if (!defined $format || exists $valid_format{$format}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $format or exists $valid_format{$format}
MC/DC0%0/2
not defined $format or exists $valid_format{$format}
not defined $format
exists $valid_format{$format}
Truth table0%0/3
not(A) or B
A= not defined $formatB= exists $valid_format{$format}
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | sub set_options | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_options: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 0 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | $self->{_options_} = $options if (!defined $options || exists $valid_options{$options}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $options or exists $valid_options{$options}
MC/DC0%0/2
not defined $options or exists $valid_options{$options}
not defined $options
exists $valid_options{$options}
Truth table0%0/3
not(A) or B
A= not defined $optionsB= exists $valid_options{$options}
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | sub translate | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
translate: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | my $query = '/translate?'; | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | my $lang = (defined $self->{_from_lang_}) ? $self->{_from_lang_}.'-'.$self->{_to_lang_} : $self->{_to_lang_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | $self->{_post_} = 'key='.$self->{_key_}.'&text='.$self->{_text_}.'&lang='.$lang; | |||||||||||||||||||||||||||
| 177 | 0 | ▶ | $self->{_post_} .= '&format='.$self->{_format_} if (defined $self->{_format_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | $self->{_post_} .= '&options='.$self->{_options_} if (defined $self->{_options_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | my $response = $self->{_http_}->get($self->{_base_} . $query . $self->{_post_}); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | die "Invalid API key\n" if ($response->{status} eq '401'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | die "Blocked API key\n" if ($response->{status} eq '402'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | die "Exceeded the daily limit on the amount of translated text\n" if ($response->{status} eq '404'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | die "Exceeded the maximum text size\n" if ($response->{status} eq '413'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | die "The text cannot be translated\n" if ($response->{status} eq '422'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | die "The specified translation direction is not supported\n" if ($response->{status} eq '501'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | die "Failed to translate text! (response code $response->{status})\n" unless ($response->{success}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | if (defined wantarray && length $response->{content}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined wantarray and length $response->{"content"}
MC/DC0%0/2
defined wantarray and length $response->{"content"}
defined wantarray
length $response->{"content"}
Truth table0%0/3
A and B
A= defined wantarrayB= length $response->{"content"}
| |||||||||||||||||||||||||||||
| 190 | 0 | my $json_respond = JSON->new->utf8->decode($response->{content}); | |||||||||||||||||||||||||||
| 191 | 0 | ▶ | if (defined $self->{_options_}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 0 | return ($json_respond->{detected}->{lang}, $json_respond->{text}[0]); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | else { | ||||||||||||||||||||||||||||
| 195 | 0 | ▶ | return (wantarray) ? ($json_respond->{text}[0]) : $json_respond->{text}[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # | ||||||||||||||||||||||||||||
| 201 | # See §2.7 of Пользовательское соглашение сервиса «API Яндекс.Переводчик» | ||||||||||||||||||||||||||||
| 202 | # at https://yandex.ru/legal/translate_api/ | ||||||||||||||||||||||||||||
| 203 | # | ||||||||||||||||||||||||||||
| 204 | # See §2.7 of Terms of Use of API Yandex.Translate Service | ||||||||||||||||||||||||||||
| 205 | # at https://yandex.com/legal/translate_api/ | ||||||||||||||||||||||||||||
| 206 | # | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | sub get_yandex_technology_reference | ||||||||||||||||||||||||||||
| 209 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_yandex_technology_reference: called CC=10 SCAR=30.7
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | 3 | my ( $self, $attribute ) = @_; | |||||||||||||||||||||||||||
| 211 | 3 | ▶ | if (defined wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 3 | my %yandex_attribute; | |||||||||||||||||||||||||||
| 213 | 3 | ▶ | if (defined $attribute && ref($attribute) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $attribute and ref $attribute eq "HASH"
MC/DC0%0/2
defined $attribute and ref $attribute eq "HASH"
defined $attribute
ref $attribute eq "HASH"
Truth table33%1/3
A and B
A= defined $attributeB= ref $attribute eq "HASH"
| |||||||||||||||||||||||||||||
| 214 | 0 | ▶ | while (my ( $key, $value ) = each %{ $attribute }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 0 | ▶ | $yandex_attribute{$key} = $key.'="'.encode_entities($value).'"' if (lc $key ne 'href'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | # | ||||||||||||||||||||||||||||
| 220 | # Sort %yandex_attribute so that the same $yandex_attributes value | ||||||||||||||||||||||||||||
| 221 | # will consistently be produced for a given $attribute hash. | ||||||||||||||||||||||||||||
| 222 | # | ||||||||||||||||||||||||||||
| 223 | 0 | ▶ | my $yandex_attributes = (%yandex_attribute) ? ' '.join(' ', map { $yandex_attribute{$_} } sort { $a cmp $b } keys %yandex_attribute) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 3 | my %yandex_url = ( | |||||||||||||||||||||||||||
| 226 | ru => 'http://translate.yandex.ru/', | ||||||||||||||||||||||||||||
| 227 | en => 'http://translate.yandex.com/', | ||||||||||||||||||||||||||||
| 228 | tr => 'http://translate.yandex.com.tr/', | ||||||||||||||||||||||||||||
| 229 | ); | ||||||||||||||||||||||||||||
| 230 | 3 | my %yandex_text = ( | |||||||||||||||||||||||||||
| 231 | ru => 'Переведено сервисом Яндекс.Переводчик', | ||||||||||||||||||||||||||||
| 232 | en => 'Powered by Yandex.Translate', | ||||||||||||||||||||||||||||
| 233 | tr => 'Tarafından desteklenmektedir Yandex.Çeviri', | ||||||||||||||||||||||||||||
| 234 | ); | ||||||||||||||||||||||||||||
| 235 | 3 | ▶ | my $yandex_url = (exists $yandex_url{$self->{_ui_}}) ? $yandex_url{$self->{_ui_}} : $yandex_url{$default_ui}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 3 | ▶ | my $yandex_text = (exists $yandex_text{$self->{_ui_}}) ? $yandex_text{$self->{_ui_}} : $yandex_text{$default_ui}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 3 | my $yandex_element = '<a'.$yandex_attributes.' href="'.$yandex_url.'">'.$yandex_text.'</a>'; | |||||||||||||||||||||||||||
| 238 | 3 | ▶ | return (wantarray) ? ($yandex_element) : $yandex_element; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 5 | 1; | |||||||||||||||||||||||||||
| 243 | __END__ | ||||||||||||||||||||||||||||