[LON-CAPA-cvs] cvs: loncom(GCI_1) /localize lonlocal.pm

raeburn raeburn@source.lon-capa.org
Mon, 14 Sep 2009 22:32:09 -0000


raeburn		Mon Sep 14 22:32:09 2009 EDT

  Modified files:              (Branch: GCI_1)
    /loncom/localize	lonlocal.pm 
  Log:
  - lonlocal.pm for GCI_1 updated to 2.8.X
  - Backport 1.52, 1.53. 
  
  
Index: loncom/localize/lonlocal.pm
diff -u loncom/localize/lonlocal.pm:1.50 loncom/localize/lonlocal.pm:1.50.4.1
--- loncom/localize/lonlocal.pm:1.50	Fri Sep 19 12:31:06 2008
+++ loncom/localize/lonlocal.pm	Mon Sep 14 22:32:09 2009
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Localization routines
 #
-# $Id: lonlocal.pm,v 1.50 2008/09/19 12:31:06 raeburn Exp $
+# $Id: lonlocal.pm,v 1.50.4.1 2009/09/14 22:32:09 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -233,6 +233,56 @@
     return 'en';
 }
 
+sub preferred_languages {
+    my @languages=();
+    if (($Apache::lonnet::env{'request.role.adv'}) && ($Apache::lonnet::env{'form.languages'})) {
+        @languages=(@languages,split(/\s*(\,|\;|\:)\s*/,$Apache::lonnet::env{'form.languages'}));
+    }
+    if ($Apache::lonnet::env{'course.'.$Apache::lonnet::env{'request.course.id'}.'.languages'}) {
+        @languages=(@languages,split(/\s*(\,|\;|\:)\s*/,
+                 $Apache::lonnet::env{'course.'.$Apache::lonnet::env{'request.course.id'}.'.languages'}));
+    }
+
+    if ($Apache::lonnet::env{'environment.languages'}) {
+        @languages=(@languages,
+                    split(/\s*(\,|\;|\:)\s*/,$Apache::lonnet::env{'environment.languages'}));
+    }
+    my $browser=$ENV{'HTTP_ACCEPT_LANGUAGE'};
+    if ($browser) {
+        my @browser =
+            map { (split(/\s*;\s*/,$_))[0] } (split(/\s*,\s*/,$browser));
+        push(@languages,@browser);
+    }
+
+    foreach my $domtype ($Apache::lonnet::env{'user.domain'},$Apache::lonnet::env{'request.role.domain'},
+                         $Apache::lonnet::perlvar{'lonDefDomain'}) {
+        if ($domtype ne '') {
+            my %domdefs = &Apache::lonnet::get_domain_defaults($domtype);
+            if ($domdefs{'lang_def'} ne '') {
+                push(@languages,$domdefs{'lang_def'});
+            }
+        }
+    }
+    return &get_genlanguages(@languages);
+}
+
+sub get_genlanguages {
+    my (@languages) = @_;
+# turn "en-ca" into "en-ca,en"
+    my @genlanguages;
+    foreach my $lang (@languages) {
+        unless ($lang=~/\w/) { next; }
+        push(@genlanguages,$lang);
+        if ($lang=~/(\-|\_)/) {
+            push(@genlanguages,(split(/(\-|\_)/,$lang))[0]);
+        }
+    }
+    #uniqueify the languages list
+    my %count;
+    @genlanguages = map { $count{$_}++ == 0 ? $_ : () } @genlanguages;
+    return @genlanguages;
+}
+
 # ============================================================== What encoding?
 
 sub current_encoding {
@@ -279,7 +329,7 @@
 	my $headers=$r->headers_in;
 	$ENV{'HTTP_ACCEPT_LANGUAGE'}=$headers->{'Accept-language'};
     }
-    my @languages=&Apache::loncommon::preferred_languages;
+    my @languages=&preferred_languages();
     $ENV{'HTTP_ACCEPT_LANGUAGE'}='';
     $lh=Apache::localize->get_handle(@languages);
     if ($r) {
@@ -290,15 +340,24 @@
 
 # ========================================================== Localize localtime
 sub gettimezone {
-    my $timezone; 
-    if ($Apache::lonnet::env{'course.'.$Apache::lonnet::env{'request.course.id'}.'.timezone'}) {
-        $timezone = $Apache::lonnet::env{'course.'.$Apache::lonnet::env{'request.course.id'}.'.timezone'};    
-    } elsif ($Apache::lonnet::env{'request.course.id'} ne '') {
-        my $cdom = $Apache::lonnet::env{'course.'.$Apache::lonnet::env{'request.course.id'}.'.domain'};
-        if ($cdom ne '') {
-            my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
-            if ($domdefaults{'timezone_def'} ne '') {
-                $timezone = $domdefaults{'timezone_def'};
+    my ($timezone) = @_;
+    if ($timezone ne '') {
+        if (!DateTime::TimeZone->is_valid_name($timezone)) {
+            $timezone = 'local';
+        }
+        return $timezone;
+    }
+    my $cid = $Apache::lonnet::env{'request.course.id'};
+    if ($cid ne '') {
+        if ($Apache::lonnet::env{'course.'.$cid.'.timezone'}) {
+            $timezone = $Apache::lonnet::env{'course.'.$cid.'.timezone'};
+        } else {
+            my $cdom = $Apache::lonnet::env{'course.'.$cid.'.domain'};
+            if ($cdom ne '') {
+                my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
+                if ($domdefaults{'timezone_def'} ne '') {
+                    $timezone = $domdefaults{'timezone_def'};
+                }
             }
         }
     } elsif ($Apache::lonnet::env{'request.role.domain'} ne '') {
@@ -323,7 +382,7 @@
 }
 
 sub locallocaltime {
-    my $thistime=shift;
+    my ($thistime,$timezone) = @_;
     if (!defined($thistime) || $thistime eq '') {
 	return &mt('Never');
     }
@@ -337,7 +396,7 @@
     }
 
     my $dt = DateTime->from_epoch(epoch => $thistime)
-                     ->set_time_zone(&gettimezone());
+                     ->set_time_zone(&gettimezone($timezone));
     if ((&current_language=~/^en/) || (!$lh)) {
 
 	return $dt->strftime("%a %b %e %I:%M:%S %P %Y (%Z)");