| 1 | package Lingua::Text; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 16 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use Object::Configure; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 16 | ▶ | use Params::Get; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 16 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 16 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 16 | ▶ | use I18N::LangTags::Detect; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | # enforce mode lets Sub::Private work correctly with OO dispatch ($self->_helper). | ||||||||||||||||||||||||||||||||||
| 16 | # Tests bypass the check automatically when HARNESS_ACTIVE is set by prove. | ||||||||||||||||||||||||||||||||||
| 17 | 16 | ▶ | BEGIN { $Sub::Private::config{mode} = 'enforce' } | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 18 | 16 | ▶ | use Sub::Private; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | Lingua::Text - Store the same text in many languages; retrieve it in the user's language automatically | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | =head1 VERSION | ||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | Version 0.09 | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | 16 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||||||||
| 31 | 16 | our $AUTOLOAD; | |||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 34 | # Constants | ||||||||||||||||||||||||||||||||||
| 35 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | # ISO 639-1 language code with optional ISO 3166-1 country suffix (e.g. 'en', 'en_US'). | ||||||||||||||||||||||||||||||||||
| 38 | # The pattern is case-sensitive: lower-case lang, upper-case country. | ||||||||||||||||||||||||||||||||||
| 39 | # /x lets us annotate each component; spaces and # comments inside /x are ignored. | ||||||||||||||||||||||||||||||||||
| 40 | 16 | Readonly::Scalar my $LANG_RE => qr/ | |||||||||||||||||||||||||||||||||
| 41 | ^ | ||||||||||||||||||||||||||||||||||
| 42 | [a-z]{2} # ISO 639-1 two-letter language code (e.g. 'en', 'fr') | ||||||||||||||||||||||||||||||||||
| 43 | (?:_[A-Z]{2})? # optional ISO 3166-1 country suffix (e.g. '_US', '_GB') | ||||||||||||||||||||||||||||||||||
| 44 | $ | ||||||||||||||||||||||||||||||||||
| 45 | /x; | ||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | # Error message catalog. All templates accept the package name as first | ||||||||||||||||||||||||||||||||||
| 48 | # sprintf argument so callers never hard-code the package string. | ||||||||||||||||||||||||||||||||||
| 49 | # Capitalised "Usage:" marks programmer errors (croak); lower-case marks | ||||||||||||||||||||||||||||||||||
| 50 | # runtime warnings (carp) -- the carp.t suite relies on this distinction. | ||||||||||||||||||||||||||||||||||
| 51 | 16 | Readonly::Hash my %MESSAGES => ( | |||||||||||||||||||||||||||||||||
| 52 | new_oo_style => '%s: use ->new() not ::new() to instantiate', | ||||||||||||||||||||||||||||||||||
| 53 | set_no_args => '%s: Usage: set(text => $text, lang => $language)', | ||||||||||||||||||||||||||||||||||
| 54 | set_usage => '%s: usage: set(text => $text, lang => $language)', | ||||||||||||||||||||||||||||||||||
| 55 | str_usage => '%s: usage: as_string(lang => $language)', | ||||||||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | use overload ( | ||||||||||||||||||||||||||||||||||
| 59 | '""' => \&as_string, | ||||||||||||||||||||||||||||||||||
| 60 | 2323 | ▶ | bool => sub { 1 }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 61 | 16 | fallback => 1, # let boolean tests bypass as_string | |||||||||||||||||||||||||||||||||
| 62 | 16 | ▶ | ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 65 | # Public API | ||||||||||||||||||||||||||||||||||
| 66 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | =head2 Basic use: store translations and print in the user's language | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | use Lingua::Text; | ||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | my $greeting = Lingua::Text->new( | ||||||||||||||||||||||||||||||||||
| 75 | en => 'Hello', | ||||||||||||||||||||||||||||||||||
| 76 | fr => 'Bonjour', | ||||||||||||||||||||||||||||||||||
| 77 | de => 'Hallo', | ||||||||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | # The system locale (LANG, LC_MESSAGES, etc.) picks the language. | ||||||||||||||||||||||||||||||||||
| 81 | # No code change is needed when the user's OS language changes. | ||||||||||||||||||||||||||||||||||
| 82 | print $greeting, "\n"; | ||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | =head2 Add translations after construction | ||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | my $label = Lingua::Text->new(); | ||||||||||||||||||||||||||||||||||
| 87 | $label->en('Submit'); | ||||||||||||||||||||||||||||||||||
| 88 | $label->fr('Envoyer'); | ||||||||||||||||||||||||||||||||||
| 89 | $label->de('Abschicken'); | ||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||
| 91 | # Ask for a specific language explicitly | ||||||||||||||||||||||||||||||||||
| 92 | print $label->as_string('fr'), "\n"; # Envoyer | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | =head2 Build from a database hash | ||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | # Suppose the database returns: { en => 'Apple', fr => 'Pomme', de => 'Apfel' } | ||||||||||||||||||||||||||||||||||
| 97 | my $row = $db->selectrow_hashref('SELECT en, fr, de FROM fruit WHERE id = ?', {}, $id); | ||||||||||||||||||||||||||||||||||
| 98 | my $name = Lingua::Text->new($row); | ||||||||||||||||||||||||||||||||||
| 99 | print $name; # prints in the user's system language | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | =head2 Add one translation to an existing object (clone) | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | my $base = Lingua::Text->new(en => 'colour', fr => 'couleur'); | ||||||||||||||||||||||||||||||||||
| 104 | my $us = $base->new(en => 'color'); # American English override | ||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | # $us has en => 'color', fr => 'couleur' | ||||||||||||||||||||||||||||||||||
| 107 | # $base is unchanged: en => 'colour', fr => 'couleur' | ||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | =head2 Store text at runtime using a language variable | ||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | my $message = Lingua::Text->new(); | ||||||||||||||||||||||||||||||||||
| 112 | my $user_lang = 'de'; # could come from a web request header | ||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | $message->set(text => 'Willkommen', lang => $user_lang); | ||||||||||||||||||||||||||||||||||
| 115 | print $message->as_string($user_lang), "\n"; # Willkommen | ||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | =head2 Prepare multilingual text for HTML output | ||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | my $title = Lingua::Text->new( | ||||||||||||||||||||||||||||||||||
| 120 | en => 'read more', | ||||||||||||||||||||||||||||||||||
| 121 | fr => "lire la suite", | ||||||||||||||||||||||||||||||||||
| 122 | )->encode(); # converts accented characters to HTML entities | ||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | print "<title>$title</title>"; # safe for browsers in any locale | ||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | Lingua::Text is a simple container that stores the same piece of text in | ||||||||||||||||||||||||||||||||||
| 129 | multiple languages inside a single Perl object. When you ask the object to | ||||||||||||||||||||||||||||||||||
| 130 | print itself (or convert to a string), it automatically reads the user's | ||||||||||||||||||||||||||||||||||
| 131 | operating-system language setting and returns the correct translation. | ||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||
| 133 | =head2 How language detection works | ||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||
| 135 | The module reads the following environment variables, in this order, and uses | ||||||||||||||||||||||||||||||||||
| 136 | the first one it finds that contains a two-letter language code: | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | =over 4 | ||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||
| 140 | =item 1. C<LANGUAGE> (space- or colon-separated preference list, e.g. C<fr:en>) | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | =item 2. C<LC_ALL> | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | =item 3. C<LC_MESSAGES> | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | =item 4. C<LANG> (e.g. C<en_US.UTF-8> or C<de_DE>) | ||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | =back | ||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||
| 150 | In CGI or web environments, C<I18N::LangTags::Detect> also inspects the | ||||||||||||||||||||||||||||||||||
| 151 | C<HTTP_ACCEPT_LANGUAGE> request header (when available in the environment) | ||||||||||||||||||||||||||||||||||
| 152 | before falling back to the variables above. The memoisation cache tracks | ||||||||||||||||||||||||||||||||||
| 153 | this variable alongside the four above, so the cache is invalidated | ||||||||||||||||||||||||||||||||||
| 154 | correctly when the header changes between requests. | ||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||
| 156 | Only the first two letters are used (e.g. C<en_GB.UTF-8> becomes C<en>), so | ||||||||||||||||||||||||||||||||||
| 157 | you only need to store one translation per language, not per locale variant. | ||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | If none of these variables is set, or if C<LANG> is set to the POSIX C<C> | ||||||||||||||||||||||||||||||||||
| 160 | or C<C.UTF-8> locale, the module returns C<undef> (or C<'en'> for the C | ||||||||||||||||||||||||||||||||||
| 161 | locale). | ||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||
| 163 | =head2 How string interpolation works | ||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | The object overloads Perl's C<""> (stringify) operator. This means you can | ||||||||||||||||||||||||||||||||||
| 166 | use a Lingua::Text object directly in strings: | ||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | print "Hello: $greeting\n"; | ||||||||||||||||||||||||||||||||||
| 169 | my $html = "<h1>" . $title . "</h1>"; | ||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | You do B<not> need to call C<as_string()> yourself in most cases. | ||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||
| 173 | =head2 Language code format | ||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||
| 175 | Language codes follow ISO 639-1: two lower-case letters, e.g. C<en>, C<fr>, | ||||||||||||||||||||||||||||||||||
| 176 | C<de>, C<es>, C<zh>. An optional ISO 3166-1 country suffix may follow an | ||||||||||||||||||||||||||||||||||
| 177 | underscore: C<en_US>, C<zh_CN>. The module stores and retrieves by the | ||||||||||||||||||||||||||||||||||
| 178 | two-letter code only; the country part of an environment variable is ignored | ||||||||||||||||||||||||||||||||||
| 179 | during lookup. | ||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||
| 181 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||
| 183 | =head2 new | ||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||
| 185 | Create a new Lingua::Text object. The object starts empty, or pre-loaded | ||||||||||||||||||||||||||||||||||
| 186 | with one or more translations you provide. | ||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | Calling C<new()> on an B<existing> object creates an independent copy (a | ||||||||||||||||||||||||||||||||||
| 189 | clone) with any extra translations you supply merged in. The original object | ||||||||||||||||||||||||||||||||||
| 190 | is never modified. | ||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||
| 192 | =head3 ARGUMENTS | ||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||
| 194 | You may pass arguments in any of the following forms: | ||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||
| 196 | =over 4 | ||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||
| 198 | =item No arguments -- creates an empty object | ||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||
| 200 | my $t = Lingua::Text->new(); | ||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||
| 202 | =item Key/value pairs -- one pair per language | ||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||
| 204 | my $t = Lingua::Text->new(en => 'boat', fr => 'bateau'); | ||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||
| 206 | =item A hash reference | ||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||
| 208 | my $t = Lingua::Text->new({ en => 'boat', fr => 'bateau' }); | ||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||
| 210 | =item A single plain string -- stored under the current system locale language | ||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||
| 212 | # Only works if a locale environment variable is set. | ||||||||||||||||||||||||||||||||||
| 213 | # If no locale is set, the text is silently discarded. See COMMON PITFALLS. | ||||||||||||||||||||||||||||||||||
| 214 | my $t = Lingua::Text->new('hello'); | ||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||
| 216 | =item An existing Lingua::Text object -- creates a clone | ||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||
| 218 | my $clone = $original->new(); # exact copy | ||||||||||||||||||||||||||||||||||
| 219 | my $clone = $original->new(de => 'zusatz'); # copy + one more language | ||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||
| 221 | =back | ||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||
| 223 | =head3 RETURN VALUE | ||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||
| 225 | Returns a blessed Lingua::Text reference on success. | ||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||
| 227 | Returns C<undef> and emits a C<carp> warning when called as a plain function | ||||||||||||||||||||||||||||||||||
| 228 | with arguments (e.g. C<Lingua::Text::new(en => 'hi')> without the arrow). | ||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||
| 230 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||
| 232 | use Lingua::Text; | ||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||
| 234 | # Empty object | ||||||||||||||||||||||||||||||||||
| 235 | my $t = Lingua::Text->new(); | ||||||||||||||||||||||||||||||||||
| 236 | $t->en('Good morning'); | ||||||||||||||||||||||||||||||||||
| 237 | $t->de('Guten Morgen'); | ||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||
| 239 | # Pre-populated from a hash reference | ||||||||||||||||||||||||||||||||||
| 240 | my %translations = (en => 'cat', fr => 'chat', de => 'Katze'); | ||||||||||||||||||||||||||||||||||
| 241 | my $animal = Lingua::Text->new(\%translations); | ||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||
| 243 | # Clone and override the English translation | ||||||||||||||||||||||||||||||||||
| 244 | my $us = $animal->new(en => 'kitty'); | ||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||
| 246 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||
| 248 | =head4 Input | ||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||
| 250 | # Params::Validate::Strict-compatible schema | ||||||||||||||||||||||||||||||||||
| 251 | # (all parameters are optional; the whole argument list may be empty) | ||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||
| 253 | en => { type => 'string', optional => 1 } # and any other ISO 639-1 code | ||||||||||||||||||||||||||||||||||
| 254 | fr => { type => 'string', optional => 1 } | ||||||||||||||||||||||||||||||||||
| 255 | ... # one key per language | ||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||
| 257 | # OR a single HASHREF containing the same keys | ||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||
| 259 | # OR a single SCALAR (stored under the current locale language) | ||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||
| 261 | =head4 Language key domain | ||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||
| 263 | Keys passed to C<new()> are stored verbatim in the internal C<{texts}> hash | ||||||||||||||||||||||||||||||||||
| 264 | without validation at construction time. Access restrictions come from the | ||||||||||||||||||||||||||||||||||
| 265 | consumers: | ||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||
| 267 | =over 4 | ||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||
| 269 | =item * | ||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||
| 271 | B<AUTOLOAD accessor> -- only installs an accessor for keys matching | ||||||||||||||||||||||||||||||||||
| 272 | C</^[a-z]{2}$/>: exactly two lower-case ASCII letters. C<en_US> (with | ||||||||||||||||||||||||||||||||||
| 273 | country suffix), C<eng> (three letters), C<EN> (upper-case) and all other | ||||||||||||||||||||||||||||||||||
| 274 | keys are silently ignored by the accessor mechanism. | ||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||
| 276 | =item * | ||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||
| 278 | B<encode()> -- only processes keys accepted by C<_is_valid_language()>: | ||||||||||||||||||||||||||||||||||
| 279 | C</^[a-z]{2}(?:_[A-Z]{2})?$/>. Keys with C<en_US>-style country suffixes | ||||||||||||||||||||||||||||||||||
| 280 | are encoded; non-language keys injected by L<Object::Configure> (e.g. | ||||||||||||||||||||||||||||||||||
| 281 | C<logger>, C<config_path>) are skipped. | ||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||
| 283 | =back | ||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||
| 285 | Equivalence partitions for language keys | ||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||
| 287 | PARTITION EXAMPLE AUTOLOAD encode() | ||||||||||||||||||||||||||||||||||
| 288 | -------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 289 | 2-lower (valid) 'en' YES YES | ||||||||||||||||||||||||||||||||||
| 290 | 2-lower + _XX 'en_US' NO YES | ||||||||||||||||||||||||||||||||||
| 291 | 1 char 'e' NO NO | ||||||||||||||||||||||||||||||||||
| 292 | 3+ chars 'eng' NO NO | ||||||||||||||||||||||||||||||||||
| 293 | uppercase 'EN' NO NO | ||||||||||||||||||||||||||||||||||
| 294 | non-alpha '12' NO NO | ||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||
| 296 | =head4 Output | ||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||
| 298 | # Return::Set schema | ||||||||||||||||||||||||||||||||||
| 299 | on_success => Lingua::Text, # blessed reference | ||||||||||||||||||||||||||||||||||
| 300 | on_misuse => undef # with carp warning | ||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||
| 302 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||
| 304 | =over 4 | ||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||
| 306 | =item C<"use -E<gt>new() not ::new() to instantiate"> [carp] | ||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||
| 308 | You called C<Lingua::Text::new(...)> (double-colon, function style) with | ||||||||||||||||||||||||||||||||||
| 309 | arguments. Use C<Lingua::Text-E<gt>new(...)> (arrow, method style) instead. | ||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||
| 311 | =back | ||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||
| 313 | =cut | ||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||
| 315 | sub new { | ||||||||||||||||||||||||||||||||||
| 316 | 559 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||
| 318 | # Detect function-style misuse: Lingua::Text::new(args...) | ||||||||||||||||||||||||||||||||||
| 319 | # A legitimate invocant is either a blessed object (clone) or a class name | ||||||||||||||||||||||||||||||||||
| 320 | # that inherits from this package. Anything else (e.g. the first key of a | ||||||||||||||||||||||||||||||||||
| 321 | # hash accidentally consumed as the invocant) triggers a warning. | ||||||||||||||||||||||||||||||||||
| 322 | # SECURITY: call UNIVERSAL::isa() as a function, not as a method on $class. | ||||||||||||||||||||||||||||||||||
| 323 | # Method dispatch on an attacker-controlled string would invoke any isa() | ||||||||||||||||||||||||||||||||||
| 324 | # defined on that package; the function form bypasses method resolution. | ||||||||||||||||||||||||||||||||||
| 325 | 559 | ▶ | my $is_class = !ref($class) && defined($class) && eval { UNIVERSAL::isa($class, __PACKAGE__) }; | ||||||||||||||||||||||||||||||||
|
Condition100%6/6
!ref($class) && defined $class && eval {
not ref $class and defined $class
MC/DC100%3/3
!ref($class) && defined $class && eval {
do {
UNIVERSAL::isa($class, __PACKAGE__)
}
}
not ref $class
defined $class
eval {
do {
UNIVERSAL::isa($class, __PACKAGE__)
}
}
Truth table100%4/4
not(A) and B && C
A= not ref $classB= defined $classC= eval {
do {
UNIVERSAL::isa($class, __PACKAGE__)
}
}
| |||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||
| 327 | # A valid object invocant must be a Lingua::Text instance (or subclass), | ||||||||||||||||||||||||||||||||||
| 328 | # not just any blessed reference. A foreign blessed object (e.g. from | ||||||||||||||||||||||||||||||||||
| 329 | # a completely unrelated package) would pass `blessed()` but crash the clone | ||||||||||||||||||||||||||||||||||
| 330 | # path at `%{$class->{'texts'}}` because it has no 'texts' slot. | ||||||||||||||||||||||||||||||||||
| 331 | # UNIVERSAL::isa() as a function is used (not method dispatch) for the same | ||||||||||||||||||||||||||||||||||
| 332 | # security reason as above. | ||||||||||||||||||||||||||||||||||
| 333 | 559 | ▶ | my $is_object = blessed($class) && UNIVERSAL::isa($class, __PACKAGE__); | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
builtin::blessed($class) && UNIVERSAL::isa($class, __PACKAGE__)
MC/DC100%2/2
builtin::blessed($class) && UNIVERSAL::isa($class, __PACKAGE__)
builtin::blessed($class)
UNIVERSAL::isa($class, __PACKAGE__)
Truth table100%3/3
A && B
A= builtin::blessed($class)B= UNIVERSAL::isa($class, __PACKAGE__)
| |||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||
| 335 | # Syllogism: | ||||||||||||||||||||||||||||||||||
| 336 | # P1: A valid invocant is a Lingua::Text blessed object (clone path) or | ||||||||||||||||||||||||||||||||||
| 337 | # a class name that inherits from Lingua::Text (isa check). | ||||||||||||||||||||||||||||||||||
| 338 | # P2: !defined($class) implies both $is_class and $is_object are false | ||||||||||||||||||||||||||||||||||
| 339 | # (each requires defined($class) in their own derivation). | ||||||||||||||||||||||||||||||||||
| 340 | # Conclusion: De Morgan reduction -- the original | ||||||||||||||||||||||||||||||||||
| 341 | # "!defined || (!class && !object)" | ||||||||||||||||||||||||||||||||||
| 342 | # is logically equivalent to "!($is_object || $is_class)". | ||||||||||||||||||||||||||||||||||
| 343 | 559 | ▶ | unless($is_object || $is_class) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$is_object or $is_class
MC/DC100%2/2
$is_object or $is_class
$is_object
$is_class
Truth table100%3/3
A or B
A= $is_objectB= $is_class
| |||||||||||||||||||||||||||||||||||
| 344 | # undef invocant = bare Lingua::Text::new() with no args -- allowed | ||||||||||||||||||||||||||||||||||
| 345 | 18 | ▶ | if(defined($class)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 346 | 11 | Carp::carp(_err('new_oo_style')); | |||||||||||||||||||||||||||||||||
| 347 | 11 | return; | |||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||
| 349 | 7 | $class = __PACKAGE__; | |||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||
| 352 | # Build the initial parameter hash from whatever form the caller used. | ||||||||||||||||||||||||||||||||||
| 353 | # A single non-ref scalar is the positional form: store under the locale lang | ||||||||||||||||||||||||||||||||||
| 354 | # if one is detected, or silently discard it when no locale is set (the | ||||||||||||||||||||||||||||||||||
| 355 | # documented pitfall). We must NOT let it reach get_params(), which croaks | ||||||||||||||||||||||||||||||||||
| 356 | # on an odd-length list (get_params(undef, $scalar) = 1 arg = odd). | ||||||||||||||||||||||||||||||||||
| 357 | 548 | my $params; | |||||||||||||||||||||||||||||||||
| 358 | 548 | ▶ | if((scalar(@_) == 1) && (!ref($_[0]))) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
scalar @_ == 1 and not ref $_[0]
MC/DC100%2/2
scalar @_ == 1 and not ref $_[0]
scalar @_ == 1
not ref $_[0]
Truth table100%3/3
A and not(B)
A= scalar @_ == 1B= not ref $_[0]
| |||||||||||||||||||||||||||||||||||
| 359 | 17 | my $lang = _get_language(); | |||||||||||||||||||||||||||||||||
| 360 | 17 | ▶ | $params = { $lang => $_[0] } if $lang; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 361 | } elsif(@_) { | ||||||||||||||||||||||||||||||||||
| 362 | 352 | $params = Params::Get::get_params(undef, \@_); | |||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||
| 365 | # Clone path: caller is an existing object | ||||||||||||||||||||||||||||||||||
| 366 | 548 | ▶ | if($is_object) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 367 | 27 | ▶ | if($params) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 368 | 18 | return bless { texts => {%{$class->{'texts'}}, %{$params}} }, ref($class); | |||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||
| 370 | 9 | return bless { %{$class} }, ref($class); | |||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||
| 373 | # Allow Object::Configure to inject defaults (e.g. from a config file) | ||||||||||||||||||||||||||||||||||
| 374 | 521 | $params = Object::Configure::configure($class, $params); | |||||||||||||||||||||||||||||||||
| 375 | 521 | ▶ | return bless( $params ? { texts => $params } : {}, $class ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||
| 378 | =head2 set | ||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||
| 380 | Store or replace a single translation. Use this when the language code is | ||||||||||||||||||||||||||||||||||
| 381 | held in a variable at runtime; if the language is fixed at coding time, the | ||||||||||||||||||||||||||||||||||
| 382 | shorthand accessor (e.g. C<$t-E<gt>en('Hello')>) is simpler. | ||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||
| 384 | =head3 ARGUMENTS | ||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||
| 386 | text -- the translation string (required) | ||||||||||||||||||||||||||||||||||
| 387 | lang -- two-letter language code (optional; defaults to the system locale) | ||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||
| 389 | The arguments may be passed as a hash, a hash reference, or as a plain | ||||||||||||||||||||||||||||||||||
| 390 | positional string (only C<text>; C<lang> then comes from the system locale). | ||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||
| 392 | =head3 RETURN VALUE | ||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||
| 394 | Returns the object itself (C<$self>) on success, so calls may be chained. | ||||||||||||||||||||||||||||||||||
| 395 | Because the object stringifies to its current-locale translation, the return | ||||||||||||||||||||||||||||||||||
| 396 | value often I<looks> like the text you just stored -- but it is the object, | ||||||||||||||||||||||||||||||||||
| 397 | not the string. See L</COMMON PITFALLS>. | ||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||
| 399 | Returns C<undef> and emits a C<carp> warning when required arguments are | ||||||||||||||||||||||||||||||||||
| 400 | missing or when no language can be determined. | ||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||
| 402 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||
| 404 | my $t = Lingua::Text->new(); | ||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||
| 406 | # Hash-reference form | ||||||||||||||||||||||||||||||||||
| 407 | $t->set({ text => 'House', lang => 'en' }); | ||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||
| 409 | # Flat hash form | ||||||||||||||||||||||||||||||||||
| 410 | $t->set(text => 'Maison', lang => 'fr'); | ||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||
| 412 | # Positional: lang comes from the system locale | ||||||||||||||||||||||||||||||||||
| 413 | $t->set('Haus'); # stored under whatever language $ENV{LANG} says | ||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||
| 415 | # Method chaining (set() returns $self) | ||||||||||||||||||||||||||||||||||
| 416 | my $t = Lingua::Text->new()->set(text => 'Hello', lang => 'en') | ||||||||||||||||||||||||||||||||||
| 417 | ->set(text => 'Hola', lang => 'es'); | ||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||
| 419 | # Store the language code from a variable | ||||||||||||||||||||||||||||||||||
| 420 | my $user_lang = 'de'; | ||||||||||||||||||||||||||||||||||
| 421 | $t->set(text => 'Willkommen', lang => $user_lang); | ||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||
| 423 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||
| 425 | =head4 Input | ||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||
| 427 | # Params::Validate::Strict-compatible schema | ||||||||||||||||||||||||||||||||||
| 428 | text => { | ||||||||||||||||||||||||||||||||||
| 429 | type => 'string', | ||||||||||||||||||||||||||||||||||
| 430 | }, | ||||||||||||||||||||||||||||||||||
| 431 | lang => { | ||||||||||||||||||||||||||||||||||
| 432 | type => 'string', | ||||||||||||||||||||||||||||||||||
| 433 | optional => 1, | ||||||||||||||||||||||||||||||||||
| 434 | default => _get_language(), # falls back to system locale | ||||||||||||||||||||||||||||||||||
| 435 | regex => qr/^[a-z]{2}(?:_[A-Z]{2})?$/, | ||||||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||
| 438 | =head4 lang parameter domain (BVA) | ||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||
| 440 | The C<lang> value is validated by the regex C</^[a-z]{2}(?:_[A-Z]{2})?$/>. | ||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||
| 442 | # BOUNDARY VALUE ANALYSIS | ||||||||||||||||||||||||||||||||||
| 443 | # | ||||||||||||||||||||||||||||||||||
| 444 | # PARTITION EXAMPLE VALID? REASON | ||||||||||||||||||||||||||||||||||
| 445 | # ------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 446 | # 1 char (below minimum) 'e' NO too short | ||||||||||||||||||||||||||||||||||
| 447 | # 2 lowercase (MINIMUM) 'en' YES minimum valid length | ||||||||||||||||||||||||||||||||||
| 448 | # 3 lowercase 'eng' NO exceeds simple maximum | ||||||||||||||||||||||||||||||||||
| 449 | # 5 chars with suffix (MAX) 'en_US' YES 2-lower + _ + 2-UPPER | ||||||||||||||||||||||||||||||||||
| 450 | # 6 chars, country 3-long 'en_USA' NO country must be exactly 2 | ||||||||||||||||||||||||||||||||||
| 451 | # 4 chars, country 1-long 'en_U' NO country must be exactly 2 | ||||||||||||||||||||||||||||||||||
| 452 | # trailing underscore only 'en_' NO country code absent | ||||||||||||||||||||||||||||||||||
| 453 | # 2 uppercase 'EN' NO must be lower-case | ||||||||||||||||||||||||||||||||||
| 454 | # mixed case 'En' NO must be lower-case | ||||||||||||||||||||||||||||||||||
| 455 | # 2 digits '12' NO digits not in [a-z] | ||||||||||||||||||||||||||||||||||
| 456 | # empty string '' NO zero length | ||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||
| 458 | =head4 text parameter domain | ||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||
| 460 | Any defined scalar is a valid text value. Length has no documented limit. | ||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||
| 462 | # BOUNDARY VALUE ANALYSIS | ||||||||||||||||||||||||||||||||||
| 463 | # | ||||||||||||||||||||||||||||||||||
| 464 | # PARTITION EXAMPLE VALID? REASON | ||||||||||||||||||||||||||||||||||
| 465 | # --------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 466 | # empty string '' YES length 0 (minimum) | ||||||||||||||||||||||||||||||||||
| 467 | # falsy '0' '0' YES defined, even if falsy | ||||||||||||||||||||||||||||||||||
| 468 | # regular ASCII 'hello' YES representative valid | ||||||||||||||||||||||||||||||||||
| 469 | # Unicode "\x{E9}tude" YES stored verbatim | ||||||||||||||||||||||||||||||||||
| 470 | # 1 MB string 'x' x 1_000_000 YES no documented size limit | ||||||||||||||||||||||||||||||||||
| 471 | # undef undef NO undefined = missing text | ||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||
| 473 | =head4 Output | ||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||
| 475 | # Return::Set schema | ||||||||||||||||||||||||||||||||||
| 476 | on_success => $self, # the same Lingua::Text object | ||||||||||||||||||||||||||||||||||
| 477 | on_no_args => undef # with croak (programmer error) | ||||||||||||||||||||||||||||||||||
| 478 | on_missing_lang => undef # with carp | ||||||||||||||||||||||||||||||||||
| 479 | on_invalid_lang => undef # with carp (lang fails ISO 639-1 check) | ||||||||||||||||||||||||||||||||||
| 480 | on_missing_text => undef # with carp | ||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||
| 482 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||
| 484 | =over 4 | ||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||
| 486 | =item C<"Usage: set(text =E<gt> $text, lang =E<gt> $language)"> [croak] | ||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||
| 488 | C<set()> was called with no arguments at all. Provide at least C<text>. | ||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||
| 490 | =item C<"usage: set(text =E<gt> $text, lang =E<gt> $language)"> [carp] | ||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||
| 492 | One of three conditions: C<text> is missing or undef; C<lang> is missing and | ||||||||||||||||||||||||||||||||||
| 493 | no locale environment variable is set; or C<lang> was supplied but fails the | ||||||||||||||||||||||||||||||||||
| 494 | ISO 639-1 validation check (e.g. a three-letter code like C<'abc'>). | ||||||||||||||||||||||||||||||||||
| 495 | Pass C<lang> as a two-letter code (e.g. C<'en'>) to resolve all three. | ||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||
| 497 | =back | ||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||
| 499 | =cut | ||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||
| 501 | sub set { | ||||||||||||||||||||||||||||||||||
| 502 | 192 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||
| 504 | 192 | ▶ | Carp::croak(_err('set_no_args')) unless @_; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||
| 506 | 185 | my $params = Params::Get::get_params('text', @_); | |||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||
| 508 | # SECURITY: use // (defined-or), not || (truthiness). | ||||||||||||||||||||||||||||||||||
| 509 | # || would silently substitute the system locale when the caller explicitly | ||||||||||||||||||||||||||||||||||
| 510 | # passes lang => '' or lang => '0', masking an invalid-argument error. | ||||||||||||||||||||||||||||||||||
| 511 | 185 | ▶ | my $lang = $params->{'lang'} // _get_language(); | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
$params->{"lang"} // _get_language()
MC/DC100%2/2
$params->{"lang"} // _get_language()
$params->{"lang"}
_get_language()
Truth table100%3/3
A // B
A= $params->{"lang"}B= _get_language()
| |||||||||||||||||||||||||||||||||||
| 512 | 185 | ▶ | return _carp_set_usage() unless defined($lang); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||
| 514 | # Syllogism: | ||||||||||||||||||||||||||||||||||
| 515 | # P1 (object invariant): every key in $self->{texts} must satisfy $LANG_RE. | ||||||||||||||||||||||||||||||||||
| 516 | # P2: $lang was resolved from caller input -- it may be any string. | ||||||||||||||||||||||||||||||||||
| 517 | # Conclusion: validate at the API boundary before writing; reject here to | ||||||||||||||||||||||||||||||||||
| 518 | # maintain the invariant rather than letting bad keys propagate. | ||||||||||||||||||||||||||||||||||
| 519 | 179 | ▶ | return _carp_set_usage() unless _is_valid_language($lang); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||
| 521 | 118 | my $text = $params->{'text'}; | |||||||||||||||||||||||||||||||||
| 522 | 118 | ▶ | return _carp_set_usage() unless defined($text); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||
| 524 | 109 | $self->{'texts'}->{$lang} = $text; | |||||||||||||||||||||||||||||||||
| 525 | 109 | return $self; | |||||||||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||
| 528 | =head2 as_string | ||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||
| 530 | Return the stored translation for a given language, or for the system locale | ||||||||||||||||||||||||||||||||||
| 531 | when no language is specified. | ||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||
| 533 | This method is also invoked automatically whenever Perl needs to convert the | ||||||||||||||||||||||||||||||||||
| 534 | object to a string -- for example inside C<print>, in string concatenation, or | ||||||||||||||||||||||||||||||||||
| 535 | in a comparison like C<$t eq 'hello'>. You rarely need to call it directly. | ||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||
| 537 | =head3 ARGUMENTS | ||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||
| 539 | lang -- two-letter language code (optional; defaults to the system locale) | ||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||
| 541 | Accepted forms: | ||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||
| 543 | $t->as_string() # uses system locale | ||||||||||||||||||||||||||||||||||
| 544 | $t->as_string('fr') # positional | ||||||||||||||||||||||||||||||||||
| 545 | $t->as_string(lang => 'fr') # named | ||||||||||||||||||||||||||||||||||
| 546 | $t->as_string({ lang => 'fr' }) # hash reference | ||||||||||||||||||||||||||||||||||
| 547 | "$t" # stringification overload (uses system locale) | ||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||
| 549 | =head3 RETURN VALUE | ||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||
| 551 | =over 4 | ||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||
| 553 | =item Returns the translation string when the language is known and stored. | ||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||
| 555 | =item Returns C<undef> -- B<without> a warning -- when the language is known | ||||||||||||||||||||||||||||||||||
| 556 | but no translation exists for it. This is the normal "not translated yet" | ||||||||||||||||||||||||||||||||||
| 557 | case. | ||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||
| 559 | =item Returns C<undef> -- B<with> a C<carp> warning -- when no language can | ||||||||||||||||||||||||||||||||||
| 560 | be determined (no argument supplied I<and> no locale environment variable set). | ||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||
| 562 | =back | ||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||
| 564 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||
| 566 | use Lingua::Text; | ||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||
| 568 | my $t = Lingua::Text->new(en => 'boat', fr => 'bateau'); | ||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||
| 570 | # Explicit language | ||||||||||||||||||||||||||||||||||
| 571 | print $t->as_string('en'), "\n"; # boat | ||||||||||||||||||||||||||||||||||
| 572 | print $t->as_string(lang => 'fr'), "\n"; # bateau | ||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||
| 574 | # System locale (assume LANG=de_DE.UTF-8) | ||||||||||||||||||||||||||||||||||
| 575 | print $t->as_string(), "\n"; # undef -- no German stored | ||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||
| 577 | # String interpolation (same as calling as_string() with no args) | ||||||||||||||||||||||||||||||||||
| 578 | $ENV{LANG} = 'en_US.UTF-8'; | ||||||||||||||||||||||||||||||||||
| 579 | print "I see a $t.\n"; # I see a boat. | ||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||
| 581 | # Checking for a missing translation | ||||||||||||||||||||||||||||||||||
| 582 | if(!defined($t->as_string('zh'))) { | ||||||||||||||||||||||||||||||||||
| 583 | print "No Chinese translation available.\n"; | ||||||||||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||
| 586 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||
| 588 | =head4 Input | ||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||
| 590 | # Params::Validate::Strict-compatible schema | ||||||||||||||||||||||||||||||||||
| 591 | lang => { | ||||||||||||||||||||||||||||||||||
| 592 | type => 'string', | ||||||||||||||||||||||||||||||||||
| 593 | optional => 1, | ||||||||||||||||||||||||||||||||||
| 594 | default => _get_language(), | ||||||||||||||||||||||||||||||||||
| 595 | # NOTE: unlike set(), as_string() performs NO format validation on | ||||||||||||||||||||||||||||||||||
| 596 | # the lang argument. Any defined scalar is used directly as a hash | ||||||||||||||||||||||||||||||||||
| 597 | # key into the internal {texts} store. An unrecognised key simply | ||||||||||||||||||||||||||||||||||
| 598 | # returns undef without a warning (the 'on_not_found' state below). | ||||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||
| 601 | =head4 lang argument domain | ||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||
| 603 | # EQUIVALENCE PARTITIONS | ||||||||||||||||||||||||||||||||||
| 604 | # | ||||||||||||||||||||||||||||||||||
| 605 | # INPUT RESULT | ||||||||||||||||||||||||||||||||||
| 606 | # --------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 607 | # Stored key ('en' exists) returns the text (no warning) | ||||||||||||||||||||||||||||||||||
| 608 | # Valid format, not stored returns undef (no warning) | ||||||||||||||||||||||||||||||||||
| 609 | # Invalid format, not stored returns undef (no warning) | ||||||||||||||||||||||||||||||||||
| 610 | # Empty string '' returns undef (no warning) | ||||||||||||||||||||||||||||||||||
| 611 | # undef falls back to _get_language() | ||||||||||||||||||||||||||||||||||
| 612 | # no argument falls back to _get_language() | ||||||||||||||||||||||||||||||||||
| 613 | # | ||||||||||||||||||||||||||||||||||
| 614 | # Argument forms accepted: | ||||||||||||||||||||||||||||||||||
| 615 | # as_string('en') positional | ||||||||||||||||||||||||||||||||||
| 616 | # as_string(lang => 'en') named hash | ||||||||||||||||||||||||||||||||||
| 617 | # as_string({ lang => 'en' }) hash reference | ||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||
| 619 | =head4 Output | ||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||
| 621 | # Return::Set schema | ||||||||||||||||||||||||||||||||||
| 622 | on_found => SCALAR, # the translation string | ||||||||||||||||||||||||||||||||||
| 623 | on_not_found => undef, # language is known; no translation stored | ||||||||||||||||||||||||||||||||||
| 624 | on_no_language => undef # with carp warning | ||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||
| 626 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||
| 628 | =over 4 | ||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||
| 630 | =item C<"usage: as_string(lang =E<gt> $language)"> [carp] | ||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||
| 632 | No C<lang> was supplied and no locale environment variable (C<LANG>, | ||||||||||||||||||||||||||||||||||
| 633 | C<LC_MESSAGES>, C<LC_ALL>, C<LANGUAGE>) is set. Either pass the language | ||||||||||||||||||||||||||||||||||
| 634 | explicitly or set the locale before calling. | ||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||
| 636 | =back | ||||||||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||||||||
| 638 | =cut | ||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||
| 640 | sub as_string { | ||||||||||||||||||||||||||||||||||
| 641 | 233 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||
| 643 | # Guard against the overload calling convention: Perl passes ($self, $other, $swap) | ||||||||||||||||||||||||||||||||||
| 644 | # when the "" handler is invoked during a comparison. We only want the first arg. | ||||||||||||||||||||||||||||||||||
| 645 | # Fast path: single positional scalar (e.g. as_string('en')) skips Params::Get | ||||||||||||||||||||||||||||||||||
| 646 | # to avoid parameter-parsing overhead on what is often the hottest call site. | ||||||||||||||||||||||||||||||||||
| 647 | 233 | my $lang; | |||||||||||||||||||||||||||||||||
| 648 | 233 | ▶ | if(@_ && defined($_[0])) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and defined $_[0]
MC/DC100%2/2
@_ and defined $_[0]
@_
defined $_[0]
Truth table100%3/3
A and B
A= @_B= defined $_[0]
| |||||||||||||||||||||||||||||||||||
| 649 | 93 | ▶ | if(my $params = Params::Get::get_params('lang', \@_)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 650 | 93 | $lang = $params->{'lang'}; | |||||||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||||||||
| 653 | 233 | ▶ | $lang //= _get_language(); | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
$lang //= _get_language()
MC/DC100%2/2
$lang //= _get_language()
$lang
_get_language()
Truth table100%3/3
A //= B
A= $langB= _get_language()
| |||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||
| 655 | 233 | ▶ | if(defined($lang)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 656 | 204 | return $self->{'texts'}->{$lang}; | |||||||||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||||||||
| 658 | 29 | Carp::carp(_err('str_usage')); | |||||||||||||||||||||||||||||||||
| 659 | 29 | return; | |||||||||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||
| 662 | =head2 encode | ||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||
| 664 | Convert every stored translation from raw Unicode text to HTML entities. | ||||||||||||||||||||||||||||||||||
| 665 | Call this once on an object before embedding its text in HTML pages, to make | ||||||||||||||||||||||||||||||||||
| 666 | accented characters and other special characters safe for all browsers. | ||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||
| 668 | For example, the French word C<"\x{E9}tude"> (e-acute followed by "tude") | ||||||||||||||||||||||||||||||||||
| 669 | becomes C<"étude"> after encoding. | ||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||
| 671 | B<Important:> This method modifies the object I<in place> and B<cannot be | ||||||||||||||||||||||||||||||||||
| 672 | undone>. There is no C<decode()> counterpart. See L</COMMON PITFALLS>. | ||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||
| 674 | =head3 ARGUMENTS | ||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||
| 676 | None. | ||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||
| 678 | =head3 RETURN VALUE | ||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||
| 680 | Returns the object itself (C<$self>), so C<encode()> can be chained directly | ||||||||||||||||||||||||||||||||||
| 681 | onto C<new()>. | ||||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||||
| 683 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||||
| 685 | use Lingua::Text; | ||||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||||
| 687 | # Build an object and encode it in one step | ||||||||||||||||||||||||||||||||||
| 688 | my $t = Lingua::Text->new( | ||||||||||||||||||||||||||||||||||
| 689 | en => 'study', | ||||||||||||||||||||||||||||||||||
| 690 | fr => "\x{E9}tude", # e with acute accent (Unicode codepoint U+00E9) | ||||||||||||||||||||||||||||||||||
| 691 | )->encode(); | ||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||
| 693 | print $t->fr(); # étude | ||||||||||||||||||||||||||||||||||
| 694 | print $t->en(); # study (plain ASCII is unchanged) | ||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||
| 696 | # Use encoded text safely inside HTML | ||||||||||||||||||||||||||||||||||
| 697 | my $html = "<p>" . $t . "</p>"; | ||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||
| 699 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||
| 701 | =head4 Input | ||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||
| 703 | None. | ||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||
| 705 | =head4 Output | ||||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||||
| 707 | # Return::Set schema | ||||||||||||||||||||||||||||||||||
| 708 | on_success => $self # the same Lingua::Text object, with texts encoded | ||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||
| 710 | =cut | ||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||
| 712 | sub encode { | ||||||||||||||||||||||||||||||||||
| 713 | 108 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||
| 715 | # Iterate over keys to avoid stale iterator state from each(). | ||||||||||||||||||||||||||||||||||
| 716 | 108 | for my $lang (keys %{$self->{'texts'}}) { | |||||||||||||||||||||||||||||||||
| 717 | # SECURITY V2: Object::Configure may inject non-language keys (e.g. 'logger') | ||||||||||||||||||||||||||||||||||
| 718 | # that carry blessed references. utf8::decode() modifies its argument IN | ||||||||||||||||||||||||||||||||||
| 719 | # PLACE, which would corrupt the referenced object's internal string buffer. | ||||||||||||||||||||||||||||||||||
| 720 | # encode_entities() would then stringify it, potentially leaking its repr. | ||||||||||||||||||||||||||||||||||
| 721 | # Only process keys that satisfy the object invariant (valid ISO 639-1 code). | ||||||||||||||||||||||||||||||||||
| 722 | 239 | ▶ | next unless _is_valid_language($lang); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||
| 724 | 155 | my $v = $self->{'texts'}->{$lang}; | |||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||
| 726 | # SECURITY V3: a caller may store undef via $t->en(undef). utf8::decode(undef) | ||||||||||||||||||||||||||||||||||
| 727 | # emits "Use of uninitialized value" under 'use warnings' and is a no-op, | ||||||||||||||||||||||||||||||||||
| 728 | # but encode_entities(undef) returns '' which silently drops the entry. | ||||||||||||||||||||||||||||||||||
| 729 | # Skip explicitly so the stored undef is preserved unchanged. | ||||||||||||||||||||||||||||||||||
| 730 | 155 | ▶ | next unless defined($v) && !ref($v); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $v and not ref $v
MC/DC100%2/2
defined $v and not ref $v
defined $v
not ref $v
Truth table100%3/3
A and not(B)
A= defined $vB= not ref $v
| |||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||
| 732 | 142 | ▶ | utf8::decode($v) unless utf8::is_utf8($v); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 733 | 142 | $self->{'texts'}->{$lang} = HTML::Entities::encode_entities($v); | |||||||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||||||||
| 735 | 108 | return $self; | |||||||||||||||||||||||||||||||||
| 736 | } | ||||||||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||||||||
| 738 | =head2 Language accessor methods | ||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||
| 740 | Any two-letter method name that is a valid ISO 639-1 language code acts as a | ||||||||||||||||||||||||||||||||||
| 741 | combined getter and setter for that language's translation. These methods do | ||||||||||||||||||||||||||||||||||
| 742 | not need to be declared; Perl's C<AUTOLOAD> mechanism intercepts them. | ||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||
| 744 | $t->en('Hello'); # store an English translation | ||||||||||||||||||||||||||||||||||
| 745 | my $text = $t->fr(); # retrieve the French translation (undef if not stored) | ||||||||||||||||||||||||||||||||||
| 746 | $t->zh(''); # store an empty string (distinct from undef/not-stored) | ||||||||||||||||||||||||||||||||||
| 747 | $t->en(0); # store the string "0" (falsy values work correctly) | ||||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||
| 749 | =head3 ARGUMENTS | ||||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||||
| 751 | =over 4 | ||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||
| 753 | =item B<Setter form> -- pass one scalar argument | ||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||
| 755 | The value is stored under the method's language code and also returned. | ||||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||||
| 757 | =item B<Getter form> -- call with no arguments | ||||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||||
| 759 | Returns the stored string for that language, or C<undef> if nothing has been | ||||||||||||||||||||||||||||||||||
| 760 | stored. | ||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||
| 762 | =back | ||||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||||
| 764 | =head3 RETURN VALUE | ||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||
| 766 | =over 4 | ||||||||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||||||||
| 768 | =item Returns the stored translation string (getter) or the value just stored | ||||||||||||||||||||||||||||||||||
| 769 | (setter). | ||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||
| 771 | =item Returns C<undef> -- silently -- when the language has no stored | ||||||||||||||||||||||||||||||||||
| 772 | translation. | ||||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||||
| 774 | =item Returns C<undef> silently for any method name that is not a recognised | ||||||||||||||||||||||||||||||||||
| 775 | two-letter ISO 639-1 code. This means typos (e.g. C<$t-E<gt>enn()>) fail | ||||||||||||||||||||||||||||||||||
| 776 | invisibly. See L</COMMON PITFALLS>. | ||||||||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||||||||
| 778 | =back | ||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||
| 780 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||||||||
| 782 | my $t = Lingua::Text->new(); | ||||||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||||||
| 784 | $t->en('Good morning'); | ||||||||||||||||||||||||||||||||||
| 785 | $t->fr('Bonjour'); | ||||||||||||||||||||||||||||||||||
| 786 | $t->de('Guten Morgen'); | ||||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||||
| 788 | print $t->en(), "\n"; # Good morning | ||||||||||||||||||||||||||||||||||
| 789 | print $t->ja(), "\n"; # (nothing -- no Japanese stored) | ||||||||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||||||||
| 791 | # Chaining setters is not supported; use set() for that. | ||||||||||||||||||||||||||||||||||
| 792 | # Setters return the stored value, not $self. | ||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||
| 794 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||
| 796 | =head4 Method name domain (BVA) | ||||||||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||||||||
| 798 | The accessor is only created for method names that match C</^[a-z]{2}$/>: | ||||||||||||||||||||||||||||||||||
| 799 | exactly two lower-case ASCII letters. All other names return C<undef> | ||||||||||||||||||||||||||||||||||
| 800 | silently and never install an accessor. | ||||||||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||||||||
| 802 | # BOUNDARY VALUE ANALYSIS | ||||||||||||||||||||||||||||||||||
| 803 | # | ||||||||||||||||||||||||||||||||||
| 804 | # NAME CHARS MATCHES /^[a-z]{2}$/ RESULT | ||||||||||||||||||||||||||||||||||
| 805 | # --------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 806 | # 'e' 1 NO (below minimum) undef (silent) | ||||||||||||||||||||||||||||||||||
| 807 | # 'en' 2 YES (minimum valid) accessor works | ||||||||||||||||||||||||||||||||||
| 808 | # 'eng' 3 NO (above maximum) undef (silent) | ||||||||||||||||||||||||||||||||||
| 809 | # 'EN' 2 NO (uppercase) undef (silent) | ||||||||||||||||||||||||||||||||||
| 810 | # 'En' 2 NO (mixed case) undef (silent) | ||||||||||||||||||||||||||||||||||
| 811 | # 'en_US' 5 NO (underscore) undef (silent) | ||||||||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||||||||
| 813 | =head4 Input | ||||||||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||||||||
| 815 | # Setter: one positional SCALAR (may be undef, '', or '0') | ||||||||||||||||||||||||||||||||||
| 816 | # Getter: no arguments | ||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||
| 818 | =head4 Value domain (setter) | ||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||
| 820 | Any scalar value -- including C<undef>, C<''>, and C<'0'> -- is accepted | ||||||||||||||||||||||||||||||||||
| 821 | and stored verbatim. C<encode()> will later skip C<undef> and reference | ||||||||||||||||||||||||||||||||||
| 822 | values; the accessor itself is a transparent store. | ||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||
| 824 | # EQUIVALENCE PARTITIONS | ||||||||||||||||||||||||||||||||||
| 825 | # | ||||||||||||||||||||||||||||||||||
| 826 | # VALUE STORED? ENCODE() EFFECT | ||||||||||||||||||||||||||||||||||
| 827 | # -------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 828 | # 'hello' YES encoded if non-ASCII chars | ||||||||||||||||||||||||||||||||||
| 829 | # '' YES unchanged (no entities) | ||||||||||||||||||||||||||||||||||
| 830 | # '0' YES unchanged | ||||||||||||||||||||||||||||||||||
| 831 | # undef YES skipped by encode() (preserved) | ||||||||||||||||||||||||||||||||||
| 832 | # arrayref YES skipped by encode() (preserved) | ||||||||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||||||||
| 834 | =head4 Output | ||||||||||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||||||||
| 836 | # Return::Set schema | ||||||||||||||||||||||||||||||||||
| 837 | on_set => SCALAR | undef # the value just stored | ||||||||||||||||||||||||||||||||||
| 838 | on_get => SCALAR | undef # the stored value, or undef if absent | ||||||||||||||||||||||||||||||||||
| 839 | on_invalid => undef # silently, for non-language method names | ||||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||||
| 841 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||||
| 843 | None. All failure cases return C<undef> silently. See L</COMMON PITFALLS> | ||||||||||||||||||||||||||||||||||
| 844 | for guidance on typo detection. | ||||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||||
| 846 | =cut | ||||||||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||||||||
| 848 | sub AUTOLOAD { | ||||||||||||||||||||||||||||||||||
| 849 | 641 | ▶ | my $self = shift or return; | ||||||||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=6 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||
| 851 | 641 | my ($key) = $AUTOLOAD =~ /::(\w+)\z/; | |||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||
| 853 | 641 | ▶ | return if $key eq 'DESTROY'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 854 | 88 | ▶ | return unless ref($self) eq __PACKAGE__; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 855 | # Syllogism: | ||||||||||||||||||||||||||||||||||
| 856 | # P1: _is_valid_language accepts ^[a-z]{2}(?:_[A-Z]{2})?$ (case-sensitive). | ||||||||||||||||||||||||||||||||||
| 857 | # P2: AUTOLOAD method names are bare identifiers; the \w+ capture never | ||||||||||||||||||||||||||||||||||
| 858 | # produces the _XX suffix, so the relevant subset is ^[a-z]{2}$. | ||||||||||||||||||||||||||||||||||
| 859 | # P3: Any key passing /^[a-z]{2}$/ (no i-flag) is exactly two lowercase | ||||||||||||||||||||||||||||||||||
| 860 | # letters -- a strict subset of what _is_valid_language accepts. | ||||||||||||||||||||||||||||||||||
| 861 | # Conclusion: the _is_valid_language call is a tautology after the regex | ||||||||||||||||||||||||||||||||||
| 862 | # and is removed. The i-flag is also dropped to make the check strict. | ||||||||||||||||||||||||||||||||||
| 863 | 87 | ▶ | return unless $key =~ /^[a-z]{2}$/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||
| 865 | # Install a real method for $key so all future calls bypass AUTOLOAD entirely. | ||||||||||||||||||||||||||||||||||
| 866 | # Each two-letter code gets its own closure installed exactly once. This | ||||||||||||||||||||||||||||||||||
| 867 | # reduces repeated accessor cost from AUTOLOAD overhead (~5 regex ops) to a | ||||||||||||||||||||||||||||||||||
| 868 | # direct method dispatch, which is the dominant cost when rendering a page | ||||||||||||||||||||||||||||||||||
| 869 | # that stringifies many objects through the same language code. | ||||||||||||||||||||||||||||||||||
| 870 | { | ||||||||||||||||||||||||||||||||||
| 871 | 16 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 872 | 55 | *{__PACKAGE__ . '::' . $key} = sub { | |||||||||||||||||||||||||||||||||
| 873 | 519 | ▶ | my $s = shift or return; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 874 | 519 | ▶ | $s->{'texts'}->{$key} = shift if @_; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 875 | 519 | return $s->{'texts'}->{$key}; | |||||||||||||||||||||||||||||||||
| 876 | 55 | }; | |||||||||||||||||||||||||||||||||
| 877 | } | ||||||||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||||||||
| 879 | # Use @_ presence (not truthiness) so falsy values like '0' or '' are stored | ||||||||||||||||||||||||||||||||||
| 880 | 55 | ▶ | if(@_) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 881 | 16 | $self->{'texts'}->{$key} = shift; | |||||||||||||||||||||||||||||||||
| 882 | } | ||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||
| 884 | 55 | return $self->{'texts'}->{$key}; | |||||||||||||||||||||||||||||||||
| 885 | } | ||||||||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||||||||
| 887 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 888 | # Private helpers | ||||||||||||||||||||||||||||||||||
| 889 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 890 | |||||||||||||||||||||||||||||||||||
| 891 | # Purpose: Format a message from the catalog with the package name filled in. | ||||||||||||||||||||||||||||||||||
| 892 | # Entry: $key -- key into %MESSAGES. | ||||||||||||||||||||||||||||||||||
| 893 | # Exit: Formatted string ready for Carp. | ||||||||||||||||||||||||||||||||||
| 894 | # Effects: None. | ||||||||||||||||||||||||||||||||||
| 895 | sub _err :Private { | ||||||||||||||||||||||||||||||||||
| 896 | 132 | ▶ | my ($key) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_err: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 897 | 132 | my $fmt = $MESSAGES{$key}; | |||||||||||||||||||||||||||||||||
| 898 | 132 | ▶ | return sprintf($fmt, __PACKAGE__) if defined($fmt); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 899 | # Defensive: a future caller added a new key without a matching %MESSAGES entry. | ||||||||||||||||||||||||||||||||||
| 900 | # confess rather than returning a string so the programmer sees the bad key | ||||||||||||||||||||||||||||||||||
| 901 | # immediately at the call site instead of getting a silent wrong message. | ||||||||||||||||||||||||||||||||||
| 902 | 2 | Carp::confess("Internal error: _err() called with unknown key '$key' -- add it to %MESSAGES"); | |||||||||||||||||||||||||||||||||
| 903 | 16 | ▶ | } | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 904 | |||||||||||||||||||||||||||||||||||
| 905 | # Purpose: Issue a carp warning for a set() usage mistake and return undef. | ||||||||||||||||||||||||||||||||||
| 906 | # Extracted to eliminate the duplicated carp + return pattern in set(). | ||||||||||||||||||||||||||||||||||
| 907 | # Entry: None (uses package-level _err). | ||||||||||||||||||||||||||||||||||
| 908 | # Exit: undef. | ||||||||||||||||||||||||||||||||||
| 909 | # Effects: Emits a Carp warning visible at the caller's call site. | ||||||||||||||||||||||||||||||||||
| 910 | sub _carp_set_usage :Private { | ||||||||||||||||||||||||||||||||||
| 911 | 78 | ▶ | Carp::carp(_err('set_usage')); | ||||||||||||||||||||||||||||||||
|
Subroutine
_carp_set_usage: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 912 | 78 | return; | |||||||||||||||||||||||||||||||||
| 913 | 16 | ▶ | } | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||||||||
| 915 | # https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html | ||||||||||||||||||||||||||||||||||
| 916 | # https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html | ||||||||||||||||||||||||||||||||||
| 917 | # | ||||||||||||||||||||||||||||||||||
| 918 | # Purpose: Determine the two-letter ISO 639-1 language code for the running | ||||||||||||||||||||||||||||||||||
| 919 | # environment by probing standard locale environment variables in | ||||||||||||||||||||||||||||||||||
| 920 | # precedence order. | ||||||||||||||||||||||||||||||||||
| 921 | # Entry: Environment variables: LANGUAGE, LC_ALL, LC_MESSAGES, LANG. | ||||||||||||||||||||||||||||||||||
| 922 | # Exit: Lower-case two-letter code (e.g. 'en', 'fr'), or undef if the | ||||||||||||||||||||||||||||||||||
| 923 | # locale is absent, empty, or set to the POSIX 'C' locale. | ||||||||||||||||||||||||||||||||||
| 924 | # Returns 'en' when LANG=C or LANG=C.<encoding>. | ||||||||||||||||||||||||||||||||||
| 925 | # Effects: Read-only. The result is memoised against an env-var snapshot so | ||||||||||||||||||||||||||||||||||
| 926 | # repeated calls within the same process incur only five string | ||||||||||||||||||||||||||||||||||
| 927 | # comparisons rather than a full I18N::LangTags::Detect scan. | ||||||||||||||||||||||||||||||||||
| 928 | # The cache is invalidated automatically whenever LANGUAGE, LC_ALL, | ||||||||||||||||||||||||||||||||||
| 929 | # LC_MESSAGES, LANG, or HTTP_ACCEPT_LANGUAGE changes (e.g. local %ENV | ||||||||||||||||||||||||||||||||||
| 930 | # in tests). | ||||||||||||||||||||||||||||||||||
| 931 | { | ||||||||||||||||||||||||||||||||||
| 932 | 16 | my ($cached_lang, %cached_env); | |||||||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||||||||
| 934 | sub _get_language :Private { | ||||||||||||||||||||||||||||||||||
| 935 | # Fast path: env vars unchanged since last call -- return cached result. | ||||||||||||||||||||||||||||||||||
| 936 | # Using 'exists' rather than defined($cached_lang) so a cached undef is | ||||||||||||||||||||||||||||||||||
| 937 | # also served from cache (avoids re-probing an env with no locale set). | ||||||||||||||||||||||||||||||||||
| 938 | 186 | ▶ | if(exists $cached_env{LANG}) { | ||||||||||||||||||||||||||||||||
|
Subroutine
_get_language: called CC=21 SCAR=30.5
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 939 | 172 | my $stale = 0; | |||||||||||||||||||||||||||||||||
| 940 | 172 | for my $v (qw(LANGUAGE LC_ALL LC_MESSAGES LANG HTTP_ACCEPT_LANGUAGE)) { | |||||||||||||||||||||||||||||||||
| 941 | 651 | ▶ | if(($ENV{$v} // '') ne $cached_env{$v}) { $stale = 1; last } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$ENV{$v} // ""
MC/DC100%1/1
$ENV{$v} // ""
$ENV{$v}
Truth table100%2/2
A
A= $ENV{$v}
| |||||||||||||||||||||||||||||||||||
| 942 | } | ||||||||||||||||||||||||||||||||||
| 943 | 172 | ▶ | return $cached_lang unless $stale; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 944 | } | ||||||||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||||||||
| 946 | # Slow path: probe the environment. | ||||||||||||||||||||||||||||||||||
| 947 | # SECURITY V1 (taint mode): under perl -T, %ENV values are tainted. | ||||||||||||||||||||||||||||||||||
| 948 | # Perl only untaints a substring via a regex CAPTURE GROUP -- the captured | ||||||||||||||||||||||||||||||||||
| 949 | # $1 is untainted once the match succeeds. substr() on a tainted string | ||||||||||||||||||||||||||||||||||
| 950 | # propagates taint, so the previous substr()-based extraction was wrong | ||||||||||||||||||||||||||||||||||
| 951 | # for taint-mode callers. Restore explicit capturing groups here. | ||||||||||||||||||||||||||||||||||
| 952 | 154 | my $lang; | |||||||||||||||||||||||||||||||||
| 953 | 154 | for my $tag (I18N::LangTags::Detect::detect()) { | |||||||||||||||||||||||||||||||||
| 954 | 34 | ▶ | if($tag =~ /^([a-z]{2})/i) { $lang = lc($1); last } # $1 untainted | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 955 | } | ||||||||||||||||||||||||||||||||||
| 956 | 154 | ▶ | if(!defined($lang) && ($ENV{'LANGUAGE'} // '') =~ /^([a-z]{2})/i) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
not defined $lang and ($ENV{"LANGUAGE"} // "") =~ /^([a-z]{2})/i
$ENV{"LANGUAGE"} // ""
MC/DC100%2/2
not defined $lang and ($ENV{"LANGUAGE"} // "") =~ /^([a-z]{2})/i
not defined $lang
$ENV{"LANGUAGE"}
Truth table100%3/3
not(A) and B
A= not defined $langB= $ENV{"LANGUAGE"}
| |||||||||||||||||||||||||||||||||||
| 957 | 6 | $lang = lc($1); # $1 untainted | |||||||||||||||||||||||||||||||||
| 958 | } | ||||||||||||||||||||||||||||||||||
| 959 | 154 | ▶ | if(!defined($lang)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 960 | 116 | for my $var ('LC_ALL', 'LC_MESSAGES', 'LANG') { | |||||||||||||||||||||||||||||||||
| 961 | 334 | ▶ | my $val = $ENV{$var} // next; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{$var} // (next)
MC/DC100%1/1
$ENV{$var} // (next)
$ENV{$var}
Truth table100%2/2
A
A= $ENV{$var}
| |||||||||||||||||||||||||||||||||||
| 962 | 99 | ▶ | if($val =~ /^([a-z]{2})/i) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 963 | 67 | my $code = lc($1); # $1 untainted | |||||||||||||||||||||||||||||||||
| 964 | 67 | ▶ | if(_is_valid_language($code)) { $lang = $code; last } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 965 | } | ||||||||||||||||||||||||||||||||||
| 966 | } | ||||||||||||||||||||||||||||||||||
| 967 | } | ||||||||||||||||||||||||||||||||||
| 968 | # POSIX 'C' locale is treated as English. | ||||||||||||||||||||||||||||||||||
| 969 | # (?:\z|\.) matches 'C' at end-of-string or 'C.' (e.g. 'C.UTF-8'). | ||||||||||||||||||||||||||||||||||
| 970 | # Non-capturing group: the dot is tested but its value is never needed. | ||||||||||||||||||||||||||||||||||
| 971 | 154 | ▶ | $lang //= 'en' if ($ENV{'LANG'} // '') =~ /^C(?:\z|\.)/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
$ENV{"LANG"} // ""
$lang //= "en"
MC/DC100%1/1
$ENV{"LANG"} // ""
$ENV{"LANG"}
MC/DC0%0/1
$lang //= "en"
$lang
Truth table100%2/2
A
A= $ENV{"LANG"}
Truth table50%1/2
A
A= $lang
| |||||||||||||||||||||||||||||||||||
| 972 | |||||||||||||||||||||||||||||||||||
| 973 | # Cache result and snapshot the five vars we check. | ||||||||||||||||||||||||||||||||||
| 974 | 154 | $cached_lang = $lang; | |||||||||||||||||||||||||||||||||
| 975 | 154 | ▶ | $cached_env{$_} = $ENV{$_} // '' for qw(LANGUAGE LC_ALL LC_MESSAGES LANG HTTP_ACCEPT_LANGUAGE); | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{$_} // ""
MC/DC100%1/1
$ENV{$_} // ""
$ENV{$_}
Truth table100%2/2
A
A= $ENV{$_}
| |||||||||||||||||||||||||||||||||||
| 976 | 154 | return $lang; | |||||||||||||||||||||||||||||||||
| 977 | 16 | ▶ | } | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 978 | } | ||||||||||||||||||||||||||||||||||
| 979 | |||||||||||||||||||||||||||||||||||
| 980 | # Purpose: Validate that a string is a well-formed language code accepted by | ||||||||||||||||||||||||||||||||||
| 981 | # this module (ISO 639-1 with optional ISO 3166-1 country suffix). | ||||||||||||||||||||||||||||||||||
| 982 | # Entry: $lang -- candidate string. | ||||||||||||||||||||||||||||||||||
| 983 | # Exit: True if $lang matches $LANG_RE; false otherwise. | ||||||||||||||||||||||||||||||||||
| 984 | 16 | sub _is_valid_language :Private { | |||||||||||||||||||||||||||||||||
| 985 | 520 | ▶ | my ($lang) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_valid_language: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 986 | 520 | return $lang =~ $LANG_RE; | |||||||||||||||||||||||||||||||||
| 987 | 16 | ▶ | } | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 988 | |||||||||||||||||||||||||||||||||||
| 989 | =head1 COMMON PITFALLS | ||||||||||||||||||||||||||||||||||
| 990 | |||||||||||||||||||||||||||||||||||
| 991 | This section documents behaviours that often surprise first-time users. | ||||||||||||||||||||||||||||||||||
| 992 | |||||||||||||||||||||||||||||||||||
| 993 | =head2 encode() cannot be undone | ||||||||||||||||||||||||||||||||||
| 994 | |||||||||||||||||||||||||||||||||||
| 995 | C<encode()> modifies the object in place. There is no C<decode()>. If you | ||||||||||||||||||||||||||||||||||
| 996 | call C<encode()> twice on the same object, every special character is encoded | ||||||||||||||||||||||||||||||||||
| 997 | twice (e.g. C<é> becomes C<&eacute;>). Always call C<encode()> | ||||||||||||||||||||||||||||||||||
| 998 | once, as the last step, and only on a freshly built object. | ||||||||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||||||||
| 1000 | # WRONG -- double-encoding | ||||||||||||||||||||||||||||||||||
| 1001 | my $t = Lingua::Text->new(fr => "\x{E9}tude")->encode()->encode(); | ||||||||||||||||||||||||||||||||||
| 1002 | print $t->fr(); # &eacute;tude (broken HTML) | ||||||||||||||||||||||||||||||||||
| 1003 | |||||||||||||||||||||||||||||||||||
| 1004 | # CORRECT | ||||||||||||||||||||||||||||||||||
| 1005 | my $t = Lingua::Text->new(fr => "\x{E9}tude")->encode(); | ||||||||||||||||||||||||||||||||||
| 1006 | print $t->fr(); # étude | ||||||||||||||||||||||||||||||||||
| 1007 | |||||||||||||||||||||||||||||||||||
| 1008 | =head2 Missing translation returns undef, not an empty string | ||||||||||||||||||||||||||||||||||
| 1009 | |||||||||||||||||||||||||||||||||||
| 1010 | When a translation is not stored for the requested language, C<as_string()> | ||||||||||||||||||||||||||||||||||
| 1011 | and the accessor methods return C<undef>, not C<"">. Use C<defined()> to test | ||||||||||||||||||||||||||||||||||
| 1012 | for absence, not a truth check. | ||||||||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||||||||
| 1014 | my $t = Lingua::Text->new(en => 'hello'); | ||||||||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||||||||
| 1016 | my $de = $t->de(); | ||||||||||||||||||||||||||||||||||
| 1017 | print "Missing\n" unless defined($de); # correct | ||||||||||||||||||||||||||||||||||
| 1018 | print "Missing\n" unless $de; # WRONG -- also triggers for $de = '0' | ||||||||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||||||||
| 1020 | =head2 Typos in language codes are silent | ||||||||||||||||||||||||||||||||||
| 1021 | |||||||||||||||||||||||||||||||||||
| 1022 | The AUTOLOAD accessor returns C<undef> for any two-letter name that is not a | ||||||||||||||||||||||||||||||||||
| 1023 | valid ISO 639-1 code, and also for valid codes that have no stored translation. | ||||||||||||||||||||||||||||||||||
| 1024 | A typo like C<$t-E<gt>enn()> produces C<undef> without any warning. | ||||||||||||||||||||||||||||||||||
| 1025 | |||||||||||||||||||||||||||||||||||
| 1026 | $t->en('hello'); | ||||||||||||||||||||||||||||||||||
| 1027 | print $t->enn(); # undef -- no warning, no error | ||||||||||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||||||||
| 1029 | To guard against this, always check with C<defined()> and, during development, | ||||||||||||||||||||||||||||||||||
| 1030 | consider adding a test that verifies the expected languages are present. | ||||||||||||||||||||||||||||||||||
| 1031 | |||||||||||||||||||||||||||||||||||
| 1032 | =head2 new('text') silently drops the text when no locale is set | ||||||||||||||||||||||||||||||||||
| 1033 | |||||||||||||||||||||||||||||||||||
| 1034 | When you pass a single string to C<new()>, it is stored under the current | ||||||||||||||||||||||||||||||||||
| 1035 | system language. If no locale environment variable (C<LANG>, C<LC_MESSAGES>, | ||||||||||||||||||||||||||||||||||
| 1036 | etc.) is set, the language cannot be determined and the text is silently | ||||||||||||||||||||||||||||||||||
| 1037 | discarded. | ||||||||||||||||||||||||||||||||||
| 1038 | |||||||||||||||||||||||||||||||||||
| 1039 | # Safe: locale is set | ||||||||||||||||||||||||||||||||||
| 1040 | $ENV{LANG} = 'en_GB.UTF-8'; | ||||||||||||||||||||||||||||||||||
| 1041 | my $t = Lingua::Text->new('hello'); | ||||||||||||||||||||||||||||||||||
| 1042 | print $t->en(); # hello | ||||||||||||||||||||||||||||||||||
| 1043 | |||||||||||||||||||||||||||||||||||
| 1044 | # Unsafe: no locale | ||||||||||||||||||||||||||||||||||
| 1045 | delete $ENV{LANG}; | ||||||||||||||||||||||||||||||||||
| 1046 | delete $ENV{LC_MESSAGES}; | ||||||||||||||||||||||||||||||||||
| 1047 | my $t = Lingua::Text->new('hello'); # text is lost | ||||||||||||||||||||||||||||||||||
| 1048 | print defined($t->en()) ? $t->en() : 'lost'; # lost | ||||||||||||||||||||||||||||||||||
| 1049 | |||||||||||||||||||||||||||||||||||
| 1050 | # Safe alternative: always name the language explicitly | ||||||||||||||||||||||||||||||||||
| 1051 | my $t = Lingua::Text->new(en => 'hello'); | ||||||||||||||||||||||||||||||||||
| 1052 | |||||||||||||||||||||||||||||||||||
| 1053 | =head2 set() and the language accessors return $self or the value, not the text | ||||||||||||||||||||||||||||||||||
| 1054 | |||||||||||||||||||||||||||||||||||
| 1055 | C<set()> returns the object (C<$self>). Because of the stringify overload, | ||||||||||||||||||||||||||||||||||
| 1056 | when you print or compare the return value, it I<looks> like a string -- but it | ||||||||||||||||||||||||||||||||||
| 1057 | is an object. Do not rely on this behaviour; retrieve the value separately. | ||||||||||||||||||||||||||||||||||
| 1058 | |||||||||||||||||||||||||||||||||||
| 1059 | my $t = Lingua::Text->new(); | ||||||||||||||||||||||||||||||||||
| 1060 | my $result = $t->set(text => 'Hello', lang => 'en'); | ||||||||||||||||||||||||||||||||||
| 1061 | |||||||||||||||||||||||||||||||||||
| 1062 | # $result is the Lingua::Text object, not the string 'Hello' | ||||||||||||||||||||||||||||||||||
| 1063 | # This works due to stringify overload, but is misleading: | ||||||||||||||||||||||||||||||||||
| 1064 | print $result; # Hello (via stringify) | ||||||||||||||||||||||||||||||||||
| 1065 | |||||||||||||||||||||||||||||||||||
| 1066 | # Clearer intent: | ||||||||||||||||||||||||||||||||||
| 1067 | $t->set(text => 'Hello', lang => 'en'); | ||||||||||||||||||||||||||||||||||
| 1068 | print $t->as_string('en'); # Hello | ||||||||||||||||||||||||||||||||||
| 1069 | |||||||||||||||||||||||||||||||||||
| 1070 | The AUTOLOAD setter (e.g. C<$t-E<gt>en('Hello')>) returns the stored value | ||||||||||||||||||||||||||||||||||
| 1071 | directly, not C<$self>. | ||||||||||||||||||||||||||||||||||
| 1072 | |||||||||||||||||||||||||||||||||||
| 1073 | =head2 A Lingua::Text object is always true | ||||||||||||||||||||||||||||||||||
| 1074 | |||||||||||||||||||||||||||||||||||
| 1075 | The object overloads the boolean C<bool> operator to always return true, even | ||||||||||||||||||||||||||||||||||
| 1076 | when it contains no translations. Do B<not> use an object as a truth test to | ||||||||||||||||||||||||||||||||||
| 1077 | check whether it has any content. | ||||||||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||||||||
| 1079 | my $t = Lingua::Text->new(); # empty | ||||||||||||||||||||||||||||||||||
| 1080 | if($t) { print "always reached\n"; } # always true | ||||||||||||||||||||||||||||||||||
| 1081 | |||||||||||||||||||||||||||||||||||
| 1082 | # To check whether a specific language is stored: | ||||||||||||||||||||||||||||||||||
| 1083 | if(defined($t->en())) { print "English is stored\n"; } | ||||||||||||||||||||||||||||||||||
| 1084 | |||||||||||||||||||||||||||||||||||
| 1085 | =head2 Function-style call with arguments is an error | ||||||||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||||||||
| 1087 | Perl allows calling methods as functions, but Lingua::Text::new() (with | ||||||||||||||||||||||||||||||||||
| 1088 | double-colon and arguments) will emit a warning and return C<undef>. | ||||||||||||||||||||||||||||||||||
| 1089 | |||||||||||||||||||||||||||||||||||
| 1090 | my $t = Lingua::Text::new(en => 'hi'); # WRONG -- carp warning, returns undef | ||||||||||||||||||||||||||||||||||
| 1091 | my $t = Lingua::Text->new(en => 'hi'); # correct | ||||||||||||||||||||||||||||||||||
| 1092 | |||||||||||||||||||||||||||||||||||
| 1093 | Calling C<Lingua::Text::new()> with B<no> arguments works (it creates an empty | ||||||||||||||||||||||||||||||||||
| 1094 | object), but the arrow form is still preferred. | ||||||||||||||||||||||||||||||||||
| 1095 | |||||||||||||||||||||||||||||||||||
| 1096 | =head1 PERFORMANCE | ||||||||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||||||||
| 1098 | Three optimisations address the hot paths in typical web or CGI use (many | ||||||||||||||||||||||||||||||||||
| 1099 | objects stringified per request, same accessor called repeatedly): | ||||||||||||||||||||||||||||||||||
| 1100 | |||||||||||||||||||||||||||||||||||
| 1101 | =over 4 | ||||||||||||||||||||||||||||||||||
| 1102 | |||||||||||||||||||||||||||||||||||
| 1103 | =item B<Memoised locale detection> | ||||||||||||||||||||||||||||||||||
| 1104 | |||||||||||||||||||||||||||||||||||
| 1105 | C<_get_language()> is called on every C<as_string()> invocation (including | ||||||||||||||||||||||||||||||||||
| 1106 | stringify via C<"">). The result is cached against a snapshot of the five | ||||||||||||||||||||||||||||||||||
| 1107 | relevant environment variables (C<LANGUAGE>, C<LC_ALL>, C<LC_MESSAGES>, | ||||||||||||||||||||||||||||||||||
| 1108 | C<LANG>, C<HTTP_ACCEPT_LANGUAGE>). As long as those variables do not change, | ||||||||||||||||||||||||||||||||||
| 1109 | subsequent calls cost only five string comparisons instead of a full | ||||||||||||||||||||||||||||||||||
| 1110 | C<I18N::LangTags::Detect::detect()> scan. The cache is invalidated | ||||||||||||||||||||||||||||||||||
| 1111 | automatically when any of those variables change -- including C<local %ENV> | ||||||||||||||||||||||||||||||||||
| 1112 | in tests. | ||||||||||||||||||||||||||||||||||
| 1113 | |||||||||||||||||||||||||||||||||||
| 1114 | =item B<AUTOLOAD method installation> | ||||||||||||||||||||||||||||||||||
| 1115 | |||||||||||||||||||||||||||||||||||
| 1116 | The first call to any two-letter accessor (e.g. C<$t-E<gt>en()>) installs a | ||||||||||||||||||||||||||||||||||
| 1117 | real subroutine in the package symbol table. All subsequent calls for that | ||||||||||||||||||||||||||||||||||
| 1118 | code dispatch directly, bypassing the AUTOLOAD regex and guard overhead | ||||||||||||||||||||||||||||||||||
| 1119 | entirely. | ||||||||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||||||||
| 1121 | =item B<Single-argument fast path in as_string()> | ||||||||||||||||||||||||||||||||||
| 1122 | |||||||||||||||||||||||||||||||||||
| 1123 | C<$t-E<gt>as_string('en')> skips C<Params::Get::get_params()> when exactly | ||||||||||||||||||||||||||||||||||
| 1124 | one non-reference argument is supplied. Named (C<lang =E<gt> 'en'>) and | ||||||||||||||||||||||||||||||||||
| 1125 | hashref (C<{ lang =E<gt> 'en' }>) forms still go through the full parser. | ||||||||||||||||||||||||||||||||||
| 1126 | |||||||||||||||||||||||||||||||||||
| 1127 | =back | ||||||||||||||||||||||||||||||||||
| 1128 | |||||||||||||||||||||||||||||||||||
| 1129 | =head1 LIMITATIONS | ||||||||||||||||||||||||||||||||||
| 1130 | |||||||||||||||||||||||||||||||||||
| 1131 | =over 4 | ||||||||||||||||||||||||||||||||||
| 1132 | |||||||||||||||||||||||||||||||||||
| 1133 | =item * B<No decode()>: Once C<encode()> is called, all translations are | ||||||||||||||||||||||||||||||||||
| 1134 | HTML-entity-encoded. There is no method to reverse this. | ||||||||||||||||||||||||||||||||||
| 1135 | |||||||||||||||||||||||||||||||||||
| 1136 | =item * B<No language fallback>: If C<en_GB> is the system locale but only | ||||||||||||||||||||||||||||||||||
| 1137 | C<en> is stored, the module finds the text correctly (because the country | ||||||||||||||||||||||||||||||||||
| 1138 | suffix is stripped). However, if C<en_GB> is stored as a key and the system | ||||||||||||||||||||||||||||||||||
| 1139 | locale is C<en_US>, the text is I<not> found. Only two-letter codes are used | ||||||||||||||||||||||||||||||||||
| 1140 | as storage keys. | ||||||||||||||||||||||||||||||||||
| 1141 | |||||||||||||||||||||||||||||||||||
| 1142 | =item * B<Silent AUTOLOAD failures>: Unrecognised method names (including | ||||||||||||||||||||||||||||||||||
| 1143 | typos) return C<undef> without any diagnostic. | ||||||||||||||||||||||||||||||||||
| 1144 | |||||||||||||||||||||||||||||||||||
| 1145 | =item * B<Object::Configure semantics>: The exact effect of | ||||||||||||||||||||||||||||||||||
| 1146 | L<Object::Configure> depends on the installed version and any project-level | ||||||||||||||||||||||||||||||||||
| 1147 | configuration present. Without a configuration file, it passes parameters | ||||||||||||||||||||||||||||||||||
| 1148 | through unchanged. | ||||||||||||||||||||||||||||||||||
| 1149 | |||||||||||||||||||||||||||||||||||
| 1150 | =back | ||||||||||||||||||||||||||||||||||
| 1151 | |||||||||||||||||||||||||||||||||||
| 1152 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||||||||
| 1153 | |||||||||||||||||||||||||||||||||||
| 1154 | Runtime: | ||||||||||||||||||||||||||||||||||
| 1155 | |||||||||||||||||||||||||||||||||||
| 1156 | =over 4 | ||||||||||||||||||||||||||||||||||
| 1157 | |||||||||||||||||||||||||||||||||||
| 1158 | =item * L<Carp> -- C<carp>/C<croak> for warnings and errors | ||||||||||||||||||||||||||||||||||
| 1159 | |||||||||||||||||||||||||||||||||||
| 1160 | =item * L<HTML::Entities> -- HTML entity encoding in C<encode()> | ||||||||||||||||||||||||||||||||||
| 1161 | |||||||||||||||||||||||||||||||||||
| 1162 | =item * L<I18N::LangTags::Detect> -- locale detection from environment variables | ||||||||||||||||||||||||||||||||||
| 1163 | |||||||||||||||||||||||||||||||||||
| 1164 | =item * L<Object::Configure> -- optional config-file defaults for C<new()> | ||||||||||||||||||||||||||||||||||
| 1165 | |||||||||||||||||||||||||||||||||||
| 1166 | =item * L<Params::Get> -- flexible argument parsing for public methods | ||||||||||||||||||||||||||||||||||
| 1167 | |||||||||||||||||||||||||||||||||||
| 1168 | =item * L<Readonly> -- constant definitions for the message catalog and patterns | ||||||||||||||||||||||||||||||||||
| 1169 | |||||||||||||||||||||||||||||||||||
| 1170 | =item * L<Scalar::Util> -- C<blessed()> for safe type testing | ||||||||||||||||||||||||||||||||||
| 1171 | |||||||||||||||||||||||||||||||||||
| 1172 | =item * L<Sub::Private> -- enforcement of private method access | ||||||||||||||||||||||||||||||||||
| 1173 | |||||||||||||||||||||||||||||||||||
| 1174 | =back | ||||||||||||||||||||||||||||||||||
| 1175 | |||||||||||||||||||||||||||||||||||
| 1176 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 1177 | |||||||||||||||||||||||||||||||||||
| 1178 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||||||||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||||||||
| 1180 | =head1 BUGS | ||||||||||||||||||||||||||||||||||
| 1181 | |||||||||||||||||||||||||||||||||||
| 1182 | Please report bugs at L<https://github.com/nigelhorne/Lingua-Text/issues>. | ||||||||||||||||||||||||||||||||||
| 1183 | |||||||||||||||||||||||||||||||||||
| 1184 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 1185 | |||||||||||||||||||||||||||||||||||
| 1186 | =over 4 | ||||||||||||||||||||||||||||||||||
| 1187 | |||||||||||||||||||||||||||||||||||
| 1188 | =item * L<Object::Configure> -- runtime object configuration | ||||||||||||||||||||||||||||||||||
| 1189 | |||||||||||||||||||||||||||||||||||
| 1190 | =item * L<I18N::LangTags::Detect> -- the locale detection module used internally | ||||||||||||||||||||||||||||||||||
| 1191 | |||||||||||||||||||||||||||||||||||
| 1192 | =item * L<HTML::Entities> -- the HTML encoding module used by C<encode()> | ||||||||||||||||||||||||||||||||||
| 1193 | |||||||||||||||||||||||||||||||||||
| 1194 | =item * L<Test Dashboard|https://nigelhorne.github.io/CGI-Info/coverage/> | ||||||||||||||||||||||||||||||||||
| 1195 | |||||||||||||||||||||||||||||||||||
| 1196 | =back | ||||||||||||||||||||||||||||||||||
| 1197 | |||||||||||||||||||||||||||||||||||
| 1198 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||||||||
| 1200 | This module is provided as-is without any warranty. | ||||||||||||||||||||||||||||||||||
| 1201 | |||||||||||||||||||||||||||||||||||
| 1202 | Documentation: | ||||||||||||||||||||||||||||||||||
| 1203 | |||||||||||||||||||||||||||||||||||
| 1204 | perldoc Lingua::Text | ||||||||||||||||||||||||||||||||||
| 1205 | |||||||||||||||||||||||||||||||||||
| 1206 | Online resources: | ||||||||||||||||||||||||||||||||||
| 1207 | |||||||||||||||||||||||||||||||||||
| 1208 | =over 4 | ||||||||||||||||||||||||||||||||||
| 1209 | |||||||||||||||||||||||||||||||||||
| 1210 | =item * MetaCPAN: L<https://metacpan.org/release/Lingua-Text> | ||||||||||||||||||||||||||||||||||
| 1211 | |||||||||||||||||||||||||||||||||||
| 1212 | =item * Bug tracker: L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Lingua-Text> | ||||||||||||||||||||||||||||||||||
| 1213 | |||||||||||||||||||||||||||||||||||
| 1214 | =item * CPANTS: L<http://cpants.cpanauthors.org/dist/Lingua-Text> | ||||||||||||||||||||||||||||||||||
| 1215 | |||||||||||||||||||||||||||||||||||
| 1216 | =item * CPAN Testers: L<http://matrix.cpantesters.org/?dist=Lingua-Text> | ||||||||||||||||||||||||||||||||||
| 1217 | |||||||||||||||||||||||||||||||||||
| 1218 | =item * Dependencies: L<http://deps.cpantesters.org/?module=Lingua-Text> | ||||||||||||||||||||||||||||||||||
| 1219 | |||||||||||||||||||||||||||||||||||
| 1220 | =back | ||||||||||||||||||||||||||||||||||
| 1221 | |||||||||||||||||||||||||||||||||||
| 1222 | =encoding utf-8 | ||||||||||||||||||||||||||||||||||
| 1223 | |||||||||||||||||||||||||||||||||||
| 1224 | =head1 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||
| 1225 | |||||||||||||||||||||||||||||||||||
| 1226 | =head2 new | ||||||||||||||||||||||||||||||||||
| 1227 | |||||||||||||||||||||||||||||||||||
| 1228 | -- Type definitions | ||||||||||||||||||||||||||||||||||
| 1229 | STRING == seq CHAR | ||||||||||||||||||||||||||||||||||
| 1230 | LANG == { l : STRING | l =~ /^[a-z]{2}(_[A-Z]{2})?$/ } | ||||||||||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||||||||
| 1232 | -- State: a partial function from language codes to strings | ||||||||||||||||||||||||||||||||||
| 1233 | ┌─ LinguaText ──────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1234 | │ texts : LANG ⇸ STRING | ||||||||||||||||||||||||||||||||||
| 1235 | └───────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1236 | |||||||||||||||||||||||||||||||||||
| 1237 | -- Construction: initialise texts from zero or more lang/string pairs | ||||||||||||||||||||||||||||||||||
| 1238 | ┌─ new ─────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1239 | │ ΔLinguaText | ||||||||||||||||||||||||||||||||||
| 1240 | │ init? : LANG ⇸ STRING | ||||||||||||||||||||||||||||||||||
| 1241 | ├───────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1242 | │ texts' = init? | ||||||||||||||||||||||||||||||||||
| 1243 | └───────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1244 | |||||||||||||||||||||||||||||||||||
| 1245 | -- Clone: merge the parent's texts with any new pairs (new pairs win) | ||||||||||||||||||||||||||||||||||
| 1246 | ┌─ clone ───────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1247 | │ ΔLinguaText | ||||||||||||||||||||||||||||||||||
| 1248 | │ parent? : LinguaText | ||||||||||||||||||||||||||||||||||
| 1249 | │ extra? : LANG ⇸ STRING | ||||||||||||||||||||||||||||||||||
| 1250 | ├───────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1251 | │ texts' = parent?.texts ⊕ extra? | ||||||||||||||||||||||||||||||||||
| 1252 | └───────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1253 | |||||||||||||||||||||||||||||||||||
| 1254 | =head2 set | ||||||||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||||||||
| 1256 | -- set: add or replace one translation; all others remain unchanged | ||||||||||||||||||||||||||||||||||
| 1257 | ┌─ set ─────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1258 | │ ΔLinguaText | ||||||||||||||||||||||||||||||||||
| 1259 | │ lang? : LANG | ||||||||||||||||||||||||||||||||||
| 1260 | │ text? : STRING | ||||||||||||||||||||||||||||||||||
| 1261 | ├───────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1262 | │ texts' = texts ⊕ { lang? ↦ text? } | ||||||||||||||||||||||||||||||||||
| 1263 | └───────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1264 | -- ⊕ denotes relational override: lang? wins over any existing entry | ||||||||||||||||||||||||||||||||||
| 1265 | |||||||||||||||||||||||||||||||||||
| 1266 | =head2 as_string | ||||||||||||||||||||||||||||||||||
| 1267 | |||||||||||||||||||||||||||||||||||
| 1268 | -- as_string: look up one translation; state is unchanged (Xi schema) | ||||||||||||||||||||||||||||||||||
| 1269 | ┌─ as_string ───────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1270 | │ ΞLinguaText | ||||||||||||||||||||||||||||||||||
| 1271 | │ lang? : LANG ∪ { current_locale } | ||||||||||||||||||||||||||||||||||
| 1272 | │ result! : STRING ∪ { undef } | ||||||||||||||||||||||||||||||||||
| 1273 | ├───────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1274 | │ lang? ∈ dom texts ⟹ result! = texts(lang?) | ||||||||||||||||||||||||||||||||||
| 1275 | │ lang? ∉ dom texts ⟹ result! = undef | ||||||||||||||||||||||||||||||||||
| 1276 | └───────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1277 | -- The state schema Ξ means texts is not modified by this operation. | ||||||||||||||||||||||||||||||||||
| 1278 | |||||||||||||||||||||||||||||||||||
| 1279 | =head2 encode | ||||||||||||||||||||||||||||||||||
| 1280 | |||||||||||||||||||||||||||||||||||
| 1281 | -- Let entity : STRING -> STRING be HTML::Entities::encode_entities | ||||||||||||||||||||||||||||||||||
| 1282 | -- and dec : STRING -> STRING be utf8::decode (applied only when needed) | ||||||||||||||||||||||||||||||||||
| 1283 | ┌─ encode ──────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1284 | │ ΔLinguaText | ||||||||||||||||||||||||||||||||||
| 1285 | │ ∀ l : dom texts | ||||||||||||||||||||||||||||||||||
| 1286 | │ • texts'(l) = entity(dec(texts(l))) | ||||||||||||||||||||||||||||||||||
| 1287 | └───────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||
| 1288 | -- texts' replaces every value; dom texts is unchanged. | ||||||||||||||||||||||||||||||||||
| 1289 | |||||||||||||||||||||||||||||||||||
| 1290 | =head1 LICENCE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||
| 1291 | |||||||||||||||||||||||||||||||||||
| 1292 | Copyright 2021-2026 Nigel Horne. | ||||||||||||||||||||||||||||||||||
| 1293 | |||||||||||||||||||||||||||||||||||
| 1294 | Usage is subject to the GPL2 licence terms. | ||||||||||||||||||||||||||||||||||
| 1295 | If you use it, please let me know. | ||||||||||||||||||||||||||||||||||
| 1296 | |||||||||||||||||||||||||||||||||||
| 1297 | =cut | ||||||||||||||||||||||||||||||||||
| 1298 | |||||||||||||||||||||||||||||||||||
| 1299 | 16 | 1; | |||||||||||||||||||||||||||||||||