File: Translator.pm

package info (click to toggle)
liblog-report-perl 0.94-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 568 kB
  • sloc: perl: 3,649; makefile: 7
file content (70 lines) | stat: -rw-r--r-- 1,431 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyrights 2007-2011 by Mark Overmeer.
#  For other contributors see ChangeLog.
# See the manual pages for details on the licensing terms.
# Pod stripped from pm file by OODoc 2.00.
package Log::Report::Translator;
use vars '$VERSION';
$VERSION = '0.94';


use warnings;
use strict;

use File::Spec ();

use Log::Report 'log-report', syntax => 'SHORT';

use Log::Report::Lexicon::Index ();

my %lexicons;

sub _filename_to_lexicon($);


sub new(@)
{   my $class = shift;
    (bless {}, $class)->init( {callerfn => (caller)[1], @_} );
}

sub init($)
{   my ($self, $args) = @_;
    my $lex = delete $args->{lexicons}
           || _filename_to_lexicon $args->{callerfn};

    my @lex;
    foreach my $lex (ref $lex eq 'ARRAY' ? @$lex : $lex)
    {   push @lex, $lexicons{$lex} ||=   # lexicon indexes are shared
            Log::Report::Lexicon::Index->new($lex);
    }
    $self->{lexicons} = \@lex;
    $self->{charset}  = $args->{charset} || 'utf-8';
    $self;
}

sub _filename_to_lexicon($)
{   my $fn = shift;
    $fn =~ s/\.pm$//;
    File::Spec->catdir($fn, 'messages');
}


sub lexicons() { @{shift->{lexicons}} }


sub charset() {shift->{charset}}


# this is called as last resort: if a translator cannot find
# any lexicon or has no matching language.
sub translate($)
{   my $msg = $_[1];

      defined $msg->{_count} && $msg->{_count} != 1
    ? $msg->{_plural}
    : $msg->{_msgid};
}


sub load($@) { undef }

1;