[Koha-devel] CVS: koha/translator translator.pl,NONE,1.1 translator.readme,NONE,1.1

Chris Cormack rangi at users.sourceforge.net
Wed Jun 27 00:13:52 CEST 2001


Update of /cvsroot/koha/koha/translator
In directory usw-pr-cvs1:/tmp/cvs-serv29400/translator

Added Files:
	translator.pl translator.readme 
Log Message:
Scripts to permit koha webpages to display in different natural languages
Courtesy of Pawel Skuza


--- NEW FILE ---
#!/usr/bin/perl

use strict;

use locale;
use POSIX;
use Locale::gettext;

my $tofile;
if (exists $ENV{"HTTP_ACCEPT"}) {
  $tofile=0;
} else {
  $tofile=1;
}


print "Content-Type: text/html
\n\n" unless($tofile);

my $path=$ENV{"PATH_TRANSLATED"};

textdomain("koha");

my %lang=(
  fr => "fr_FR",
  pl => "pl_PL",
);

my @lang=split/,/,$ENV{"HTTP_ACCEPT_LANGUAGE"};
my $lang="us_US";

foreach (@lang){
  my $lg=$lang{$_};
  setlocale(LC_MESSAGES,$lg);
  my $tmp = gettext($_);
  if ($tmp ne $_)
  {
    $lang=$tmp;
    last;
  }
}

setlocale(LC_MESSAGES,$lang);

my @katalog;
my $plik;

if ($tofile){

  @katalog=`ls -R`;

  $plik=$ARGV[0];
  if ($plik eq ''){ 
    $plik="koha.gettext.c"; 
  }

} else {
  @katalog=("cos");
}

my $kat;
my (%dgettxt, %dane, %dane2, @dane2);
my $i;

$dgettxt{'iso-8859-1'}=1;
my $txt =<<TXT;
<HTML>
<META http-equiv=Content-Type content="text/thml; 
    charset=${\(gettext('iso-8859-1'))}">
TXT

foreach(@katalog){

  if (/:$/){
    $kat="$`/";
    next;
  }

  if ($tofile){
    unless ($_=~/(\.html|\.inc)$/i) {
      next;
    }
  }

  print "$kat$_" if ($tofile);

  my $dane;
  {
    local $/;
    if ($tofile){
      open PL, "$kat$_";
    } else {
      open PL, "$path";
    }
    $dane=<PL>;
    close PL;
  }


  $dane=~s/<html>/$txt/i;
  
  $dane=~s/%/&zamien/ges;		# change %	(specjal symbol)
  $dane=~s/\\\'/&zamien/ges;		# change \'
  $dane=~s/\\\"/&zamien/ges;		# change \"
  
  # taka out graphics
  $dane=~s/[\"\']\/?([\w-\/\.]*?\.gif)[\"\']/&zamien($1)/ges;
  
#  $dane=~s/messenger\s*\((.*?)\)\s*[\}\{;]/&zamien($1)/ges;
#  $dane=~s/\.write(ln)?\s*\((.*?)\)\s*[\};]/&zamien($2)/ges;

  # take out string in field alt
  $dane=~s/alt\s*=\s*[\"]([^\"]*)[\"]/&zamien($1)/iges;
  $dane=~s/alt\s*=\s*[\']([^\']*)[\']/&zamien($1)/iges;
  
  $dane=~s/<!--.*?-->/&zamien/ges;
  $dane=~s/<script.*?<\/script>/&zamien/iges;

  $dane=~s/<[\w\/]\w*\s*((\w*\s*=\s*(\'[^\']*\'|\"[^\"]*\"|[\w-\/?&,\.=%#]*)|[%\d*%]|\w)\s*)*>/&zamien/ges;
  $dane=~s/<!\[.*?\]>/&zamien/ges;
  $dane=~s/<![^>]*>/&zamien/ges;
  $dane=~s/<#.*?#>/&zamien/ges;

  my $dane2=$dane;
  $dane2=~s/(\s*%\d+%\s*)+/%/gs;
  $dane2=~s/^%//g;
  $dane2=~s/%$//g;
  foreach my $tmp(split/%/,$dane2){
    my $tmp_ok = $tmp;
    $tmp_ok=~s/\s+/ /gs;
    next unless ($tmp_ok=~/\w+/);
    $dgettxt{$tmp_ok}++ unless $dgettxt{$tmp_ok};
    $tmp=~s/([\)\(])/\\$1/g;
    $dane=~s/$tmp/gettext($tmp_ok)/es;
  }
  unless ($tofile){
    while($dane=~/%\d+%/){
      $dane=~s/%(\d+)%/$dane2{$1}/g;
    }
    print $dane;
  }
}

if ($tofile) {

  open PK, ">$plik";
  foreach my $tmp(sort keys %dgettxt){
     $tmp=~s/\"/\\\"/gs;
     print PK "gettext(\"$tmp\");\n";
  }
  close PK;
}

exit;

###########################################################

sub zamien {
    my $tmp = $&;
    unless ($dgettxt{$_[0]}) {
    	$dgettxt{$_[0]}++;
    }
    $tmp=~s/$_[0]/gettext($&)/es;
    unless ($dane{$tmp}) {
        push @dane2, $tmp;
	$dane2{$i}=$tmp;
    	$dane{$tmp}=$i++;
    }
    return "%${\($dane{$tmp})}%";
}


--- NEW FILE ---
This is small note about 'translator.pl'.
Author: Pawel Skuza
email: pawelzc at cz.top.pl

Script 'translator.pl' do two things:
1. Prepare strings which can be translate;
2. Translate strings used gettext.

1. Prepare strings which can be translate.
Copy file 'translator.pl' into root koha directory and execute it.
eg. 
    cd /usr/src/koha
    cp scripts/translator.pl .
    ./translator.pl some_file_name
    xgettext some_file_name -okoha.po
    vi koha.po 					//now you must translate koha.po
    msgfmt koha.po -okoha.mo
    cp koha.mo /usr/share/locale/$$/LC_MESSAGES //where $$ is your language
    
note: if you omnit 'some_file_name' translator create 'koha.gettext.c'
    
2. Translate strings used gettext.
You have to configure your apache. Put this lines in koha and opac virtual:

    Action text/html /cgi-bin/koha/translator.pl
    Action text/plane /cgi-bin/koha/translator.pl

Don't forget set up the language preference in your browser.





More information about the Koha-devel mailing list