| 1 | package oEdtk::Dict; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Config::IniFiles; | ||
| 6 | use Exporter; | ||
| 7 | our $VERSION = 2.1126; | ||
| 8 | our @ISA = qw(Exporter); | ||
| 9 | |||
| 10 | # Create a dictionary from an INI file. | ||
| 11 | sub new { | ||
| 12 | my ( $class, $path, $options ) = @_; | ||
| 13 | |||
| 14 | my $invert = $options->{'invert'} || 0; | ||
| 15 | my $section = $options->{'section'} || 'DEFAULT'; | ||
| 16 | my $ignore_case = $options->{'ignore_case'} || 1; | ||
| 17 | |||
| 18 | # warn "INFO: Path EDTK_DICO >$path< \n"; | ||
| 19 | tie my %ini, 'Config::IniFiles', ( -file => $path, -default => $section ); | ||
| 20 | |||
| 21 | my $dico = $ini{$section}; | ||
| 22 | if ($invert) { | ||
| 23 | |||
| 24 | # Invert the hash. | ||
| 25 | my %tr = (); | ||
| 26 | while ( my ( $key, $val ) = each %$dico ) { | ||
| 27 | if ( ref($val) eq 'ARRAY' ) { | ||
| 28 | $tr{$_} = $key foreach @$val; | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | $tr{$val} = $key; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | $dico = \%tr; | ||
| 35 | } | ||
| 36 | if ($ignore_case) { | ||
| 37 | my $dico2 = {}; | ||
| 38 | while ( my ( $key, $val ) = each %$dico ) { | ||
| 39 | $dico2->{ lc($key) } = $val; | ||
| 40 | } | ||
| 41 | $dico = $dico2; | ||
| 42 | } | ||
| 43 | my $self = { | ||
| 44 | ignore_case => $ignore_case, | ||
| 45 | dico => $dico | ||
| 46 | }; | ||
| 47 | |||
| 48 | # warn "INFO: Dict object $class $self ($section $path) created...\n"; | ||
| 49 | return bless $self, $class; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub translate { | ||
| 53 | |||
| 54 | # pass the 'check' option to request a check that the value is present in the dictionary | ||
| 55 | # if the value is absent from the dictionary, returns 'undef' | ||
| 56 | my ( $self, $word, $check ) = @_; | ||
| 57 | |||
| 58 | my $key = $word; | ||
| 59 | if ( $self->{'ignore_case'} ) { | ||
| 60 | $key = lc($key); | ||
| 61 | } | ||
| 62 | my $val = $self->{'dico'}->{$key}; | ||
| 63 | if ( !$check && !defined($val) ) { | ||
| 64 | $val = $word; | ||
| 65 | } | ||
| 66 | |||
| 67 | return $val; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub substitue { | ||
| 71 | my ( $self, $var ) = @_; | ||
| 72 | |||
| 73 | # warn "DEBUG: Dict::substitute value = '$var'\n"; | ||
| 74 | |||
| 75 | if ( defined($var) && length($var) > 0 ) { | ||
| 76 | while ( my ( $key, $val ) = each %{ $self->{'dico'} } ) { | ||
| 77 | $var =~ s/$key/$val/ig; | ||
| 78 | } | ||
| 79 | |||
| 80 | # warn "DEBUG: Dict::substitute value = '$var'\n"; | ||
| 81 | } | ||
| 82 | |||
| 83 | return $var; | ||
| 84 | } | ||
| 85 | |||
| 86 | 1; | ||
| 87 | |||
| 88 | =head1 NAME | ||
| 89 | |||
| 90 | oEdtk::Dict - INI-backed translation dictionary | ||
| 91 | |||
| 92 | =head1 SYNOPSIS | ||
| 93 | |||
| 94 | use oEdtk::Dict; | ||
| 95 | |||
| 96 | my $dict = oEdtk::Dict->new('countries.ini', { section => 'COUNTRIES' }); | ||
| 97 | my $label = $dict->translate('FR'); | ||
| 98 | my $text = $dict->substitue('Payable a FR'); | ||
| 99 | |||
| 100 | =head1 DESCRIPTION | ||
| 101 | |||
| 102 | Loads a dictionary from one section of an INI file and offers word lookup and | ||
| 103 | global substitution. It backs the corporate-entity, character translation and | ||
| 104 | country dictionaries used across the toolkit. | ||
| 105 | |||
| 106 | =head1 METHODS | ||
| 107 | |||
| 108 | =head2 new | ||
| 109 | |||
| 110 | my $dict = oEdtk::Dict->new($path, $options); | ||
| 111 | |||
| 112 | Read the C<$path> INI file and build the dictionary. C<$options> accepts: | ||
| 113 | C<invert> (swap keys and values, default off), C<section> (INI section to read, | ||
| 114 | default C<DEFAULT>) and C<ignore_case> (fold keys to lowercase, default on). | ||
| 115 | |||
| 116 | =head2 translate | ||
| 117 | |||
| 118 | my $value = $dict->translate($word, $check); | ||
| 119 | |||
| 120 | Look up C<$word> and return its translation. When C<$check> is true an unknown | ||
| 121 | word yields C<undef>; otherwise the word is returned unchanged. | ||
| 122 | |||
| 123 | =head2 substitue | ||
| 124 | |||
| 125 | my $text = $dict->substitue($var); | ||
| 126 | |||
| 127 | Case-insensitively replace every dictionary entry found in C<$var> and return | ||
| 128 | the result. (The name keeps the historical spelling.) | ||
| 129 | |||
| 130 | =head1 AUTHOR | ||
| 131 | |||
| 132 | The oEdtk team. | ||
| 133 | |||
| 134 | =cut |