From 7c9dced1e223d99ca5284554a9c93edbd0f8054a Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Mon, 29 May 2006 03:41:35 +0000 Subject: [PATCH] Lirama::Loc3 --- Lirama/Loc2/Auto.pm | 140 ------------------------------------ Lirama/{Loc2.pm => Loc3.pm} | 69 +++++++++--------- 2 files changed, 37 insertions(+), 172 deletions(-) delete mode 100644 Lirama/Loc2/Auto.pm rename Lirama/{Loc2.pm => Loc3.pm} (63%) diff --git a/Lirama/Loc2/Auto.pm b/Lirama/Loc2/Auto.pm deleted file mode 100644 index f5a5340..0000000 --- a/Lirama/Loc2/Auto.pm +++ /dev/null @@ -1,140 +0,0 @@ -package Lirama::Loc2::Auto; - -use strict; -use warnings; -use utf8; - -use Lirama::Loc2; - -our $VERSION = '2.00'; - -sub optimize { - my $self = shift; - $self->[1]{optimize} = $_[0] if @_; - return $self->[1]{optimize}; -} # optimize - -sub TIEHASH { - my ($class, $path, @langpref) = @_; - - my $langs = @langpref>0; # languages specified - langpref(@langpref) if $langs; # set default language preference - - my $node = [ # array for faster access to common [0] - {}, # files - { # configuration - path => $path, - optimize => $langs, # assume langpref won't change if specified - } - ]; - return bless $node, $class; -} # new - -sub FETCH { - my ($self, $file) = @_; - - unless (exists $self->[0]{$file}) { - my $filename = $self->[1]{path}.'/'.$file; # perl file returning l10n hash - my $extlang = '.'.langpref(); # file tag for the preferred language - my $ext = ".pl"; # extension for $filename - - $filename .= $extlang # get language-optimized file - if $self->[1]{optimize} and -r $filename.$extlang.$ext; - - tie %{$self->[0]{$file}}, "Lirama::Loc2", do $filename.$ext; - } # initialize Loc for this file - - return $self->[0]{$file} -} # FETCH - -# Basically same as Tie::StdHash - -sub EXISTS { exists $_[0]->[0]{$_[1]} } -sub FIRSTKEY { my $a = scalar keys %{$_[0]->[0]}; each %{$_[0]->[0]} } -sub NEXTKEY { each %{$_[0]->[0]} } - -1; - -__END__ - - -=head1 NAME - -Lirama::Loc::Auto - Localize strings, initialize automatically - -=head1 SYNOPSIS - - use Lirama::Loc::Auto; - - tie my %L, "Lirama::Loc::Auto", "path/to/translation/files", "en"; - - print $L{numbers}{7}; # "seven" - -=head1 DESCRIPTION - -Setup a hash containing multiple L objects, which -open automatically on demand. - -=over 4 - -=item C - -Will setup a hash to automatically load data files from P<$path/*.pl>. -These files should only return an anonimous translation hash. - -If @languages is set, it will be taken as the default language preference. -[todo:optimize] - -=item C<$tie{$file}{$string}> - -The first time a translation is accessed from C<$tie{$file}{$string}>, it will -create a Lirama::Loc object from P<$path/$file.pl>. - -=item C - -=item C - -bla - -=back - -=head1 EXAMPLE - -We'll create P<./includes/loc/index.pl> with the following contents: - - { - welcome => { - en => "welcome", - eo => "bonvenon", - nl => "welkom", - }, - "%d visits" => { - en => sub { "%d user".($_[0]!=1 && "s")." have visited this site." }, - }, - } - -Setup a hash with user defined language preferences, and a fallback to English. - - my @langpref = split(',', $ENV{HTTP_ACCEPT_LANGUAGE}); - tie my %loc, "Lirama::Loc::Auto", "includes/loc", @langpref, "en"; - -And display the I string: - - print $loc{index}{welcome}; - -If the user wanted Dutch texts, it will show I. In case the user wanted -French for example, it will fallback to English. - - print $loc{index}{["%d visits", ++$counter]}; - -=head1 SEE ALSO - -L - -=head1 AUTHOR - -Mischa POSLAWSKY - -Copyright 2005 Mischa POSLAWSKY. All rights reserved. - -=cut diff --git a/Lirama/Loc2.pm b/Lirama/Loc3.pm similarity index 63% rename from Lirama/Loc2.pm rename to Lirama/Loc3.pm index 0ea0410..2a89689 100644 --- a/Lirama/Loc2.pm +++ b/Lirama/Loc3.pm @@ -1,22 +1,9 @@ -package Lirama::Loc2; +package Lirama::Loc3; use strict; use warnings; -use utf8; #todo: does it even matter? -our $VERSION = '2.00'; - -require Exporter; -our @ISA = qw(Exporter); -our @EXPORT = qw(langpref); -our @EXPORT_OK = qw(loc); - -our @langpref;# = qw(en eo nl sv el hu de fr ru kr); - -sub langpref { - @langpref = @_ if @_; # set order of languages (prefered language first) - return wantarray ? @langpref : $langpref[0]; -} +our $VERSION = '3.00'; sub loc($) { my $this = shift; @@ -24,35 +11,54 @@ sub loc($) { # if it isn't, we're done already: ref $_[0] eq "HASH" or return $_[0]; # localize to most preferred language - defined $_[0]{$_} and return $_[0]{$_} for @langpref; -} + defined $_[0]{$_} and return $_[0]{$_} for @{$this->{-langpref}}; +} # loc sub TIEHASH { + #todo: set -path return bless $_[1], $_[0]; # bless the l10n hash } sub FETCH { my $this = shift; - # custom expand: get preferred language from given hash - # return $this->loc($_[0]) if ref $_[0] eq "HASH"; # deprecated in favor of loc() + # get setting (denoted by leading dash) + return wantarray ? @{$this->{$_[0]}} : $this->{$_[0]}->[0] + if $_[0] eq "-langpref"; + return $this->{$_[0]} + if $_[0] eq "-path"; # array ref used for passing arguments @_ = @{$_[0]} if ref $_[0] eq "ARRAY"; # get localized string by identifier local $_ = shift; + # add default path unless specified + $_ = $this->{-path} . '_' . $_ unless /_/; #todo: shouldn't occur - find out where this is done, then fix and remove this check # defined $_ or return ''; $_ = $this->loc($this->{$_}) if exists $this->{$_}; -# $_ = ref $this->{$_} eq "HASH" ? $this->loc($this->{$_}) : $this->{$_} if exists $this->{$_}; - # static output if no arguments given - return $_ unless @_; # unnecessary but faster for common case + #todo: else remove path # adaptive string (code) $_ = $_->(@_) if ref $_ eq "CODE"; + # static output if no arguments given + return $_ unless @_; # unnecessary but faster for common case # dynamic output return sprintf $_, @_; -} +} # FETCH + +sub STORE { + my ($this, $option, $val) = @_; + if ($option eq "-langpref") { + # set order of languages (prefered language first) + $this->{$option} = $val; + } # -langpref + else { + $this->{$option} = $val; +# $_[0]->{$_[1]} = $_[2]; + } +} # STORE # Same as found in Tie::StdHash +#todo: make path-aware sub EXISTS { exists $_[0]->{$_[1]} } sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} } sub NEXTKEY { each %{$_[0]} } @@ -64,21 +70,20 @@ __END__ =head1 NAME -Lirama::Loc - Localize strings +Lirama::Loc3 - Localize strings =head1 SYNOPSIS - use Lirama::Loc; - - langpref(qw/nl en eo/); # prefer I (dutch) texts + use Lirama::Loc3; - tie my %loc, "Lirama::Loc", { - test => { + tie my %loc, "Lirama::Loc3", { + _test => { en => "this is a test", eo => "cxi tio estas testo", }, }; + $loc{-langpref} = [qw/nl en eo/]; # prefer I (dutch) texts print $loc{test}; # "this is a test", since dutch is unavailable =head1 DESCRIPTION @@ -99,14 +104,14 @@ may yet be a very bad idea (does it work correctly in modperl?) =item C -True if identifier is localized; non-existing strings still return -themselves, so in standard meaning everything would exist. +True if identifier is localized; +even though non-existing strings still return themselves. =back =head1 SEE ALSO -L +L L -- 2.30.0