summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ross <midgey@rockbox.org>2009-10-17 05:24:11 +0000
committerTom Ross <midgey@rockbox.org>2009-10-17 05:24:11 +0000
commit6d4d44caf69e06ea51937da9c74e78af0ac99a62 (patch)
tree836b93c14e5bc7490e40184710cbc71c4784ede8
parentb91ae3c3cd70b3f87b69ba3527875daa96c3056f (diff)
downloadrockbox-6d4d44caf69e06ea51937da9c74e78af0ac99a62.tar.gz
rockbox-6d4d44caf69e06ea51937da9c74e78af0ac99a62.zip
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
-rwxr-xr-xtools/genlang121
1 files 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;
22# the ability. But, starting now, this ability is no longer provided since I 22# the ability. But, starting now, this ability is no longer provided since I
23# figured it was boring and unnecessary to write support for now since we 23# figured it was boring and unnecessary to write support for now since we
24# don't use it anymore. 24# don't use it anymore.
25
26my %user2num =
27 ('core' => 1);
28 25
29 26
30if(!$ARGV[0]) { 27if(!$ARGV[0]) {
@@ -131,6 +128,9 @@ my %source; # id string to source phrase hash
131my %dest; # id string to dest phrase hash 128my %dest; # id string to dest phrase hash
132my %voice; # id string to voice phrase hash 129my %voice; # id string to voice phrase hash
133 130
131my %users =
132 ('core' => 0);
133
134my $input = $ARGV[0]; 134my $input = $ARGV[0];
135 135
136my @m; 136my @m;
@@ -214,16 +214,17 @@ if($english) {
214 # it before we read the translated file. For -b it isn't necessary, but for 214 # it before we read the translated file. For -b it isn't necessary, but for
215 # -u it is convenient. 215 # -u it is convenient.
216 216
217 my $idnum=0; # start with a true number 217 my @idnum = ((0)); # start with a true number
218 my $vidnum=0x8000; # first voice id 218 my @vidnum = ((0x8000)); # first voice id
219 open(ENG, "<$english") || die "Error: can't open $english"; 219 open(ENG, "<$english") || die "Error: can't open $english";
220 my @phrase; 220 my @phrase;
221 my $id; 221 my $id;
222 my $maybeid; 222 my $maybeid;
223 my $user; 223 my $user;
224 my $maybeuser;
225 my $withindest; 224 my $withindest;
226 my $numphrases = 0; 225 my $numphrases = 0;
226 my $numusers = 1; # core is already in the users map
227
227 while(<ENG>) { 228 while(<ENG>) {
228 229
229 # get rid of DOS newlines 230 # get rid of DOS newlines
@@ -239,14 +240,14 @@ if($english) {
239 # voice-only entries get a difference range 240 # voice-only entries get a difference range
240 if($id =~ /^VOICE_/) { 241 if($id =~ /^VOICE_/) {
241 # Assign an ID number to this entry 242 # Assign an ID number to this entry
242 $idmap{$id}=$vidnum; 243 $idmap[$user]{$id}=$vidnum[$user];
243 $vidnum++; 244 $vidnum[$user]++;
244 } 245 }
245 else { 246 else {
246 # Assign an ID number to this entry 247 # Assign an ID number to this entry
247 $idmap{$id}=$idnum; 248 $idmap[$user]{$id}=$idnum[$user];
248 $idnum++; 249 $idnum[$user]++;
249 # print STDERR "DEST: bumped idnum to $idnum\n"; 250 # print STDERR "DEST: bumped idnum to $idnum[$user]\n";
250 } 251 }
251 252
252 # this is the end of a phrase, add it to the english hash 253 # this is the end of a phrase, add it to the english hash
@@ -269,7 +270,6 @@ if($english) {
269 # we unconditionally always use all IDs when the "update" 270 # we unconditionally always use all IDs when the "update"
270 # feature is used 271 # feature is used
271 $id = $maybeid; 272 $id = $maybeid;
272 $user = $user2num{$maybeuser};
273 # print "DEST: use this id $id\n"; 273 # print "DEST: use this id $id\n";
274 } 274 }
275 else { 275 else {
@@ -295,7 +295,11 @@ if($english) {
295 $sortorder{$maybeid}=$numphrases++; 295 $sortorder{$maybeid}=$numphrases++;
296 } 296 }
297 if($_ =~ /^ *user: ([^ \t\n]+)/i) { 297 if($_ =~ /^ *user: ([^ \t\n]+)/i) {
298 $maybeuser=$1; 298 $user = $users{$1};
299 if(!(defined $user)) {
300 $user = ++$numusers;
301 $users{$1} = $user;
302 }
299 } 303 }
300 } 304 }
301 close(ENG); 305 close(ENG);
@@ -378,8 +382,13 @@ sub compare {
378 print @show; 382 print @show;
379} 383}
380 384
381my $idcount; # counter for lang ID numbers 385my @idcount; # counter for lang ID numbers
382my $voiceid=0x8000; # counter for voice-only ID numbers 386my @voiceid; # counter for voice-only ID numbers
387
388foreach $i (keys %users) {
389 push @idcount, 0;
390 push @voiceid, 0x8000;
391}
383 392
384# 393#
385# Now start the scanning of the selected language string 394# Now start the scanning of the selected language string
@@ -465,10 +474,22 @@ while(<LANG>) {
465 print STDERR "$input:$line:1: warning: empty dest before line in non-deprecated phrase!\n"; 474 print STDERR "$input:$line:1: warning: empty dest before line in non-deprecated phrase!\n";
466 } 475 }
467 476
468 my $user = $user2num{trim($phrase{'user'})}; 477 my $userstr = trim($phrase{'user'});
469 if(!$user) { 478 my $user = $users{$userstr};
470 print STDERR "$input:$line:1: warning: unknown user!\n"; 479 if ($userstr eq "") {
471 $user = 1; 480 print STDERR "$input:$line:1: warning: missing user!\n";
481 $user = $users{"core"};
482 }
483 elsif(!(defined $user)) {
484 if($english) {
485 print STDERR "$input:$line:1: warning: user was not found in $english!\n";
486 $user = keys %users; # set to an invalid user so it won't be added
487 }
488 else {
489 # we found a new user, add it to the usermap
490 $user = ++$numusers;
491 $users{$userstr} = $user;
492 }
472 } 493 }
473 494
474 # Use the ID name to figure out which id number range we 495 # Use the ID name to figure out which id number range we
@@ -476,14 +497,14 @@ while(<LANG>) {
476 # separated. 497 # separated.
477 498
478 if($idstr =~ /^VOICE/) { 499 if($idstr =~ /^VOICE/) {
479 $idnum = $voiceid++; 500 $idnum = $voiceid[$user]++;
480 } 501 }
481 else { 502 else {
482 $idnum = $idcount++; 503 $idnum = $idcount[$user]++;
483 } 504 }
484 505
485 $id{$idstr} = $idnum; 506 $id{$idstr} = $idnum;
486 $idnum[$idnum]=$idstr; 507 $idnum[$user][$idnum]=$idstr;
487 508
488 $source{$idstr}=$src; 509 $source{$idstr}=$src;
489 $dest{$idstr}=$dest; 510 $dest{$idstr}=$dest;
@@ -570,16 +591,16 @@ if ($sortfile) {
570if($prefix) { 591if($prefix) {
571 # We create a .c and .h file 592 # We create a .c and .h file
572 593
573 open(HFILE, ">$prefix/lang.h") || 594 open(HFILE_CORE, ">$prefix/lang.h") ||
574 die "Error: couldn't create file $prefix/lang.h\n"; 595 die "Error: couldn't create file $prefix/lang.h\n";
575 open(CFILE, ">$prefix/lang_core.c") || 596 open(CFILE_CORE, ">$prefix/lang_core.c") ||
576 die "Error: couldn't create file $prefix/lang_core.c\n"; 597 die "Error: couldn't create file $prefix/lang_core.c\n";
577 598
578 # get header file name 599 # get header file name
579 $headername = "$prefix/lang.h"; 600 $headername = "$prefix/lang.h";
580 $headername =~ s/(.*\/)*//; 601 $headername =~ s/(.*\/)*//;
581 602
582 print HFILE <<MOO 603 print HFILE_CORE <<MOO
583/* This file was automatically generated using genlang */ 604/* This file was automatically generated using genlang */
584/* 605/*
585 * The str() macro/functions is how to access strings that might be 606 * The str() macro/functions is how to access strings that might be
@@ -599,8 +620,8 @@ enum \{
599MOO 620MOO
600 ; 621 ;
601 622
602 print CFILE <<MOO 623 print CFILE_CORE <<MOO
603/* This file was automaticly generated using genlang, the strings come 624/* This file was automatically generated using genlang, the strings come
604 from "$input" */ 625 from "$input" */
605 626
606#include "$headername" 627#include "$headername"
@@ -612,17 +633,17 @@ MOO
612 633
613 # Output the ID names for the enum in the header file 634 # Output the ID names for the enum in the header file
614 my $i; 635 my $i;
615 for $i (1 .. $idcount) { 636 for $i (1 .. $idcount[$users{"core"}]) {
616 my $name=$idnum[$i - 1]; # get the ID name 637 my $name=$idnum[$users{"core"}][$i - 1]; # get the ID name
617 638
618 $name =~ s/\"//g; # cut off the quotes 639 $name =~ s/\"//g; # cut off the quotes
619 640
620 printf HFILE (" %s, /* %d */\n", $name, $i-1); 641 printf HFILE_CORE (" %s, /* %d */\n", $name, $i-1);
621 } 642 }
622 643
623# Output separation marker for last string ID and the upcoming voice IDs 644# Output separation marker for last string ID and the upcoming voice IDs
624 645
625 print HFILE <<MOO 646 print HFILE_CORE <<MOO
626 LANG_LAST_INDEX_IN_ARRAY, /* this is not a string, this is a marker */ 647 LANG_LAST_INDEX_IN_ARRAY, /* this is not a string, this is a marker */
627 /* --- below this follows voice-only strings --- */ 648 /* --- below this follows voice-only strings --- */
628 VOICEONLY_DELIMITER = 0x8000, 649 VOICEONLY_DELIMITER = 0x8000,
@@ -630,20 +651,20 @@ MOO
630 ; 651 ;
631 652
632# Output the ID names for the enum in the header file 653# Output the ID names for the enum in the header file
633 for $i (0x8000 .. ($voiceid-1)) { 654 for $i (0x8000 .. ($voiceid[$users{"core"}]-1)) {
634 my $name=$idnum[$i]; # get the ID name 655 my $name=$idnum[$users{"core"}][$i]; # get the ID name
635 656
636 $name =~ s/\"//g; # cut off the quotes 657 $name =~ s/\"//g; # cut off the quotes
637 658
638 printf HFILE (" %s,\n", $name); 659 printf HFILE_CORE (" %s,\n", $name);
639 } 660 }
640 661
641 # Output end of enum 662 # Output end of enum
642 print HFILE "\n};\n/* end of generated enum list */\n"; 663 print HFILE_CORE "\n};\n/* end of generated enum list */\n";
643 664
644 # Output the target phrases for the source file 665 # Output the target phrases for the source file
645 for $i (1 .. $idcount) { 666 for $i (1 .. $idcount[$users{"core"}]) {
646 my $name=$idnum[$i - 1]; # get the ID 667 my $name=$idnum[$users{"core"}][$i - 1]; # get the ID
647 my $dest = $dest{$name}; # get the destination phrase 668 my $dest = $dest{$name}; # get the destination phrase
648 669
649 $dest =~ s:\"$:\\0\":; # insert a \0 before the second quote 670 $dest =~ s:\"$:\\0\":; # insert a \0 before the second quote
@@ -653,18 +674,18 @@ MOO
653 $dest = '"\0"'; 674 $dest = '"\0"';
654 } 675 }
655 676
656 printf CFILE (" %s\n", $dest); 677 printf CFILE_CORE (" %s\n", $dest);
657 } 678 }
658 679
659# Output end of string chunk 680# Output end of string chunk
660 print CFILE <<MOO 681 print CFILE_CORE <<MOO
661; 682;
662/* end of generated string list */ 683/* end of generated string list */
663MOO 684MOO
664; 685;
665 686
666 close(HFILE); 687 close(HFILE_CORE);
667 close(CFILE); 688 close(CFILE_CORE);
668} # end of the c/h file generation 689} # end of the c/h file generation
669elsif($binary) { 690elsif($binary) {
670 # Creation of a binary lang file was requested 691 # Creation of a binary lang file was requested
@@ -680,15 +701,15 @@ elsif($binary) {
680 $langoptions); # magic lang file header 701 $langoptions); # magic lang file header
681 702
682 # loop over the target phrases 703 # loop over the target phrases
683 for $i (1 .. $idcount) { 704 for $i (1 .. $idcount[$users{"core"}]) {
684 my $name=$idnum[$i - 1]; # get the ID 705 my $name=$idnum[$users{"core"}][$i - 1]; # get the ID
685 my $dest = $dest{$name}; # get the destination phrase 706 my $dest = $dest{$name}; # get the destination phrase
686 707
687 if($dest) { 708 if($dest) {
688 $dest =~ s/^\"(.*)\"\s*$/$1/g; # cut off quotes 709 $dest =~ s/^\"(.*)\"\s*$/$1/g; # cut off quotes
689 710
690 # Now, make sure we get the number from the english sort order: 711 # Now, make sure we get the number from the english sort order:
691 $idnum = $idmap{$name}; 712 $idnum = $idmap[$users{"core"}]{$name};
692 713
693 printf OUTF ("%c%c%s\x00", ($idnum>>8), ($idnum&0xff), $dest); 714 printf OUTF ("%c%c%s\x00", ($idnum>>8), ($idnum&0xff), $dest);
694 } 715 }
@@ -701,22 +722,22 @@ elsif($voiceout) {
701 my @engl; 722 my @engl;
702 723
703 # This loops over the strings in the translated language file order 724 # This loops over the strings in the translated language file order
704 my @ids = ((0 .. ($idcount-1))); 725 my @ids = ((0 .. ($idcount[$users{"core"}]-1)));
705 push @ids, (0x8000 .. ($voiceid-1)); 726 push @ids, (0x8000 .. ($voiceid[$users{"core"}]-1));
706 727
707 #for my $id (@ids) { 728 #for my $id (@ids) {
708 # print "$id\n"; 729 # print "$id\n";
709 #} 730 #}
710 731
711 for $i (@ids) { 732 for $i (@ids) {
712 my $name=$idnum[$i]; # get the ID 733 my $name=$idnum[$users{"core"}][$i]; # get the ID
713 my $dest = $voice{$name}; # get the destination voice string 734 my $dest = $voice{$name}; # get the destination voice string
714 735
715 if($dest) { 736 if($dest) {
716 $dest =~ s/^\"(.*)\"\s*$/$1/g; # cut off quotes 737 $dest =~ s/^\"(.*)\"\s*$/$1/g; # cut off quotes
717 738
718 # Now, make sure we get the number from the english sort order: 739 # Now, make sure we get the number from the english sort order:
719 $idnum = $idmap{$name}; 740 $idnum = $idmap[$users{"core"}]{$name};
720 741
721 if(length($idnum)) { 742 if(length($idnum)) {
722 $engl[$idnum] = $i; 743 $engl[$idnum] = $i;
@@ -739,7 +760,7 @@ elsif($voiceout) {
739 next; 760 next;
740 } 761 }
741 762
742 my $name=$idnum[$o]; # get the ID 763 my $name=$idnum[$users{"core"}][$o]; # get the ID
743 my $dest = $voice{$name}; # get the destination voice string 764 my $dest = $voice{$name}; # get the destination voice string
744 765
745 print "#$i ($o)\nid: $name\nvoice: $dest\n"; 766 print "#$i ($o)\nid: $name\nvoice: $dest\n";
@@ -749,7 +770,7 @@ elsif($voiceout) {
749 770
750 771
751if($verbose) { 772if($verbose) {
752 printf("%d ID strings scanned\n", $idcount); 773 printf("%d ID strings scanned\n", $idcount[$users{"core"}]);
753 774
754 print "* head *\n"; 775 print "* head *\n";
755 for(keys %head) { 776 for(keys %head) {