Андрей (andy) wrote in changelog,
Андрей
andy
changelog

[bml] r137: LJSUP-9653 (ljlang refactoring)

Committer: ailyin
LJSUP-9653 (ljlang refactoring)
U   trunk/lib/Apache/BML.pm
Modified: trunk/lib/Apache/BML.pm
===================================================================
--- trunk/lib/Apache/BML.pm	2011-08-22 06:28:30 UTC (rev 136)
+++ trunk/lib/Apache/BML.pm	2011-09-01 07:24:41 UTC (rev 137)
@@ -51,6 +51,9 @@
 my %FileConfig;      # filename -> hashref
 my %FileLastStat;    # filename -> time we last looked at its modtime
 
+my ( $CurrentSite, %Implementations );
+my ( $MLGetter, $CurrentLanguage );
+
 use vars qw($base_recent_mod);
 
 # the request we're handling (Apache->request).  using this way
@@ -796,13 +799,7 @@
         if ($type eq "_ML")
         {
             my $code = $data;
-            return $code
-                if $req->{'lang'} eq 'debug';
-            my $getter = $req->{'env'}->{'HOOK-ml_getter'};
-            return "[ml_getter not defined]" unless $getter;
-            $code = LJ::Request->uri . $code
-                if rindex($code, '.', 0) == 0;
-            return $getter->($req->{'lang'}, $code);
+            return BML::ml($code);
         }
 
         # an _INFO block contains special internal information, like which
@@ -1646,8 +1643,11 @@
     return $_[0];
 }
 
-sub decide_language
-{
+sub decide_language {
+    if ( my $impl = implementation('decide_language') ) {
+        return $impl->(@_);
+    }
+
     return undef unless Apache::BML::is_initialized();
 
     my BML::Request $req = $Apache::BML::cur_req;
@@ -1965,28 +1965,60 @@
     return $a;
 }
 
-sub get_language
-{
+sub current_site {
+    my (@args) = @_;
+
+    my $ret = $CurrentSite;
+    if (@args) { $CurrentSite = $args[0]; }
+    return $ret;
+}
+
+sub implementation {
+    my ( $key, @args ) = @_;
+
+    # don't rely on auto-vivification
+    $Implementations{$CurrentSite} ||= {};
+
+    my $ret = $Implementations{$CurrentSite}->{$key};
+    if (@args) { $Implementations{$CurrentSite}->{$key} = $args[0]; }
+    return $ret;
+}
+
+sub get_language {
+    if ( my $impl = implementation('get_language') ) {
+        return $impl->(@_);
+    }
+
     return undef unless Apache::BML::is_initialized();
     return $Apache::BML::cur_req->{'lang'};
 }
 
-sub get_language_default
-{
+sub get_language_default {
     return "en" unless Apache::BML::is_initialized();
     return $Apache::BML::cur_req->{'env'}->{'DefaultLanguage'} || "en";
 }
 
 sub get_language_scope {
+    if ( my $impl = implementation('get_language_scope') ) {
+        return $impl->(@_);
+    }
+
     return $BML::ML_SCOPE;
 }
 
 sub set_language_scope {
+    if ( my $impl = implementation('set_language_scope') ) {
+        return $impl->(@_);
+    }
+
     $BML::ML_SCOPE = shift;
 }
 
-sub set_language
-{
+sub set_language {
+    if ( my $impl = implementation('set_language') ) {
+        return $impl->(@_);
+    }
+
     my ($lang, $getter) = @_;  # getter is optional
     my BML::Request $req = $Apache::BML::cur_req;
     LJ::Request->notes('langpref' => $lang);
@@ -1999,43 +2031,30 @@
         $getter ||= $req->{'env'}->{'HOOK-ml_getter'};
     }
 
-    no strict 'refs';
-    if ($lang eq "debug") {
-        no warnings 'redefine';
-        *{"BML::ml"} = sub {
-            return $_[0];
-        };
-        *{"BML::ML::FETCH"} = sub {
-            return $_[1];
-        };
-    } elsif ($getter) {
-        no warnings 'redefine';
-        *{"BML::ml"} = sub {
-            my ($code, $vars) = @_;
-            $code = $BML::ML_SCOPE . $code
-                if rindex($code, '.', 0) == 0;
-            return $getter->($lang, $code, undef, $vars);
-        };
-        *{"BML::ML::FETCH"} = sub {
-            my $code = $_[1];
-            $code = $BML::ML_SCOPE . $code
-                if rindex($code, '.', 0) == 0;
-            return $getter->($lang, $code);
-        };
-    };
+    $CurrentLanguage = $lang;
+    $MLGetter = $getter if $getter;
 
 }
 
 # multi-lang string
-# note: sub is changed when BML::set_language is called
-sub ml
-{
-    return "[ml_getter not defined]";
-}
+sub ml {
+    if ( my $impl = implementation('ml') ) {
+        return $impl->(@_);
+    }
 
-sub ml_multi {
-    my $codes = shift;
-    return LJ::Lang::get_text_multi(get_language(), undef, $codes);
+    my ( $code, $vars ) = @_;
+
+    return "[ml_getter not defined]" unless $MLGetter;
+
+    if ( $CurrentLanguage eq 'debug' ) {
+        return $code;
+    }
+
+    if ( $code =~ /^[.]/ ) {
+        $code = BML::get_language_scope() . $code;
+    }
+
+    return $MLGetter->( $CurrentLanguage, $code, undef, $vars );
 }
 
 sub eurl
@@ -2288,9 +2307,9 @@
     return $self;
 }
 
-# note: sub is changed when BML::set_language is called.
 sub FETCH {
-    return "[ml_getter not defined]";
+    my ( $class, $key ) = @_;
+    return BML::ml($key);
 }
 
 # do nothing

Tags: andy, bml, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments