| 1 | package Catmandu::I18N; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use Catmandu::Sane; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use Catmandu::Util qw(:check :is :array); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Catmandu; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Catmandu::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | has fallback_languages => ( | ||||||||||||||||||||||||||||
| 10 | is => "ro", | ||||||||||||||||||||||||||||
| 11 | 8 | ▶ | isa => sub { check_array_ref( $_[0] ); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | lazy => 1, | ||||||||||||||||||||||||||||
| 13 | 5 | ▶ | default => sub { [ "i-default", "en", "en-US" ]; } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has config => ( | ||||||||||||||||||||||||||||
| 17 | is => "ro", | ||||||||||||||||||||||||||||
| 18 | 8 | ▶ | isa => sub { check_hash_ref( $_[0] ); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | lazy => 1, | ||||||||||||||||||||||||||||
| 20 | 8 | ▶ | coerce => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 21 | 8 | my $c = $_[0]; | |||||||||||||||||||||||||||
| 22 | 8 | ▶ | return $c if is_hash_ref($c); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | if( is_string($c) ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | $c = Catmandu::Util::data_at( $c, Catmandu->config() ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 3 | $c; | |||||||||||||||||||||||||||
| 29 | }, | ||||||||||||||||||||||||||||
| 30 | 3 | required => 1 | |||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | has on_failure => ( | ||||||||||||||||||||||||||||
| 34 | is => "ro", | ||||||||||||||||||||||||||||
| 35 | 7 | ▶ | isa => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 36 | 7 | ▶ | check_string( $_[0] ) && array_includes([qw(undef auto die)],$_[0]) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
check_string($_[0]) and array_includes(["undef", "auto", "die"], $_[0])
MC/DC0%0/2
check_string($_[0]) and array_includes(["undef", "auto", "die"], $_[0])
check_string($_[0])
array_includes(["undef", "auto", "die"], $_[0])
Truth table33%1/3
A and B
A= check_string($_[0])B= array_includes(["undef", "auto", "die"], $_[0])
| |||||||||||||||||||||||||||||
| 37 | die("should be either 'undef','auto' or 'die'"); | ||||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||
| 39 | lazy => 1, | ||||||||||||||||||||||||||||
| 40 | 3 | ▶ | default => sub { "undef"; } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 3 | ); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3 | has handle => ( | |||||||||||||||||||||||||||
| 44 | is => "lazy", | ||||||||||||||||||||||||||||
| 45 | init_arg => undef | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | our $IDX = 0; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 7 | ▶ | sub _build_handle { | ||||||||||||||||||||||||||
|
Subroutine
_build_handle: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 7 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 7 | my $fallback_languages = "(".join( ",", map { "\"$_\"" } @{ $self->fallback_languages() } ).")"; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 7 | my $c = Data::Dumper->new( [$self->config()] )->Deepcopy(1)->Indent(0)->Dump(); | |||||||||||||||||||||||||||
| 57 | 7 | $c = substr( $c, index($c, "{" ) ); | |||||||||||||||||||||||||||
| 58 | 7 | $c =~ s/;$//o; | |||||||||||||||||||||||||||
| 59 | 7 | my $package_name = "Catmandu::I18N::_Handle::$IDX"; | |||||||||||||||||||||||||||
| 60 | 7 | my $perl = <<EOF; | |||||||||||||||||||||||||||
| 61 | package $package_name; | ||||||||||||||||||||||||||||
| 62 | use Catmandu::Sane; | ||||||||||||||||||||||||||||
| 63 | use parent "Locale::Maketext"; | ||||||||||||||||||||||||||||
| 64 | use Locale::Maketext::Lexicon $c; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub fallback_languages { | ||||||||||||||||||||||||||||
| 67 | my \@langs = $fallback_languages; | ||||||||||||||||||||||||||||
| 68 | \@langs; | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | sub fallback_language_classes {} | ||||||||||||||||||||||||||||
| 71 | EOF | ||||||||||||||||||||||||||||
| 72 | 7 | ▶ | if($self->on_failure() eq "auto"){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | $perl .= <<EOF; | |||||||||||||||||||||||||||
| 75 | our %Lexicon; | ||||||||||||||||||||||||||||
| 76 | \$Lexicon{_AUTO} = 1; | ||||||||||||||||||||||||||||
| 77 | EOF | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 7 | $perl .= "1;"; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 3 | ▶ | eval $perl or Catmandu::Error->throw( $@ ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
fallback_language_classes: called CC=1 SCAR=0.0
Subroutine
fallback_language_classes: called CC=1 SCAR=0.0
Subroutine
fallback_languages: called CC=1 SCAR=0.0
Subroutine
fallback_languages: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 7 | $IDX++; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 7 | $package_name; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 40 | ▶ | sub t { | ||||||||||||||||||||||||||
|
Subroutine
t: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 40 | my( $self, $lang, @args ) = @_; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 40 | ▶ | Catmandu::Error->throw( "no lang provided" ) unless is_string( $lang ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 41 | ▶ | if( scalar(@{ $self->fallback_languages() }) == 0 && !exists( $self->config()->{$lang} ) ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @{$self->fallback_languages;} == 0 and not exists $self->config->{$lang}
MC/DC100%2/2
scalar @{$self->fallback_languages;} == 0 and not exists $self->config->{$lang}
scalar @{$self->fallback_languages;} == 0
not exists $self->config->{$lang}
Truth table100%3/3
A and not(B)
A= scalar @{$self->fallback_languages;} == 0B= not exists $self->config->{$lang}
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | Catmandu::Error->throw( "language $lang not found in config, and array of fallback_languages is empty" ); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 38 | my $lh = $self->handle()->get_handle( $lang ); | |||||||||||||||||||||||||||
| 103 | 38 | ▶ | Catmandu::Error->throw( "unable to find handle for language $lang" ) unless defined( $lh ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 38 | ▶ | $lh->fail_with(sub {}) if $self->on_failure() eq "undef"; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 38 | $lh->maketext( @args ); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | our $VERSION = "0.01"; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =encoding utf8 | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 NAME | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Catmandu::I18N - tools for text localisation | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =begin markdown | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # STATUS | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | [](https://travis-ci.org/LibreCat/Catmandu-I18N) | ||||||||||||||||||||||||||||
| 123 | [](https://coveralls.io/r/LibreCat/Catmandu-I18N) | ||||||||||||||||||||||||||||
| 124 | [](https://cpants.cpanauthors.org/dist/Catmandu-I18N) | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =end markdown | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | use Catmandu::Sane; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | use Catmandu::I18N; | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | my $i = Catmandu::I18N->new( | ||||||||||||||||||||||||||||
| 135 | config => { | ||||||||||||||||||||||||||||
| 136 | en => [ | ||||||||||||||||||||||||||||
| 137 | "Gettext", | ||||||||||||||||||||||||||||
| 138 | "/path/to/en.po" | ||||||||||||||||||||||||||||
| 139 | ], | ||||||||||||||||||||||||||||
| 140 | nl => [ | ||||||||||||||||||||||||||||
| 141 | "Gettext", | ||||||||||||||||||||||||||||
| 142 | "/path/to/nl.po" | ||||||||||||||||||||||||||||
| 143 | ] | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | $i->t( "my-lang", "my-key" ); | ||||||||||||||||||||||||||||
| 148 | $i->t( "my-lang", "my-key2", "arg-1", "arg-2" ); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 CONSTRUCTOR ARGUMENTS | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =over | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =item config | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Configuration for Locale::Maketext. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Must be either: | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | * hash reference | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | * string (e.g. "i18n") | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | When the config is a string, it is interpreted as the path to the I18N configuration in Catmandu config. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Required | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item on_failure | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | What to do when a lookup does not give a result. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Possible values: | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | * "undef" : return undef | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | * "auto" : return key itself. Always a result. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | * "die" : die | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Default: "undef" | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Note: "undef" should be a string, as opposed to undef. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =item fallback_languages | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | array of fallback language codes | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | * must be array reference | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | * default is [ "i-default","en","en-US" ] as determined by L<Locale::Maketext> | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | When L<Locale::Maketext> does not find the specified language in your config, it | ||||||||||||||||||||||||||||
| 193 | will fallback to one of these, and then load the handle for that. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Only if that fallback language does not exist in the config, will it fail. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Example 1: | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | fallback_languages is [ "en" ] | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | you have only language "en" in your config, but you request language "nl", | ||||||||||||||||||||||||||||
| 202 | then you'll get the message in English. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Example 2: | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | fallback_languages is [ "en" ] | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | you have only language "nl" in your config, but you request language "fr", | ||||||||||||||||||||||||||||
| 209 | then the creation of the message will fail | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | You can set this to an empty array for consistent behaviour. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =back | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head1 NOTES | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | * the lexicon implementation determines the format of the message. | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | e.g. only L<Locale::Maketext::Lexicon::Gettext> supports placeholders like %1. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | For other implementations you need to use placeholders like [_1] | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Nicolas Franck C<< <nicolas.franck at ugent.be> >> | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | L<Catmandu::Fix::i18n>, L<Locale::Maketext::Lexicon::CatmanduConfig>, L<Catmandu>, L<Locale::Maketext> | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 234 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 235 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =cut | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 3 | 1; | |||||||||||||||||||||||||||