2 eval 'exec perl -wS $0 ${1+"$@"}'
6 #*************************************************************************
8 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 # Copyright 2008 by Sun Microsystems, Inc.
12 # OpenOffice.org - a multi-platform office productivity suite
14 # $RCSfile: localize.pl,v $
18 # This file is part of OpenOffice.org.
20 # OpenOffice.org is free software: you can redistribute it and/or modify
21 # it under the terms of the GNU Lesser General Public License version 3
22 # only, as published by the Free Software Foundation.
24 # OpenOffice.org is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU Lesser General Public License version 3 for more details
28 # (a copy is included in the LICENSE file that accompanied this code).
30 # You should have received a copy of the GNU Lesser General Public License
31 # version 3 along with OpenOffice.org. If not, see
32 # <http://www.openoffice.org/license.html>
33 # for a copy of the LGPLv3 License.
35 #*************************************************************************
43 use File
::Glob
qw(:glob csh_glob);
49 #use lib ("$ENV{SOLARENV}/bin/modules", "$ENV{COMMON_ENV_TOOLS}/modules");
55 if ( !defined($ENV{SOLARENV
}) ) {
56 die "No environment found (environment variable SOLARENV is undefined)";
58 push(@lib_dirs, "$ENV{SOLARENV}/bin/modules");
59 push(@lib_dirs, "$ENV{COMMON_ENV_TOOLS}/modules") if defined($ENV{COMMON_ENV_TOOLS
});
72 #my %sl_modules; # Contains all modules where en-US and de is source language
73 my $use_default_date = '0';
75 # ( leftpart ) ( rightpart )
76 # prj file dummy type gid lid helpid pform width lang text helptext qhelptext title timestamp
77 my $sdf_regex = "((([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*))\t([^\t]*)\t(([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t)([^\t]*))";
78 my $file_types = "(src|hrc|xcs|xcu|lng|ulf|xrm|xhp|xcd|xgf|xxl|xrb)";
79 # Always use this date to prevent cvs conflicts
80 my $default_date = "2002-02-02 02:02:02";
86 if ( defined $ENV{USE_SHELL
} && $ENV{USE_SHELL
} eq '4nt' ) {
93 #%sl_modules = fetch_sourcelanguage_dirlist();
96 if ( $mode eq "merge" ) {
98 splitfile
( $sdffile );
99 unlink $sdffile; # remove temp file!
101 elsif( $mode eq "extract" ) {
102 collectfiles
( $outputfile );
110 #########################################################
114 my $currentFile = '';
115 my $cur_sdffile = '';
116 my $last_sdffile = '';
123 STDOUT
->autoflush( 1 );
125 #print STDOUT "Open File $sdffile\n";
126 open MYFILE
, "< $sdffile"
127 or die "Can't open '$sdffile'\n";
131 my $line = defined $_ ?
$_ : '';
132 my $prj = defined $3 ?
$3 : '';
133 my $file = defined $4 ?
$4 : '';
134 my $type = defined $6 ?
$6 : '';
135 my $gid = defined $7 ?
$7 : '';
136 my $lid = defined $8 ?
$8 : '';
137 my $lang = defined $12 ?
$12 : '';
138 my $plattform = defined $10 ?
$10 : '';
139 my $helpid = defined $9 ?
$9 : '';
141 next if( $prj eq "binfilter" ); # Don't merge strings into binfilter module
143 $currentFile = $srcpath . '\\' . $prj . '\\' . $file;
144 if ( $WIN ) { $currentFile =~ s/\//\\/g
; }
145 else { $currentFile =~ s/\\/\//g
; }
147 $cur_sdffile = $currentFile;
148 #if( $cur_sdffile =~ /\.$file_types[\s]*$/ ){
149 if( $WIN ) { $cur_sdffile =~ s/\\[^\\]*\.$file_types[\s]*$/\\localize.sdf/; }
150 else { $cur_sdffile =~ s/\/[^\/]*\
.$file_types[\s
]*$/\/localize
.sdf
/; }
154 if( $line =~ /(.*)\t[^\t\$]*$/ ){
155 $line = $1."\t".$default_date;
160 $lastFile = $currentFile; # ?
161 $last_sdffile = $cur_sdffile;
164 if( $lang eq "en-US" ){}
165 elsif( $cur_sdffile eq $last_sdffile )
167 $block{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } = $line ;
171 writesdf
( $lastFile , \
%block );
172 $lastFile = $currentFile; #?
173 $last_sdffile = $cur_sdffile;
175 #if( ! $lang eq "en-US" ) {
176 $block{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } = $line ;
180 } #else { print STDOUT "splitfile REGEX kaputt\n";}
183 writesdf
( $lastFile , \
%block );
188 #########################################################
190 #sub fetch_sourcelanguage_dirlist
193 # my $working_path = getcwd();
197 # my @all_dirs = csh_glob( "*" );
199 # foreach my $file ( @all_dirs )
203 # my $module = $file;
204 # $file .= "/prj/l10n";
205 # $file =~ s/\//\\/ , if( $WIN ) ;
207 # if( -f $file ) # Test file <module>/prj/l10n
209 # $sl_dirlist{ $module } = 1;
210 # if( $bVerbose eq "1" ) { print STDOUT "$module: de and en-US source language detected\n"; }
215 # chdir $working_path;
217 # return %sl_dirlist;
220 #sub has_two_sourcelanguages
222 # my $module = shift;
223 # return defined $sl_modules{ $module } ;
227 my $lastFile = shift;
228 my $blockhash_ref = shift;
229 my $localizeFile = $lastFile;
232 if( $localizeFile =~ /\.$file_types[\s]*$/ ){
233 if( $WIN ) { $localizeFile =~ s/\\[^\\]*\.$file_types[\s]*$/\\localize.sdf/; }
234 else { $localizeFile =~ s/\/[^\/]*\
.$file_types[\s
]*$/\/localize
.sdf
/; }
236 print STDERR
"Strange filetype found '$localizeFile'\n";
239 if( $bVerbose ){ print STDOUT
"$localizeFile\n"; }
240 if( open DESTFILE
, "< $localizeFile" ){
242 #or die "Can't open/create '\$localizeFile'";
247 my $line = defined $_ ?
$_ : '';
248 my $prj = defined $3 ?
$3 : '';
249 my $file = defined $4 ?
$4 : '';
250 my $type = defined $6 ?
$6 : '';
251 my $gid = defined $7 ?
$7 : '';
252 my $lid = defined $8 ?
$8 : '';
253 my $lang = defined $12 ?
$12 : '';
254 my $plattform = defined $10 ?
$10 : '';
255 my $helpid = defined $9 ?
$9 : '';
258 $index{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } = $line ;
260 } #else { print STDOUT "writesdf REGEX kaputt $_\n";}
265 #### Copy new strings
266 my @mykeys = keys( %{ $blockhash_ref } );
267 my $isDirty = "FALSE";
268 foreach my $key( @mykeys ){
269 if( ! defined $index{ $key } ){
271 $index{ $key } = $blockhash_ref->{ $key} ;
273 }elsif( $index{ $key } ne $blockhash_ref->{ $key } ){
274 # Overwrite old entry
275 $index{ $key } = $blockhash_ref->{ $key };
283 if( !$bVerbose ){ print STDOUT
"."; }
284 if( $isDirty eq "TRUE" ){
285 if( open DESTFILE
, "+> $localizeFile" ){
286 print DESTFILE get_license_header
();
287 @mykeys = sort keys( %index );
288 foreach my $key( @mykeys ){
289 print DESTFILE
( $index{ $key } , "\n" );
293 print STDOUT
"WARNING: File $localizeFile is not writable , try to merge ...\n";
294 my ( $TMPFILE , $tmpfile ) = File
::Temp
::tempfile
();
295 if( open DESTFILE
, "+> $tmpfile " ){
296 @mykeys = keys( %index );
297 foreach my $key( @mykeys ){
298 print DESTFILE
( $index{ $key } , "\n" );
301 if( move
( $localizeFile , $localizeFile.".backup" ) ){
302 if( copy
( $tmpfile , $localizeFile ) ){
303 unlink $localizeFile.".backup";
304 } else { print STDERR
"Can't open/create '$localizeFile', original file is renamed to $localizeFile.backup\n"; }
305 } else { print STDERR
"Can't open/create '$localizeFile'\n"; }
307 print STDERR
"WARNING: Can't open/create '$localizeFile'\n";
312 # if( $no_sort eq '' ){
313 # sort_outfile( $localizeFile );
317 sub get_license_header
{
320 "# #### ### # # ### ##### ##### #### ##### ##### \n".
321 "# # # # # ## # # # # # # # # # \n".
322 "# # # # # # # # # # # ### # # # # \n".
323 "# # # # # # ## # # # # # # # # \n".
324 "# #### ### # # ### # ##### #### ##### # \n".
326 "# DO NOT EDIT! This file will be overwritten by localisation process\n".
328 "#*************************************************************************\n".
330 "# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\n".
332 "# Copyright 2008 by Sun Microsystems, Inc.\n".
334 "# OpenOffice.org - a multi-platform office productivity suite\n".
337 "localize.pl,v \$\n".
342 "# This file is part of OpenOffice.org.\n".
344 "# OpenOffice.org is free software: you can redistribute it and/or modify\n".
345 "# it under the terms of the GNU Lesser General Public License version 3\n".
346 "# only, as published by the Free Software Foundation.\n".
348 "# OpenOffice.org is distributed in the hope that it will be useful,\n".
349 "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n".
350 "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n".
351 "# GNU Lesser General Public License version 3 for more details\n".
352 "# (a copy is included in the LICENSE file that accompanied this code).\n".
354 "# You should have received a copy of the GNU Lesser General Public License\n".
355 "# version 3 along with OpenOffice.org. If not, see\n".
356 "# <http://www.openoffice.org/license.html>\n".
357 "# for a copy of the LGPLv3 License.\n".
359 "#*************************************************************************\n";
361 ######## Check input sdf file and use only the correct part
364 my ( $TMPHANDLE , $tmpfile ) = File
::Temp
::tempfile
();
365 close ( $TMPHANDLE );
368 $command = "$ENV{WRAPCMD} gsicheck";
370 $command = "gsicheck";
372 my $errfile = $sdffile.".err";
373 $command .= " -k -c -wcf $tmpfile -wef ".fix_cygwin_path
($errfile)." -l \"\" ".fix_cygwin_path
($sdffile);
374 #my $rc = system( $command );
375 if ($bVerbose) { print STDOUT
"localize.pl running $command\n"; }
376 my $output = `$command`;
378 if ( $output ne "" ){
379 print STDOUT
"### gsicheck ###\n";
380 print STDOUT
"### The file $errfile have been written containing the errors in your sdf file. Those lines will not be merged: ###\n\n";
381 print STDOUT
"$output\n";
382 print STDOUT
"################\n";
385 # Remove the 0 Byte file
390 #########################################################
391 # find search function
394 my $file = $File::Find
::name
;
395 if( -f
$file && $file =~ /.*localize.sdf$/ && !( $file =~ /.*\.svn.*/ ) ) {
396 push @sdfparticles , $file;
397 if( $bVerbose eq "1" ) { print STDOUT
"$file\n"; }
403 print STDOUT
"### Localize\n";
404 my $localizehash_ref;
405 my ( $bAll , $bUseLocalize, $langhash_ref , $bHasSourceLanguage , $bFakeEnglish ) = parseLanguages
();
407 # Enable autoflush on STDOUT
409 STDOUT
->autoflush( 1 );
411 ### Search sdf particles
412 print STDOUT
"### Searching sdf particles\n";
413 my $working_path = getcwd
();
415 find
( { wanted
=> \
&wanted
, follow
=> 1, follow_skip
=> 2 }, getcwd
() );
418 my $nFound = $#sdfparticles +1;
419 print "\n $nFound files found !\n";
421 my ( $LOCALIZEPARTICLE , $localizeSDF ) = File
::Temp
::tempfile
();
422 close( $LOCALIZEPARTICLE );
424 my ( $ALLPARTICLES_MERGED , $particleSDF_merged ) = File
::Temp
::tempfile
();
425 close( $ALLPARTICLES_MERGED );
426 my ( $LOCALIZE_LOG , $my_localize_log ) = File
::Temp
::tempfile
();
427 close( $LOCALIZE_LOG );
429 ## Get the localize de,en-US extract
430 if( $bAll || $bUseLocalize ){
431 print "### Fetching source language strings\n";
436 $command = "$ENV{WRAPCMD} localize_sl";
438 $command = "localize_sl";
442 # if ( -x $command ){
444 if( !$bVerbose ){ $args .= " -QQ "; }
445 $args .= " -e -f ".fix_cygwin_path
($localizeSDF)." -l ";
447 if( $bAll ) {$args .= " en-US";}
450 foreach my $isokey ( keys( %{ $langhash_ref } ) ){
451 push @list , $isokey;
452 if( $langhash_ref->{ $isokey } ne "" ){
453 push @list , $langhash_ref->{ $isokey };
456 remove_duplicates
( \
@list );
457 foreach my $isokey ( @list ){
459 ( $isokey=~ /^en-US$/i )
461 if( $bFlag eq "TRUE" ){ $args .= ",en-US"; }
463 $args .= "en-US"; $bFlag = "TRUE";
472 # if ( $WIN eq "TRUE" ) { $args .= " > $my_localize_log"; }
473 # else { $args .= " >& $my_localize_log"; }
475 if ( $bVerbose ) { print STDOUT
"localize.pl running $command.$args.\n"; }
477 my $rc = system( $command.$args );
479 if( $rc < 0 ){ print STDERR
"ERROR: localize rc = $rc\n"; exit( -1 ); }
480 ( $localizehash_ref ) = read_file
( $localizeSDF , $langhash_ref );
484 open ALLPARTICLES_MERGED
, "+>> $particleSDF_merged"
485 or die "Can't open $particleSDF_merged";
487 ## Fill fackback hash
488 my( $fallbackhashhash_ref ) = fetch_fallback
( \
@sdfparticles , $localizeSDF , $langhash_ref );
492 foreach my $cur_lang ( keys( %{ $langhash_ref } ) ){
493 #print STDOUT "DBG: G1 cur_lang=$cur_lang\n";
494 $cur_fallback = $langhash_ref->{ $cur_lang };
495 if( $cur_fallback ne "" ){
496 # Insert fallback strings
497 #print STDOUT "DBG: Renaming $cur_fallback to $cur_lang in fallbackhash\n";
498 rename_language
( $fallbackhashhash_ref , $cur_fallback , $cur_lang );
500 foreach my $currentfile ( @sdfparticles ){
501 if ( open MYFILE
, "< $currentfile" ) {
504 my $line = defined $_ ?
$_ : '';
505 my $prj = defined $3 ?
$3 : '';
506 my $file = defined $4 ?
$4 : '';
507 my $type = defined $6 ?
$6 : '';
508 my $gid = defined $7 ?
$7 : '';
509 my $lid = defined $8 ?
$8 : '';
510 my $lang = defined $12 ?
$12 : '';
511 my $plattform = defined $10 ?
$10 : '';
512 my $helpid = defined $9 ?
$9 : '';
516 if ( $lang eq $cur_lang ){
517 # Overwrite fallback strings with collected strings
518 #if( ( !has_two_sourcelanguages( $cur_lang) && $cur_lang eq "de" ) || $cur_lang ne "en-US" ){
519 $fallbackhashhash_ref->{ $cur_lang }{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } = $line ;
525 }else { print STDERR
"WARNING: Can't open file $currentfile"; }
528 foreach my $line ( keys( %{$fallbackhashhash_ref->{ $cur_lang } } )) {
529 if( #$cur_lang ne "de" &&
530 $cur_lang ne "en-US" ){
531 print ALLPARTICLES_MERGED
( $fallbackhashhash_ref->{ $cur_lang }{ $line }, "\n" );
536 foreach my $currentfile ( @sdfparticles ){
537 if ( open MYFILE
, "< $currentfile" ) {
539 print ALLPARTICLES_MERGED
( $_, "\n" ); # recheck de / en-US !
542 else { print STDERR
"WARNING: Can't open file $currentfile"; }
545 close ALLPARTICLES_MERGED
;
554 if( open DESTFILE
, "+> $outputfile" ){
555 if( !open LOCALIZEPARTICLE
, "< $localizeSDF" ) { print STDERR
"ERROR: Can't open file $localizeSDF\n"; }
556 if( !open ALLPARTICLES_MERGED
, "< $particleSDF_merged" ) { print STDERR
"ERROR: Can't open file $particleSDF_merged\n"; }
560 while ( <LOCALIZEPARTICLE
> ){
562 my $leftpart = defined $2 ?
$2 : '';
563 my $lang = defined $12 ?
$12 : '';
564 my $rightpart = defined $13 ?
$13 : '';
565 my $timestamp = defined $18 ?
$18 : '';
567 my $prj = defined $3 ?
$3 : '';
568 my $file = defined $4 ?
$4 : '';
569 my $type = defined $6 ?
$6 : '';
570 my $gid = defined $7 ?
$7 : '';
571 my $lid = defined $8 ?
$8 : '';
572 #my $lang = defined $12 ? $12 : '';
573 my $plattform = defined $10 ?
$10 : '';
574 my $helpid = defined $9 ?
$9 : '';
577 if( $use_default_date )
579 $extract_date = "$default_date\n" ;
581 elsif( $extract_date eq "" ) {
582 $extract_date = $timestamp ;
583 $extract_date =~ tr/\r\n//d;
584 $extract_date .= "\n";
587 if( $bAll ){ print DESTFILE
$leftpart."\t".$lang."\t".$rightpart.$extract_date ; }
589 foreach my $sLang ( keys( %{ $langhash_ref } ) ){
590 if( $sLang=~ /all/i ) {
591 push @
{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } , $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
592 #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date;
594 #if( $sLang eq "de" && $lang eq "de" ) {
595 # push @{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } , $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
596 #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date;
598 if( $sLang eq "en-US" && $lang eq "en-US" ) {
599 push @order , $prj.$gid.$lid.$file.$type.$plattform.$helpid;
600 if( !$bFakeEnglish ){ push @
{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } , $leftpart."\t".$lang."\t".$rightpart.$extract_date ; }
601 #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date;
609 while ( <ALLPARTICLES_MERGED
> ){
611 my $leftpart = defined $2 ?
$2 : '';
612 my $prj = defined $3 ?
$3 : '';
613 my $lang = defined $12 ?
$12 : '';
614 my $rightpart = defined $13 ?
$13 : '';
615 my $timestamp = defined $18 ?
$18 : '';
617 #my $prj = defined $3 ? $3 : '';
618 my $file = defined $4 ?
$4 : '';
619 my $type = defined $6 ?
$6 : '';
620 my $gid = defined $7 ?
$7 : '';
621 my $lid = defined $8 ?
$8 : '';
622 #my $lang = defined $12 ? $12 : '';
623 my $plattform = defined $10 ?
$10 : '';
624 my $helpid = defined $9 ?
$9 : '';
627 if( $use_default_date )
629 $extract_date = "$default_date\n" ;
631 elsif( $extract_date eq "" )
633 $extract_date = $timestamp;
636 if( ! ( $prj =~ /binfilter/i ) ) {
637 push @
{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } , $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
638 #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
644 foreach my $curkey ( @order ){
645 foreach my $curlist ( $output{ $curkey } ){
646 foreach my $line ( @
{$curlist} ){
647 print DESTFILE
$line;
652 }else { print STDERR
"Can't open $outputfile";}
655 close LOCALIZEPARTICLE
;
656 close ALLPARTICLES_MERGED
;
658 #print STDOUT "DBG: \$localizeSDF $localizeSDF \$particleSDF_merged $particleSDF_merged\n";
659 unlink $localizeSDF , $particleSDF_merged , $my_localize_log;
661 #sort_outfile( $outputfile );
662 #remove_obsolete( $outputfile ) , if $bHasSourceLanguage ne "";
665 #########################################################
672 print STDOUT
"### Removing obsolete strings\n";
674 # Kick out all strings without en-US reference
675 if ( open ( SORTEDFILE
, "< $outfile" ) ){
676 while( <SORTEDFILE
> ){
678 my $line = defined $_ ?
$_ : '';
679 my $language = defined $12 ?
$12 : '';
680 my $prj = defined $3 ?
$3 : '';
681 my $file = defined $4 ?
$4 : '';
682 my $type = defined $6 ?
$6 : '';
683 my $gid = defined $7 ?
$7 : '';
684 my $lid = defined $8 ?
$8 : '';
685 my $plattform = defined $10 ?
$10 : '';
686 my $helpid = defined $9 ?
$9 : '';
688 my $leftpart = $prj.$gid.$lid.$file.$type.$plattform.$helpid;
690 if( $language eq "en-US" ){ # source string found, 1. entry
691 $enusleftpart = $leftpart;
692 push @good_lines , $line;
694 if( !defined $enusleftpart or !defined $leftpart ){
695 print STDERR
"BADLINE: $line\n";
696 print STDERR
"\$enusleftpart = $enusleftpart\n";
697 print STDERR
"\$leftpart = $leftpart\n";
699 if( $enusleftpart eq $leftpart ){ # matching language
700 push @good_lines , $line;
703 # print STDERR "OUT: \$enusleftpart=$enusleftpart \$leftpart=$leftpart \$line=$line\n";
709 } else { print STDERR
"ERROR: Can't open file $outfile\n";}
712 if ( open ( SORTEDFILE
, "> $outfile" ) ){
713 foreach my $newline ( @good_lines ) {
714 print SORTEDFILE
$newline;
717 } else { print STDERR
"ERROR: Can't open file $outfile\n";}
720 #########################################################
723 print STDOUT
"### Sorting ... $outfile ...";
728 #if ( open ( SORTEDFILE , "< $outputfile" ) ){
729 if ( open ( SORTEDFILE
, "< $outfile" ) ){
731 while ( <SORTEDFILE
> ){
733 if( $line =~ /^[^\#]/ ){
738 @sorted_lines = sort {
740 my $xa_left_part = "";
741 my $xa_right_part = "";
742 my $xa_timestamp = "";
744 my $xb_left_part = "";
745 my $xb_right_part = "";
746 my $xb_timestamp = "";
752 if( $a=~ /$sdf_regex/ ){
753 $xa_left_part = defined $2 ?
$2 : '';
754 $xa_lang = defined $12 ?
$12 : '';
755 $xa_right_part = defined $13 ?
$13 : '';
756 $xa_left_part = remove_last_column
( $xa_left_part );
759 if( $b=~ /$sdf_regex/ ){
760 $xb_left_part = defined $2 ?
$2 : '';
761 $xb_lang = defined $12 ?
$12 : '';
762 $xb_right_part = defined $13 ?
$13 : '';
763 $xb_left_part = remove_last_column
( $xb_left_part );
767 if( ( $xa_left_part cmp $xb_left_part ) == 0 ){ # Left part equal
768 if( ( $xa_lang cmp $xb_lang ) == 0 ){ # Lang equal
769 return ( $xa_right_part cmp $xb_right_part ); # Right part compare
771 elsif( $xa_lang eq "en-US" ) { return -1; } # en-US wins
772 elsif( $xb_lang eq "en-US" ) { return 1; } # en-US wins
773 else { return $xa_lang cmp $xb_lang; } # lang compare
776 return $xa_left_part cmp $xb_left_part; # Left part compare
780 if ( open ( SORTEDFILE
, "> $outfile" ) ){
781 print SORTEDFILE get_license_header
();
782 foreach my $newline ( @sorted_lines ) {
783 print SORTEDFILE
$newline;
784 #print STDOUT $newline;
788 } else { print STDERR
"WARNING: Can't open file $outfile\n";}
792 #########################################################
793 sub remove_last_column
{
795 my @alist = split ( "\t" , $string );
797 return join( "\t" , @alist );
800 #########################################################
802 my $fallbackhashhash_ref = shift;
803 my $cur_fallback = shift;
804 my $cur_lang = shift;
807 foreach my $key( keys ( %{ $fallbackhashhash_ref->{ $cur_fallback } } ) ){
808 $line = $fallbackhashhash_ref->{ $cur_fallback }{ $key };
809 if( $line =~ /$sdf_regex/ ){
810 my $leftpart = defined $2 ?
$2 : '';
811 my $lang = defined $12 ?
$12 : '';
812 my $rightpart = defined $13 ?
$13 : '';
814 $fallbackhashhash_ref->{ $cur_lang }{ $key } = $leftpart."\t".$cur_lang."\t".$rightpart;
819 ############################################################
820 sub remove_duplicates
{
821 my $list_ref = shift;
823 foreach my $key ( @
{ $list_ref } ){ $tmphash{ $key } = '' ; }
824 @
{$list_ref} = keys( %tmphash );
827 ##############################################################
829 my $sdfparticleslist_ref = shift;
830 my $localizeSDF = shift;
831 my $langhash_ref = shift;
832 my %fallbackhashhash;
836 foreach my $key ( keys ( %{ $langhash_ref } ) ){
837 $cur_lang = $langhash_ref->{ $key };
838 if ( $cur_lang ne "" ) {
839 push @langlist , $cur_lang;
842 remove_duplicates
( \
@langlist );
843 foreach $cur_lang ( @langlist ){
844 if( $cur_lang eq "en-US" ){
845 read_fallbacks_from_source
( $localizeSDF , $cur_lang , \
%fallbackhashhash );
851 foreach $cur_lang( @langlist ){
852 if( $cur_lang ne "en-US" ){
853 push @tmplist , $cur_lang;
857 @langlist = @tmplist;
858 if ( $#langlist +1 ){
859 read_fallbacks_from_particles
( $sdfparticleslist_ref , \
@langlist , \
%fallbackhashhash );
862 return (\
%fallbackhashhash);
865 #########################################################
868 my $localizeFile = shift;
869 my $index_ref = shift;
871 if( open DESTFILE
, "+> $localizeFile" ){
872 foreach my $key( %{ $index_ref } ){
873 print DESTFILE
($index_ref->{ $key }, "\n" );
877 print STDERR
"Can't open/create '$localizeFile'";
881 #########################################################
885 my $langhash_ref = shift;
888 open MYFILE
, "< $sdffile"
889 or die "Can't open '$sdffile'\n";
892 my $line = defined $_ ?
$_ : '';
893 my $prj = defined $3 ?
$3 : '';
894 my $file = defined $4 ?
$4 : '';
895 my $type = defined $6 ?
$6 : '';
896 my $gid = defined $7 ?
$7 : '';
897 my $lid = defined $8 ?
$8 : '';
898 my $plattform = defined $10 ?
$10 : '';
899 my $lang = defined $12 ?
$12 : '';
900 my $helpid = defined $9 ?
$9 : '';
902 foreach my $isolang ( keys ( %{ $langhash_ref } ) ){
903 if( $isolang=~ /$lang/i || $isolang=~ /all/i ) { $block{$prj.$gid.$lid.$file.$type.$plattform.$helpid } = $line ; }
910 #########################################################
911 sub read_fallbacks_from_particles
{
913 my $sdfparticleslist_ref = shift;
914 my $isolanglist_ref = shift;
915 my $fallbackhashhash_ref = shift;
917 foreach my $currentfile ( @
{ $sdfparticleslist_ref } ){
918 if ( open MYFILE
, "< $currentfile" ) {
921 my $line = defined $_ ?
$_ : '';
922 my $prj = defined $3 ?
$3 : '';
923 my $file = defined $4 ?
$4 : '';
924 my $type = defined $6 ?
$6 : '';
925 my $gid = defined $7 ?
$7 : '';
926 my $lid = defined $8 ?
$8 : '';
927 my $lang = defined $12 ?
$12 : '';
928 my $plattform = defined $10 ?
$10 : '';
929 my $helpid = defined $9 ?
$9 : '';
933 foreach my $isolang ( @
{$isolanglist_ref} ){
934 if( $isolang=~ /$lang/i ) {
935 $fallbackhashhash_ref->{ $isolang }{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } = $line ;
940 }else { print STDERR
"WARNING: Can't open file $currentfile"; }
944 #########################################################
945 sub read_fallbacks_from_source
{
949 my $fallbackhashhash_ref = shift;
951 # read fallback for single file
952 open MYFILE
, "< $sdffile"
953 or die "Can't open '$sdffile'\n";
957 my $line = defined $_ ?
$_ : '';
958 my $prj = defined $3 ?
$3 : '';
959 my $file = defined $4 ?
$4 : '';
960 my $type = defined $6 ?
$6 : '';
961 my $gid = defined $7 ?
$7 : '';
962 my $lid = defined $8 ?
$8 : '';
963 my $helpid = defined $9 ?
$9 : '';
964 my $lang = defined $12 ?
$12 : '';
965 my $plattform = defined $10 ?
$10 : '';
968 if( $isolang=~ /$lang/i ) { $fallbackhashhash_ref->{ $isolang }{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } = $line ;
974 #########################################################
979 my $bHasSourceLanguage="";
986 if( $languages=~ /all/ ){
988 $bHasSourceLanguage = "TRUE";
991 elsif( $languages=~ /.*,.*/ ){
992 my @tmpstr = split "," , $languages;
993 for my $lang ( @tmpstr ){
994 if( $lang=~ /([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*)(=([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*))?/ ){
998 if( ( $iso && $iso=~ /(en-US)/i ) || ( $fallback && $fallback=~ /(en-US)/i ) ) {
999 $bUseLocalize = "TRUE";
1001 if( ( $iso && $iso=~ /(en-US)/i ) ) {
1002 $bHasSourceLanguage = "TRUE";
1004 if( $fallback ) { $langhash{ $iso } = $fallback; }
1005 else { $langhash{ $iso } = ""; }
1011 if( $languages=~ /([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*)(=([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*))?/ ){
1015 if( ( $iso && $iso=~ /(en-US)/i ) || ( $fallback && $fallback=~ /(en-US)/i ) ) {
1016 $bUseLocalize = "TRUE";
1019 if( ( $iso && $iso=~ /(en-US)/i ) ) {
1020 $bHasSourceLanguage = "TRUE";
1023 if( $fallback ) { $langhash{ $iso } = $fallback; }
1024 else { $langhash{ $iso } = ""; }
1027 # HACK en-US always needed!
1028 if( !$bHasSourceLanguage ){
1029 #$bHasSourceLanguage = "TRUE";
1030 $bUseLocalize = "TRUE";
1031 $bFakeEnglish = "TRUE";
1032 $langhash{ "en-US" } = "";
1034 return ( $bAll , $bUseLocalize , \
%langhash , $bHasSourceLanguage, $bFakeEnglish);
1037 #########################################################
1043 my $success = GetOptions
('f=s' => \
$sdffile , 'l=s' => \
$languages , 's=s' => \
$srcpath , 'h' => \
$help , 'v' => \
$bVerbose ,
1044 'm' => \
$merge , 'e' => \
$extract , 'x' => \
$no_sort , 'd' => \
$use_default_date );
1045 $outputfile = $sdffile;
1047 #print STDOUT "DBG: lang = $languages\n";
1049 $srcpath = "$ENV{SRC_ROOT}";
1051 print STDERR
"No path to the source root found!\n\n";
1056 if( $help || !$success || $#ARGV > 1 || ( !$sdffile ) ){
1060 if( $merge && $sdffile && ! ( -r
$sdffile)){
1061 print STDERR
"Can't open file '$sdffile'\n";
1064 if( !( $languages=~ /[a-zA-Z]{2,3}(-[a-zA-Z\-]*)*(=[a-zA-Z]{2,3}(-[a-zA-Z\-]*)*)?(,[a-zA-Z]{2,3}(-[a-zA-Z\-]*)*(=[a-zA-Z]{2,3}(-[a-zA-Z\-]*)*)?)*/ ) ){
1065 print STDERR
"Please check the -l iso code\n";
1068 if( ( !$merge && !$extract ) || ( $merge && $extract ) ){ usage
();exit( -1 );}
1069 if( $extract ){ $mode = "extract"; }
1070 else { $mode = "merge"; }
1073 #########################################################
1076 print STDERR
"Usage: localize.pl\n";
1077 print STDERR
"Split or collect SDF files\n";
1078 print STDERR
" merge: -m -f <sdffile> -l l1[=f1][,l2[=f2]][...] [ -s <sourceroot> ]\n";
1079 print STDERR
" extract: -e -f <outputfile> -l <lang> [ -s <sourceroot> ] [-d]\n";
1080 print STDERR
"Options:\n";
1081 print STDERR
" -h help\n";
1082 print STDERR
" -m Merge mode\n";
1083 print STDERR
" -e Extract mode\n";
1084 print STDERR
" -f <sdffile> To split a big SDF file into particles\n";
1085 print STDERR
" <outputfile> To collect and join all particles to one big file\n";
1086 print STDERR
" -s <sourceroot> Path to the modules, if no \$SRC_ROOT is set\n";
1087 print STDERR
" -l ( all | <isocode> | <isocode>=fallback ) comma seperated languages\n";
1088 print STDERR
" -d Use default date in extracted sdf file\n";
1089 print STDERR
" -v Verbose\n";
1090 print STDERR
"\nExample:\n";
1091 print STDERR
"\nlocalize -e -l en-US,pt-BR=en-US -f my.sdf\n( Extract en-US and pt-BR with en-US fallback )\n";
1092 print STDERR
"\nlocalize -m -l cs -f my.sdf\n( Merge cs translation into the sourcecode ) \n";
1099 if ( $^O
eq 'cygwin' )
1101 $path = qx{cygpath
-m
"$path"};
1108 # my $line = defined $_ ? $_ : '';
1109 # my $leftpart = defined $2 ? $2 : '';
1110 # my $prj = defined $3 ? $3 : '';
1111 # my $file = defined $4 ? $4 : '';
1112 # my $dummy = defined $5 ? $5 : '';
1113 # my $type = defined $6 ? $6 : '';
1114 # my $gid = defined $7 ? $7 : '';
1115 # my $lid = defined $8 ? $8 : '';
1116 # my $helpid = defined $9 ? $9 : '';
1117 # my $plattform = defined $10 ? $10 : '';
1118 # my $width = defined $11 ? $11 : '';
1119 # my $lang = defined $12 ? $12 : '';
1120 # my $rightpart = defined $13 ? $13 : '';
1121 # my $text = defined $14 ? $14 : '';
1122 # my $helptext = defined $15 ? $15 : '';
1123 # my $quickhelptext = defined $16 ? $16 : '';
1124 # my $title = defined $17 ? $17 : '';
1125 # my $timestamp = defined $18 ? $18 : '';