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

raeburn raeburn@source.lon-capa.org
Mon, 28 Mar 2011 21:10:41 -0000


raeburn		Mon Mar 28 21:10:41 2011 EDT

  Modified files:              
    /loncom/localize	lonlocal.pm 
  Log:
  - New arg for: &get_language_handle() - can supply the language code for 
    the language to use.
  - New routine: choose_language() - used when a script is run from command 
    line, and user is prompted to choose a language (by entering two-letter
    language code).
  
  
Index: loncom/localize/lonlocal.pm
diff -u loncom/localize/lonlocal.pm:1.61 loncom/localize/lonlocal.pm:1.62
--- loncom/localize/lonlocal.pm:1.61	Wed Jul  7 16:30:48 2010
+++ loncom/localize/lonlocal.pm	Mon Mar 28 21:10:41 2011
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Localization routines
 #
-# $Id: lonlocal.pm,v 1.61 2010/07/07 16:30:48 raeburn Exp $
+# $Id: lonlocal.pm,v 1.62 2011/03/28 21:10:41 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -334,12 +334,17 @@
 # ========= Get a handle (do not invoke in vain, leave this to access handlers)
 
 sub get_language_handle {
-    my $r=shift;
+    my ($r,$chosen) = @_;
     if ($r) {
 	my $headers=$r->headers_in;
 	$ENV{'HTTP_ACCEPT_LANGUAGE'}=$headers->{'Accept-language'};
     }
-    my @languages=&preferred_languages();
+    my @languages;
+    if ($chosen ne '') {
+        @languages=($chosen);
+    } else {
+        @languages=&preferred_languages();
+    }
     $ENV{'HTTP_ACCEPT_LANGUAGE'}='';
     $lh=Apache::localize->get_handle(@languages);
     $current_language=&current_language();
@@ -569,6 +574,44 @@
     $$str_ref =~s/([\[\]])/~$1/g;
 }
 
+=pod
+
+=item * choose_language
+
+choose_language prompts a user to enter a two letter language code via
+keyboard when running a script from the command line. Default is en.
+
+=cut
+
+sub choose_language {
+    my %languages = (
+                      ar => 'Arabic',
+                      de => 'German',
+                      en => 'English',
+                      es => 'Spanish',
+                      fa => 'Persian',
+                      fr => 'French',
+                      he => 'Hebrew',
+                      ja => 'Japanese',
+                      pt => 'Portuguese',
+                      ru => 'Russian',
+                      tr => 'Turkish',
+                      zh => 'Chinese (Simplified)'
+                   );
+    my @posslangs = sort(keys(%languages));
+    my $langlist = join('|',@posslangs);
+    my $lang = 'en';
+    print 'Language: English (en). Change? ['.$langlist.']? ';
+    my $langchoice = <STDIN>;
+    chomp($langchoice);
+    $langchoice =~ s/(^\s+|\s+$)//g;
+    $langchoice = lc($langchoice);
+    if (defined($languages{$langchoice})) {
+        $lang = $langchoice;
+    }
+    return $lang;
+}
+
 1;
 
 __END__