merge the formfield patch from ooo-build
[ooovba.git] / transex3 / scripts / localize.pl
blobf1f8f7d1bfced62b5c9146ebb255d4060417d863
2 eval 'exec perl -wS $0 ${1+"$@"}'
3 if 0;
6 #*************************************************************************
8 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9 #
10 # Copyright 2008 by Sun Microsystems, Inc.
12 # OpenOffice.org - a multi-platform office productivity suite
14 # $RCSfile: localize.pl,v $
16 # $Revision: 1.18.6.2 $
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 #*************************************************************************
37 use strict;
38 use Getopt::Long;
39 use IO::Handle;
40 use File::Find;
41 use File::Temp;
42 use File::Path;
43 use File::Copy;
44 use File::Glob qw(:glob csh_glob);
45 use Cwd;
47 my $CVS_BINARY = "/usr/bin/cvs";
48 # ver 1.1
50 #### module lookup
51 #use lib ("$ENV{SOLARENV}/bin/modules", "$ENV{COMMON_ENV_TOOLS}/modules");
53 #### module lookup
54 # OOo conform
55 my @lib_dirs;
56 BEGIN {
57 if ( !defined($ENV{SOLARENV}) ) {
58 die "No environment found (environment variable SOLARENV is undefined)";
60 push(@lib_dirs, "$ENV{SOLARENV}/bin/modules");
61 push(@lib_dirs, "$ENV{COMMON_ENV_TOOLS}/modules") if defined($ENV{COMMON_ENV_TOOLS});
63 use lib (@lib_dirs);
65 #### globals ####
66 my $sdffile = '';
67 my $no_sort = '';
68 my $create_dirs = '';
69 my $multi_localize_files = '';
70 my $module_to_merge = '';
71 my $sort_sdf_before = '';
72 my $outputfile = '';
73 my $no_gsicheck = '';
74 my $mode = '';
75 my $bVerbose = "0";
76 my $srcpath = '';
77 my $WIN;
78 my $languages;
79 #my %sl_modules; # Contains all modules where en-US and de is source language
80 my $use_default_date = '0';
81 my %is_ooo_module;
82 my %is_so_module;
83 my $DELIMITER;
85 # ( leftpart ) ( rightpart )
86 # prj file dummy type gid lid helpid pform width lang text helptext qhelptext title timestamp
87 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]*))";
88 my $file_types = "(src|hrc|xcs|xcu|lng|ulf|xrm|xhp|xcd|xgf|xxl|xrb)";
89 # Always use this date to prevent cvs conflicts
90 my $default_date = "2002-02-02 02:02:02";
91 my @sdfparticles;
93 #### main ####
94 parse_options();
95 check_modules_scm();
97 if ( defined $ENV{USE_SHELL} && $ENV{USE_SHELL} eq '4nt' ) {
98 $WIN = 'TRUE';
99 $DELIMITER = "\\";
101 else {
102 $WIN = '';
103 $DELIMITER = "/";
106 my $binpath = '';
107 if( defined $ENV{UPDMINOREXT} )
109 $binpath = $ENV{SOLARVER}.$DELIMITER.$ENV{INPATH}.$DELIMITER."bin".$ENV{UPDMINOREXT}.$DELIMITER ;
111 else
113 $binpath = $ENV{SOLARVER}.$DELIMITER.$ENV{INPATH}.$DELIMITER."bin".$DELIMITER ;
116 #%sl_modules = fetch_sourcelanguage_dirlist();
119 if ( $mode eq "merge" ) {
120 if ( ! $no_gsicheck ){
121 merge_gsicheck();
123 splitfile( $sdffile );
124 if ( ! $no_gsicheck ){
125 unlink $sdffile; # remove temp file!
128 elsif( $mode eq "extract" ) {
129 collectfiles( $outputfile );
131 else {
132 usage();
135 exit(0);
137 #########################################################
138 sub splitfile{
140 my $lastFile = '';
141 my $currentFile = '';
142 my $cur_sdffile = '';
143 my $last_sdffile = '';
144 my $delim;
145 my $badDelim;
146 my $start = 'TRUE';
147 my %index = ();
148 my %block;
150 STDOUT->autoflush( 1 );
152 #print STDOUT "Open File $sdffile\n";
153 open MYFILE , "< $sdffile"
154 or die "Can't open '$sdffile'\n";
156 # my %lang_hash;
157 my %string_hash_ooo;
158 my %string_hash_so;
159 my %so_modules;
160 $so_modules{ "extras_full" } = "TRUE";
162 while( <MYFILE>){
163 if( /$sdf_regex/ ){
164 my $line = defined $_ ? $_ : '';
165 my $prj = defined $3 ? $3 : '';
166 my $file = defined $4 ? $4 : '';
167 my $type = defined $6 ? $6 : '';
168 my $gid = defined $7 ? $7 : '';
169 my $lid = defined $8 ? $8 : '';
170 my $lang = defined $12 ? $12 : '';
171 my $plattform = defined $10 ? $10 : '';
172 my $helpid = defined $9 ? $9 : '';
173 next if( $prj eq "binfilter" ); # Don't merge strings into binfilter module
174 chomp( $line );
176 if( is_openoffice_module( $prj ) )
178 $string_hash_ooo { $lang }{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } = $line;
180 else
182 $string_hash_so{ $lang }{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } = $line;
186 close( MYFILE );
188 if( !defined $ENV{SRC_ROOT} ){
189 print "Error, no SRC_ROOT in env found.\n";
190 exit( -1 );
192 my $src_root = $ENV{SRC_ROOT};
193 #print $WIN eq "TRUE" ? $src_root."\\l10n_so\n" : $src_root."/l10n_so\n";
194 my $so_l10n_path = $WIN eq "TRUE" ? $src_root."\\l10n_so\\source" : $src_root."/l10n_so/source";
195 my $ooo_l10n_path = $WIN eq "TRUE" ? $src_root."\\l10n\\source" : $src_root."/l10n/source";
197 #print "$so_l10n_path\n";
198 #print "$ooo_l10n_path\n";
200 write_sdf( \%string_hash_so , $so_l10n_path );
201 write_sdf( \%string_hash_ooo , $ooo_l10n_path );
204 sub check_modules_scm
206 #my @ooo_modules;
207 #my @so_modules;
208 my $src_path = $ENV{ SRC_ROOT } ;
209 my $last_dir = getcwd();
210 chdir $src_path ;
211 my @modules = <*/.svn/entries>;
213 foreach my $module ( @modules )
215 #print "$module \n";
216 if( open ( FILE , "<$module" ) )
218 while( <FILE> )
221 my @path = split ( "/" , $module ) ;
223 if( /svn.services.openoffice.org/ )
225 my $mod = $path[ 0 ];
226 #push @ooo_modules , $mod;
227 $is_ooo_module{ $mod } = "true";
228 # print "$module -> ooo ";
230 elsif ( /jumbo2.germany.sun.com/ )
232 my $mod = $path[ 0 ];
233 #push @so_modules , $mod;
234 # print "$module -> so ";
235 #$so_lookup_hash{ $mod } = "true";
237 #else
239 # print "ERROR: Is $module a SO or OOo module? Can not parese the $module/.svn/entries file ... please check mwsfinnish/merge/splitsdf.pl line 280\n";
240 # exit -1;
245 chdir $last_dir ;
246 #print "OOO\n";
247 #print @ooo_modules;
248 #print "\nSO\n";
249 #print @so_modules;
253 #sub parse
255 # my $command = "$CVS_BINARY -d:pserver:anoncvs\@anoncvs.services.openoffice.org:/cvs co -c";
256 # my $output = `$command`;
257 # my $rc = $? << 8;
258 # if ( $output eq "" || $rc < 0 ){
259 # print STDERR "ERROR: Can not fetch cvs alias list, please login to the cvs server and press at the password prompt just return\ncvs -d:pserver:anoncvs\@anoncvs.services.openoffice.org:/cvs login\n";
260 # exit ( -1 );
262 # my @list = split /\n/ , $output ;
263 # foreach my $string( @list )
266 # # print "Found '$1'\n" , if( $string =~ /^(\w*)/ && $1 ne "" );
268 # $is_ooo_module{ $1 } = "TRUE", if( $string =~ /^(\w*)/ && $1 ne "" );
270 # # foreach my $key( keys( %is_ooo_module ) )
271 # #{
272 # # print "$key\n";
273 # #}
275 sub is_openoffice_module
277 my $module = shift;
278 return "TRUE", if defined $is_ooo_module{ $module };
279 return "";
282 sub write_sdf
284 my $string_hash = shift;
285 my $l10n_file = shift;
287 foreach my $lang( keys( %{ $string_hash } ) )
289 my @sdf_file;
291 # mkdir!!!!
292 my $current_l10n_file = $WIN eq "TRUE" ? $l10n_file."\\$lang\\localize.sdf" : $l10n_file."/$lang/localize.sdf";
293 print "Writing '$current_l10n_file'\n";
294 if( open DESTFILE , "< $current_l10n_file" ){
296 while(<DESTFILE>){
297 if( /$sdf_regex/ ){
298 my $line = defined $_ ? $_ : '';
299 my $prj = defined $3 ? $3 : '';
300 my $file = defined $4 ? $4 : '';
301 my $type = defined $6 ? $6 : '';
302 my $gid = defined $7 ? $7 : '';
303 my $lid = defined $8 ? $8 : '';
304 my $lang = defined $12 ? $12 : '';
305 my $plattform = defined $10 ? $10 : '';
306 my $helpid = defined $9 ? $9 : '';
308 chomp( $line );
309 if ( defined $string_hash->{ $lang }{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } )
311 # Changed String!
312 push @sdf_file , $string_hash->{ $lang }{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } ;
313 $string_hash->{ $lang }{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } = undef;
315 else
317 # No new string
318 push @sdf_file , $line;
323 close( DESTFILE );
324 #Now just append the enw strings
325 #FIXME!!! Implement insertion in the correct order
326 foreach my $key ( keys ( %{ $string_hash->{ $lang } } ) )
328 push @sdf_file , $string_hash->{ $lang }{ $key } , if ( defined $string_hash->{ $lang }{ $key } );
329 #print "WARNING: Not defined = ".$string_hash->{ $lang }{ $key }."\n", if( ! defined $string_hash->{ $lang }{ $key } );
332 # Write the new file
333 my ( $TMPFILE , $tmpfile ) = File::Temp::tempfile();
334 if( open DESTFILE , "+> $tmpfile " ){
335 print DESTFILE get_license_header();
336 foreach my $string( @sdf_file ){
337 print DESTFILE "$string\n";
339 close ( DESTFILE );
340 if( move( $current_l10n_file , $current_l10n_file.".backup" ) ){
341 if( copy( $tmpfile , $current_l10n_file ) ){
342 unlink $l10n_file.".backup";
343 } else { print STDERR "Can't open/create '$l10n_file', original file is renamed to $l10n_file.backup\n"; }
344 } else { print STDERR "Can't open/create '$l10n_file'\n"; }
345 }else{
346 print STDERR "WARNING: Can't open/create '$l10n_file'\n";
348 unlink $tmpfile;
352 #########################################################
354 sub get_license_header{
355 return
356 "#\n".
357 "# #### ### # # ### ##### ##### #### ##### ##### \n".
358 "# # # # # ## # # # # # # # # # \n".
359 "# # # # # # # # # # # ### # # # # \n".
360 "# # # # # # ## # # # # # # # # \n".
361 "# #### ### # # ### # ##### #### ##### # \n".
362 "#\n".
363 "# DO NOT EDIT! This file will be overwritten by localisation process\n".
364 "#\n".
365 "#*************************************************************************\n".
366 "#\n".
367 "# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\n".
368 "# \n".
369 "# Copyright 2008 by Sun Microsystems, Inc.\n".
370 "#\n".
371 "# OpenOffice.org - a multi-platform office productivity suite\n".
372 "#\n".
373 "# \$RCSfile:".
374 "localize.pl,v \$\n".
375 "#\n".
376 "# \$Revision: ".
377 "1.17.4.1 \$\n".
378 "#\n".
379 "# This file is part of OpenOffice.org.\n".
380 "#\n".
381 "# OpenOffice.org is free software: you can redistribute it and/or modify\n".
382 "# it under the terms of the GNU Lesser General Public License version 3\n".
383 "# only, as published by the Free Software Foundation.\n".
384 "#\n".
385 "# OpenOffice.org is distributed in the hope that it will be useful,\n".
386 "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n".
387 "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n".
388 "# GNU Lesser General Public License version 3 for more details\n".
389 "# (a copy is included in the LICENSE file that accompanied this code).\n".
390 "#\n".
391 "# You should have received a copy of the GNU Lesser General Public License\n".
392 "# version 3 along with OpenOffice.org. If not, see\n".
393 "# <http://www.openoffice.org/license.html>\n".
394 "# for a copy of the LGPLv3 License.\n".
395 "#\n".
396 "#*************************************************************************\n";
398 ######## Check input sdf file and use only the correct part
399 sub merge_gsicheck{
400 my $command = '';
401 my ( $TMPHANDLE , $tmpfile ) = File::Temp::tempfile();
402 close ( $TMPHANDLE );
404 if( $ENV{WRAPCMD} ){
405 $command = "$ENV{WRAPCMD} gsicheck";
406 }else{
407 $command = "gsicheck";
409 my $errfile = $sdffile.".err";
410 $command .= " -k -c -wcf $tmpfile -wef ".fix_cygwin_path($errfile)." -l \"\" ".fix_cygwin_path($sdffile);
411 #my $rc = system( $command );
412 if ($bVerbose) { print STDOUT "localize.pl running $command\n"; }
413 my $output = `$command`;
414 my $rc = $? << 8;
415 if ( $output ne "" ){
416 print STDOUT "### gsicheck ###\n";
417 print STDOUT "### The file $errfile have been written containing the errors in your sdf file. Those lines will not be merged: ###\n\n";
418 print STDOUT "$output\n";
419 print STDOUT "################\n";
421 }else{
422 # Remove the 0 Byte file
423 unlink $errfile;
425 $sdffile = $tmpfile;
427 #########################################################
428 # find search function
429 sub wanted
431 my $file = $File::Find::name;
432 if( -f $file && $file =~ /.*localize.sdf$/ && !( $file =~ /.*\.svn.*/ ) ) {
433 push @sdfparticles , $file;
434 if( $bVerbose eq "1" ) { print STDOUT "$file\n"; }
435 else { print "."; }
439 sub add_paths
441 my $langhash_ref = shift;
442 my $root_dir = $ENV{ SRC_ROOT };
443 my $ooo_l10n_dir = "$root_dir"."$DELIMITER"."l10n"."$DELIMITER"."source";
444 my $so_l10n_dir = "$root_dir"."$DELIMITER"."l10n_so"."$DELIMITER"."source";
446 if( -e $ooo_l10n_dir )
448 foreach my $lang ( keys( %{ $langhash_ref } ) )
450 my $loc_file = "$ooo_l10n_dir"."$DELIMITER"."$lang"."$DELIMITER"."localize.sdf";
451 if( -e $loc_file )
453 push @sdfparticles , "$ooo_l10n_dir"."$DELIMITER"."$lang"."$DELIMITER"."localize.sdf";
455 else { print "WARNING: $loc_file not found ....\n"; }
458 else { die "ERROR: Can not find directory $ooo_l10n_dir!!!" }
459 if( -e $so_l10n_dir )
461 foreach my $lang ( keys( %{ $langhash_ref } ) )
463 my $loc_file = "$so_l10n_dir"."$DELIMITER"."$lang"."$DELIMITER"."localize.sdf";
464 if( -e $loc_file )
466 push @sdfparticles , "$ooo_l10n_dir"."$DELIMITER"."$lang"."$DELIMITER"."localize.sdf";
468 else { #print "WARNING: $loc_file not found ....\n";
474 sub collectfiles{
475 print STDOUT "### Localize\n";
476 my $localizehash_ref;
477 my ( $bAll , $bUseLocalize, $langhash_ref , $bHasSourceLanguage , $bFakeEnglish ) = parseLanguages();
479 # Enable autoflush on STDOUT
480 # $| = 1;
481 STDOUT->autoflush( 1 );
483 ### Search sdf particles
484 #print STDOUT "### Searching sdf particles\n";
485 my $working_path = getcwd();
486 #chdir $srcpath;
487 #find ( { wanted => \&wanted , follow => 1 }, getcwd() );
488 #chdir $working_path;
489 add_paths( $langhash_ref );
490 #my $nFound = $#sdfparticles +1;
491 #print "\n $nFound files found !\n";
493 my ( $LOCALIZEPARTICLE , $localizeSDF ) = File::Temp::tempfile();
494 close( $LOCALIZEPARTICLE );
496 my ( $ALLPARTICLES_MERGED , $particleSDF_merged ) = File::Temp::tempfile();
497 close( $ALLPARTICLES_MERGED );
498 my ( $LOCALIZE_LOG , $my_localize_log ) = File::Temp::tempfile();
499 close( $LOCALIZE_LOG );
501 ## Get the localize en-US extract
502 if( $bAll || $bUseLocalize ){
503 print "### Fetching source language strings\n";
504 my $command = "";
505 my $args = "";
507 if( $ENV{WRAPCMD} ){
508 $command = $ENV{WRAPCMD}.$binpath."localize_sl";
509 }else{
510 $command = $binpath."localize_sl";
512 print $command;
513 # -e
514 # if ( -x $command ){
515 if( $command ){
516 if( !$bVerbose ){ $args .= " -QQ "; }
517 $args .= " -e -f ".fix_cygwin_path($localizeSDF)." -l ";
518 my $bFlag="";
519 if( $bAll ) {$args .= " en-US";}
520 else{
521 my @list;
522 foreach my $isokey ( keys( %{ $langhash_ref } ) ){
523 push @list , $isokey;
524 if( $langhash_ref->{ $isokey } ne "" ){
525 push @list , $langhash_ref->{ $isokey };
528 remove_duplicates( \@list );
529 foreach my $isokey ( @list ){
530 switch :{
531 ( $isokey=~ /^en-US$/i )
532 && do{
533 if( $bFlag eq "TRUE" ){ $args .= ",en-US"; }
534 else {
535 $args .= "en-US"; $bFlag = "TRUE";
539 } #switch
540 } #foreach
541 } # if
542 } # if
543 # if ( !$bVerbose ){
544 # if ( $WIN eq "TRUE" ) { $args .= " > $my_localize_log"; }
545 # else { $args .= " >& $my_localize_log"; }
547 if ( $bVerbose ) { print STDOUT "localize.pl running $command.$args.\n"; }
549 my $rc = system( $command.$args );
551 if( $rc < 0 ){ print STDERR "ERROR: localize rc = $rc\n"; exit( -1 ); }
552 ( $localizehash_ref ) = read_file( $localizeSDF , $langhash_ref );
555 ## Get sdf particles
556 #*****************
557 open ALLPARTICLES_MERGED , "+>> $particleSDF_merged"
558 or die "Can't open $particleSDF_merged";
560 ## Fill fackback hash
561 my( $fallbackhashhash_ref ) = fetch_fallback( \@sdfparticles , $localizeSDF , $langhash_ref );
562 my %block;
563 my $cur_fallback;
564 if( !$bAll) {
565 foreach my $cur_lang ( keys( %{ $langhash_ref } ) ){
566 #print STDOUT "DBG: G1 cur_lang=$cur_lang\n";
567 $cur_fallback = $langhash_ref->{ $cur_lang };
568 if( $cur_fallback ne "" ){
569 # Insert fallback strings
570 #print STDOUT "DBG: Renaming $cur_fallback to $cur_lang in fallbackhash\n";
571 rename_language( $fallbackhashhash_ref , $cur_fallback , $cur_lang );
573 foreach my $currentfile ( @sdfparticles ){
574 if ( open MYFILE , "< $currentfile" ) {
575 while(<MYFILE>){
576 if( /$sdf_regex/ ){
577 my $line = defined $_ ? $_ : '';
578 my $prj = defined $3 ? $3 : '';
579 my $file = defined $4 ? $4 : '';
580 my $type = defined $6 ? $6 : '';
581 my $gid = defined $7 ? $7 : '';
582 my $lid = defined $8 ? $8 : '';
583 my $lang = defined $12 ? $12 : '';
584 my $plattform = defined $10 ? $10 : '';
585 my $helpid = defined $9 ? $9 : '';
587 chomp( $line );
589 if ( $lang eq $cur_lang ){
590 # Overwrite fallback strings with collected strings
591 #if( ( !has_two_sourcelanguages( $cur_lang) && $cur_lang eq "de" ) || $cur_lang ne "en-US" ){
592 $fallbackhashhash_ref->{ $cur_lang }{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } = $line ;
598 }else { print STDERR "WARNING: Can't open file $currentfile"; }
601 foreach my $line ( keys( %{$fallbackhashhash_ref->{ $cur_lang } } )) {
602 if( #$cur_lang ne "de" &&
603 $cur_lang ne "en-US" ){
604 print ALLPARTICLES_MERGED ( $fallbackhashhash_ref->{ $cur_lang }{ $line }, "\n" );
608 } else {
609 foreach my $currentfile ( @sdfparticles ){
610 if ( open MYFILE , "< $currentfile" ) {
611 while( <MYFILE> ){
612 print ALLPARTICLES_MERGED ( $_, "\n" ); # recheck de / en-US !
615 else { print STDERR "WARNING: Can't open file $currentfile"; }
618 close ALLPARTICLES_MERGED;
619 #***************
621 # Hash of array
622 my %output;
623 my @order;
625 ## Join both
626 if( $outputfile ){
627 if( open DESTFILE , "+> $outputfile" ){
628 if( !open LOCALIZEPARTICLE , "< $localizeSDF" ) { print STDERR "ERROR: Can't open file $localizeSDF\n"; }
629 if( !open ALLPARTICLES_MERGED , "< $particleSDF_merged" ) { print STDERR "ERROR: Can't open file $particleSDF_merged\n"; }
631 # Insert localize
632 my $extract_date="";
633 while ( <LOCALIZEPARTICLE> ){
634 if( /$sdf_regex/ ){
635 my $leftpart = defined $2 ? $2 : '';
636 my $lang = defined $12 ? $12 : '';
637 my $rightpart = defined $13 ? $13 : '';
638 my $timestamp = defined $18 ? $18 : '';
640 my $prj = defined $3 ? $3 : '';
641 my $file = defined $4 ? $4 : '';
642 my $type = defined $6 ? $6 : '';
643 my $gid = defined $7 ? $7 : '';
644 my $lid = defined $8 ? $8 : '';
645 #my $lang = defined $12 ? $12 : '';
646 my $plattform = defined $10 ? $10 : '';
647 my $helpid = defined $9 ? $9 : '';
650 if( $use_default_date )
652 $extract_date = "$default_date\n" ;
654 elsif( $extract_date eq "" ) {
655 $extract_date = $timestamp ;
656 $extract_date =~ tr/\r\n//d;
657 $extract_date .= "\n";
660 if( $bAll ){ print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date ; }
661 else {
662 foreach my $sLang ( keys( %{ $langhash_ref } ) ){
663 if( $sLang=~ /all/i ) {
664 push @{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } , $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
665 #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date;
667 #if( $sLang eq "de" && $lang eq "de" ) {
668 # push @{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } , $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
669 #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date;
671 if( $sLang eq "en-US" && $lang eq "en-US" ) {
672 push @order , $prj.$gid.$lid.$file.$type.$plattform.$helpid;
673 if( !$bFakeEnglish ){ push @{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } , $leftpart."\t".$lang."\t".$rightpart.$extract_date ; }
674 #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date;
681 # Insert particles
682 while ( <ALLPARTICLES_MERGED> ){
683 if( /$sdf_regex/ ){
684 my $leftpart = defined $2 ? $2 : '';
685 my $prj = defined $3 ? $3 : '';
686 my $lang = defined $12 ? $12 : '';
687 my $rightpart = defined $13 ? $13 : '';
688 my $timestamp = defined $18 ? $18 : '';
690 #my $prj = defined $3 ? $3 : '';
691 my $file = defined $4 ? $4 : '';
692 my $type = defined $6 ? $6 : '';
693 my $gid = defined $7 ? $7 : '';
694 my $lid = defined $8 ? $8 : '';
695 #my $lang = defined $12 ? $12 : '';
696 my $plattform = defined $10 ? $10 : '';
697 my $helpid = defined $9 ? $9 : '';
700 if( $use_default_date )
702 $extract_date = "$default_date\n" ;
704 elsif( $extract_date eq "" )
706 $extract_date = $timestamp;
709 if( ! ( $prj =~ /binfilter/i ) ) {
710 push @{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } , $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
711 #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
716 # Write!
717 foreach my $curkey ( @order ){
718 foreach my $curlist ( $output{ $curkey } ){
719 foreach my $line ( @{$curlist} ){
720 print DESTFILE $line;
725 }else { print STDERR "Can't open $outputfile";}
727 close DESTFILE;
728 close LOCALIZEPARTICLE;
729 close ALLPARTICLES_MERGED;
731 #print STDOUT "DBG: \$localizeSDF $localizeSDF \$particleSDF_merged $particleSDF_merged\n";
732 unlink $localizeSDF , $particleSDF_merged , $my_localize_log;
734 #sort_outfile( $outputfile );
735 #remove_obsolete( $outputfile ) , if $bHasSourceLanguage ne "";
738 #########################################################
739 sub remove_obsolete{
740 my $outfile = shift;
741 my @lines;
742 my $enusleftpart;
743 my @good_lines;
745 print STDOUT "### Removing obsolete strings\n";
747 # Kick out all strings without en-US reference
748 if ( open ( SORTEDFILE , "< $outfile" ) ){
749 while( <SORTEDFILE> ){
750 if( /$sdf_regex/ ){
751 my $line = defined $_ ? $_ : '';
752 my $language = defined $12 ? $12 : '';
753 my $prj = defined $3 ? $3 : '';
754 my $file = defined $4 ? $4 : '';
755 my $type = defined $6 ? $6 : '';
756 my $gid = defined $7 ? $7 : '';
757 my $lid = defined $8 ? $8 : '';
758 my $plattform = defined $10 ? $10 : '';
759 my $helpid = defined $9 ? $9 : '';
761 my $leftpart = $prj.$gid.$lid.$file.$type.$plattform.$helpid;
763 if( $language eq "en-US" ){ # source string found, 1. entry
764 $enusleftpart = $leftpart;
765 push @good_lines , $line;
766 }else{
767 if( !defined $enusleftpart or !defined $leftpart ){
768 print STDERR "BADLINE: $line\n";
769 print STDERR "\$enusleftpart = $enusleftpart\n";
770 print STDERR "\$leftpart = $leftpart\n";
772 if( $enusleftpart eq $leftpart ){ # matching language
773 push @good_lines , $line;
775 #else{
776 # print STDERR "OUT: \$enusleftpart=$enusleftpart \$leftpart=$leftpart \$line=$line\n";
781 close SORTEDFILE;
782 } else { print STDERR "ERROR: Can't open file $outfile\n";}
784 # Write file
785 if ( open ( SORTEDFILE , "> $outfile" ) ){
786 foreach my $newline ( @good_lines ) {
787 print SORTEDFILE $newline;
789 close SORTEDFILE;
790 } else { print STDERR "ERROR: Can't open file $outfile\n";}
793 #########################################################
794 sub sort_outfile{
795 my $outfile = shift;
796 print STDOUT "### Sorting ... $outfile ...";
797 my @lines;
798 my @sorted_lines;
801 #if ( open ( SORTEDFILE , "< $outputfile" ) ){
802 if ( open ( SORTEDFILE , "< $outfile" ) ){
803 my $line;
804 while ( <SORTEDFILE> ){
805 $line = $_;
806 if( $line =~ /^[^\#]/ ){
807 push @lines , $line;
810 close SORTEDFILE;
811 @sorted_lines = sort {
812 my $xa_lang = "";
813 my $xa_left_part = "";
814 my $xa_right_part = "";
815 my $xa_timestamp = "";
816 my $xb_lang = "";
817 my $xb_left_part = "";
818 my $xb_right_part = "";
819 my $xb_timestamp = "";
820 my $xa = "";
821 my $xb = "";
822 my @alist;
823 my @blist;
825 if( $a=~ /$sdf_regex/ ){
826 $xa_left_part = defined $2 ? $2 : '';
827 $xa_lang = defined $12 ? $12 : '';
828 $xa_right_part = defined $13 ? $13 : '';
829 $xa_left_part = remove_last_column( $xa_left_part );
832 if( $b=~ /$sdf_regex/ ){
833 $xb_left_part = defined $2 ? $2 : '';
834 $xb_lang = defined $12 ? $12 : '';
835 $xb_right_part = defined $13 ? $13 : '';
836 $xb_left_part = remove_last_column( $xb_left_part );
840 if( ( $xa_left_part cmp $xb_left_part ) == 0 ){ # Left part equal
841 if( ( $xa_lang cmp $xb_lang ) == 0 ){ # Lang equal
842 return ( $xa_right_part cmp $xb_right_part ); # Right part compare
844 elsif( $xa_lang eq "en-US" ) { return -1; } # en-US wins
845 elsif( $xb_lang eq "en-US" ) { return 1; } # en-US wins
846 else { return $xa_lang cmp $xb_lang; } # lang compare
848 else {
849 return $xa_left_part cmp $xb_left_part; # Left part compare
851 } @lines;
853 if ( open ( SORTEDFILE , "> $outfile" ) ){
854 print SORTEDFILE get_license_header();
855 foreach my $newline ( @sorted_lines ) {
856 print SORTEDFILE $newline;
857 #print STDOUT $newline;
860 close SORTEDFILE;
861 } else { print STDERR "WARNING: Can't open file $outfile\n";}
862 print "done\n";
865 #########################################################
866 sub remove_last_column{
867 my $string = shift;
868 my @alist = split ( "\t" , $string );
869 pop @alist;
870 return join( "\t" , @alist );
873 #########################################################
874 sub rename_language{
875 my $fallbackhashhash_ref = shift;
876 my $cur_fallback = shift;
877 my $cur_lang = shift;
878 my $line;
880 foreach my $key( keys ( %{ $fallbackhashhash_ref->{ $cur_fallback } } ) ){
881 $line = $fallbackhashhash_ref->{ $cur_fallback }{ $key };
882 if( $line =~ /$sdf_regex/ ){
883 my $leftpart = defined $2 ? $2 : '';
884 my $lang = defined $12 ? $12 : '';
885 my $rightpart = defined $13 ? $13 : '';
887 $fallbackhashhash_ref->{ $cur_lang }{ $key } = $leftpart."\t".$cur_lang."\t".$rightpart;
892 ############################################################
893 sub remove_duplicates{
894 my $list_ref = shift;
895 my %tmphash;
896 foreach my $key ( @{ $list_ref } ){ $tmphash{ $key } = '' ; }
897 @{$list_ref} = keys( %tmphash );
900 ##############################################################
901 sub fetch_fallback{
902 my $sdfparticleslist_ref = shift;
903 my $localizeSDF = shift;
904 my $langhash_ref = shift;
905 my %fallbackhashhash;
906 my $cur_lang;
907 my @langlist;
909 foreach my $key ( keys ( %{ $langhash_ref } ) ){
910 $cur_lang = $langhash_ref->{ $key };
911 if ( $cur_lang ne "" ) {
912 push @langlist , $cur_lang;
915 remove_duplicates( \@langlist );
916 foreach $cur_lang ( @langlist ){
917 if( $cur_lang eq "en-US" ){
918 read_fallbacks_from_source( $localizeSDF , $cur_lang , \%fallbackhashhash );
922 # remove de / en-US
923 my @tmplist;
924 foreach $cur_lang( @langlist ){
925 if( $cur_lang ne "en-US" ){
926 push @tmplist , $cur_lang;
930 @langlist = @tmplist;
931 if ( $#langlist +1 ){
932 read_fallbacks_from_particles( $sdfparticleslist_ref , \@langlist , \%fallbackhashhash );
935 return (\%fallbackhashhash);
938 #########################################################
939 sub write_file{
941 my $localizeFile = shift;
942 my $index_ref = shift;
944 if( open DESTFILE , "+> $localizeFile" ){
945 foreach my $key( %{ $index_ref } ){
946 print DESTFILE ($index_ref->{ $key }, "\n" );
948 close DESTFILE;
949 }else {
950 print STDERR "Can't open/create '$localizeFile'";
954 #########################################################
955 sub read_file{
957 my $sdffile = shift;
958 my $langhash_ref = shift;
959 my %block = ();
961 open MYFILE , "< $sdffile"
962 or die "Can't open '$sdffile'\n";
963 while( <MYFILE>){
964 if( /$sdf_regex/ ){
965 my $line = defined $_ ? $_ : '';
966 my $prj = defined $3 ? $3 : '';
967 my $file = defined $4 ? $4 : '';
968 my $type = defined $6 ? $6 : '';
969 my $gid = defined $7 ? $7 : '';
970 my $lid = defined $8 ? $8 : '';
971 my $plattform = defined $10 ? $10 : '';
972 my $lang = defined $12 ? $12 : '';
973 my $helpid = defined $9 ? $9 : '';
975 foreach my $isolang ( keys ( %{ $langhash_ref } ) ){
976 if( $isolang=~ /$lang/i || $isolang=~ /all/i ) { $block{$prj.$gid.$lid.$file.$type.$plattform.$helpid } = $line ; }
980 return (\%block);
983 #########################################################
984 sub read_fallbacks_from_particles{
986 my $sdfparticleslist_ref = shift;
987 my $isolanglist_ref = shift;
988 my $fallbackhashhash_ref = shift;
989 my $block_ref;
990 foreach my $currentfile ( @{ $sdfparticleslist_ref } ){
991 if ( open MYFILE , "< $currentfile" ) {
992 while(<MYFILE>){
993 if( /$sdf_regex/ ){
994 my $line = defined $_ ? $_ : '';
995 my $prj = defined $3 ? $3 : '';
996 my $file = defined $4 ? $4 : '';
997 my $type = defined $6 ? $6 : '';
998 my $gid = defined $7 ? $7 : '';
999 my $lid = defined $8 ? $8 : '';
1000 my $lang = defined $12 ? $12 : '';
1001 my $plattform = defined $10 ? $10 : '';
1002 my $helpid = defined $9 ? $9 : '';
1004 chomp( $line );
1006 foreach my $isolang ( @{$isolanglist_ref} ){
1007 if( $isolang=~ /$lang/i ) {
1008 $fallbackhashhash_ref->{ $isolang }{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } = $line ;
1013 }else { print STDERR "WARNING: Can't open file $currentfile"; }
1017 #########################################################
1018 sub read_fallbacks_from_source{
1020 my $sdffile = shift;
1021 my $isolang = shift;
1022 my $fallbackhashhash_ref = shift;
1023 my $block_ref;
1024 # read fallback for single file
1025 open MYFILE , "< $sdffile"
1026 or die "Can't open '$sdffile'\n";
1028 while( <MYFILE>){
1029 if( /$sdf_regex/ ){
1030 my $line = defined $_ ? $_ : '';
1031 my $prj = defined $3 ? $3 : '';
1032 my $file = defined $4 ? $4 : '';
1033 my $type = defined $6 ? $6 : '';
1034 my $gid = defined $7 ? $7 : '';
1035 my $lid = defined $8 ? $8 : '';
1036 my $helpid = defined $9 ? $9 : '';
1037 my $lang = defined $12 ? $12 : '';
1038 my $plattform = defined $10 ? $10 : '';
1040 chomp( $line );
1041 if( $isolang=~ /$lang/i ) { $fallbackhashhash_ref->{ $isolang }{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } = $line ;
1047 #########################################################
1048 sub parseLanguages{
1050 my $bAll;
1051 my $bUseLocalize;
1052 my $bHasSourceLanguage="";
1053 my $bFakeEnglish="";
1054 my %langhash;
1055 my $iso="";
1056 my $fallback="";
1058 #### -l all
1059 if( $languages=~ /all/ ){
1060 $bAll = "TRUE";
1061 $bHasSourceLanguage = "TRUE";
1063 ### -l fr=de,de
1064 elsif( $languages=~ /.*,.*/ ){
1065 my @tmpstr = split "," , $languages;
1066 for my $lang ( @tmpstr ){
1067 if( $lang=~ /([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*)(=([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*))?/ ){
1068 $iso = $1;
1069 $fallback = $4;
1071 if( ( $iso && $iso=~ /(en-US)/i ) || ( $fallback && $fallback=~ /(en-US)/i ) ) {
1072 $bUseLocalize = "TRUE";
1074 if( ( $iso && $iso=~ /(en-US)/i ) ) {
1075 $bHasSourceLanguage = "TRUE";
1077 if( $fallback ) { $langhash{ $iso } = $fallback; }
1078 else { $langhash{ $iso } = ""; }
1082 ### -l de
1083 else{
1084 if( $languages=~ /([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*)(=([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*))?/ ){
1085 $iso = $1;
1086 $fallback = $4;
1088 if( ( $iso && $iso=~ /(en-US)/i ) || ( $fallback && $fallback=~ /(en-US)/i ) ) {
1089 $bUseLocalize = "TRUE";
1092 if( ( $iso && $iso=~ /(en-US)/i ) ) {
1093 $bHasSourceLanguage = "TRUE";
1096 if( $fallback ) { $langhash{ $iso } = $fallback; }
1097 else { $langhash{ $iso } = ""; }
1100 # HACK en-US always needed!
1101 if( !$bHasSourceLanguage ){
1102 #$bHasSourceLanguage = "TRUE";
1103 $bUseLocalize = "TRUE";
1104 $bFakeEnglish = "TRUE";
1105 $langhash{ "en-US" } = "";
1107 return ( $bAll , $bUseLocalize , \%langhash , $bHasSourceLanguage, $bFakeEnglish);
1110 #########################################################
1111 sub parse_options{
1113 my $help;
1114 my $merge;
1115 my $extract;
1116 my $success = GetOptions('f=s' => \$sdffile , 'l=s' => \$languages , 's=s' => \$srcpath , 'h' => \$help , 'v' => \$bVerbose ,
1117 'm' => \$merge , 'e' => \$extract , 'x' => \$no_sort , 'd' => \$use_default_date , 'c' => \$create_dirs ,
1118 'n' => \$no_gsicheck );
1119 $outputfile = $sdffile;
1121 #print STDOUT "DBG: lang = $languages\n";
1122 if( !$srcpath ){
1123 $srcpath = "$ENV{SRC_ROOT}";
1124 if( !$srcpath ){
1125 print STDERR "No path to the source root found!\n\n";
1126 usage();
1127 exit(1);
1130 if( $help || !$success || $#ARGV > 1 || ( !$sdffile ) ){
1131 usage();
1132 exit(1);
1134 if( $merge && $sdffile && ! ( -r $sdffile)){
1135 print STDERR "Can't open file '$sdffile'\n";
1136 exit(1);
1138 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\-]*)*)?)*/ ) ){
1139 print STDERR "Please check the -l iso code\n";
1140 exit(1);
1142 if( ( !$merge && !$extract ) || ( $merge && $extract ) ){ usage();exit( -1 );}
1143 if( $extract ){ $mode = "extract"; }
1144 else { $mode = "merge"; }
1146 #my $multi_localize_files = ''; h
1147 #my $module_to_merge = ''; i
1148 #my $sort_sdf_before = ''; g
1150 #########################################################
1151 sub usage{
1153 print STDERR "Usage: localize.pl\n";
1154 print STDERR "Split or collect SDF files\n";
1155 print STDERR " merge: -m -f <sdffile> -l l1[=f1][,l2[=f2]][...] [ -s <sourceroot> ] [ -c ]\n";
1156 print STDERR " extract: -e -f <outputfile> -l <lang> [ -s <sourceroot> ] [-d]\n";
1157 print STDERR "Options:\n";
1158 print STDERR " -h help\n";
1159 print STDERR " -m Merge mode\n";
1160 print STDERR " -e Extract mode\n";
1161 print STDERR " -f <sdffile> To split a big SDF file into particles\n";
1162 print STDERR " <outputfile> To collect and join all particles to one big file\n";
1163 print STDERR " -s <sourceroot> Path to the modules, if no \$SRC_ROOT is set\n";
1164 print STDERR " -l ( all | <isocode> | <isocode>=fallback ) comma seperated languages\n";
1165 print STDERR " -d Use default date in extracted sdf file\n";
1166 print STDERR " -c Create needed directories\n";
1167 print STDERR " -g Sort sdf file before mergeing\n";
1168 print STDERR " -h File with localize.sdf's\n!";
1169 print STDERR " -n No gsicheck\n";
1170 print STDERR " -i Module to merge\n";
1171 print STDERR " -v Verbose\n";
1172 print STDERR "\nExample:\n";
1173 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";
1174 print STDERR "\nlocalize -m -l cs -f my.sdf\n( Merge cs translation into the sourcecode ) \n";
1177 sub fix_cygwin_path
1179 my ( $path ) = @_;
1181 if ( $^O eq 'cygwin' )
1183 $path = qx{cygpath -m "$path"};
1184 chomp($path);
1187 return $path;
1190 # my $line = defined $_ ? $_ : '';
1191 # my $leftpart = defined $2 ? $2 : '';
1192 # my $prj = defined $3 ? $3 : '';
1193 # my $file = defined $4 ? $4 : '';
1194 # my $dummy = defined $5 ? $5 : '';
1195 # my $type = defined $6 ? $6 : '';
1196 # my $gid = defined $7 ? $7 : '';
1197 # my $lid = defined $8 ? $8 : '';
1198 # my $helpid = defined $9 ? $9 : '';
1199 # my $plattform = defined $10 ? $10 : '';
1200 # my $width = defined $11 ? $11 : '';
1201 # my $lang = defined $12 ? $12 : '';
1202 # my $rightpart = defined $13 ? $13 : '';
1203 # my $text = defined $14 ? $14 : '';
1204 # my $helptext = defined $15 ? $15 : '';
1205 # my $quickhelptext = defined $16 ? $16 : '';
1206 # my $title = defined $17 ? $17 : '';
1207 # my $timestamp = defined $18 ? $18 : '';