Index: applications/editors/josm/i18n/i18n.pl
===================================================================
--- applications/editors/josm/i18n/i18n.pl	(revision 31088)
+++ applications/editors/josm/i18n/i18n.pl	(revision 31111)
@@ -2,4 +2,5 @@
 
 use utf8;
+use strict;
 use open qw/:std :encoding(utf8)/;
 use Term::ReadKey;
@@ -7,5 +8,4 @@
 
 my $waswarn = 0;
-my $maxcount = 0;
 my $lang_pattern = '([a-z]{2}_[A-Z]{2}|[a-z]{2,3}|[a-z]{2}\@[a-z]+)';
 my $lang_pattern_file = '([a-z]{2}_[A-Z]{2}|[a-z]{2,3}|[a-z]{2}-[a-z]+)';
@@ -20,15 +20,53 @@
 }
 
-sub loadfiles($@)
+sub loadpot($)
+{
+  my ($file) = @_;
+  my %all = ();
+  my %keys = ();
+  die "Could not open file $file." if(!open FILE,"<:utf8",$file);
+  my %postate = (last => "", type => "");
+  my $linenum = 0;
+  print "Reading file $file\n";
+  while(<FILE>)
+  {
+    ++$linenum;
+    my $fn = "$file:$linenum";
+    chomp;
+    if($_ =~ /^#/ || !$_)
+    {
+      checkpo(\%postate, \%all, "pot", "line $linenum in $file", \%keys, 1, undef);
+      $postate{fuzzy} = 1 if ($_ =~ /fuzzy/);
+    }
+    elsif($_ =~ /^"(.*)"$/) {$postate{last} .= $1;}
+    elsif($_ =~ /^(msg.+) "(.*)"$/)
+    {
+      my ($n, $d) = ($1, $2);
+      my $new = !${postate}{fuzzy} && (($n eq "msgid" && $postate{type} ne "msgctxt") || ($n eq "msgctxt"));
+      checkpo(\%postate, \%all, "pot", "line $linenum in $file", \%keys, $new, undef);
+      $postate{last} = $d;
+      $postate{type} = $n;
+      $postate{src} = $fn if $new;
+    }
+    else
+    {
+      die "Strange line $linenum in $file: $_.";
+    }
+  }
+  checkpo(\%postate, \%all, "pot", "line $linenum in $file", \%keys, 1, undef);
+  close(FILE);
+  return \%all;
+}
+
+sub loadfiles($$@)
 {
   my $desc;
-  my $all;
-  my ($lang,@files) = @_;
+  my %all = ();
+  my %keys = ();
+  my ($lang,$use,@files) = @_;
   foreach my $file (@files)
   {
     die "Could not open file $file." if(!open FILE,"<:utf8",$file);
-    my $linenum = 0;
-
-    my $cnt = -1; # don't count translators info
+
     if($file =~ /\/$lang_pattern\.po$/)
     {
@@ -45,5 +83,5 @@
         if($_ =~ /^#/ || !$_)
         {
-          checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, 1);
+          checkpo(\%postate, \%all, $l, "line $linenum in $file", \%keys, 1, $use);
           $postate{fuzzy} = 1 if ($_ =~ /fuzzy/);
         }
@@ -52,7 +90,6 @@
         {
           my ($n, $d) = ($1, $2);
-          ++$cnt if $n eq "msgid";
           my $new = !${postate}{fuzzy} && (($n eq "msgid" && $postate{type} ne "msgctxt") || ($n eq "msgctxt"));
-          checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, $new);
+          checkpo(\%postate, \%all, $l, "line $linenum in $file", \%keys, $new, $use);
           $postate{last} = $d;
           $postate{type} = $n;
@@ -64,5 +101,5 @@
         }
       }
-      checkpo(\%postate, \%all, $l, "line $linenum in $file", $keys, 1);
+      checkpo(\%postate, \%all, $l, "line $linenum in $file", \%keys, 1, $use);
     }
     else
@@ -70,5 +107,4 @@
       die "File format not supported for file $file.";
     }
-    $maxcount = $cnt if $cnt > $maxcount;
     close(FILE);
   }
@@ -79,5 +115,5 @@
 my $alwaysup = 0;
 my $noask = 0;
-my $conflicts;
+my %conflicts;
 sub copystring($$$$$$$)
 {
@@ -99,5 +135,4 @@
     else
     {
-
       my $f = $data->{$en}{_file} || "";
       $f = ($f ? "$f;".$data->{$en}{"_src.$l"} : $data->{$en}{"_src.$l"}) if $data->{$en}{"_src.$l"};
@@ -154,7 +189,17 @@
 }
 
-sub checkpo($$$$$$)
-{
-  my ($postate, $data, $l, $txt, $keys, $new) = @_;
+# Check a current state for new data
+#
+# @param postate Pointer to current status hash
+# @param data    Pointer to final data array
+# @param l       current language
+# @param txt     output text in case of error, usually file and line number
+# @param keys    pointer to hash for info keys extracted from the first msgid "" entry
+# @param new     whether a data set is finish or not yet complete
+# @param use     hash to strings to use or undef for all strings
+#
+sub checkpo($$$$$$$)
+{
+  my ($postate, $data, $l, $txt, $keys, $new, $use) = @_;
 
   if($postate->{type} eq "msgid") {$postate->{msgid} = $postate->{last};}
@@ -167,11 +212,16 @@
   if($new)
   {
-    if((!$postate->{fuzzy}) && $postate->{msgstr} && $postate->{msgid})
+    my $en = $postate->{context} ?  "___$postate->{context}___$postate->{msgid}" : $postate->{msgid};
+    if((!$postate->{fuzzy}) && ($l eq "pot" || $postate->{msgstr}) && $postate->{msgid}
+    && (!$use || $use->{$en}))
     {
       copystring($data, $postate->{msgid}, $l, $postate->{msgstr},$txt,$postate->{context}, 1);
-      for($i = 1; exists($postate->{"msgstr_$i"}); ++$i)
-      { copystring($data, $postate->{msgid}, "$l.$i", $postate->{"msgstr_$i"},$txt,$postate->{context}, 1); }
-      if($postate->{msgid_1})
-      { copystring($data, $postate->{msgid}, "en.1", $postate->{msgid_1},$txt,$postate->{context}, 1); }
+      if(!$use || $use->{$en}{"en.1"})
+      {
+        for(my $i = 1; exists($postate->{"msgstr_$i"}); ++$i)
+        { copystring($data, $postate->{msgid}, "$l.$i", $postate->{"msgstr_$i"},$txt,$postate->{context}, 1); }
+        if($postate->{msgid_1})
+        { copystring($data, $postate->{msgid}, "en.1", $postate->{msgid_1},$txt,$postate->{context}, 1); }
+      }
       copystring($data, $postate->{msgid}, "_src.$l", $postate->{src},$txt,$postate->{context}, 1);
     }
@@ -259,4 +309,5 @@
     $maxlen = $len if $len > $maxlen;
   }
+  my $maxcount = keys(%{$data});
   foreach my $file (@files)
   {
@@ -306,5 +357,5 @@
       { }
       my $val;
-      $eq = 0;
+      my $eq = 0;
       if($la eq "en")
       {
@@ -357,8 +408,26 @@
   my %lang;
   my @po;
-  my $basename = shift @ARGV;
+  my $potfile;
+  my $basename = "./";
+  foreach my $arg (@ARGV)
+  {
+    next if $arg !~ /^--/;
+    if($arg =~ /^--basedir=(.+)$/)
+    {
+      $basename = $1;
+    }
+    elsif($arg =~ /^--potfile=(.+)$/)
+    {
+      $potfile = $1;
+    }
+    else
+    {
+      die "Unknown argument $arg.";
+    }
+  }
   $basename .= "/" if !($basename =~ /[\/\\:]$/);
   foreach my $arg (@ARGV)
   {
+    next if $arg =~ /^--/;
     foreach my $f (glob $arg)
     {
@@ -368,5 +437,6 @@
     }
   }
-  my %data = loadfiles(\%lang,@po);
+  my %data = loadfiles(\%lang,$potfile ? loadpot($potfile) : undef, @po);
+
   my @clang;
   foreach my $la (sort keys %lang)
