File Coverage

blib/lib/Zonemaster/Engine/Translator.pm
Criterion Covered Total %
statement 47 50 94.0
branch 2 2 100.0
condition 0 2 0.0
subroutine 15 16 93.7
pod 3 3 100.0
total 67 73 91.7


line stmt bran cond sub pod time code
1             package Zonemaster::Engine::Translator;
2              
3 2     2   25404 use version; our $VERSION = version->declare("v1.0.7");
  2         6  
  2         16  
4              
5 2     2   219 use 5.014002;
  2         7  
6 2     2   10 use strict;
  2         4  
  2         45  
7 2     2   9 use warnings;
  2         4  
  2         57  
8              
9 2     2   9 use Moose;
  2         4  
  2         15  
10 2     2   12801 use Carp;
  2         5  
  2         132  
11 2     2   11 use Zonemaster::Engine;
  2         4  
  2         48  
12              
13 2     2   9 use POSIX qw[setlocale LC_MESSAGES];
  2         4  
  2         15  
14 2     2   995 use Locale::TextDomain qw[Zonemaster-Engine]; # This must be the same name as "name" in Makefile.PL
  2         11360  
  2         12  
15              
16             has 'locale' => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_get_locale' );
17             has 'data' => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_load_data' );
18              
19             ###
20             ### Builder Methods
21             ###
22              
23             sub BUILD {
24 1     1 1 3 my ( $self ) = @_;
25              
26 1         6 $self->locale;
27              
28 1         28 return $self;
29             }
30              
31             sub _get_locale {
32 0   0 0   0 my $locale = $ENV{LANG} || $ENV{LC_ALL} || $ENV{LC_MESSAGES} || 'en_US.UTF-8';
33 0         0 setlocale( LC_MESSAGES, $locale );
34              
35 0         0 return $locale;
36             }
37              
38             sub _load_data {
39 1     1   2 my %data;
40              
41 1         4 $data{SYSTEM} = _system_translation();
42 1         9 foreach my $mod ( 'Basic', Zonemaster::Engine->modules ) {
43 10         14 my $module = 'Zonemaster::Engine::Test::' . $mod;
44 10         66 $data{ uc( $mod ) } = $module->translation();
45             }
46              
47 1         32 return \%data;
48             }
49              
50             ###
51             ### Method modifiers
52             ###
53              
54             after 'locale' => sub {
55             my ( $self ) = @_;
56              
57             setlocale( LC_MESSAGES, $self->{locale} );
58             };
59              
60             ###
61             ### Working methods
62             ###
63              
64             sub to_string {
65 1     1 1 7 my ( $self, $entry ) = @_;
66              
67 1         29 return sprintf( "%7.2f %-9s %s", $entry->timestamp, $entry->level, $self->translate_tag( $entry ) );
68             }
69              
70             sub translate_tag {
71 2     2 1 10 my ( $self, $entry ) = @_;
72              
73 2         58 my $string = $self->data->{ $entry->module }{ $entry->tag };
74              
75 2 100       7 if ( not $string ) {
76 1         7 return $entry->string;
77             }
78              
79 1         3 return __x( $string, %{ $entry->printable_args } );
  1         5  
80             }
81              
82             sub _system_translation {
83             return {
84 1     1   21 "CANNOT_CONTINUE" => "Not enough data about {zone} was found to be able to run tests.",
85             "CONFIG_FILE" => "Configuration was read from {name}.",
86             "DEPENDENCY_VERSION" => "Using prerequisite module {name} version {version}.",
87             "GLOBAL_VERSION" => "Using version {version} of the Zonemaster engine.",
88             "LOGGER_CALLBACK_ERROR" => "Logger callback died with error: {exception}",
89             "LOOKUP_ERROR" => "DNS query to {ns} for {name}/{type}/{class} failed with error: {message}",
90             "MODULE_ERROR" => "Fatal error in {module}: {msg}",
91             "MODULE_VERSION" => "Using module {module} version {version}.",
92             "MODULE_END" => "Module {module} finished running.",
93             "NO_NETWORK" => "Both IPv4 and IPv6 are disabled.",
94             "POLICY_FILE" => "Policy was read from {name}.",
95             "POLICY_DISABLED" => "The module {name} was disabled by the policy.",
96             "UNKNOWN_METHOD" => "Request to run unknown method {method} in module {module}.",
97             "UNKNOWN_MODULE" => "Request to run {method} in unknown module {module}. Known modules: {known}.",
98             "SKIP_IPV4_DISABLED" => "IPv4 is disabled, not sending query to {ns}.",
99             "SKIP_IPV6_DISABLED" => "IPv6 is disabled, not sending query to {ns}.",
100             "FAKE_DELEGATION" => "Followed a fake delegation.",
101             "ADDED_FAKE_DELEGATION" => "Added a fake delegation for domain {domain} to name server {ns}.",
102             "FAKE_DELEGATION_TO_SELF" => "Name server {ns} not adding fake delegation for domain {domain} to itself.",
103             "FAKE_DELEGATION_IN_ZONE_NO_IP" => "The fake delegation of domain {domain} includes an in-zone name server {ns} without mandatory glue (without IP address).",
104             "FAKE_DELEGATION_NO_IP" => "The fake delegation of domain {domain} includes a name server {ns} that cannot be resolved to any IP address.",
105             "PACKET_BIG" => "Packet size ({size}) exceeds common maximum size of {maxsize} bytes (try with \"{command}\").",
106             };
107             } ## end sub _system_translation
108              
109 2     2   1430 no Moose;
  2         4  
  2         16  
110             __PACKAGE__->meta->make_immutable;
111              
112             1;
113              
114             =head1 NAME
115              
116             Zonemaster::Engine::Translator - translation support for Zonemaster
117              
118             =head1 SYNOPSIS
119              
120             my $trans = Zonemaster::Engine::Translator->new({ locale => 'sv_SE.UTF-8' });
121             say $trans->to_string($entry);
122              
123             =head1 ATTRIBUTES
124              
125             =over
126              
127             =item locale
128              
129             The locale that should be used to find translation data. If not
130             explicitly provided, defaults to (in order) the contents of the
131             environment variable LANG, LC_ALL, LC_MESSAGES or, if none of them are
132             set, to C<en_US.UTF-8>.
133              
134             =item data
135              
136             A reference to a hash with translation data. This is unlikely to be useful to
137             end-users.
138              
139             =back
140              
141             =head1 METHODS
142              
143             =over
144              
145             =item to_string($entry)
146              
147             Takes a L<Zonemaster::Engine::Logger::Entry> object as its argument and returns a translated string with the timestamp, level, message and arguments in the
148             entry.
149              
150             =item translate_tag
151              
152             Takes a L<Zonemaster::Engine::Logger::Entry> object as its argument and returns a translation of its tag and arguments.
153              
154             =item BUILD
155              
156             Internal method that's only mentioned here to placate L<Pod::Coverage>.
157              
158             =back
159              
160             =cut