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

[livejournal] r19923: LJSUP-9653 (ljlang refactoring)

Committer: ailyin
LJSUP-9653 (ljlang refactoring)
U   trunk/cgi-bin/Apache/LiveJournal.pm
U   trunk/cgi-bin/LJ/Lang.pm
U   trunk/cgi-bin/LJ/Setting/Language.pm
U   trunk/cgi-bin/modperl.pl
U   trunk/cgi-bin/weblib.pl
U   trunk/htdocs/tools/endpoints/setlang.bml
U   trunk/htdocs/tools/setlang.bml
Modified: trunk/cgi-bin/Apache/LiveJournal.pm
===================================================================
--- trunk/cgi-bin/Apache/LiveJournal.pm	2011-09-01 06:46:19 UTC (rev 19922)
+++ trunk/cgi-bin/Apache/LiveJournal.pm	2011-09-01 07:25:21 UTC (rev 19923)
@@ -97,6 +97,8 @@
     LJ::Request->free();
     LJ::Request->init($r);
 
+    BML::current_site('livejournal');
+
     LJ::run_hooks("post_read_request");
 
     $class = __PACKAGE__ unless $class;

Modified: trunk/cgi-bin/LJ/Lang.pm
===================================================================
--- trunk/cgi-bin/LJ/Lang.pm	2011-09-01 06:46:19 UTC (rev 19922)
+++ trunk/cgi-bin/LJ/Lang.pm	2011-09-01 07:25:21 UTC (rev 19923)
@@ -5,6 +5,9 @@
 use lib "$ENV{'LJHOME'}/cgi-bin";
 require "ljhooks.pl";
 
+use base qw( Exporter );
+our @EXPORT_OK = qw( ml );
+
 use LJ::LangDatFile;
 use LJ::TimeUtil;
 
@@ -554,33 +557,6 @@
     return 1;
 }
 
-sub get_effective_lang {
-
-    return LJ::run_hook('effective_lang')
-        if LJ::are_hooks('effective_lang');
-
-    my $lang;
-    if ( LJ::is_web_context() ) {
-        $lang = BML::get_language();
-    }
-
-    if ( my $remote = LJ::get_remote() ) {
-
-        # we have a user; try their browse language
-        $lang ||= $remote->prop("browselang");
-    }
-
-    load_lang_struct() unless $LS_CACHED;
-
-    # did we get a valid language code?
-    if ( $lang && $LN_CODE{$lang} ) {
-        return $lang;
-    }
-
-    # had no language code, or invalid.  return default
-    return $LJ::DEFAULT_LANG;
-}
-
 sub get_remote_lang {
     if ( my $remote = LJ::get_remote() ) {
         return $remote->prop('browselang')
@@ -594,21 +570,6 @@
     return $LJ::DEFAULT_LANG;
 }
 
-sub ml {
-    my ( $code, $vars ) = @_;
-
-    if ( LJ::is_web_context() ) {
-
-        # this means we should use BML::ml and not do our own handling
-        my $text = BML::ml( $code, $vars );
-        $LJ::_ML_USED_STRINGS{$code} = $text if $LJ::IS_DEV_SERVER;
-        return $text;
-    }
-
-    my $lang = LJ::Lang::get_effective_lang();
-    return get_text( $lang, $code, undef, $vars );
-}
-
 sub string_exists {
     my ( $code, $vars ) = @_;
 
@@ -861,34 +822,6 @@
     return $list;
 }
 
-sub set_lang {
-    my $lang = shift;
-
-    my $l      = LJ::Lang::get_lang($lang);
-    my $remote = LJ::get_remote();
-
-    # default cookie value to set
-    my $cval = $l->{'lncode'} . '/' . time();
-
-    # if logged in, change userprop and make cookie expiration
-    # the same as their login expiration
-    if ($remote) {
-        $remote->set_prop( 'browselang' => $l->{lncode} );
-
-        if ( $remote->{'_session'}->{'exptype'} eq 'long' ) {
-            $cval = [ $cval, $remote->{'_session'}->{'timeexpire'} ];
-        }
-    }
-
-    # set cookie
-    $BML::COOKIE{'langpref'} = $cval;
-
-    # set language through BML so it will apply immediately
-    BML::set_language( $l->{'lncode'} );
-
-    return;
-}
-
 # The translation system supports the ability to add multiple plural forms of
 # the word given different rules in a languge. This functionality is much like
 # the plural support in the S2 styles code. To use this code you must use the
@@ -1010,4 +943,131 @@
     return 1;
 }
 
+my ( $current_language, $guessed_language, $language_scope );
+
+sub decide_language {
+    return $guessed_language if $guessed_language;
+
+    my %existing_language =
+        map { $_ => 1 } ( @LJ::LANGS, @LJ::LANGS_IN_PROGRESS, 'debug' );
+
+    if ( LJ::is_web_context() ) {
+        # 'uselang' get param goes first
+        if ( my $uselang = LJ::Request->get_param('uselang') ) {
+            if ( $existing_language{$uselang} ) {
+                return ( $guessed_language = $uselang );
+            }
+        }
+
+        # next, 'langpref' cookie
+        if ( my $cookieval = LJ::Request->cookie('langpref') ) {
+            my ( $lang, $mtime ) = split m{/}, $cookieval;
+
+            if ( $existing_language{$lang} ) {
+                # let BML know of mtime for backwards compatibility,
+                # although it may end up not being used in case
+                # this is not a BML page
+                BML::note_mod_time($mtime);
+
+                return ( $guessed_language = $lang );
+            }
+        }
+
+        # if that failed, resort to Accept-Language
+        if ( my $headerval = LJ::Request->header_in('Accept-Language') ) {
+            my %weights;
+
+            foreach my $langval ( split /\s*,\s*/, $headerval ) {
+                my ( $lang, $weight ) = split /;q=/, $langval;
+
+                # $lang may contain country code, remove it:
+                $lang =~ s/-.*//;
+
+                # weight may not be specified, default to 1
+                $weight ||= 1.0;
+
+                $weights{$lang} = $weight;
+            }
+
+            my @langs =
+                reverse sort { $weights{$a} <=> $weights{$b} } keys %weights;
+
+            foreach my $lang (@langs) {
+                next unless $existing_language{$lang};
+                return ( $guessed_language = $lang );
+            }
+        }
+
+        # all else failing, default to the default language
+        return ( $guessed_language = $LJ::DEFAULT_LANG );
+    }
+
+    # alright, this is not a web context, so there is little we can do,
+    # but at least let's try to extract it from remote, in case
+    # someone set it to whatever
+    if ( my $remote = LJ::get_remote() ) {
+        if ( my $lang = $remote->prop('browselang') ) {
+            if ( $existing_language{$lang} ) {
+                return ( $guessed_language = $lang );
+            }
+        }
+    }
+
+    # failing that, it's the default language, alas;
+    # however, let's not cache it so that we can try remote
+    # again if it's set between the calls
+    return $LJ::DEFAULT_LANG;
+}
+
+sub current_language {
+    my @args = @_;
+
+    my $ret = $current_language || decide_language();
+
+    if (@args) {
+        $current_language = $args[0];
+        $guessed_language = undef;
+    }
+
+    return $ret;
+}
+
+*get_effective_lang = \&current_language;
+
+sub current_scope {
+    my @args = @_;
+
+    my $ret = $language_scope;
+    if (@args) { $language_scope = $args[0]; }
+    return $ret;
+}
+
+sub ml {
+    my ( $code, $vars ) = @_;
+
+    if ( current_language() eq 'debug' ) {
+        return $code;
+    }
+
+    if ( $code =~ /^[.]/ ) {
+        $code = current_scope() . $code;
+    }
+
+    return get_text( current_language(), $code, undef, $vars );
+}
+
+sub init_bml {
+    BML::current_site('livejournal');
+
+    BML::implementation( 'decide_language' => \&decide_language );
+
+    BML::implementation( 'get_language' => \&current_language );
+    BML::implementation( 'set_language' => \&current_language );
+
+    BML::implementation( 'get_language_scope' => \&current_scope );
+    BML::implementation( 'set_language_scope' => \&current_scope );
+
+    BML::implementation( 'ml' => \&ml );
+}
+
 1;

Modified: trunk/cgi-bin/LJ/Setting/Language.pm
===================================================================
--- trunk/cgi-bin/LJ/Setting/Language.pm	2011-09-01 06:46:19 UTC (rev 19922)
+++ trunk/cgi-bin/LJ/Setting/Language.pm	2011-09-01 07:25:21 UTC (rev 19923)
@@ -54,7 +54,7 @@
     $class->error_check($u, $args);
 
     my $val = $class->get_arg($args, "lang");
-    LJ::Lang::set_lang($val);
+    LJ::set_remote_language($val);
 
     return 1;
 }

Modified: trunk/cgi-bin/modperl.pl
===================================================================
--- trunk/cgi-bin/modperl.pl	2011-09-01 06:46:19 UTC (rev 19922)
+++ trunk/cgi-bin/modperl.pl	2011-09-01 07:25:21 UTC (rev 19923)
@@ -56,6 +56,6 @@
 use Sys::Hostname;
 $LJ::HARDWARE_SERVER_NAME = hostname();
 
+LJ::Lang::init_bml();
 
-
 1;

Modified: trunk/cgi-bin/weblib.pl
===================================================================
--- trunk/cgi-bin/weblib.pl	2011-09-01 06:46:19 UTC (rev 19922)
+++ trunk/cgi-bin/weblib.pl	2011-09-01 07:25:21 UTC (rev 19923)
@@ -2271,4 +2271,33 @@
     }
 }
 
+sub set_remote_language {
+    my ($lang) = @_;
+
+    my $l      = LJ::Lang::get_lang($lang);
+    my $remote = LJ::get_remote();
+
+    my $exptime = 0;
+
+    my $cval = $l->{'lncode'} . '/' . time();
+
+    # if logged in, change userprop and make cookie expiration
+    # the same as their login expiration
+    if ($remote) {
+        $remote->set_prop( 'browselang' => $l->{lncode} );
+
+        if ( $remote->{'_session'}->{'exptype'} eq 'long' ) {
+            $exptime = $remote->{'_session'}->{'timeexpire'};
+        }
+    }
+
+    # set cookie
+    LJ::Request->set_cookie( 'langpref' => $cval, 'expires' => $exptime );
+
+    # set language through BML so it will apply immediately
+    BML::set_language( $l->{'lncode'} );
+
+    return;
+}
+
 1;

Modified: trunk/htdocs/tools/endpoints/setlang.bml
===================================================================
--- trunk/htdocs/tools/endpoints/setlang.bml	2011-09-01 06:46:19 UTC (rev 19922)
+++ trunk/htdocs/tools/endpoints/setlang.bml	2011-09-01 07:25:21 UTC (rev 19923)
@@ -11,7 +11,7 @@
         });
     };
 
-    LJ::Lang::set_lang($GET{lang});
+    LJ::set_remote_language( $GET{'lang'} );
 
     return 1;
 }

Modified: trunk/htdocs/tools/setlang.bml
===================================================================
--- trunk/htdocs/tools/setlang.bml	2011-09-01 06:46:19 UTC (rev 19922)
+++ trunk/htdocs/tools/setlang.bml	2011-09-01 07:25:21 UTC (rev 19923)
@@ -9,7 +9,7 @@
     my $returnto = LJ::CleanHTML::canonical_url($GET{returnto});
 
     if ($lang && $returnto) {
-        LJ::Lang::set_lang($lang);
+        LJ::set_remote_language($lang);
         return BML::redirect($returnto);
     }
 

Tags: andy, bml, livejournal, pl, 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 

  • 4 comments