Annotation of loncom/localize/lonlocal.pm, revision 1.11

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Localization routines
                      3: #
1.11    ! www         4: # $Id: lonlocal.pm,v 1.10 2003/09/22 18:16:43 bowersj2 Exp $
1.1       www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: ######################################################################
                     29: ######################################################################
1.10      bowersj2   30: 
                     31: =pod
                     32: 
                     33: =head1 NAME
                     34: 
                     35: Apache::lonlocal - provides localization services
                     36: 
                     37: =head1 SYNOPSIS
                     38: 
                     39: lonlocal provides localization services for LON-CAPA programmers based
                     40: on Locale::Maketext. See
                     41: C<http://search.cpan.org/dist/Locale-Maketext/lib/Locale/Maketext.pod>
                     42: for more information on Maketext.
                     43: 
                     44: =head1 OVERVIEWX<internationalization>
                     45: 
                     46: As of LON-CAPA 1.1, we've started to localize LON-CAPA using the
                     47: Locale::Maketext module. Internationalization is the bulk of the work
                     48: right now (pre-1.1); localizing can be done anytime, and involves 
                     49: little or no programming.
                     50: 
                     51: The internationalization process involves putting a wrapper around
                     52: on-screen user messages and menus and turning them into keys,
                     53: which the MaketextX<Maketext> library translates into the desired
                     54: language output using a look-up table ("lexicon").X<lexicon>
                     55: 
                     56: As keys we are currently using the plain English messages, and
                     57: Maketext is configured to replace the message by its own key if no
                     58: translation is found. This makes it easy to phase in the
                     59: internationalization without disturbing the screen output.
                     60: 
                     61: Internationalization is somewhat tedious and effectively impossible
                     62: for a non-fluent speaker to perform, but is fairly easy to create
                     63: translations, requiring no programming skill. As a result, this is one
                     64: area where you can really help LON-CAPA out, even if you aren't a
                     65: programmer, and we'd really appreciate it.
                     66: 
                     67: =head1 How To Localize Handlers For Programmers
                     68: 
                     69: Into the "use" section of a module, we need to insert
                     70: 
                     71:  use Apache::lonlocal;
                     72: 
                     73: Note that there are B<no parentheses>, we B<want> to pollute our
                     74: namespace. 
                     75: 
                     76: Inside might be something like this
                     77: 
                     78:  sub message {
                     79:      my $status=shift;
                     80:      my $message='Status unknown';
                     81:      if ($status eq 'WON') {
                     82:         $message='You have won.';
                     83:      } elsif ($status eq 'LOST') {
                     84:         $message='You are a total looser.';
                     85:      }
                     86:      return $message;
                     87:  }
                     88:  ...
                     89:  $r->print('<h3>Gamble your Homework Points</h3>');
                     90:  ...
                     91:  $r->print(<<ENDMSG);
                     92:  <font size="1">Rules:</font>
                     93:  <font size="0">No purchase necessary. Illegal where not allowed.</font>
                     94:  ENDMSG
                     95: 
                     96: We have to now wrap the subroutine &mt()X<mt> ("maketext") around our 
                     97: messages, but not around markup, etc. We also want minimal disturbance. 
                     98: The first two examples are easy:
                     99: 
                    100:  sub message {
                    101:      my $status=shift;
                    102:      my $message='Status unknown';
                    103:      if ($status eq 'WON') {
                    104:         $message='You have won.';
                    105:      } elsif ($status eq 'LOST') {
                    106:         $message='You are a total looser.';
                    107:      }
                    108:      return &mt($message);
                    109:  }
                    110:  ...
                    111:  $r->print('<h3>'.&mt('Gamble your Homework Points').'</h3>');
                    112: 
                    113: The last one is a bummer, since you cannot call subroutines inside of 
                    114: (<<MARKER). I have written a little subroutine to generate a translated 
                    115: hash for that purpose:
                    116: 
                    117:  my %lt=&Apache::lonlocal::texthash('header' => 'Rules', 'disclaimer' => 
                    118:  'No purchase necessary. Illegal where not allowed.');
                    119:  $r->print(<<ENDMSG);
                    120:  <font size="1">$lt{'header'}:</font>
                    121:  <font size="0">$lt{'disclaimer'}</font>
                    122:  ENDMSG
                    123: 
                    124: As a programmer, your job is done here. If everything worked, you 
                    125: should see no changes on the screen.
                    126: 
                    127: =head1 How To Localize LON-CAPA for Translators
                    128: 
                    129: As a translator, you need to provide the lexicon for the keys, which in 
                    130: this case is the plain text message. The lexicons sit in 
                    131: loncom/localize/localize, with the language code as filename, for 
                    132: example de.pm for the German translation. The file then simply looks 
                    133: like this:
                    134: 
                    135:     'You have won.'
                    136:  => 'Sie haben gewonnen.',
                    137: 
                    138:     'You are a total looser.'
                    139:  => 'Sie sind der totale Verlierer.',
                    140: 
                    141:     'Rules'
                    142:  => 'Regeln',
                    143: 
                    144:     'No purchase necessary. Illegal where not allowed.'
                    145:  => 'Es ist erlaubt, einfach zu verlieren, und das ist Ihre Schuld.'
                    146: 
                    147: The German translation lexicon is in pretty okay shape, but not 
                    148: complete yet. Portuguese currently only covers the login screen. 
                    149: Russian is purely experimental. Looks like UTF-8 is the way to encode 
                    150: this, at least for latin/greek-based languages, but we still have to 
                    151: learn a lot.
                    152: 
                    153: Comments may be added with the # symbol, which outside of a string
                    154: (the things with the apostrophe surrounding them, which are the 
                    155: keys and translations) will cause the translation routines to
                    156: ignore the rest of the line.
                    157: 
                    158: This is a relatively easy task, and any help is appreciated.
                    159: 
                    160: Maketext can do a whole lot more, see
                    161: C<http://search.cpan.org/dist/Locale-Maketext/lib/Locale/Maketext.pod>
                    162: but for most purposes, we do not have to mess with that.
                    163: 
                    164: =cut
1.1       www       165: 
                    166: package Apache::lonlocal;
                    167: 
                    168: use strict;
                    169: use Apache::localize;
1.3       www       170: use Apache::File;
1.1       www       171: 
                    172: require Exporter;
                    173: 
                    174: our @ISA = qw (Exporter);
                    175: our @EXPORT = qw(mt);
                    176: 
1.4       www       177: my $reroute;
                    178: 
1.1       www       179: # ========================================================= The language handle
                    180: 
                    181: use vars qw($lh);
                    182: 
                    183: # ===================================================== The "MakeText" function
                    184: 
                    185: sub mt (@) {
1.3       www       186:     unless ($ENV{'environment.translator'}) {
                    187: 	return $lh->maketext(@_);
                    188:     } else {
1.4       www       189: 	my $trans=$lh->maketext(@_);
                    190: 	my $link='<a target="trans" href="/cgi-bin/translator.pl?arg1='.
1.3       www       191: 	    &Apache::lonnet::escape($_[0]).'&arg2='.
                    192: 	    &Apache::lonnet::escape($_[1]).'&arg3='.
                    193: 	    &Apache::lonnet::escape($_[2]).'&lang='.
                    194: 	    $ENV{'environment.translator'}.
1.4       www       195: 	    '">[['.$trans.']]</a>';
                    196: 	if ($ENV{'transreroute'}) {
                    197: 	    $reroute.=$link;
                    198: 	    return $trans;
                    199: 	} else {
                    200: 	    return $link;
                    201: 	}
                    202:     }
                    203: }
                    204: 
1.6       www       205: # ============================================================== What language?
                    206: 
                    207: sub current_language {
1.11    ! www       208:     my $lang=$lh->maketext('language_code');
        !           209:     return ($lang eq 'language_code'?'en':$lang);
1.6       www       210: }
                    211: 
1.8       www       212: # ============================================================== What encoding?
                    213: 
                    214: sub current_encoding {
                    215:     my $enc=$lh->maketext('char_encoding');
                    216:     return ($enc eq 'char_encoding'?'':$enc);
                    217: }
                    218: 
1.4       www       219: # ============================================================== Translate hash
                    220: 
                    221: sub texthash {
                    222:     my %hash=@_;
                    223:     foreach (keys %hash) {
                    224: 	$hash{$_}=&mt($hash{$_});
                    225:     }
                    226:     return %hash;
                    227: }
1.5       www       228: # ======================================================== Re-route translation
                    229: 
                    230: sub clearreroutetrans {
                    231:     &reroutetrans();
                    232:     $reroute='';
                    233: }
1.4       www       234: 
                    235: # ======================================================== Re-route translation
                    236: 
                    237: sub reroutetrans {
                    238:     $ENV{'transreroute'}=1;
                    239: }
1.5       www       240: 
1.4       www       241: # ==================================================== End re-route translation
                    242: sub endreroutetrans {
                    243:     $ENV{'transreroute'}=0;
                    244:     if ($ENV{'environment.translator'}) {
                    245: 	return $reroute;
                    246:     } else {
                    247: 	return '';
1.3       www       248:     }
1.1       www       249: }
                    250: 
                    251: # ========= Get a handle (do not invoke in vain, leave this to access handlers)
                    252: 
                    253: sub get_language_handle {
1.9       www       254:     my $r=shift;
1.2       www       255:     $lh=Apache::localize->get_handle(&Apache::loncommon::preferred_languages);
1.8       www       256:     $r->content_languages(["&current_language()"]);
                    257:     my $enc=&current_encoding();
                    258:     if ($enc) {
                    259: 
                    260:     }
1.1       www       261: }
                    262: 
                    263: 1;
                    264: 
                    265: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>