<commit_before>package Zonemaster::Translator v0.0.5;

use 5.14.2;
use strict;
use warnings;

use Moose;
use Carp;
use Zonemaster;

use POSIX qw[setlocale LC_MESSAGES];
use Locale::TextDomain qw[Zonemaster];

has 'locale' => ( is => 'rw', isa => 'Str',     lazy => 1, builder => '_get_locale' );
has 'data'   => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_load_data' );

###
### Builder Methods
###

sub BUILD {
    my ( $self ) = @_;

    $self->locale;

    return $self;
}

sub _get_locale {
    my $locale = $ENV{LANG} || $ENV{LC_ALL} || $ENV{LC_MESSAGES} || 'en_US.UTF-8';
    setlocale( LC_MESSAGES, $locale );

    return $locale;
}

sub _load_data {
    my %data;

    $data{SYSTEM} = _system_translation();
    foreach my $mod ( 'Basic', Zonemaster->modules ) {
        my $module = 'Zonemaster::Test::' . $mod;
        $data{ uc( $mod ) } = $module->translation();
    }

    return \%data;
}

###
### Method modifiers
###

after 'locale' => sub {
    my ( $self ) = @_;

    setlocale( LC_MESSAGES, $self->{locale} );
};

###
### Working methods
###

sub to_string {
    my ( $self, $entry ) = @_;

    return sprintf( "%7.2f %-9s %s", $entry->timestamp, $entry->level, $self->translate_tag( $entry ) );
}

sub translate_tag {
    my ( $self, $entry ) = @_;

    my $string = $self->data->{ $entry->module }{ $entry->tag };

    if ( not $string ) {
        return $entry->string;
    }

    return __x( $string, %{ $entry->args } );
}

sub _system_translation {
    return {
        "CANNOT_CONTINUE"       => "Not enough data about {zone} was found to be able to run tests.",
        "CONFIG_FILE"           => "Configuration was read from {name}.",
        "DEPENDENCY_VERSION"    => "Using prerequisite module {name} version {version}.",
        "LOGGER_CALLBACK_ERROR" => "Logger callback died with error: {exception}",
        "LOOKUP_ERROR"          => "DNS query to {ns} for {name}/{type}/{class} failed with error: {message}",
        "MODULE_ERROR"          => "Fatal error in {module}: {msg}",
        "MODULE_VERSION"        => "Using module {module} version {version}.",
        "POLICY_FILE"           => "Policy was read from {name}.",
        "POLICY_DISABLED"       => "The module {name} was disabled by the policy.",
        "UNKNOWN_METHOD"        => "Request to run unknown method {method} in module {moduke}.",
        "UNKNOWN_MODULE"        => "Request to run {method} in unknown module {module}. Known modules: {known}.",
        "SKIP_IPV4_DISABLED"    => "IPv4 is disabled, not sending query to {ns}.",
        "SKIP_IPV6_DISABLED"    => "IPv6 is disabled, not sending query to {ns}.",
    };
}<commit_msg>Typo in error message UNKNOWN_METHOD.<commit_after>package Zonemaster::Translator v0.0.5;

use 5.14.2;
use strict;
use warnings;

use Moose;
use Carp;
use Zonemaster;

use POSIX qw[setlocale LC_MESSAGES];
use Locale::TextDomain qw[Zonemaster];

has 'locale' => ( is => 'rw', isa => 'Str',     lazy => 1, builder => '_get_locale' );
has 'data'   => ( is => 'ro', isa => 'HashRef', lazy => 1, builder => '_load_data' );

###
### Builder Methods
###

sub BUILD {
    my ( $self ) = @_;

    $self->locale;

    return $self;
}

sub _get_locale {
    my $locale = $ENV{LANG} || $ENV{LC_ALL} || $ENV{LC_MESSAGES} || 'en_US.UTF-8';
    setlocale( LC_MESSAGES, $locale );

    return $locale;
}

sub _load_data {
    my %data;

    $data{SYSTEM} = _system_translation();
    foreach my $mod ( 'Basic', Zonemaster->modules ) {
        my $module = 'Zonemaster::Test::' . $mod;
        $data{ uc( $mod ) } = $module->translation();
    }

    return \%data;
}

###
### Method modifiers
###

after 'locale' => sub {
    my ( $self ) = @_;

    setlocale( LC_MESSAGES, $self->{locale} );
};

###
### Working methods
###

sub to_string {
    my ( $self, $entry ) = @_;

    return sprintf( "%7.2f %-9s %s", $entry->timestamp, $entry->level, $self->translate_tag( $entry ) );
}

sub translate_tag {
    my ( $self, $entry ) = @_;

    my $string = $self->data->{ $entry->module }{ $entry->tag };

    if ( not $string ) {
        return $entry->string;
    }

    return __x( $string, %{ $entry->args } );
}

sub _system_translation {
    return {
        "CANNOT_CONTINUE"       => "Not enough data about {zone} was found to be able to run tests.",
        "CONFIG_FILE"           => "Configuration was read from {name}.",
        "DEPENDENCY_VERSION"    => "Using prerequisite module {name} version {version}.",
        "LOGGER_CALLBACK_ERROR" => "Logger callback died with error: {exception}",
        "LOOKUP_ERROR"          => "DNS query to {ns} for {name}/{type}/{class} failed with error: {message}",
        "MODULE_ERROR"          => "Fatal error in {module}: {msg}",
        "MODULE_VERSION"        => "Using module {module} version {version}.",
        "POLICY_FILE"           => "Policy was read from {name}.",
        "POLICY_DISABLED"       => "The module {name} was disabled by the policy.",
        "UNKNOWN_METHOD"        => "Request to run unknown method {method} in module {module}.",
        "UNKNOWN_MODULE"        => "Request to run {method} in unknown module {module}. Known modules: {known}.",
        "SKIP_IPV4_DISABLED"    => "IPv4 is disabled, not sending query to {ns}.",
        "SKIP_IPV6_DISABLED"    => "IPv6 is disabled, not sending query to {ns}.",
    };
}
{'fraction_non_alphanumeric': 0.17191601049868765, 'fraction_numerical': 0.006374203224596925, 'mean_word_length': 2.778328611898017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6511997', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1800, 'n_words': 494}