lib/Locale/Maketext.pm Locale::Maketext
lib/Locale/Maketext.pod Locale::Maketext documentation
lib/Locale/Maketext/README Locale::Maketext
-lib/Locale/Maketext/t/00about.t See if Locale::Maketext works
-lib/Locale/Maketext/t/01make.t See if Locale::Maketext works
-lib/Locale/Maketext/t/02get.t See if Locale::Maketext works
-lib/Locale/Maketext/t/03http.t See if Locale::Maketext works
-lib/Locale/Maketext/t/04super.t See if Locale::Maketext works
-lib/Locale/Maketext/t/05super.t See if Locale::Maketext works
-lib/Locale/Maketext/t/06super.t See if Locale::Maketext works
-lib/Locale/Maketext/t/90utf8.t Locale::Maketext
+lib/Locale/Maketext/t/01_about_verbose.t See if Locale::Maketext works
+lib/Locale/Maketext/t/10_make.t See if Locale::Maketext works
+lib/Locale/Maketext/t/20_get.t See if Locale::Maketext works
+lib/Locale/Maketext/t/40_super.t See if Locale::Maketext works
+lib/Locale/Maketext/t/50_super.t See if Locale::Maketext works
+lib/Locale/Maketext/t/60_super.t See if Locale::Maketext works
+lib/Locale/Maketext/t/90_utf8.t See if Locale::Maketext works
lib/Locale/Maketext/TPJ13.pod Locale::Maketext documentation article
lib/locale.pm For "use locale"
lib/Locale/Script.pm Locale::Codes
-# Time-stamp: "2004-01-19 15:11:14 AST"
+# Time-stamp: "2004-03-30 16:33:31 AST"
require 5;
package Locale::Maketext;
use vars qw( @ISA $VERSION $MATCH_SUPERS $USING_LANGUAGE_TAGS
$USE_LITERALS $MATCH_SUPERS_TIGHTLY);
use Carp ();
-use I18N::LangTags 0.21 ();
+use I18N::LangTags 0.30 ();
#--------------------------------------------------------------------------
BEGIN { unless(defined &DEBUG) { *DEBUG = sub () {0} } }
# define the constant 'DEBUG' at compile-time
-$VERSION = "1.08";
+$VERSION = "1.09";
@ISA = ();
$MATCH_SUPERS = 1;
my($base_class, @languages) = @_;
$base_class = ref($base_class) || $base_class;
# Complain if they use __PACKAGE__ as a project base class?
-
- @languages = $base_class->_ambient_langprefs() unless @languages;
+
+ if( @languages ) {
+ DEBUG and print "Lgs\@", __LINE__, ": ", map("<$_>", @languages), "\n";
+ if($USING_LANGUAGE_TAGS) { # An explicit language-list was given!
+ @languages =
+ map {; $_, I18N::LangTags::alternate_language_tags($_) }
+ # Catch alternation
+ map I18N::LangTags::locale2language_tag($_),
+ # If it's a lg tag, fine, pass thru (untainted)
+ # If it's a locale ID, try converting to a lg tag (untainted),
+ # otherwise nix it.
+ @languages;
+ DEBUG and print "Lgs\@", __LINE__, ": ", map("<$_>", @languages), "\n";
+ }
+ } else {
+ @languages = $base_class->_ambient_langprefs;
+ }
+
@languages = $base_class->_langtag_munging(@languages);
my %seen;
sub _langtag_munging {
my($base_class, @languages) = @_;
+ # We have all these DEBUG statements because otherwise it's hard as hell
+ # to diagnose ifwhen something goes wrong.
+
DEBUG and print "Lgs1: ", map("<$_>", @languages), "\n";
if($USING_LANGUAGE_TAGS) {
- @languages = map &I18N::LangTags::locale2language_tag($_), @languages;
- # if it's a lg tag, fine, pass thru (untainted)
- # if it's a locale ID, try converting to a lg tag (untainted),
- # otherwise nix it.
-
- @languages = map {; $_, I18N::LangTags::alternate_language_tags($_) }
- @languages; # catch alternation
DEBUG and print "Lgs\@", __LINE__, ": ", map("<$_>", @languages), "\n";
-
@languages = $base_class->_add_supers( @languages );
- if( defined &I18N::LangTags::panic_languages ) {
- push @languages, I18N::LangTags::panic_languages(@languages);
- DEBUG and print "After adding panic languages:\n",
- " Lgs\@", __LINE__, ": ", map("<$_>", @languages), "\n";
- }
+ push @languages, I18N::LangTags::panic_languages(@languages);
+ DEBUG and print "After adding panic languages:\n",
+ " Lgs\@", __LINE__, ": ", map("<$_>", @languages), "\n";
push @languages, $base_class->fallback_languages;
# You are free to override fallback_languages to return empty-list!
DEBUG and print "Lgs\@", __LINE__, ": ", map("<$_>", @languages), "\n";
- @languages = # final bit of processing:
+ @languages = # final bit of processing to turn them into classname things
map {
my $it = $_; # copy
$it =~ tr<-A-Z><_a-z>; # lc, and turn - to _
###########################################################################
sub _ambient_langprefs {
- my $base_class = $_[0];
-
- return $base_class->_http_accept_langs
- if length( $ENV{'REQUEST_METHOD'} || '' ); # I'm a CGI
- # it's off in its own routine because it's complicated
-
- # Not running as a CGI: try to puzzle out from the environment
- my @languages;
-
- if(length( $ENV{'LANG'} || '' )) {
- push @languages, split m/[,:]/, $ENV{'LANG'};
- # LANG can be only /one/ locale as far as I know, but what the hey.
- }
-
- if(length( $ENV{'LANGUAGE'} || '' )) {
- push @languages, split m/[,:]/, $ENV{'LANGUAGE'};
- }
-
- print "Noting ENV LANG ", join(',', @languages),"\n" if DEBUG;
- # Those are really locale IDs, but they get xlated a few lines down.
-
- if(&_try_use('Win32::Locale')) {
- # If we have that module installed...
- push @languages, Win32::Locale::get_language() || ''
- if defined &Win32::Locale::get_language;
- }
-
- return @languages;
+ require I18N::LangTags::Detect;
+ return I18N::LangTags::Detect::detect();
}
###########################################################################
} elsif( $MATCH_SUPERS_TIGHTLY ) {
DEBUG and print "Before adding new supers tightly:\n",
" Lgs\@", __LINE__, ": ", map("<$_>", @languages), "\n";
-
- my %seen_encoded;
- foreach my $lang (@languages) {
- $seen_encoded{ I18N::LangTags::encode_language_tag($lang) } = 1
- }
-
- my(@output_languages);
- foreach my $lang (@languages) {
- push @output_languages, $lang;
- foreach my $s ( I18N::LangTags::super_languages($lang) ) {
- # Note that super_languages returns the longest first.
- last if $seen_encoded{ I18N::LangTags::encode_language_tag($s) };
- push @output_languages, $s;
- }
- }
- @languages = @output_languages;
-
+ @languages = I18N::LangTags::implicate_supers( @languages );
DEBUG and print "After adding new supers tightly:\n",
" Lgs\@", __LINE__, ": ", map("<$_>", @languages), "\n";
} else {
-
- push @languages, map I18N::LangTags::super_languages($_), @languages;
+ DEBUG and print "Before adding supers to end:\n",
+ " Lgs\@", __LINE__, ": ", map("<$_>", @languages), "\n";
+ @languages = I18N::LangTags::implicate_supers_strictly( @languages );
DEBUG and print "After adding supers to end:\n",
" Lgs\@", __LINE__, ": ", map("<$_>", @languages), "\n";
}
use Locale::Maketext::GutsLoader;
-sub _http_accept_langs {
- # Deal with HTTP "Accept-Language:" stuff. Hassle.
- # This code is more lenient than RFC 3282, which you must read.
- # Hm. Should I just move this into I18N::LangTags at some point?
- no integer;
-
- my $in = (@_ > 1) ? $_[1] : $ENV{'HTTP_ACCEPT_LANGUAGE'};
- # (always ends up untainting)
-
- return() unless defined $in and length $in;
-
- $in =~ s/\([^\)]*\)//g; # nix just about any comment
-
- if( $in =~ m/^\s*([a-zA-Z][-a-zA-Z]+)\s*$/s ) {
- # Very common case: just one language tag
- return lc $1;
- } elsif( $in =~ m/^\s*[a-zA-Z][-a-zA-Z]+(?:\s*,\s*[a-zA-Z][-a-zA-Z]+)*\s*$/s ) {
- # Common case these days: just "foo, bar, baz"
- return map lc($_), $in =~ m/([a-zA-Z][-a-zA-Z]+)/g;
- }
-
- # Else it's complicated...
-
- $in =~ s/\s+//g; # Yes, we can just do without the WS!
- my @in = $in =~ m/([^,]+)/g;
- my %pref;
-
- my $q;
- foreach my $tag (@in) {
- next unless $tag =~
- m/^([a-zA-Z][-a-zA-Z]+)
- (?:
- ;q=
- (
- \d* # a bit too broad of a RE, but so what.
- (?:
- \.\d+
- )?
- )
- )?
- $
- /sx
- ;
- $q = (defined $2 and length $2) ? $2 : 1;
- #print "$1 with q=$q\n";
- push @{ $pref{$q} }, lc $1;
- }
-
- return # Read off %pref, in descending key order...
- map @{$pref{$_}},
- sort {$b <=> $a}
- keys %pref;
-}
-
###########################################################################
my %tried = ();
Revision history for Perl suite Locale::Maketext
- Time-stamp: "2004-01-19 15:11:56 AST"
+ Time-stamp: "2004-03-30 21:38:07 AST"
+2004-03-30 Sean M. Burke sburke@cpan.org
+ * Release 1.09:
+
+ * Moved the language-preference-detecting code into new module
+ I18N::LangTags::Detect.
+
+ Thanks to Autrijus Tang for catching some errors in the dist!
+
+
+
2004-01-19 Sean M. Burke sburke@cpan.org
* Release 1.08:
README for Locale::Maketext
- Time-stamp: "2004-01-11 18:36:09 AST"
+ Time-stamp: "2004-03-30 16:02:27 AST"
Locale::Maketext
article in The Perl Journal #13 (a corrected version of which appears
in this dist).
-This is a complete rewrite from the basically undocumented 0.x
-versions.
-
-
PREREQUISITES
+++ /dev/null
-
-require 5;
-use Test;
-BEGIN { plan tests => 1; }
-use Locale::Maketext 1.01;
-
-print "#\n#\n",
- "# Locale::Maketext v$Locale::Maketext::VERSION\n",
- "# I18N::LangTags v", $I18N::LangTags::VERSION || "?", "\n",
- "#\n#\n",
-;
-
-print "# Running under perl version $] for $^O",
- (chr(65) eq 'A') ? "\n" : " in a non-ASCII world\n";
-
-print "# Win32::BuildNumber ", &Win32::BuildNumber(), "\n"
- if defined(&Win32::BuildNumber) and defined &Win32::BuildNumber();
-
-print "# MacPerl verison $MacPerl::Version\n"
- if defined $MacPerl::Version;
-
-printf
- "# Current time local: %s\n# Current time GMT: %s\n",
- scalar( gmtime($^T)), scalar(localtime($^T));
-
-print "# Using Test.pm v", $Test::VERSION || "?", "\n";
-
-ok 1;
-
--- /dev/null
+
+require 5;
+# Time-stamp: "2004-03-30 17:02:53 AST"
+
+# Summary of, well, things.
+
+use Test;
+BEGIN {plan tests => 2};
+
+ok 1;
+
+use Locale::Maketext;
+
+#chdir "t" if -e "t";
+
+{
+ my @out;
+ push @out,
+ "\n\nPerl v",
+ defined($^V) ? sprintf('%vd', $^V) : $],
+ " under $^O ",
+ (defined(&Win32::BuildNumber) and defined &Win32::BuildNumber())
+ ? ("(Win32::BuildNumber ", &Win32::BuildNumber(), ")") : (),
+ (defined $MacPerl::Version)
+ ? ("(MacPerl version $MacPerl::Version)") : (),
+ "\n"
+ ;
+
+ # Ugly code to walk the symbol tables:
+ my %v;
+ my @stack = (''); # start out in %::
+ my $this;
+ my $count = 0;
+ my $pref;
+ while(@stack) {
+ $this = shift @stack;
+ die "Too many packages?" if ++$count > 1000;
+ next if exists $v{$this};
+ next if $this eq 'main'; # %main:: is %::
+
+ #print "Peeking at $this => ${$this . '::VERSION'}\n";
+
+ if(defined ${$this . '::VERSION'} ) {
+ $v{$this} = ${$this . '::VERSION'}
+ } elsif(
+ defined *{$this . '::ISA'} or defined &{$this . '::import'}
+ or ($this ne '' and grep defined *{$_}{'CODE'}, values %{$this . "::"})
+ # If it has an ISA, an import, or any subs...
+ ) {
+ # It's a class/module with no version.
+ $v{$this} = undef;
+ } else {
+ # It's probably an unpopulated package.
+ ## $v{$this} = '...';
+ }
+
+ $pref = length($this) ? "$this\::" : '';
+ push @stack, map m/^(.+)::$/ ? "$pref$1" : (), keys %{$this . '::'};
+ #print "Stack: @stack\n";
+ }
+ push @out, " Modules in memory:\n";
+ delete @v{'', '[none]'};
+ foreach my $p (sort {lc($a) cmp lc($b)} keys %v) {
+ $indent = ' ' x (2 + ($p =~ tr/:/:/));
+ push @out, ' ', $indent, $p, defined($v{$p}) ? " v$v{$p};\n" : ";\n";
+ }
+ push @out, sprintf "[at %s (local) / %s (GMT)]\n",
+ scalar(gmtime), scalar(localtime);
+ my $x = join '', @out;
+ $x =~ s/^/#/mg;
+ print $x;
+}
+
+print "# Running",
+ (chr(65) eq 'A') ? " in an ASCII world.\n" : " in a non-ASCII world.\n",
+ "#\n",
+;
+
+print "# \@INC:\n", map("# [$_]\n", @INC), "#\n#\n";
+
+print "# \%INC:\n";
+foreach my $x (sort {lc($a) cmp lc($b)} keys %INC) {
+ print "# [$x] = [", $INC{$x} || '', "]\n";
+}
+
+ok 1;
+
+++ /dev/null
-
-use Locale::Maketext;
-
-use Test;
-BEGIN { plan tests => 87 };
-
-my @in = grep m/\S/, split /\n/, q{
-
-[ sv ] sv
-[ en ] en
-[ en fi ] en, fi
-[ en-us ] en-us
-[ en-us ] en-US
-[ en-us ] EN-US
-
-[ en-au en i-klingon en-gb en-us mt-mt mt ja ] EN-au, JA;q=0.14, i-klingon;q=0.83, en-gb;q=0.71, en-us;q=0.57, mt-mt;q=0.43, mt;q=0.29, en;q=0.86
-[ en-au en i-klingon en-gb en-us mt-mt mt tli ja ] EN-au, tli;q=0.201, JA;q=0.14, i-klingon;q=0.83, en-gb;q=0.71, en-us;q=0.57, mt-mt;q=0.43, mt;q=0.29, en;q=0.86
-[ en-au en en-gb en-us ja ] en-au, ja;q=0.20, en-gb;q=0.60, en-us;q=0.40, en;q=0.80
-
-[ en-au en en-gb en-us mt-mt mt ja ] EN-au, JA;q=0.14, en-gb;q=0.71, en-us;q=0.57, mt-mt;q=0.43, mt;q=0.29, en;q=0.86
-[ en-au en en-gb en-us ja ] en-au, ja;q=0.20, en-gb;q=0.60, en-us;q=0.40, en;q=0.80
-[ en fr ] en;q=1,fr;q=.5
-[ en fr ] en;q=1,fr;q=.99
-[ en ru ko ] en, ru;q=0.7, ko;q=0.3
-[ en ru ko ] en, ru;q=0.7, KO;q=0.3
-[ en-us en ] en-us, en;q=0.50
-[ en fr ] fr ; q = 0.9, en
-[ en fr ] en,fr;q=.90
-[ ru en-uk en fr ] ru, en-UK;q=0.5, en;q=0.3, fr;q=0.1
-[ en-us fr es-mx ] en-us,fr;q=0.7,es-mx;q=0.3
-[ en-us en ] en-us, en;q=0.50
-
-[ da en-gb en ] da, en-gb;q=0.8, en;q=0.7
-[ da en-gb en ] da, en;q=0.7, en-gb;q=0.8
-[ da en-gb en ] da, en-gb;q=0.8, en;q=0.7
-[ da en-gb en ] da,en;q=0.7,en-gb;q=0.8
-[ da en-gb en ] da, en-gb ; q=0.8, en ; q=0.7
-[ da en-gb en ] da , en-gb ; q = 0.8 , en ; q =0.7
-[ da en-gb en ] da (yup, Danish) , en-gb ; q = 0.8 , en ; q =0.7
-
-[ no dk en-uk en-us ] en-UK;q=0.7, en-US;q=0.6, no;q=1.0, dk;q=0.8
-[ no dk en-uk en-us ] en-US;q=0.6, en-UK;q=0.7, no;q=1.0, dk;q=0.8
-[ no dk en-uk en-us ] en-UK;q=0.7, no;q=1.0, en-US;q=0.6, dk;q=0.8
-[ no dk en-uk en-us ] en-UK;q=0.7, no;q=1.0, dk;q=0.8, en-US;q=0.6
-
-[ fi en ] fi;q=1, en;q=0.2
-[ de-de de en en-us en-gb ] de-DE, de;q=0.80, en;q=0.60, en-US;q=0.40, en-GB;q=0.20
-[ ru ] ru; q=1, *; q=0.1
-[ ru en ] ru, en; q=0.1
-[ ja en ] ja,en;q=0.5
-[ en ] en; q=1.0
-[ ja ] ja; q=1.0
-[ ja ] ja; q=1.0
-[ en ja ] en; q=0.5, ja; q=0.5
-[ fr-ca fr en ] fr-ca, fr;q=0.8, en;q=0.7
-[ NIX ] NIX
-};
-
-foreach my $in (@in) {
- $in =~ s/^\s*\[([^\]]+)\]\s*//s or die "Bad input: $in";
- my @should = do { my $x = $1; $x =~ m/(\S+)/g };
-
- if($in eq 'NIX') { $in = ''; @should = (); }
-
- local $ENV{'HTTP_ACCEPT_LANGUAGE'};
-
- foreach my $modus (
- sub {
- print "# Testing with arg...\n";
- $ENV{'HTTP_ACCEPT_LANGUAGE'} = 'PLORK';
- return $_[0];
- },
- sub {
- print "# Testing wath HTTP_ACCEPT_LANGUAGE...\n";
- $ENV{'HTTP_ACCEPT_LANGUAGE'} = $_[0];
- return();
- },
- ) {
- my @args = &$modus($in);
-
- # ////////////////////////////////////////////////////
- my @out = Locale::Maketext->_http_accept_langs(@args);
- # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
-
- if(
- @out == @should
- and lc( join "\e", @out ) eq lc( join "\e", @should )
- ) {
- print "# Happily got [@out] from [$in]\n";
- ok 1;
- } else {
- ok 0;
- print "#Got: [@out]\n",
- "# but wanted: [@should]\n",
- "# < \"$in\"\n#\n";
- }
- }
-}
-
-print "#\n#\n# Bye-bye!\n";
-ok 1;
-
delete $ENV{'MYORP'};
delete $ENV{'SWUZ'};
+
print "# Test LANG...\n";
+$ENV{'LC_ALL'} = '';
+$ENV{'LC_MESSAGES'} = '';
$ENV{'REQUEST_METHOD'} = '';
$ENV{'LANG'} = 'Eu_MT';
$ENV{'LANGUAGE'} = '';
+# Time-stamp: "2004-03-30 18:02:24 AST"
#sub Locale::Maketext::DEBUG () {10}
use Locale::Maketext;
pt-br => pt-br pt
pt-br fr => pt-br fr pt
- pt-br fr pt => pt-br fr pt pt
- pt-br fr pt de => pt-br fr pt de pt
- de pt-br fr pt => de pt-br fr pt pt
+ pt-br fr pt => pt-br fr pt
+ pt-br fr pt de => pt-br fr pt de
+ de pt-br fr pt => de pt-br fr pt
de pt-br fr => de pt-br fr pt
hai pt-br fr => hai pt-br fr pt
# Now test multi-part complicateds:
pt-br-janeiro fr => pt-br-janeiro fr pt-br pt
pt-br-janeiro de fr => pt-br-janeiro de fr pt-br pt
-pt-br-janeiro de pt fr => pt-br-janeiro de pt fr pt-br pt
+pt-br-janeiro de pt fr => pt-br-janeiro de pt fr pt-br
ja pt-br-janeiro fr => ja pt-br-janeiro fr pt-br pt
ja pt-br-janeiro de fr => ja pt-br-janeiro de fr pt-br pt
-ja pt-br-janeiro de pt fr => ja pt-br-janeiro de pt fr pt-br pt
+ja pt-br-janeiro de pt fr => ja pt-br-janeiro de pt fr pt-br
-pt-br-janeiro de pt-br fr => pt-br-janeiro de pt-br fr pt-br pt pt
+pt-br-janeiro de pt-br fr => pt-br-janeiro de pt-br fr pt
# an odd case, since we don't filter for uniqueness in this sub
};