2 package Locale::Maketext;
4 use vars qw( @ISA $VERSION $MATCH_SUPERS $USING_LANGUAGE_TAGS
5 $USE_LITERALS $MATCH_SUPERS_TIGHTLY);
8 use I18N::LangTags::Detect ();
10 #--------------------------------------------------------------------------
12 BEGIN { unless(defined &DEBUG) { *DEBUG = sub () {0} } }
13 # define the constant 'DEBUG' at compile-time
15 # turn on utf8 if we have it (this is what GutsLoader.pm used to do essentially )
16 # use if (exists $INC{'utf8.pm'} || eval 'use utf8'), 'utf8';
19 # if we have it || we can load it
20 if ( exists $INC{'utf8.pm'} || eval { local $SIG{'__DIE__'};require utf8; } ) {
22 DEBUG and warn " utf8 on for _compile()\n";
25 DEBUG and warn " utf8 not available for _compile() ($INC{'utf8.pm'})\n$@\n";
34 $MATCH_SUPERS_TIGHTLY = 1;
35 $USING_LANGUAGE_TAGS = 1;
36 # Turning this off is somewhat of a security risk in that little or no
37 # checking will be done on the legality of tokens passed to the
38 # eval("use $module_name") in _try_use. If you turn this off, you have
39 # to do your own taint checking.
41 $USE_LITERALS = 1 unless defined $USE_LITERALS;
42 # a hint for compiling bracket-notation things.
46 ###########################################################################
49 my($handle, $num, @forms) = @_;
51 return $num if @forms == 0; # what should this mean?
52 return $forms[2] if @forms > 2 and $num == 0; # special zeroth case
55 # Note that the formatting of $num is preserved.
56 return( $handle->numf($num) . ' ' . $handle->numerate($num, @forms) );
57 # Most human languages put the number phrase before the qualified phrase.
62 # return this lexical item in a form appropriate to this number
63 my($handle, $num, @forms) = @_;
66 return '' unless @forms;
67 if(@forms == 1) { # only the headword form specified
68 return $s ? $forms[0] : ($forms[0] . 's'); # very cheap hack.
70 else { # sing and plural were specified
71 return $s ? $forms[0] : $forms[1];
75 #--------------------------------------------------------------------------
78 my($handle, $num) = @_[0,1];
79 if($num < 10_000_000_000 and $num > -10_000_000_000 and $num == int($num)) {
80 $num += 0; # Just use normal integer stringification.
81 # Specifically, don't let %G turn ten million into 1E+007
84 $num = CORE::sprintf('%G', $num);
85 # "CORE::" is there to avoid confusion with the above sub sprintf.
87 while( $num =~ s/^([-+]?\d+)(\d{3})/$1,$2/s ) {1} # right from perlfaq5
88 # The initial \d+ gobbles as many digits as it can, and then we
89 # backtrack so it un-eats the rightmost three, and then we
90 # insert the comma there.
92 $num =~ tr<.,><,.> if ref($handle) and $handle->{'numf_comma'};
93 # This is just a lame hack instead of using Number::Format
99 my($handle, $format, @params) = @_;
100 return CORE::sprintf($format, @params);
101 # "CORE::" is there to avoid confusion with myself!
104 #=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
106 use integer; # vroom vroom... applies to the whole rest of the module
109 my $it = ref($_[0]) || $_[0];
110 return undef unless $it =~ m/([^':]+)(?:::)?$/s;
119 (ref($it) && $it->{'encoding'})
120 || 'iso-8859-1' # Latin-1
124 #--------------------------------------------------------------------------
126 sub fallback_languages { return('i-default', 'en', 'en-US') }
128 sub fallback_language_classes { return () }
130 #--------------------------------------------------------------------------
132 sub fail_with { # an actual attribute method!
133 my($handle, @params) = @_;
134 return unless ref($handle);
135 $handle->{'fail'} = $params[0] if @params;
136 return $handle->{'fail'};
139 #--------------------------------------------------------------------------
141 sub failure_handler_auto {
142 # Meant to be used like:
143 # $handle->fail_with('failure_handler_auto')
148 $handle->{'failure_lex'} ||= {};
149 my $lex = $handle->{'failure_lex'};
151 my $value ||= ($lex->{$phrase} ||= $handle->_compile($phrase));
153 # Dumbly copied from sub maketext:
154 return ${$value} if ref($value) eq 'SCALAR';
155 return $value if ref($value) ne 'CODE';
157 local $SIG{'__DIE__'};
158 eval { $value = &$value($handle, @_) };
160 # If we make it here, there was an exception thrown in the
161 # call to $value, and so scream:
163 # pretty up the error message
164 $@ =~ s{\s+at\s+\(eval\s+\d+\)\s+line\s+(\d+)\.?\n?}
165 {\n in bracket code [compiled line $1],}s;
166 #$err =~ s/\n?$/\n/s;
167 Carp::croak "Error in maketexting \"$phrase\":\n$@ as used";
168 # Rather unexpected, but suppose that the sub tried calling
169 # a method that didn't exist.
176 #==========================================================================
180 my $class = ref($_[0]) || $_[0];
181 my $handle = bless {}, $class;
186 sub init { return } # no-op
188 ###########################################################################
191 # Remember, this can fail. Failure is controllable many ways.
192 Carp::croak 'maketext requires at least one parameter' unless @_ > 1;
194 my($handle, $phrase) = splice(@_,0,2);
195 Carp::confess('No handle/phrase') unless (defined($handle) && defined($phrase));
197 # backup $@ in case it it's still being used in the calling code.
198 # If no failures, we'll re-set it back to what it was later.
201 # Copy @_ case one of its elements is $@.
207 if (exists $handle->{'_external_lex_cache'}{$phrase}) {
208 DEBUG and warn "* Using external lex cache version of \"$phrase\"\n";
209 $value = $handle->{'_external_lex_cache'}{$phrase};
213 @{ $isa_scan{ref($handle) || $handle} || $handle->_lex_refs }
215 DEBUG and warn "* Looking up \"$phrase\" in $h_r\n";
216 if(exists $h_r->{$phrase}) {
217 DEBUG and warn " Found \"$phrase\" in $h_r\n";
218 unless(ref($value = $h_r->{$phrase})) {
219 # Nonref means it's not yet compiled. Compile and replace.
220 if ($handle->{'use_external_lex_cache'}) {
221 $value = $handle->{'_external_lex_cache'}{$phrase} = $handle->_compile($value);
224 $value = $h_r->{$phrase} = $handle->_compile($value);
229 # extending packages need to be able to localize _AUTO and if readonly can't "local $h_r->{'_AUTO'} = 1;"
230 # but they can "local $handle->{'_external_lex_cache'}{'_AUTO'} = 1;"
231 elsif($phrase !~ m/^_/s and ($handle->{'use_external_lex_cache'} ? ( exists $handle->{'_external_lex_cache'}{'_AUTO'} ? $handle->{'_external_lex_cache'}{'_AUTO'} : $h_r->{'_AUTO'} ) : $h_r->{'_AUTO'})) {
232 # it's an auto lex, and this is an autoable key!
233 DEBUG and warn " Automaking \"$phrase\" into $h_r\n";
234 if ($handle->{'use_external_lex_cache'}) {
235 $value = $handle->{'_external_lex_cache'}{$phrase} = $handle->_compile($phrase);
238 $value = $h_r->{$phrase} = $handle->_compile($phrase);
242 DEBUG>1 and print " Not found in $h_r, nor automakable\n";
247 unless(defined($value)) {
248 DEBUG and warn "! Lookup of \"$phrase\" in/under ", ref($handle) || $handle, " fails.\n";
249 if(ref($handle) and $handle->{'fail'}) {
250 DEBUG and warn "WARNING0: maketext fails looking for <$phrase>\n";
252 if(ref($fail = $handle->{'fail'}) eq 'CODE') { # it's a sub reference
253 $@ = $at; # Put $@ back in case we altered it along the way.
254 return &{$fail}($handle, $phrase, @_);
255 # If it ever returns, it should return a good value.
257 else { # It's a method name
258 $@ = $at; # Put $@ back in case we altered it along the way.
259 return $handle->$fail($phrase, @_);
260 # If it ever returns, it should return a good value.
264 # All we know how to do is this;
265 Carp::croak("maketext doesn't know how to say:\n$phrase\nas needed");
269 if(ref($value) eq 'SCALAR'){
270 $@ = $at; # Put $@ back in case we altered it along the way.
273 if(ref($value) ne 'CODE'){
274 $@ = $at; # Put $@ back in case we altered it along the way.
279 local $SIG{'__DIE__'};
280 eval { $value = &$value($handle, @_) };
282 # If we make it here, there was an exception thrown in the
283 # call to $value, and so scream:
285 # pretty up the error message
286 $@ =~ s{\s+at\s+\(eval\s+\d+\)\s+line\s+(\d+)\.?\n?}
287 {\n in bracket code [compiled line $1],}s;
288 #$err =~ s/\n?$/\n/s;
289 Carp::croak "Error in maketexting \"$phrase\":\n$@ as used";
290 # Rather unexpected, but suppose that the sub tried calling
291 # a method that didn't exist.
294 $@ = $at; # Put $@ back in case we altered it along the way.
297 $@ = $at; # Put $@ back in case we altered it along the way.
300 ###########################################################################
302 sub get_handle { # This is a constructor and, yes, it CAN FAIL.
303 # Its class argument has to be the base class for the current
304 # application's l10n files.
306 my($base_class, @languages) = @_;
307 $base_class = ref($base_class) || $base_class;
308 # Complain if they use __PACKAGE__ as a project base class?
311 DEBUG and warn 'Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
312 if($USING_LANGUAGE_TAGS) { # An explicit language-list was given!
314 map {; $_, I18N::LangTags::alternate_language_tags($_) }
316 map I18N::LangTags::locale2language_tag($_),
317 # If it's a lg tag, fine, pass thru (untainted)
318 # If it's a locale ID, try converting to a lg tag (untainted),
321 DEBUG and warn 'Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
325 @languages = $base_class->_ambient_langprefs;
328 @languages = $base_class->_langtag_munging(@languages);
331 foreach my $module_name ( map { $base_class . '::' . $_ } @languages ) {
332 next unless length $module_name; # sanity
333 next if $seen{$module_name}++ # Already been here, and it was no-go
334 || !&_try_use($module_name); # Try to use() it, but can't it.
335 return($module_name->new); # Make it!
338 return undef; # Fail!
341 ###########################################################################
343 sub _langtag_munging {
344 my($base_class, @languages) = @_;
346 # We have all these DEBUG statements because otherwise it's hard as hell
347 # to diagnose ifwhen something goes wrong.
349 DEBUG and warn 'Lgs1: ', map("<$_>", @languages), "\n";
351 if($USING_LANGUAGE_TAGS) {
352 DEBUG and warn 'Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
353 @languages = $base_class->_add_supers( @languages );
355 push @languages, I18N::LangTags::panic_languages(@languages);
356 DEBUG and warn "After adding panic languages:\n",
357 ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
359 push @languages, $base_class->fallback_languages;
360 # You are free to override fallback_languages to return empty-list!
361 DEBUG and warn 'Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
363 @languages = # final bit of processing to turn them into classname things
366 $it =~ tr<-A-Z><_a-z>; # lc, and turn - to _
367 $it =~ tr<_a-z0-9><>cd; # remove all but a-z0-9_
371 DEBUG and warn "Nearing end of munging:\n",
372 ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
375 DEBUG and warn "Bypassing language-tags.\n",
376 ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
379 DEBUG and warn "Before adding fallback classes:\n",
380 ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
382 push @languages, $base_class->fallback_language_classes;
383 # You are free to override that to return whatever.
385 DEBUG and warn "Finally:\n",
386 ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
391 ###########################################################################
393 sub _ambient_langprefs {
394 return I18N::LangTags::Detect::detect();
397 ###########################################################################
400 my($base_class, @languages) = @_;
402 if (!$MATCH_SUPERS) {
404 DEBUG and warn "Bypassing any super-matching.\n",
405 ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
408 elsif( $MATCH_SUPERS_TIGHTLY ) {
409 DEBUG and warn "Before adding new supers tightly:\n",
410 ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
411 @languages = I18N::LangTags::implicate_supers( @languages );
412 DEBUG and warn "After adding new supers tightly:\n",
413 ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
417 DEBUG and warn "Before adding supers to end:\n",
418 ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
419 @languages = I18N::LangTags::implicate_supers_strictly( @languages );
420 DEBUG and warn "After adding supers to end:\n",
421 ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
427 ###########################################################################
429 # This is where most people should stop reading.
431 ###########################################################################
434 # memoization of whether we've used this module, or found it unusable.
436 sub _try_use { # Basically a wrapper around "require Modulename"
437 # "Many men have tried..." "They tried and failed?" "They tried and died."
438 return $tried{$_[0]} if exists $tried{$_[0]}; # memoization
440 my $module = $_[0]; # ASSUME sane module name!
443 return($tried{$module} = 1)
444 if %{$module . '::Lexicon'} or @{$module . '::ISA'};
445 # weird case: we never use'd it, but there it is!
448 DEBUG and warn " About to use $module ...\n";
450 local $SIG{'__DIE__'};
452 eval "require $module"; # used to be "use $module", but no point in that.
455 DEBUG and warn "Error using $module \: $@\n";
456 return $tried{$module} = 0;
459 DEBUG and warn " OK, $module is used\n";
460 return $tried{$module} = 1;
464 #--------------------------------------------------------------------------
466 sub _lex_refs { # report the lexicon references for this handle's class
467 # returns an arrayREF!
470 my $class = ref($_[0]) || $_[0];
471 DEBUG and warn "Lex refs lookup on $class\n";
472 return $isa_scan{$class} if exists $isa_scan{$class}; # memoization!
475 my $seen_r = ref($_[1]) ? $_[1] : {};
477 if( defined( *{$class . '::Lexicon'}{'HASH'} )) {
478 push @lex_refs, *{$class . '::Lexicon'}{'HASH'};
479 DEBUG and warn '%' . $class . '::Lexicon contains ',
480 scalar(keys %{$class . '::Lexicon'}), " entries\n";
483 # Implements depth(height?)-first recursive searching of superclasses.
484 # In hindsight, I suppose I could have just used Class::ISA!
485 foreach my $superclass (@{$class . '::ISA'}) {
486 DEBUG and warn " Super-class search into $superclass\n";
487 next if $seen_r->{$superclass}++;
488 push @lex_refs, @{&_lex_refs($superclass, $seen_r)}; # call myself
491 $isa_scan{$class} = \@lex_refs; # save for next time
495 sub clear_isa_scan { %isa_scan = (); return; } # end on a note of simplicity!
497 #--------------------------------------------------------------------------
500 # This big scary routine compiles an entry.
501 # It returns either a coderef if there's brackety bits in this, or
502 # otherwise a ref to a scalar.
504 my $string_to_compile = $_[1]; # There are taint issues using regex on @_ - perlbug 60378,27344
506 # The while() regex is more expensive than this check on strings that don't need a compile.
507 # this op causes a ~2% speed hit for strings that need compile and a 250% speed improvement
508 # on strings that don't need compiling.
509 return \"$string_to_compile" if($string_to_compile !~ m/[\[~\]]/ms); # return a string ref if chars [~] are not in the string
511 my $target = ref($_[0]) || $_[0];
514 my(@c) = (''); # "chunks" -- scratch.
518 my $in_group = 0; # start out outside a group
519 my($m, @params); # scratch
521 while($string_to_compile =~ # Iterate over chunks.
523 [^\~\[\]]+ # non-~[] stuff (Capture everything else here)
525 ~. # ~[, ~], ~~, ~other
527 \[ # [ presumably opening a group
529 \] # ] presumably closing a group
536 DEBUG>2 and warn qq{ "$1"\n};
538 if($1 eq '[' or $1 eq '') { # "[" or end
539 # Whether this is "[" or end, force processing of any
543 $target->_die_pointing($string_to_compile, 'Unterminated bracket group');
546 $target->_die_pointing($string_to_compile, 'You can\'t nest bracket groups');
551 DEBUG>2 and warn " [end-string]\n";
556 die "How come \@c is empty?? in <$string_to_compile>" unless @c; # sanity
558 # Now actually processing the preceding literal
560 if($USE_LITERALS and (
562 ? $c[-1] !~ m/[^\x20-\x7E]/s
563 # ASCII very safe chars
564 : $c[-1] !~ m/[^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~\x07]/s
565 # EBCDIC very safe chars
567 # normal case -- all very safe chars
569 push @code, q{ '} . $c[-1] . "',\n";
570 $c[-1] = ''; # reuse this slot
573 push @code, ' $c[' . $#c . "],\n";
574 push @c, ''; # new chunk
577 # else just ignore the empty string.
581 elsif($1 eq ']') { # "]"
582 # close group -- go back in-band
586 DEBUG>2 and warn " --Closing group [$c[-1]]\n";
588 # And now process the group...
590 if(!length($c[-1]) or $c[-1] =~ m/^\s+$/s) {
591 DEBUG>2 and warn " -- (Ignoring)\n";
592 $c[-1] = ''; # reset out chink
596 #$c[-1] =~ s/^\s+//s;
597 #$c[-1] =~ s/\s+$//s;
598 ($m,@params) = split(/,/, $c[-1], -1); # was /\s*,\s*/
600 # A bit of a hack -- we've turned "~,"'s into DELs, so turn
601 # 'em into real commas here.
602 if (ord('A') == 65) { # ASCII, etc
603 foreach($m, @params) { tr/\x7F/,/ }
605 else { # EBCDIC (1047, 0037, POSIX-BC)
606 # Thanks to Peter Prymmer for the EBCDIC handling
607 foreach($m, @params) { tr/\x07/,/ }
610 # Special-case handling of some method names:
611 if($m eq '_*' or $m =~ m/^_(-?\d+)$/s) {
612 # Treat [_1,...] as [,_1,...], etc.
617 $m = 'quant'; # "*" for "times": "4 cars" is 4 times "cars"
620 $m = 'numf'; # "#" for "number": [#,_1] for "the number _1"
623 # Most common case: a simple, legal-looking method name
625 # 0-length method name means to just interpolate:
628 elsif($m =~ /^\w+(?:\:\:\w+)*$/s
629 and $m !~ m/(?:^|\:)\d/s
630 # exclude starting a (sub)package or symbol with a digit
632 # Yes, it even supports the demented (and undocumented?)
633 # $obj->Foo::bar(...) syntax.
634 $target->_die_pointing(
635 $string_to_compile, q{Can't use "SUPER::" in a bracket-group method},
638 if $m =~ m/^SUPER::/s;
639 # Because for SUPER:: to work, we'd have to compile this into
640 # the right package, and that seems just not worth the bother,
641 # unless someone convinces me otherwise.
643 push @code, ' $_[0]->' . $m . '(';
646 # TODO: implement something? or just too icky to consider?
647 $target->_die_pointing(
649 "Can't use \"$m\" as a method name in bracket group",
654 pop @c; # we don't need that chunk anymore
657 foreach my $p (@params) {
659 # Meaning: all parameters except $_[0]
660 $code[-1] .= ' @_[1 .. $#_], ';
661 # and yes, that does the right thing for all @_ < 3
663 elsif($p =~ m/^_(-?\d+)$/s) {
665 $code[-1] .= '$_[' . (0 + $1) . '], ';
667 elsif($USE_LITERALS and (
669 ? $p !~ m/[^\x20-\x7E]/s
670 # ASCII very safe chars
671 : $p !~ m/[^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~\x07]/s
672 # EBCDIC very safe chars
674 # Normal case: a literal containing only safe characters
676 $code[-1] .= q{'} . $p . q{', };
679 # Stow it on the chunk-stack, and just refer to that.
681 push @code, ' $c[' . $#c . '], ';
689 $target->_die_pointing($string_to_compile, q{Unbalanced ']'});
693 elsif(substr($1,0,1) ne '~') {
694 # it's stuff not containing "~" or "[" or "]"
695 # i.e., a literal blob
699 elsif($1 eq '~~') { # "~~"
703 elsif($1 eq '~[') { # "~["
707 elsif($1 eq '~]') { # "~]"
711 elsif($1 eq '~,') { # "~,"
713 # This is a hack, based on the assumption that no-one will actually
714 # want a DEL inside a bracket group. Let's hope that's it's true.
715 if (ord('A') == 65) { # ASCII etc
718 else { # EBCDIC (cp 1047, 0037, POSIX-BC)
727 elsif($1 eq '~') { # possible only at string-end, it seems.
732 # It's a "~X" where X is not a special character.
733 # Consider it a literal ~ and X.
740 undef $big_pile; # Well, nevermind that.
743 # It's all literals! Ahwell, that can happen.
744 # So don't bother with the eval. Return a SCALAR reference.
748 die q{Last chunk isn't null??} if @c and length $c[-1]; # sanity
749 DEBUG and warn scalar(@c), " chunks under closure\n";
750 if(@code == 0) { # not possible?
751 DEBUG and warn "Empty code\n";
754 elsif(@code > 1) { # most cases, presumably!
755 unshift @code, "join '',\n";
757 unshift @code, "use strict; sub {\n";
760 DEBUG and warn @code;
761 my $sub = eval(join '', @code);
762 die "$@ while evalling" . join('', @code) if $@; # Should be impossible.
766 #--------------------------------------------------------------------------
769 # This is used by _compile to throw a fatal error
770 my $target = shift; # class name
771 # ...leaving $_[0] the error-causing text, and $_[1] the error message
773 my $i = index($_[0], "\n");
776 my $pos = pos($_[0]) - (defined($_[2]) ? $_[2] : 0) - 1;
778 $pointy = "^=== near there\n";
780 else { # we need to space over
781 my $first_tab = index($_[0], "\t");
782 if($pos > 2 and ( -1 == $first_tab or $first_tab > pos($_[0]))) {
783 # No tabs, or the first tab is harmlessly after where we will point to,
784 # AND we're far enough from the margin that we can draw a proper arrow.
785 $pointy = ('=' x $pos) . "^ near there\n";
788 # tabs screw everything up!
789 $pointy = substr($_[0],0,$pos);
790 $pointy =~ tr/\t //cd;
791 # make everything into whitespace, but preserving tabs
792 $pointy .= "^=== near there\n";
796 my $errmsg = "$_[1], in\:\n$_[0]";
800 $errmsg .= "\n" . $pointy;
802 elsif($i == (length($_[0]) - 1) ) {
803 # Already has a newline at end.
807 # don't bother with the pointy bit, I guess.
809 Carp::croak( "$errmsg via $target, as used" );