From 6d4d44caf69e06ea51937da9c74e78af0ac99a62 Mon Sep 17 00:00:00 2001 From: Tom Ross Date: Sat, 17 Oct 2009 05:24:11 +0000 Subject: Add support to genlang to sort strings based on their user. This is useful for translatable plugins. Currently, the .lng that is generated is hard-coded to only contain strings marked as core. The output of this version of genlang should be the same as non-sorting, so we don't need to change the version number. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@23218 a1c6a512-1295-4272-9138-f99709370657 --- tools/genlang | 121 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 71 insertions(+), 50 deletions(-) diff --git a/tools/genlang b/tools/genlang index 44dfcc6fdf..c5fb403801 100755 --- a/tools/genlang +++ b/tools/genlang @@ -22,9 +22,6 @@ my $LANGUAGE_FLAG_RTL = 0x01; # the ability. But, starting now, this ability is no longer provided since I # figured it was boring and unnecessary to write support for now since we # don't use it anymore. - -my %user2num = - ('core' => 1); if(!$ARGV[0]) { @@ -131,6 +128,9 @@ my %source; # id string to source phrase hash my %dest; # id string to dest phrase hash my %voice; # id string to voice phrase hash +my %users = + ('core' => 0); + my $input = $ARGV[0]; my @m; @@ -214,16 +214,17 @@ if($english) { # it before we read the translated file. For -b it isn't necessary, but for # -u it is convenient. - my $idnum=0; # start with a true number - my $vidnum=0x8000; # first voice id + my @idnum = ((0)); # start with a true number + my @vidnum = ((0x8000)); # first voice id open(ENG, "<$english") || die "Error: can't open $english"; my @phrase; my $id; my $maybeid; my $user; - my $maybeuser; my $withindest; my $numphrases = 0; + my $numusers = 1; # core is already in the users map + while() { # get rid of DOS newlines @@ -239,14 +240,14 @@ if($english) { # voice-only entries get a difference range if($id =~ /^VOICE_/) { # Assign an ID number to this entry - $idmap{$id}=$vidnum; - $vidnum++; + $idmap[$user]{$id}=$vidnum[$user]; + $vidnum[$user]++; } else { # Assign an ID number to this entry - $idmap{$id}=$idnum; - $idnum++; - # print STDERR "DEST: bumped idnum to $idnum\n"; + $idmap[$user]{$id}=$idnum[$user]; + $idnum[$user]++; + # print STDERR "DEST: bumped idnum to $idnum[$user]\n"; } # this is the end of a phrase, add it to the english hash @@ -269,7 +270,6 @@ if($english) { # we unconditionally always use all IDs when the "update" # feature is used $id = $maybeid; - $user = $user2num{$maybeuser}; # print "DEST: use this id $id\n"; } else { @@ -295,7 +295,11 @@ if($english) { $sortorder{$maybeid}=$numphrases++; } if($_ =~ /^ *user: ([^ \t\n]+)/i) { - $maybeuser=$1; + $user = $users{$1}; + if(!(defined $user)) { + $user = ++$numusers; + $users{$1} = $user; + } } } close(ENG); @@ -378,8 +382,13 @@ sub compare { print @show; } -my $idcount; # counter for lang ID numbers -my $voiceid=0x8000; # counter for voice-only ID numbers +my @idcount; # counter for lang ID numbers +my @voiceid; # counter for voice-only ID numbers + +foreach $i (keys %users) { + push @idcount, 0; + push @voiceid, 0x8000; +} # # Now start the scanning of the selected language string @@ -465,10 +474,22 @@ while() { print STDERR "$input:$line:1: warning: empty dest before line in non-deprecated phrase!\n"; } - my $user = $user2num{trim($phrase{'user'})}; - if(!$user) { - print STDERR "$input:$line:1: warning: unknown user!\n"; - $user = 1; + my $userstr = trim($phrase{'user'}); + my $user = $users{$userstr}; + if ($userstr eq "") { + print STDERR "$input:$line:1: warning: missing user!\n"; + $user = $users{"core"}; + } + elsif(!(defined $user)) { + if($english) { + print STDERR "$input:$line:1: warning: user was not found in $english!\n"; + $user = keys %users; # set to an invalid user so it won't be added + } + else { + # we found a new user, add it to the usermap + $user = ++$numusers; + $users{$userstr} = $user; + } } # Use the ID name to figure out which id number range we @@ -476,14 +497,14 @@ while() { # separated. if($idstr =~ /^VOICE/) { - $idnum = $voiceid++; + $idnum = $voiceid[$user]++; } else { - $idnum = $idcount++; + $idnum = $idcount[$user]++; } $id{$idstr} = $idnum; - $idnum[$idnum]=$idstr; + $idnum[$user][$idnum]=$idstr; $source{$idstr}=$src; $dest{$idstr}=$dest; @@ -570,16 +591,16 @@ if ($sortfile) { if($prefix) { # We create a .c and .h file - open(HFILE, ">$prefix/lang.h") || + open(HFILE_CORE, ">$prefix/lang.h") || die "Error: couldn't create file $prefix/lang.h\n"; - open(CFILE, ">$prefix/lang_core.c") || + open(CFILE_CORE, ">$prefix/lang_core.c") || die "Error: couldn't create file $prefix/lang_core.c\n"; # get header file name $headername = "$prefix/lang.h"; $headername =~ s/(.*\/)*//; - print HFILE <>8), ($idnum&0xff), $dest); } @@ -701,22 +722,22 @@ elsif($voiceout) { my @engl; # This loops over the strings in the translated language file order - my @ids = ((0 .. ($idcount-1))); - push @ids, (0x8000 .. ($voiceid-1)); + my @ids = ((0 .. ($idcount[$users{"core"}]-1))); + push @ids, (0x8000 .. ($voiceid[$users{"core"}]-1)); #for my $id (@ids) { # print "$id\n"; #} for $i (@ids) { - my $name=$idnum[$i]; # get the ID + my $name=$idnum[$users{"core"}][$i]; # get the ID my $dest = $voice{$name}; # get the destination voice string if($dest) { $dest =~ s/^\"(.*)\"\s*$/$1/g; # cut off quotes # Now, make sure we get the number from the english sort order: - $idnum = $idmap{$name}; + $idnum = $idmap[$users{"core"}]{$name}; if(length($idnum)) { $engl[$idnum] = $i; @@ -739,7 +760,7 @@ elsif($voiceout) { next; } - my $name=$idnum[$o]; # get the ID + my $name=$idnum[$users{"core"}][$o]; # get the ID my $dest = $voice{$name}; # get the destination voice string print "#$i ($o)\nid: $name\nvoice: $dest\n"; @@ -749,7 +770,7 @@ elsif($voiceout) { if($verbose) { - printf("%d ID strings scanned\n", $idcount); + printf("%d ID strings scanned\n", $idcount[$users{"core"}]); print "* head *\n"; for(keys %head) { -- cgit v1.2.3