File Coverage

blib/lib/Localizer/Lexicon.pm
Criterion Covered Total %
statement 22 22 100.0
branch 2 4 50.0
condition 1 3 33.3
subroutine 8 8 100.0
pod 3 3 100.0
total 36 40 90.0


line stmt bran cond sub pod time code
1             package Localizer::Lexicon;
2 5     5   30 use strict;
  5         16  
  5         121  
3 5     5   20 use warnings;
  5         7  
  5         91  
4 5     5   20 use utf8;
  5         6  
  5         27  
5 5     5   162 use 5.010_001;
  5         18  
6              
7             use Class::Accessor::Lite 0.05 (
8 5         61 rw => [qw(dictionary)],
9 5     5   2032 );
  5         5302  
10              
11             sub new {
12 6     6 1 20 my $class = shift;
13 6 50       21 my $dictionary = { @_==1 ? %{$_[0]} : @_ };
  6         29  
14 6         27 bless { dictionary => $dictionary }, $class;
15             }
16              
17 3     3 1 13 sub msgids { keys %{shift->dictionary} }
  3         11  
18              
19             sub msgstr {
20 30     30 1 107 my ( $self, $msgid ) = @_;
21 30 50 33     64 ( exists $self->dictionary->{$msgid} && $self->dictionary->{$msgid} ) || undef;
22             }
23              
24             1;
25              
26             =encoding utf-8
27              
28             =head1 NAME
29              
30             Localizer::Lexicon - Default lexicon class
31              
32             =head1 SYNOPSIS
33              
34             use Localizer::Resource;
35             use Localizer::Style::Gettext;
36             use Localizer::Lexicon
37              
38             my $es = Localizer::Resource->new(
39             dictionary => Localizer::Lexicon->new( 'Hi, %1.' => 'Hola %1.' ),
40             format => Localizer::Style::Gettext->new(),
41             );
42              
43             say $es->maketext("Hi, %1.", "John"); # => Hola, John.
44              
45             =head1 DESCRIPTION
46              
47             L is just the default lexicon that is built internally when you provide a hashref as your dictionary.
48              
49             You can implement your own class to replace this one as your L dictionary, for instance, to get your translations from a database or API. Just pass your object which implements msgids() and msgstr() like this one as your dictionary.
50              
51             =head1 METHODS
52              
53             =over 4
54              
55             =item * Localizer::Lexicon->new(%args | \%args)
56              
57             Constructor. It will initialize the lexicon from a list or hashref
58              
59             e.g.
60              
61             my $de = Localizer::Lexicon->new(
62             'Hello, World!' => 'Hello, Welt!'
63             );
64              
65             =over 8
66              
67             =item dictionary: Hash Reference
68              
69             Dictionary data to localize.
70              
71             =back
72              
73             =item * $localizer->msgids();
74              
75             Returns a list of msgeid's which are the keys of the dictionary.
76              
77             =item * $localizer->msgstr($msgid);
78              
79             Return the message to translate given $msgid (dictionary data with key). If you give nonexistent key to this method, it returns undef.
80              
81             =back
82              
83             =head1 SEE ALSO
84              
85             L, L
86              
87             =head1 LICENSE
88              
89             Copyright (C) Tokuhiro Matsuno.
90              
91             This library is free software; you can redistribute it and/or modify
92             it under the same terms as Perl itself.