Ignore:
Timestamp:
2015-05-02T15:15:29+02:00 (9 years ago)
Author:
stoecker
Message:

major speedup of i18n process, add some new features, split core translations into two parts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/i18n/i18n.pl

    r31088 r31111  
    22
    33use utf8;
     4use strict;
    45use open qw/:std :encoding(utf8)/;
    56use Term::ReadKey;
     
    78
    89my $waswarn = 0;
    9 my $maxcount = 0;
    1010my $lang_pattern = '([a-z]{2}_[A-Z]{2}|[a-z]{2,3}|[a-z]{2}\@[a-z]+)';
    1111my $lang_pattern_file = '([a-z]{2}_[A-Z]{2}|[a-z]{2,3}|[a-z]{2}-[a-z]+)';
     
    2020}
    2121
    22 sub loadfiles($@)
     22sub loadpot($)
     23{
     24  my ($file) = @_;
     25  my %all = ();
     26  my %keys = ();
     27  die "Could not open file $file." if(!open FILE,"<:utf8",$file);
     28  my %postate = (last => "", type => "");
     29  my $linenum = 0;
     30  print "Reading file $file\n";
     31  while(<FILE>)
     32  {
     33    ++$linenum;
     34    my $fn = "$file:$linenum";
     35    chomp;
     36    if($_ =~ /^#/ || !$_)
     37    {
     38      checkpo(\%postate, \%all, "pot", "line $linenum in $file", \%keys, 1, undef);
     39      $postate{fuzzy} = 1 if ($_ =~ /fuzzy/);
     40    }
     41    elsif($_ =~ /^"(.*)"$/) {$postate{last} .= $1;}
     42    elsif($_ =~ /^(msg.+) "(.*)"$/)
     43    {
     44      my ($n, $d) = ($1, $2);
     45      my $new = !${postate}{fuzzy} && (($n eq "msgid" && $postate{type} ne "msgctxt") || ($n eq "msgctxt"));
     46      checkpo(\%postate, \%all, "pot", "line $linenum in $file", \%keys, $new, undef);
     47      $postate{last} = $d;
     48      $postate{type} = $n;
     49      $postate{src} = $fn if $new;
     50    }
     51    else
     52    {
     53      die "Strange line $linenum in $file: $_.";
     54    }
     55  }
     56  checkpo(\%postate, \%all, "pot", "line $linenum in $file", \%keys, 1, undef);
     57  close(FILE);
     58  return \%all;
     59}
     60
     61sub loadfiles($$@)
    2362{
    2463  my $desc;
    25   my $all;
    26   my ($lang,@files) = @_;
     64  my %all = ();
     65  my %keys = ();
     66  my ($lang,$use,@files) = @_;
    2767  foreach my $file (@files)
    2868  {
    2969    die "Could not open file $file." if(!open FILE,"<:utf8",$file);
    30     my $linenum = 0;
    31 
    32     my $cnt = -1; # don't count translators info
     70
    3371    if($file =~ /\/$lang_pattern\.po$/)
    3472    {
     
    4583        if($_ =~ /^#/ || !$_)
    4684        {
    47           checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, 1);
     85          checkpo(\%postate, \%all, $l, "line $linenum in $file", \%keys, 1, $use);
    4886          $postate{fuzzy} = 1 if ($_ =~ /fuzzy/);
    4987        }
     
    5290        {
    5391          my ($n, $d) = ($1, $2);
    54           ++$cnt if $n eq "msgid";
    5592          my $new = !${postate}{fuzzy} && (($n eq "msgid" && $postate{type} ne "msgctxt") || ($n eq "msgctxt"));
    56           checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, $new);
     93          checkpo(\%postate, \%all, $l, "line $linenum in $file", \%keys, $new, $use);
    5794          $postate{last} = $d;
    5895          $postate{type} = $n;
     
    64101        }
    65102      }
    66       checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, 1);
     103      checkpo(\%postate, \%all, $l, "line $linenum in $file", \%keys, 1, $use);
    67104    }
    68105    else
     
    70107      die "File format not supported for file $file.";
    71108    }
    72     $maxcount = $cnt if $cnt > $maxcount;
    73109    close(FILE);
    74110  }
     
    79115my $alwaysup = 0;
    80116my $noask = 0;
    81 my $conflicts;
     117my %conflicts;
    82118sub copystring($$$$$$$)
    83119{
     
    99135    else
    100136    {
    101 
    102137      my $f = $data->{$en}{_file} || "";
    103138      $f = ($f ? "$f;".$data->{$en}{"_src.$l"} : $data->{$en}{"_src.$l"}) if $data->{$en}{"_src.$l"};
     
    154189}
    155190
    156 sub checkpo($$$$$$)
    157 {
    158   my ($postate, $data, $l, $txt, $keys, $new) = @_;
     191# Check a current state for new data
     192#
     193# @param postate Pointer to current status hash
     194# @param data    Pointer to final data array
     195# @param l       current language
     196# @param txt     output text in case of error, usually file and line number
     197# @param keys    pointer to hash for info keys extracted from the first msgid "" entry
     198# @param new     whether a data set is finish or not yet complete
     199# @param use     hash to strings to use or undef for all strings
     200#
     201sub checkpo($$$$$$$)
     202{
     203  my ($postate, $data, $l, $txt, $keys, $new, $use) = @_;
    159204
    160205  if($postate->{type} eq "msgid") {$postate->{msgid} = $postate->{last};}
     
    167212  if($new)
    168213  {
    169     if((!$postate->{fuzzy}) && $postate->{msgstr} && $postate->{msgid})
     214    my $en = $postate->{context} ?  "___$postate->{context}___$postate->{msgid}" : $postate->{msgid};
     215    if((!$postate->{fuzzy}) && ($l eq "pot" || $postate->{msgstr}) && $postate->{msgid}
     216    && (!$use || $use->{$en}))
    170217    {
    171218      copystring($data, $postate->{msgid}, $l, $postate->{msgstr},$txt,$postate->{context}, 1);
    172       for($i = 1; exists($postate->{"msgstr_$i"}); ++$i)
    173       { copystring($data, $postate->{msgid}, "$l.$i", $postate->{"msgstr_$i"},$txt,$postate->{context}, 1); }
    174       if($postate->{msgid_1})
    175       { copystring($data, $postate->{msgid}, "en.1", $postate->{msgid_1},$txt,$postate->{context}, 1); }
     219      if(!$use || $use->{$en}{"en.1"})
     220      {
     221        for(my $i = 1; exists($postate->{"msgstr_$i"}); ++$i)
     222        { copystring($data, $postate->{msgid}, "$l.$i", $postate->{"msgstr_$i"},$txt,$postate->{context}, 1); }
     223        if($postate->{msgid_1})
     224        { copystring($data, $postate->{msgid}, "en.1", $postate->{msgid_1},$txt,$postate->{context}, 1); }
     225      }
    176226      copystring($data, $postate->{msgid}, "_src.$l", $postate->{src},$txt,$postate->{context}, 1);
    177227    }
     
    259309    $maxlen = $len if $len > $maxlen;
    260310  }
     311  my $maxcount = keys(%{$data});
    261312  foreach my $file (@files)
    262313  {
     
    306357      { }
    307358      my $val;
    308       $eq = 0;
     359      my $eq = 0;
    309360      if($la eq "en")
    310361      {
     
    357408  my %lang;
    358409  my @po;
    359   my $basename = shift @ARGV;
     410  my $potfile;
     411  my $basename = "./";
     412  foreach my $arg (@ARGV)
     413  {
     414    next if $arg !~ /^--/;
     415    if($arg =~ /^--basedir=(.+)$/)
     416    {
     417      $basename = $1;
     418    }
     419    elsif($arg =~ /^--potfile=(.+)$/)
     420    {
     421      $potfile = $1;
     422    }
     423    else
     424    {
     425      die "Unknown argument $arg.";
     426    }
     427  }
    360428  $basename .= "/" if !($basename =~ /[\/\\:]$/);
    361429  foreach my $arg (@ARGV)
    362430  {
     431    next if $arg =~ /^--/;
    363432    foreach my $f (glob $arg)
    364433    {
     
    368437    }
    369438  }
    370   my %data = loadfiles(\%lang,@po);
     439  my %data = loadfiles(\%lang,$potfile ? loadpot($potfile) : undef, @po);
     440
    371441  my @clang;
    372442  foreach my $la (sort keys %lang)
Note: See TracChangeset for help on using the changeset viewer.