1
0
Fork 0
forked from len0rd/rockbox

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
This commit is contained in:
Tom Ross 2009-10-17 05:24:11 +00:00
parent b91ae3c3cd
commit 6d4d44caf6

View file

@ -23,9 +23,6 @@ my $LANGUAGE_FLAG_RTL = 0x01;
# figured it was boring and unnecessary to write support for now since we # figured it was boring and unnecessary to write support for now since we
# don't use it anymore. # don't use it anymore.
my %user2num =
('core' => 1);
if(!$ARGV[0]) { if(!$ARGV[0]) {
print <<MOO print <<MOO
@ -131,6 +128,9 @@ my %source; # id string to source phrase hash
my %dest; # id string to dest phrase hash my %dest; # id string to dest phrase hash
my %voice; # id string to voice phrase hash my %voice; # id string to voice phrase hash
my %users =
('core' => 0);
my $input = $ARGV[0]; my $input = $ARGV[0];
my @m; my @m;
@ -214,16 +214,17 @@ if($english) {
# it before we read the translated file. For -b it isn't necessary, but for # it before we read the translated file. For -b it isn't necessary, but for
# -u it is convenient. # -u it is convenient.
my $idnum=0; # start with a true number my @idnum = ((0)); # start with a true number
my $vidnum=0x8000; # first voice id my @vidnum = ((0x8000)); # first voice id
open(ENG, "<$english") || die "Error: can't open $english"; open(ENG, "<$english") || die "Error: can't open $english";
my @phrase; my @phrase;
my $id; my $id;
my $maybeid; my $maybeid;
my $user; my $user;
my $maybeuser;
my $withindest; my $withindest;
my $numphrases = 0; my $numphrases = 0;
my $numusers = 1; # core is already in the users map
while(<ENG>) { while(<ENG>) {
# get rid of DOS newlines # get rid of DOS newlines
@ -239,14 +240,14 @@ if($english) {
# voice-only entries get a difference range # voice-only entries get a difference range
if($id =~ /^VOICE_/) { if($id =~ /^VOICE_/) {
# Assign an ID number to this entry # Assign an ID number to this entry
$idmap{$id}=$vidnum; $idmap[$user]{$id}=$vidnum[$user];
$vidnum++; $vidnum[$user]++;
} }
else { else {
# Assign an ID number to this entry # Assign an ID number to this entry
$idmap{$id}=$idnum; $idmap[$user]{$id}=$idnum[$user];
$idnum++; $idnum[$user]++;
# print STDERR "DEST: bumped idnum to $idnum\n"; # print STDERR "DEST: bumped idnum to $idnum[$user]\n";
} }
# this is the end of a phrase, add it to the english hash # 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" # we unconditionally always use all IDs when the "update"
# feature is used # feature is used
$id = $maybeid; $id = $maybeid;
$user = $user2num{$maybeuser};
# print "DEST: use this id $id\n"; # print "DEST: use this id $id\n";
} }
else { else {
@ -295,7 +295,11 @@ if($english) {
$sortorder{$maybeid}=$numphrases++; $sortorder{$maybeid}=$numphrases++;
} }
if($_ =~ /^ *user: ([^ \t\n]+)/i) { if($_ =~ /^ *user: ([^ \t\n]+)/i) {
$maybeuser=$1; $user = $users{$1};
if(!(defined $user)) {
$user = ++$numusers;
$users{$1} = $user;
}
} }
} }
close(ENG); close(ENG);
@ -378,8 +382,13 @@ sub compare {
print @show; print @show;
} }
my $idcount; # counter for lang ID numbers my @idcount; # counter for lang ID numbers
my $voiceid=0x8000; # counter for voice-only 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 # Now start the scanning of the selected language string
@ -465,10 +474,22 @@ while(<LANG>) {
print STDERR "$input:$line:1: warning: empty dest before line in non-deprecated phrase!\n"; print STDERR "$input:$line:1: warning: empty dest before line in non-deprecated phrase!\n";
} }
my $user = $user2num{trim($phrase{'user'})}; my $userstr = trim($phrase{'user'});
if(!$user) { my $user = $users{$userstr};
print STDERR "$input:$line:1: warning: unknown user!\n"; if ($userstr eq "") {
$user = 1; 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 # Use the ID name to figure out which id number range we
@ -476,14 +497,14 @@ while(<LANG>) {
# separated. # separated.
if($idstr =~ /^VOICE/) { if($idstr =~ /^VOICE/) {
$idnum = $voiceid++; $idnum = $voiceid[$user]++;
} }
else { else {
$idnum = $idcount++; $idnum = $idcount[$user]++;
} }
$id{$idstr} = $idnum; $id{$idstr} = $idnum;
$idnum[$idnum]=$idstr; $idnum[$user][$idnum]=$idstr;
$source{$idstr}=$src; $source{$idstr}=$src;
$dest{$idstr}=$dest; $dest{$idstr}=$dest;
@ -570,16 +591,16 @@ if ($sortfile) {
if($prefix) { if($prefix) {
# We create a .c and .h file # 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"; 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"; die "Error: couldn't create file $prefix/lang_core.c\n";
# get header file name # get header file name
$headername = "$prefix/lang.h"; $headername = "$prefix/lang.h";
$headername =~ s/(.*\/)*//; $headername =~ s/(.*\/)*//;
print HFILE <<MOO print HFILE_CORE <<MOO
/* This file was automatically generated using genlang */ /* This file was automatically generated using genlang */
/* /*
* The str() macro/functions is how to access strings that might be * The str() macro/functions is how to access strings that might be
@ -599,8 +620,8 @@ enum \{
MOO MOO
; ;
print CFILE <<MOO print CFILE_CORE <<MOO
/* This file was automaticly generated using genlang, the strings come /* This file was automatically generated using genlang, the strings come
from "$input" */ from "$input" */
#include "$headername" #include "$headername"
@ -612,17 +633,17 @@ MOO
# Output the ID names for the enum in the header file # Output the ID names for the enum in the header file
my $i; my $i;
for $i (1 .. $idcount) { for $i (1 .. $idcount[$users{"core"}]) {
my $name=$idnum[$i - 1]; # get the ID name my $name=$idnum[$users{"core"}][$i - 1]; # get the ID name
$name =~ s/\"//g; # cut off the quotes $name =~ s/\"//g; # cut off the quotes
printf HFILE (" %s, /* %d */\n", $name, $i-1); printf HFILE_CORE (" %s, /* %d */\n", $name, $i-1);
} }
# Output separation marker for last string ID and the upcoming voice IDs # Output separation marker for last string ID and the upcoming voice IDs
print HFILE <<MOO print HFILE_CORE <<MOO
LANG_LAST_INDEX_IN_ARRAY, /* this is not a string, this is a marker */ LANG_LAST_INDEX_IN_ARRAY, /* this is not a string, this is a marker */
/* --- below this follows voice-only strings --- */ /* --- below this follows voice-only strings --- */
VOICEONLY_DELIMITER = 0x8000, VOICEONLY_DELIMITER = 0x8000,
@ -630,20 +651,20 @@ MOO
; ;
# Output the ID names for the enum in the header file # Output the ID names for the enum in the header file
for $i (0x8000 .. ($voiceid-1)) { for $i (0x8000 .. ($voiceid[$users{"core"}]-1)) {
my $name=$idnum[$i]; # get the ID name my $name=$idnum[$users{"core"}][$i]; # get the ID name
$name =~ s/\"//g; # cut off the quotes $name =~ s/\"//g; # cut off the quotes
printf HFILE (" %s,\n", $name); printf HFILE_CORE (" %s,\n", $name);
} }
# Output end of enum # Output end of enum
print HFILE "\n};\n/* end of generated enum list */\n"; print HFILE_CORE "\n};\n/* end of generated enum list */\n";
# Output the target phrases for the source file # Output the target phrases for the source file
for $i (1 .. $idcount) { for $i (1 .. $idcount[$users{"core"}]) {
my $name=$idnum[$i - 1]; # get the ID my $name=$idnum[$users{"core"}][$i - 1]; # get the ID
my $dest = $dest{$name}; # get the destination phrase my $dest = $dest{$name}; # get the destination phrase
$dest =~ s:\"$:\\0\":; # insert a \0 before the second quote $dest =~ s:\"$:\\0\":; # insert a \0 before the second quote
@ -653,18 +674,18 @@ MOO
$dest = '"\0"'; $dest = '"\0"';
} }
printf CFILE (" %s\n", $dest); printf CFILE_CORE (" %s\n", $dest);
} }
# Output end of string chunk # Output end of string chunk
print CFILE <<MOO print CFILE_CORE <<MOO
; ;
/* end of generated string list */ /* end of generated string list */
MOO MOO
; ;
close(HFILE); close(HFILE_CORE);
close(CFILE); close(CFILE_CORE);
} # end of the c/h file generation } # end of the c/h file generation
elsif($binary) { elsif($binary) {
# Creation of a binary lang file was requested # Creation of a binary lang file was requested
@ -680,15 +701,15 @@ elsif($binary) {
$langoptions); # magic lang file header $langoptions); # magic lang file header
# loop over the target phrases # loop over the target phrases
for $i (1 .. $idcount) { for $i (1 .. $idcount[$users{"core"}]) {
my $name=$idnum[$i - 1]; # get the ID my $name=$idnum[$users{"core"}][$i - 1]; # get the ID
my $dest = $dest{$name}; # get the destination phrase my $dest = $dest{$name}; # get the destination phrase
if($dest) { if($dest) {
$dest =~ s/^\"(.*)\"\s*$/$1/g; # cut off quotes $dest =~ s/^\"(.*)\"\s*$/$1/g; # cut off quotes
# Now, make sure we get the number from the english sort order: # Now, make sure we get the number from the english sort order:
$idnum = $idmap{$name}; $idnum = $idmap[$users{"core"}]{$name};
printf OUTF ("%c%c%s\x00", ($idnum>>8), ($idnum&0xff), $dest); printf OUTF ("%c%c%s\x00", ($idnum>>8), ($idnum&0xff), $dest);
} }
@ -701,22 +722,22 @@ elsif($voiceout) {
my @engl; my @engl;
# This loops over the strings in the translated language file order # This loops over the strings in the translated language file order
my @ids = ((0 .. ($idcount-1))); my @ids = ((0 .. ($idcount[$users{"core"}]-1)));
push @ids, (0x8000 .. ($voiceid-1)); push @ids, (0x8000 .. ($voiceid[$users{"core"}]-1));
#for my $id (@ids) { #for my $id (@ids) {
# print "$id\n"; # print "$id\n";
#} #}
for $i (@ids) { 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 my $dest = $voice{$name}; # get the destination voice string
if($dest) { if($dest) {
$dest =~ s/^\"(.*)\"\s*$/$1/g; # cut off quotes $dest =~ s/^\"(.*)\"\s*$/$1/g; # cut off quotes
# Now, make sure we get the number from the english sort order: # Now, make sure we get the number from the english sort order:
$idnum = $idmap{$name}; $idnum = $idmap[$users{"core"}]{$name};
if(length($idnum)) { if(length($idnum)) {
$engl[$idnum] = $i; $engl[$idnum] = $i;
@ -739,7 +760,7 @@ elsif($voiceout) {
next; 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 my $dest = $voice{$name}; # get the destination voice string
print "#$i ($o)\nid: $name\nvoice: $dest\n"; print "#$i ($o)\nid: $name\nvoice: $dest\n";
@ -749,7 +770,7 @@ elsif($voiceout) {
if($verbose) { if($verbose) {
printf("%d ID strings scanned\n", $idcount); printf("%d ID strings scanned\n", $idcount[$users{"core"}]);
print "* head *\n"; print "* head *\n";
for(keys %head) { for(keys %head) {