r524 - trunk/misc

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Fri Feb 29 06:49:17 EST 2008


Author: icculus
Date: 2008-02-29 06:49:16 -0500 (Fri, 29 Feb 2008)
New Revision: 524

Added:
   trunk/misc/po2localization.pl
Log:
Initial shot at po2localization.pl ...


Added: trunk/misc/po2localization.pl
===================================================================
--- trunk/misc/po2localization.pl	                        (rev 0)
+++ trunk/misc/po2localization.pl	2008-02-29 11:49:16 UTC (rev 524)
@@ -0,0 +1,171 @@
+#!/usr/bin/perl -w
+
+use warnings;
+use strict;
+use Encode qw( decode_utf8 );
+
+# Fixes unicode dumping to stdio...hopefully you have a utf-8 terminal by now.
+binmode(STDIN, ":utf8");
+binmode(STDOUT, ":utf8");
+binmode(STDERR, ":utf8");
+
+my $now = `date '+%Y-%m-%d %H:%M:%S%z'`;
+chomp($now);
+
+my $svnver = `svnversion . 2>/dev/null`;
+chomp($svnver);
+$svnver = 'svn-' . (($svnver eq '') ? '???' : $svnver);
+
+
+my %languages;
+my %comments;
+my %msgstrs;
+my @strings;
+my $saw_template = 0;
+foreach (@ARGV) {
+    my $fname = $_;
+    my $template = /\.pot\Z/;
+
+    open(POIO, '<', $fname) or die("Failed to open $_: $!\n");
+
+    if ($template) {
+        die("multiple .pot files specified\n") if ($saw_template);
+        $saw_template = 1;
+    }
+
+    my $comment = '';
+    my $exportdate = '';
+    my $currentlang = '';
+
+    while (<POIO>) {
+        chomp;
+        s/\A\s+//;
+        s/\s+\Z//;
+        next if ($_ eq '');
+
+        if (s/\A\#\.\s*(.*)\Z/    -- $1\n/) {
+            $comment .= $_ if ($template);
+            next;
+        }
+
+        next if /\A\#/;
+
+        if (s/msgid\s*\"(.*?)\"\Z/$1/) {
+            if ($_ eq '') {   # initial string.
+                while (<POIO>) {  # Skip most of the metadata.
+                    chomp;
+                    s/\A\s+//;
+                    s/\s+\Z//;
+                    last if ($_ eq '');
+                    if (/\A\"Language-Team: (.*?) \<(.*?)\@.*?\>\\n"\Z/) {
+                        $currentlang = $2;
+                        if (defined $languages{$currentlang}) {
+                            die("Same language twice: $currentlang\n");
+                        } elsif ($currentlang eq 'en') {
+                            die("Found an 'en' translation.\n");
+                        } elsif ($currentlang eq 'en_US') {
+                            die("Found an 'en_US' translation.\n");
+                        }
+                        $languages{$currentlang} = $1 if (not $template);
+                    } elsif (/\A\"X-Launchpad-Export-Date: /) {
+                        $exportdate = $_ if ($template);
+                    }
+                }
+            } elsif ($currentlang eq '') {
+                die("No current language!\n");
+            } else {  # new string
+                my $msgstr = '';
+                my $msgid = $_;
+                while (<POIO>) {   # check for multiline msgid strings.
+                    chomp;
+                    s/\A\s+//;
+                    s/\s+\Z//;
+                    if (s/\Amsgstr \"(.*?)\"\Z/$1/) {
+                        $msgstr = $_;
+                        last;
+                    }
+                    if (s/\A\"(.*?)\"\Z/$1/) {
+                        $msgid .= $_;
+                    } else {
+                        die("unexpected line: $_\n");
+                    }
+                }
+                while (<POIO>) {   # check for multiline msgstr strings.
+                    chomp;
+                    s/\A\s+//;
+                    s/\s+\Z//;
+                    last if ($_ eq '');
+                    if (s/\A\"(.*?)\"\Z/$1/) {
+                        $msgstr .= $_;
+                    } else {
+                        die("unexpected line: $_\n");
+                    }
+                }
+
+                if ($template) {
+                    push @strings, $msgid;  # This is a list, to keep original order.
+                    $comments{$msgid} = $comment;
+                    $comment = '';
+                } elsif ($msgstr ne '') {
+                    $msgstrs{$currentlang}{$msgid} = $msgstr;
+                }
+            }
+        }
+    }
+
+    close(POIO);
+}
+
+die("no template seen\n") if (not $saw_template);
+
+
+print <<__EOF__;
+-- MojoSetup; a portable, flexible installation application.
+--
+-- Please see the file LICENSE.txt in the source's root directory.
+--
+-- DO NOT EDIT BY HAND.
+-- This file was generated with po2localization.pl, version $svnver ...
+--  on $now
+--
+-- Your own installer's localizations go into app_localization.lua instead.
+-- If you want to add strings to be translated to this file, contact Ryan
+-- (icculus\@icculus.org). If you want to add or change a translation for
+-- existing strings, please use our nice web interface here for your work:
+--
+--    https://translations.launchpad.net/mojosetup/
+--
+-- ...and that work eventually ends up in this file.
+
+MojoSetup.languages = {
+__EOF__
+
+print "    en_US = \"English (United States)\"";
+
+foreach (sort keys %languages) {
+    my $k = $_;
+    my $v = $languages{$k};
+    print ",\n    $k = \"$v\""
+}
+print "\n};\n\nMojoSetup.localization = {\n";
+
+foreach (@strings) {
+    my $msgid = $_;
+    print $comments{$msgid};
+    print "    [\"$msgid\"] = {\n";
+    my $first = 1;
+    foreach (sort keys %languages) {
+        my $k = $_;
+        my $str = $msgstrs{$k}{$msgid};
+        next if ((not defined $str) or ($str eq ''));
+        print ",\n" if (not $first);
+        print "        $k = \"$str\"";
+        $first = 0;
+    }
+    print "\n    };\n\n";
+}
+
+print "};\n\n-- end of localization.lua ...\n\n";
+
+# end of po2localization.pl ...
+


Property changes on: trunk/misc/po2localization.pl
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:eol-style
   + native




More information about the mojosetup-commits mailing list