| 1 | package Valiant::I18N; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 44 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 44 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 44 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 44 | ▶ | use Data::Localize; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 44 | ▶ | use Data::Localize::MultiLevel; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 44 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 44 | ▶ | use Valiant::Util 'throw_exception', 'debug'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 44 | ▶ | use namespace::autoclean -also => ['throw_exception', 'debug']; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 44 | our $dl; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 44 | our %locale_paths; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | #TODO add -namespace='namespace to use to find the locale dir' | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 110 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 110 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 110 | my $target = caller; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 110 | $class->init; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 110 | $class->add_locale_path(_locale_path_from_module($target)); #TODO Should we also look it parent directories? | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 44 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 110 | ▶ | *{"${target}::_t"} = sub { $class->make_tag(@_) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 3261 | ▶ | sub dl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dl: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 3261 | my $self_or_class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 3261 | ▶ | my $class = ref($self_or_class) ? ref($self_or_class) : $self_or_class; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 3261 | ▶ | $dl ||= $class->init; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$Valiant::I18N::dl ||= $class->init
MC/DC0%0/2
$Valiant::I18N::dl ||= $class->init
$Valiant::I18N::dl
$class->init
Truth table66%2/3
A ||= B
A= $Valiant::I18N::dlB= $class->init
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 3261 | return $dl; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 112 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 112 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 112 | ▶ | return if $dl; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 41 | $dl = Data::Localize->new; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 41 | my @detected_languages = $dl->languages; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 41 | ▶ | $dl->set_languages('en', @detected_languages) unless grep { /^en$/ } @detected_languages; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 41 | $class->add_locale_path(_locale_path_from_module($class)); #TODO do we need to load the $class @ISA as well? | |||||||||||||||||||||||||||||||||||||||||
| 40 | 41 | return $dl; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 151 | ▶ | sub add_locale_path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_locale_path: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 151 | my ($class, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 151 | ▶ | return if $locale_paths{$path}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 94 | debug 2, "Found new possible locales path at $path"; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 94 | my @found = glob($path); | |||||||||||||||||||||||||||||||||||||||||
| 48 | 94 | ▶ | my $flag = @found ? 1 : -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 94 | ▶ | if($flag == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 84 | debug 1, "Translation files found at $path"; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 84 | $class->dl->add_localizer(Data::Localize::MultiLevel->new(paths => [$path])); | |||||||||||||||||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 10 | debug 2, "No translation files found at $path"; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 94 | $locale_paths{$path} = $flag; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 94 | ▶ | return $flag == 1 ? 1:0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 151 | ▶ | sub _module_path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_module_path: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 151 | my $class_name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 151 | my @parts = split '::', $class_name; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 151 | my $path = join '/', @parts; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 151 | debug 3, "trying to find module path for class '$class_name' via 'INC{${path}.pm}'"; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 151 | my $inc = $INC{"${path}.pm"}; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 151 | ▶ | unless($inc) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 5 | debug 3, "Can't find ${path}.pm from parts: @{[ join ', ', @parts ]} in INC keys: @{[ join ', ', keys %INC ]}"; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 151 | return $inc; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 151 | ▶ | sub _locale_path_from_module { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_locale_path_from_module: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 151 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 151 | my $module_path = _module_path($class); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 151 | ▶ | debug 2, "Found a path '@{[ $module_path||'NotFound' ]}' for class '$class'"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$module_path || 'NotFound'
MC/DC100%1/1
$module_path || 'NotFound'
$module_path
Truth table100%2/2
A
A= $module_path
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 151 | my ($vol, $dir, $file) = File::Spec->splitpath($module_path); | |||||||||||||||||||||||||||||||||||||||||
| 76 | #my $locale_path = File::Spec->catfile($dir, 'locale','*.*'); | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 151 | my $locale_path = File::Spec->catpath($vol, $dir, File::Spec->catfile('locale', '*.*')); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 151 | ▶ | debug 2, "module path '@{[ $module_path||'NotFound' ]}' resolves to path '$locale_path' with vol:$vol, dir:$dir and file:$file"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$module_path || 'NotFound'
MC/DC100%1/1
$module_path || 'NotFound'
$module_path
Truth table100%2/2
A
A= $module_path
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 151 | return $locale_path; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 62 | ▶ | sub _lookup_translation_by_count { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_lookup_translation_by_count: called CC=14 SCAR=31.1
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 62 | my ($self, $count, $original, $translated, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 62 | ▶ | $translated = $translated->{zero} if $count == 0 and exists $translated->{zero}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$count == 0 and exists $translated->{"zero"}
MC/DC0%0/2
$count == 0 and exists $translated->{"zero"}
$count == 0
exists $translated->{"zero"}
Truth table33%1/3
A and B
A= $count == 0B= exists $translated->{"zero"}
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 62 | ▶ | $translated = $translated->{one} if $count == 1 and exists $translated->{one}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$count == 1 and exists $translated->{"one"}
MC/DC50%1/2
$count == 1 and exists $translated->{"one"}
$count == 1
exists $translated->{"one"}
Truth table66%2/3
A and B
A= $count == 1B= exists $translated->{"one"}
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 62 | ▶ | $translated = $translated->{other} if $count > 1 and exists $translated->{other}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$count > 1 and exists $translated->{"other"}
MC/DC50%1/2
$count > 1 and exists $translated->{"other"}
$count > 1
exists $translated->{"other"}
Truth table66%2/3
A and B
A= $count > 1B= exists $translated->{"other"}
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 62 | ▶ | $translated = $translated->{many} if $count > 1 and ref($translated) and exists $translated->{many}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$count > 1 and ref $translated and exists $translated->{"many"}
$count > 1 and ref $translated
MC/DC0%0/3
$count > 1 and ref $translated and exists $translated->{"many"}
$count > 1
ref $translated
exists $translated->{"many"}
Truth table50%2/4
A and B and C
A= $count > 1B= ref $translatedC= exists $translated->{"many"}
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | # if we got here check for a _default | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 62 | ▶ | if(ref($translated)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $translated = $translated->{_default} if exists $translated->{_default}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 62 | ▶ | throw_exception('MissingCountKey', tag=>$original, count=>$count) if ref $translated; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # Ok, need to do any variable subsitutions again. Just stole this from | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # Data::Localize::Format::NamedArgs | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # TODO this has an error when $args{$1} is 0 | ||||||||||||||||||||||||||||||||||||||||||
| 100 | #$translated =~ s/\{\{([^}]+)\}\}/ $args{$1} || '' /gex; | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 62 | ▶ | $translated =~ s/\{\{([^}]+)\}\}/ defined($args{$1}) ? $args{$1}: '' /gex; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 62 | debug 1, "Resolved count translation; $translated"; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 62 | return $translated; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1029 | ▶ | sub translate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
translate: called CC=24 SCAR=34.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1029 | my ($self, $key, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 1029 | ▶ | my @defaults = @{ delete($args{default})||[] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{'default'} || []
MC/DC0%0/1
delete $args{'default'} || []
delete $args{'default'}
Truth table50%1/2
A
A= delete $args{'default'}
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 1029 | ▶ | my $scope = delete($args{scope})||''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{'scope'} || ''
MC/DC100%1/1
delete $args{'scope'} || ''
delete $args{'scope'}
Truth table100%2/2
A
A= delete $args{'scope'}
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1029 | my $count = $args{count}; | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | # TODO work around 0 count bug in Data::Localize until I can get a fix in | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 1029 | ▶ | $args{count} = 'zero' if defined($count) && $count == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $count and $count == 0
MC/DC100%2/2
defined $count and $count == 0
defined $count
$count == 0
Truth table100%3/3
A and B
A= defined $countB= $count == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | $scope = join('.', @{$scope}) if (ref($scope)||'') eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $scope || ''
MC/DC0%0/1
ref $scope || ''
ref $scope
Truth table50%1/2
A
A= ref $scope
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | # TODO deal with $count | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # $key can be either a string or a tag. | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 1029 | ▶ | $key = $$key if $self->is_i18n_tag($key); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 1029 | ▶ | $key = "${scope}.${key}" if $scope; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 1029 | debug 1 , "Trying to translate '$key' with defaults: [@{[ join ',', @defaults]}]"; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 1029 | my $translated = $self->dl->localize($key, \%args); | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | # If $translated is a hashref that means we need to apply the $count | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 1029 | ▶ | $translated = $self->_lookup_translation_by_count($count, $key, $translated, %args) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $translated and defined $count
MC/DC0%0/2
ref $translated and defined $count
ref $translated
defined $count
Truth table33%1/3
A and B
A= ref $translatedB= defined $count
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | if ref($translated) && defined($count); | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | # If we still have a hashref, check for a default | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 1029 | ▶ | if(ref($translated)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $translated = $translated->{_default} if exists $translated->{_default}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | # If we still have a hashref that means we failed to figure out which of | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # the options to use so throw an error | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 1029 | ▶ | throw_exception('MissingCountKey', tag=>$key, count=>$count) if ref $translated; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # Is this a bug in Data::Localize? Seems like ->localize just returns | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # the $key if it fails to actually localize. I would think it should | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # return undef; | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 1029 | debug 1, "Proposed translation: '$translated'"; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 1029 | ▶ | unless($translated eq $key) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | debug 1, "Translated '$key' to '$translated'"; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | return $translated; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 1028 | ▶ | return $translated unless $translated eq $key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | # Ok if we got here that means the $key failed to localize. So we will | ||||||||||||||||||||||||||||||||||||||||||
| 154 | # iterate over $args{defaults}. If a defaut is a tag we try to localize | ||||||||||||||||||||||||||||||||||||||||||
| 155 | # it. First tag to localize is returned. If however we encounter a | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # default that is not a tag we just return that without trying to localize | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # it. So you should stick your ultimate fallback string at the very end | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # of the defaults list. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 1028 | debug 1, "Trying to translate defaults: [@{[ join ',', @defaults]}]"; | |||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 1028 | foreach my $default(@defaults) { | |||||||||||||||||||||||||||||||||||||||||
| 163 | 2778 | debug 2, "Trying to translate default: '$default'"; | |||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 2778 | ▶ | unless ($self->is_i18n_tag($default)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 630 | debug 1, "'$default' is just a string, so using that."; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 630 | return $default; | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 2148 | my $tag = $$default; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 2148 | my $translated = $self->dl->localize($tag, \%args); | |||||||||||||||||||||||||||||||||||||||||
| 172 | 2148 | ▶ | $translated = $self->_lookup_translation_by_count($count, $tag, $translated, %args) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $translated and defined $count
MC/DC100%2/2
ref $translated and defined $count
ref $translated
defined $count
Truth table100%3/3
A and B
A= ref $translatedB= defined $count
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | if ref($translated) and defined($count); | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | # If we still have a hashref, check for a default | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 2148 | ▶ | $translated = $translated->{_default} if ref($translated); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | # If we still have a hashref that means we failed to figure out which of | ||||||||||||||||||||||||||||||||||||||||||
| 179 | # the options to use so throw an error | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 2148 | ▶ | throw_exception('MissingCountKey', tag=>$key, count=>$count) if ref $translated; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 2148 | debug 2, "Proposed translation: '$translated'"; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 2148 | ▶ | unless($translated eq $tag) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 398 | debug 1, "Translated '$default' to '$translated'"; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 398 | return $translated; | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | # If we get this far that means the key and defaults can't be translated. So return | ||||||||||||||||||||||||||||||||||||||||||
| 190 | # message if it exists as a raw untranslated string. | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | #return $message if $message; | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | my $list = join (', ', $key, map { $$_ if $self->is_i18n_tag($_) } @defaults); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | my $path = join ',', $self->valid_paths; | |||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | throw_exception General => (msg=>"Can't find a translation for key in ($list) at paths ($path)"); | |||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | sub valid_paths { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
valid_paths: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | return grep { $locale_paths{$_} > 0 } keys %locale_paths; | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | sub detect_languages_from_header { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
detect_languages_from_header: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | my ($class, $header) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | return $class->dl->detect_languages_from_header($header); | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub set_languages { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_languages: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | my ($class, @languages) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | $class->dl->set_languages(@languages); | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 4336 | ▶ | sub is_i18n_tag { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_i18n_tag: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 4336 | my ($class, $tag) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 4336 | ▶ | return (ref($tag)||'') eq 'Valiant::I18N::Tag' ? 1:0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $tag || ''
MC/DC100%1/1
ref $tag || ''
ref $tag
Truth table100%2/2
A
A= ref $tag
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 5268 | ▶ | sub make_tag($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
make_tag: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 5268 | my ($class, $tag) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 5268 | return bless \$tag, 'Valiant::I18N::Tag'; | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | package Valiant::I18N::Tag; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 44 | ▶ | use overload ( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 44 | ne => \¬_equals, | |||||||||||||||||||||||||||||||||||||||||
| 228 | eq => \&equals, | ||||||||||||||||||||||||||||||||||||||||||
| 229 | bool => \&is_true, | ||||||||||||||||||||||||||||||||||||||||||
| 230 | '""' => \&stringify, | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 44 | ); | |||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | 8726 | ▶ | sub stringify { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stringify: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 8726 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 235 | 8726 | return $$self; | |||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 13 | ▶ | sub is_true { return ${$_[0]} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_true: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 8 | ▶ | sub equals { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
equals: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 8 | my ($self, $target) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 8 | return $$self eq "$target"; | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 5 | ▶ | sub not_equals { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
not_equals: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 5 | my ($self, $target) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 5 | return $$self ne "$target"; | |||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 44 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | Valiant::I18N - Translations | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | package Example::Person; | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | use Valiant::I18N; | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | my $tag = _t 'translation_tag'; | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | my $translated_string = Valiant::I18N->translate($tag, %args); | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | You should see L<Valiant> for some details on how this is used with the core | ||||||||||||||||||||||||||||||||||||||||||
| 269 | validations code. | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | This package defines the following class methods | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | =head2 translate ($key, %args) | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | Given a translation tag will attempt to return a translated string. Also accepts arguments as | ||||||||||||||||||||||||||||||||||||||||||
| 278 | a hash of values. The follow hash keys has special meaning (all others will be passed as placeholder | ||||||||||||||||||||||||||||||||||||||||||
| 279 | values for the translation string). | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | =item default | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | An array of translation tags to check if the passed key doesn't resolve. First one | ||||||||||||||||||||||||||||||||||||||||||
| 286 | to resolve gets returned. | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | =item scope | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | key namespace that will be prepended to the translation tag for lookup. | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | =item count | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | an integer to specify pluralization subkeys. | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | =head2 make_tag ($string) | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | Given a string, returns it blessed as a translation tag (the same thing the | ||||||||||||||||||||||||||||||||||||||||||
| 301 | exported C<_t> function creates). Translation tags are how Valiant tells | ||||||||||||||||||||||||||||||||||||||||||
| 302 | "this string should be localized" apart from plain strings. | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | =head2 is_i18n_tag ($value) | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | Returns true if the given value is a translation tag (as created by L</make_tag> | ||||||||||||||||||||||||||||||||||||||||||
| 307 | or C<_t>), false otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | =head2 set_languages (@languages) | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | Sets the list of languages (in preference order) used to resolve translations. | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | =head2 detect_languages_from_header ($header) | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | Given an HTTP C<Accept-Language> header value, detects and sets the languages | ||||||||||||||||||||||||||||||||||||||||||
| 316 | used to resolve translations. | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | L<Valiant> | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | See L<Valiant> | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | See L<Valiant> | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | =cut | ||||||||||||||||||||||||||||||||||||||||||