<commit_before>## OpenXPKI::i18n.pm
## Written 2005 by <NAME> for the OpenXPKI project
## (C) Copyright 2005-2006 The OpenXPKI Project
## $Revision: 240 $

use strict;
use warnings;
use utf8;

package OpenXPKI::i18n;

use English;

use OpenXPKI::Exception;
use Locale::Messages qw (:locale_h :libintl_h nl_putenv bind_textdomain_filter);
use POSIX qw (setlocale);
use Encode;

our $language = "";
our $locale_prefix = "";

use vars qw (@ISA @EXPORT_OK);
use base qw( Exporter );
#require Exporter;
#@ISA = qw (Exporter);
@EXPORT_OK = qw (i18nGettext set_locale_prefix set_language get_language);

sub set_locale_prefix
{
    $locale_prefix = shift;
    if (not -e $locale_prefix)
    {
        OpenXPKI::Exception->throw (
            message => "I18N_OPENXPKI_SET_LOCALE_PREFIX_DIR_DOES_NOT_EXIST",
            params  => {"DIR" => $locale_prefix});
    }
}


sub i18nGettext {
    my $text = shift;
    return $text if (not defined $text or length($text) == 0);

    my $arg_ref;
    my $ref_of_first_argument = ref($_[0]);

    # coerce arguments into a hashref
    if ($ref_of_first_argument eq "") {
	# first argument is a scalar
	my %arguments = @_;
	$arg_ref = \%arguments;
    } 
    elsif ($ref_of_first_argument eq "HASH") {
	$arg_ref = $_[0];
    }
    elsif ($ref_of_first_argument eq "REF") {
	$arg_ref = ${$_[0]};
    }

    ## we need this for utf8
    #it's too slow, I try to use "use utf8;"
    #my $i18n_string = pack "U0C*", unpack "C*", gettext ($text);
    my $i18n_string = gettext ($text);

    if ($i18n_string ne $text)
    {
	## there is a translation for this, so replace the parameters 
	## in the resulting string

	for my $parameter (keys %{$arg_ref}) {
	    warn if ($parameter !~ m{\A __\w+__ \z}xm);
            $i18n_string =~ s/$parameter/$arg_ref->{$parameter}/g;
        }
    } else {
        ## no translation found, output original string followed
	## by all parameters (and values) passed to the function

	## append arguments passed to the function
        $i18n_string = join ("; ", $text,
                                   map { $_ . " => " . $arg_ref->{$_}  } 
                                       keys %{$arg_ref});
	
        #it's too slow, I try to use "use utf8;"
        #$i18n_string = pack "U0C*", unpack "C*", $untranslated;
    }

    return $i18n_string;
}

sub set_language
{
    ## global scope intended
    $language = shift;
    if (! defined $language) {
	$language = "";
    }

    ## erase environment to block libc's automatic environment detection
    ## and enforcement
    #delete $ENV{LC_MESSAGES};
    #delete $ENV{LC_TIME};
    delete $ENV{LANGUAGE};    ## known from Debian<commit_msg>environment variable LANGUAGE set, fixes problems with a local Debian install<commit_after>## OpenXPKI::i18n.pm
## Written 2005 by <NAME> for the OpenXPKI project
## (C) Copyright 2005-2006 The OpenXPKI Project
## $Revision: 240 $

use strict;
use warnings;
use utf8;

package OpenXPKI::i18n;

use English;

use OpenXPKI::Exception;
use Locale::Messages qw (:locale_h :libintl_h nl_putenv bind_textdomain_filter);
use POSIX qw (setlocale);
use Encode;

our $language = "";
our $locale_prefix = "";

use vars qw (@ISA @EXPORT_OK);
use base qw( Exporter );
#require Exporter;
#@ISA = qw (Exporter);
@EXPORT_OK = qw (i18nGettext set_locale_prefix set_language get_language);

sub set_locale_prefix
{
    $locale_prefix = shift;
    if (not -e $locale_prefix)
    {
        OpenXPKI::Exception->throw (
            message => "I18N_OPENXPKI_SET_LOCALE_PREFIX_DIR_DOES_NOT_EXIST",
            params  => {"DIR" => $locale_prefix});
    }
}


sub i18nGettext {
    my $text = shift;
    return $text if (not defined $text or length($text) == 0);

    my $arg_ref;
    my $ref_of_first_argument = ref($_[0]);

    # coerce arguments into a hashref
    if ($ref_of_first_argument eq "") {
	# first argument is a scalar
	my %arguments = @_;
	$arg_ref = \%arguments;
    } 
    elsif ($ref_of_first_argument eq "HASH") {
	$arg_ref = $_[0];
    }
    elsif ($ref_of_first_argument eq "REF") {
	$arg_ref = ${$_[0]};
    }

    ## we need this for utf8
    #it's too slow, I try to use "use utf8;"
    #my $i18n_string = pack "U0C*", unpack "C*", gettext ($text);
    my $i18n_string = gettext ($text);

    if ($i18n_string ne $text)
    {
	## there is a translation for this, so replace the parameters 
	## in the resulting string

	for my $parameter (keys %{$arg_ref}) {
	    warn if ($parameter !~ m{\A __\w+__ \z}xm);
            $i18n_string =~ s/$parameter/$arg_ref->{$parameter}/g;
        }
    } else {
        ## no translation found, output original string followed
	## by all parameters (and values) passed to the function

	## append arguments passed to the function
        $i18n_string = join ("; ", $text,
                                   map { $_ . " => " . $arg_ref->{$_}  } 
                                       keys %{$arg_ref});
	
        #it's too slow, I try to use "use utf8;"
        #$i18n_string = pack "U0C*", unpack "C*", $untranslated;
    }

    return $i18n_string;
}

sub set_language
{
    ## global scope intended
    $language = shift;
    if (! defined $language) {
	$language = "";
    }

    ## erase environment to block libc's automatic environment detection
    ## and enforcement
    #delete $ENV{LC_MESSAGES};
    #delete $ENV{LC_TIME};
    delete $ENV{LANGUAGE};    ## known from Debian
    nl_putenv("LANGUAGE=$language");
{'fraction_non_alphanumeric': 0.1317586270529618, 'fraction_numerical': 0.018084517438641815, 'mean_word_length': 2.8880918220946916, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3392749', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1833, 'n_words': 536}