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