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
# don't use it anymore.
my %user2num =
('core' => 1);
if(!$ARGV[0]) {
print <<MOO
@ -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(<ENG>) {
# 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(<LANG>) {
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(<LANG>) {
# 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 <<MOO
print HFILE_CORE <<MOO
/* This file was automatically generated using genlang */
/*
* The str() macro/functions is how to access strings that might be
@ -599,8 +620,8 @@ enum \{
MOO
;
print CFILE <<MOO
/* This file was automaticly generated using genlang, the strings come
print CFILE_CORE <<MOO
/* This file was automatically generated using genlang, the strings come
from "$input" */
#include "$headername"
@ -612,17 +633,17 @@ MOO
# Output the ID names for the enum in the header file
my $i;
for $i (1 .. $idcount) {
my $name=$idnum[$i - 1]; # get the ID name
for $i (1 .. $idcount[$users{"core"}]) {
my $name=$idnum[$users{"core"}][$i - 1]; # get the ID name
$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
print HFILE <<MOO
print HFILE_CORE <<MOO
LANG_LAST_INDEX_IN_ARRAY, /* this is not a string, this is a marker */
/* --- below this follows voice-only strings --- */
VOICEONLY_DELIMITER = 0x8000,
@ -630,20 +651,20 @@ MOO
;
# Output the ID names for the enum in the header file
for $i (0x8000 .. ($voiceid-1)) {
my $name=$idnum[$i]; # get the ID name
for $i (0x8000 .. ($voiceid[$users{"core"}]-1)) {
my $name=$idnum[$users{"core"}][$i]; # get the ID name
$name =~ s/\"//g; # cut off the quotes
printf HFILE (" %s,\n", $name);
printf HFILE_CORE (" %s,\n", $name);
}
# 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
for $i (1 .. $idcount) {
my $name=$idnum[$i - 1]; # get the ID
for $i (1 .. $idcount[$users{"core"}]) {
my $name=$idnum[$users{"core"}][$i - 1]; # get the ID
my $dest = $dest{$name}; # get the destination phrase
$dest =~ s:\"$:\\0\":; # insert a \0 before the second quote
@ -653,18 +674,18 @@ MOO
$dest = '"\0"';
}
printf CFILE (" %s\n", $dest);
printf CFILE_CORE (" %s\n", $dest);
}
# Output end of string chunk
print CFILE <<MOO
print CFILE_CORE <<MOO
;
/* end of generated string list */
MOO
;
close(HFILE);
close(CFILE);
close(HFILE_CORE);
close(CFILE_CORE);
} # end of the c/h file generation
elsif($binary) {
# Creation of a binary lang file was requested
@ -680,15 +701,15 @@ elsif($binary) {
$langoptions); # magic lang file header
# loop over the target phrases
for $i (1 .. $idcount) {
my $name=$idnum[$i - 1]; # get the ID
for $i (1 .. $idcount[$users{"core"}]) {
my $name=$idnum[$users{"core"}][$i - 1]; # get the ID
my $dest = $dest{$name}; # get the destination phrase
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};
printf OUTF ("%c%c%s\x00", ($idnum>>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) {