xref: /aoo4110/main/l10ntools/scripts/localize.pl (revision b1cdbd2c)
1:
2eval 'exec perl -wS $0 ${1+"$@"}'
3    if 0;
4
5
6#**************************************************************
7#
8#  Licensed to the Apache Software Foundation (ASF) under one
9#  or more contributor license agreements.  See the NOTICE file
10#  distributed with this work for additional information
11#  regarding copyright ownership.  The ASF licenses this file
12#  to you under the Apache License, Version 2.0 (the
13#  "License"); you may not use this file except in compliance
14#  with the License.  You may obtain a copy of the License at
15#
16#    http://www.apache.org/licenses/LICENSE-2.0
17#
18#  Unless required by applicable law or agreed to in writing,
19#  software distributed under the License is distributed on an
20#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21#  KIND, either express or implied.  See the License for the
22#  specific language governing permissions and limitations
23#  under the License.
24#
25#**************************************************************
26
27
28
29use strict;
30use Getopt::Long;
31use IO::Handle;
32use File::Find;
33use File::Temp;
34use File::Path;
35use File::Copy;
36use File::Glob qw(:glob csh_glob);
37use Cwd;
38
39my $CVS_BINARY = "/usr/bin/cvs";
40# ver 1.1
41#
42#### module lookup
43#use lib ("$ENV{SOLARENV}/bin/modules", "$ENV{COMMON_ENV_TOOLS}/modules");
44
45#### module lookup
46# OOo conform
47my @lib_dirs;
48BEGIN {
49    if ( !defined($ENV{SOLARENV}) ) {
50        die "No environment found (environment variable SOLARENV is undefined)";
51    }
52    push(@lib_dirs, "$ENV{SOLARENV}/bin/modules");
53    push(@lib_dirs, "$ENV{COMMON_ENV_TOOLS}/modules") if defined($ENV{COMMON_ENV_TOOLS});
54}
55use lib (@lib_dirs);
56
57#### globals ####
58my $sdffile                 = '';
59my $no_sort                 = '';
60my $create_dirs             = '';
61my $multi_localize_files    = '';
62my $module_to_merge         = '';
63my $sort_sdf_before         = '';
64my $outputfile              = '';
65my $no_gsicheck             = '';
66my $mode                    = '';
67my $bVerbose                = "0";
68my $srcpath                 = '';
69my $languages;
70#my %sl_modules;     # Contains all modules where en-US and de is source language
71my $use_default_date = '0';
72my $force_ooo_module = '0';
73my %is_ooo_module;
74my %is_so_module;
75
76         #         (                           leftpart                                                     )            (           rightpart                    )
77         #            prj      file      dummy     type       gid       lid      helpid    pform     width      lang       text    helptext  qhelptext   title    timestamp
78my $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]*))";
79my $file_types = "(src|hrc|xcs|xcu|lng|ulf|xrm|xhp|xcd|xgf|xxl|xrb)";
80# Always use this date to prevent cvs conflicts
81my $default_date = "2002-02-02 02:02:02";
82my @sdfparticles;
83
84#### main ####
85parse_options();
86
87my $binpath = '';
88if( defined $ENV{UPDMINOREXT} )
89{
90    $binpath = $ENV{SOLARVER}."/".$ENV{INPATH}."/bin".$ENV{UPDMINOREXT}."/" ;
91}
92else
93{
94    $binpath = $ENV{SOLARVER}."/".$ENV{INPATH}."/bin/" ;
95}
96
97#%sl_modules = fetch_sourcelanguage_dirlist();
98
99
100if   ( $mode eq "merge"    )    {
101    if ( ! $no_gsicheck ){
102        merge_gsicheck();
103    }
104    splitfile( $sdffile );
105    if ( ! $no_gsicheck ){
106        unlink $sdffile;             # remove temp file!
107    }
108}
109elsif( $mode eq "extract"  )    {
110    collectfiles( $outputfile );
111}
112else                            {
113    usage();
114}
115
116exit(0);
117
118#########################################################
119sub splitfile{
120
121    my $lastFile        = '';
122    my $currentFile     = '';
123    my $cur_sdffile     = '';
124    my $last_sdffile    = '';
125    my $delim;
126    my $badDelim;
127    my $start           = 'TRUE';
128    my %index  = ();
129    my %block;
130
131    STDOUT->autoflush( 1 );
132
133    #print STDOUT "Open File $sdffile\n";
134    open MYFILE , "< $sdffile"
135    or die "Can't open '$sdffile'\n";
136
137#    my %lang_hash;
138    my %string_hash_ooo;
139    my %string_hash_so;
140    my %so_modules;
141    $so_modules{ "extras_full" } = "TRUE";
142
143    while( <MYFILE>){
144         if( /$sdf_regex/ ){
145            my $line           = defined $_ ? $_ : '';
146            my $prj            = defined $3 ? $3 : '';
147            my $file           = defined $4 ? $4 : '';
148            my $type           = defined $6 ? $6 : '';
149            my $gid            = defined $7 ? $7 : '';
150            my $lid            = defined $8 ? $8 : '';
151            my $lang           = defined $12 ? $12 : '';
152            my $plattform      = defined $10 ? $10 : '';
153            my $helpid         = defined $9 ? $9 : '';
154            next if( $prj eq "binfilter" );     # Don't merge strings into binfilter module
155	        chomp( $line );
156
157            if( $force_ooo_module )
158            {
159                $string_hash_ooo { $lang }{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } = $line;
160            }
161            else
162            {
163                $string_hash_so{ $lang }{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } = $line;
164            }
165        }
166    }
167    close( MYFILE );
168
169    if( !defined $ENV{SOURCE_ROOT_DIR} ){
170        print "Error, no SOURCE_ROOT_DIR in env found.\n";
171        exit( -1 );
172    }
173    my $src_root = $ENV{SOURCE_ROOT_DIR};
174    my $ooo_src_root = $src_root."/l10n";
175    my $so_l10n_path  = $src_root."/sun/l10n_so/source";
176    my $ooo_l10n_path = $ooo_src_root."/l10n/source";
177
178    #print "$so_l10n_path\n";
179    #print "$ooo_l10n_path\n";
180
181    if( $force_ooo_module )
182    {
183        write_sdf( \%string_hash_ooo , $ooo_l10n_path );
184    }
185    else
186    {
187        write_sdf( \%string_hash_so , $so_l10n_path );
188    }
189}
190
191sub write_sdf
192{
193    my $string_hash         = shift;
194    my $l10n_file           = shift;
195
196    foreach my $lang( keys( %{ $string_hash } ) )
197    {
198        my @sdf_file;
199        next , if( $lang eq "en-US" );
200
201        mkdir $l10n_file."/$lang";
202        # mkdir!!!!
203        my $current_l10n_file = $l10n_file."/$lang/localize.sdf";
204        print "Writing '$current_l10n_file'\n";
205        if( open DESTFILE , "< $current_l10n_file" ){
206
207            while(<DESTFILE>){
208                if( /$sdf_regex/ ){
209                    my $line           = defined $_ ? $_ : '';
210                    my $prj            = defined $3 ? $3 : '';
211                    my $file           = defined $4 ? $4 : '';
212                    my $type           = defined $6 ? $6 : '';
213                    my $gid            = defined $7 ? $7 : '';
214                    my $lid            = defined $8 ? $8 : '';
215                    my $lang           = defined $12 ? $12 : '';
216                    my $plattform      = defined $10 ? $10 : '';
217                    my $helpid         = defined $9 ? $9 : '';
218
219                    chomp( $line );
220                    if ( defined $string_hash->{ $lang }{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } )
221                    {
222                        # Changed String!
223                        push @sdf_file , $string_hash->{ $lang }{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } ;
224                        $string_hash->{ $lang }{ "$prj\t$file\t$type\t$gid\t$lid\t$helpid\t$plattform\t$lang" } = undef;
225                    }
226                    else
227                    {
228                        # No new string
229                        push @sdf_file , $line;
230                    }
231                }
232            }
233        }
234        close( DESTFILE );
235        #Now just append the enw strings
236        #FIXME!!! Implement insertion in the correct order
237        foreach my $key ( keys ( %{ $string_hash->{ $lang } } ) )
238        {
239            push @sdf_file , $string_hash->{ $lang }{ $key } , if ( defined $string_hash->{ $lang }{ $key } );
240            #print "WARNING: Not defined = ".$string_hash->{ $lang }{ $key }."\n", if( ! defined  $string_hash->{ $lang }{ $key } );
241        }
242
243        # Write the new file
244        my ( $TMPFILE , $tmpfile ) = File::Temp::tempfile();
245        if( open DESTFILE , "+> $tmpfile " ){
246            print DESTFILE get_license_header();
247            foreach my $string( @sdf_file ){
248                print DESTFILE "$string\n";
249            }
250            close ( DESTFILE );
251            if( move( $current_l10n_file , $current_l10n_file.".backup" ) ){
252                if( copy( $tmpfile , $current_l10n_file ) ){
253                    unlink $l10n_file.".backup";
254                 } else { print STDERR "Can't open/create '$l10n_file', original file is renamed to $l10n_file.backup\n"; }
255            } else { print STDERR "Can't open/create '$l10n_file'\n"; }
256         }else{
257            print STDERR "WARNING: Can't open/create '$l10n_file'\n";
258         }
259         unlink $tmpfile;
260     }
261}
262
263#########################################################
264
265sub get_license_header{
266    return
267"#\n".
268"#    ####    ###     #   #   ###   #####    #####  ####   #####  #####  \n".
269"#    #   #  #   #    ##  #  #   #    #      #      #   #    #      #    \n".
270"#    #   #  #   #    # # #  #   #    #      ###    #   #    #      #    \n".
271"#    #   #  #   #    #  ##  #   #    #      #      #   #    #      #    \n".
272"#    ####    ###     #   #   ###     #      #####  ####   #####    #    \n".
273"#\n".
274"#    DO NOT EDIT! This file will be overwritten by localisation process\n".
275"#\n".
276"#**************************************************************\n".
277"#  \n".
278"#  Licensed to the Apache Software Foundation (ASF) under one\n".
279"#  or more contributor license agreements.  See the NOTICE file\n".
280"#  distributed with this work for additional information\n".
281"#  regarding copyright ownership.  The ASF licenses this file\n".
282"#  to you under the Apache License, Version 2.0 (the\n".
283"#  \"License\"); you may not use this file except in compliance\n".
284"#  with the License.  You may obtain a copy of the License at\n".
285"#  \n".
286"#    http://www.apache.org/licenses/LICENSE-2.0\n".
287"#  \n".
288"#  Unless required by applicable law or agreed to in writing,\n".
289"#  software distributed under the License is distributed on an\n".
290"#  \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n".
291"#  KIND, either express or implied.  See the License for the\n".
292"#  specific language governing permissions and limitations\n".
293"#  under the License.\n".
294"#  \n".
295"#**************************************************************\n";
296}
297######## Check input sdf file and use only the correct part
298sub merge_gsicheck{
299    my $command = '';
300    my ( $TMPHANDLE , $tmpfile ) = File::Temp::tempfile();
301    close ( $TMPHANDLE );
302
303    $command = "$ENV{WRAPCMD} " if( $ENV{WRAPCMD} );
304    $command .= "$ENV{SOLARVER}/$ENV{INPATH}/bin/gsicheck";
305
306    my $errfile = $sdffile.".err";
307    $command .= " -k -c -wcf $tmpfile -wef $errfile -l \"\" $sdffile";
308    #my $rc = system( $command );
309    my $output = `$command`;
310    my $rc = $? << 8;
311    if ( $output ne "" ){
312        print STDOUT "### gsicheck ###\n";
313        print STDOUT "### The file $errfile have been written containing the errors in your sdf file. Those lines will not be merged: ###\n\n";
314        print STDOUT "$output\n";
315        print STDOUT "################\n";
316
317    }else{
318        # Remove the 0 Byte file
319        unlink $errfile;
320    }
321    $sdffile = $tmpfile;
322}
323#########################################################
324# find search function
325sub wanted
326{
327    my $file = $File::Find::name;
328    if( -f $file && $file =~ /.*localize.sdf$/ && !( $file =~ /.*\.svn.*/ ) ) {
329        push   @sdfparticles , $file;
330        if( $bVerbose eq "1" ) { print STDOUT "$file\n"; }
331        else { print ".";  }
332    }
333}
334
335sub add_paths
336{
337    my $langhash_ref            = shift;
338    my $root_dir = $ENV{ SRC_ROOT };
339    my $ooo_l10n_dir = "$root_dir/../extras/l10n/source";
340
341    if( -e $ooo_l10n_dir )
342    {
343        foreach my $lang ( keys( %{ $langhash_ref } ) )
344        {
345            my $loc_file = "$ooo_l10n_dir/$lang/localize.sdf";
346            if( -e $loc_file )
347            {
348                push @sdfparticles , "$ooo_l10n_dir/$lang/localize.sdf";
349            }
350            else { print "WARNING: $loc_file not found ....\n"; }
351        }
352    }
353    else { die "ERROR: Can not find directory $ooo_l10n_dir!!!" }
354}
355sub collectfiles{
356    print STDOUT "### Localize\n";
357    my $localizehash_ref;
358    my ( $bAll , $bUseLocalize, $langhash_ref , $bHasSourceLanguage , $bFakeEnglish ) = parseLanguages();
359
360    # Enable autoflush on STDOUT
361    # $| = 1;
362    STDOUT->autoflush( 1 );
363
364    my $working_path = getcwd();
365    chdir $ENV{SOURCE_ROOT_DIR}, if defined $ENV{SOURCE_ROOT_DIR};
366    add_paths( $langhash_ref );
367
368    my ( $LOCALIZEPARTICLE , $localizeSDF ) = File::Temp::tempfile();
369    close( $LOCALIZEPARTICLE );
370
371    my ( $ALLPARTICLES_MERGED , $particleSDF_merged )     = File::Temp::tempfile();
372    close( $ALLPARTICLES_MERGED );
373    my ( $LOCALIZE_LOG , $my_localize_log ) = File::Temp::tempfile();
374    close( $LOCALIZE_LOG );
375
376    ## Get the localize en-US extract
377    if( $bAll || $bUseLocalize ){
378        print "### Fetching source language strings\n";
379        my $command = "";
380        my $args    = "";
381
382        if( $ENV{WRAPCMD} ){
383            $command = $ENV{WRAPCMD}.$binpath."localize_sl";
384        }else{
385            $command = $binpath."localize_sl";
386        }
387        print $command;
388        # -e
389        # if ( -x $command ){
390        if( $command ){
391            if( !$bVerbose  ){ $args .= " "; }
392            $args .= " -e -f $localizeSDF -l ";
393            my $bFlag="";
394            if( $bAll ) {$args .= " en-US";}
395            else{
396              my @list;
397              foreach my $isokey ( keys( %{ $langhash_ref } ) ){
398                push @list , $isokey;
399                if( $langhash_ref->{ $isokey } ne "" ){
400                    push @list , $langhash_ref->{ $isokey };
401                }
402              }
403              remove_duplicates( \@list );
404              foreach my $isokey ( @list ){
405                switch :{
406                       ( $isokey=~ /^en-US$/i  )
407                        && do{
408                                if( $bFlag eq "TRUE" ){ $args .= ",en-US"; }
409                                else {
410                                    $args .= "en-US";  $bFlag = "TRUE";
411                                 }
412                              };
413
414                    } #switch
415                } #foreach
416              } # if
417        } # if
418        if ( $bVerbose ) { print STDOUT $command.$args."\n"; }
419
420        my $rc = system( $command.$args );
421
422        if( $rc < 0 ){    print STDERR "ERROR: localize rc = $rc\n"; exit( -1 ); }
423        ( $localizehash_ref )  = read_file( $localizeSDF , $langhash_ref );
424
425    }
426    ## Get sdf particles
427#*****************
428    open ALLPARTICLES_MERGED , "+>> $particleSDF_merged"
429    or die "Can't open $particleSDF_merged";
430
431    ## Fill fackback hash
432    my( $fallbackhashhash_ref ) = fetch_fallback( \@sdfparticles , $localizeSDF ,  $langhash_ref );
433    my %block;
434    my $cur_fallback;
435    if( !$bAll) {
436        foreach my $cur_lang ( keys( %{ $langhash_ref } ) ){
437            #print STDOUT "DBG: G1 cur_lang=$cur_lang\n";
438            $cur_fallback = $langhash_ref->{ $cur_lang };
439            if( $cur_fallback ne "" ){
440                # Insert fallback strings
441                #print STDOUT "DBG: Renaming $cur_fallback to $cur_lang in fallbackhash\n";
442                rename_language(  $fallbackhashhash_ref ,  $cur_fallback , $cur_lang );
443            }
444            foreach my $currentfile ( @sdfparticles ){
445                if ( open MYFILE , "< $currentfile" ) {
446                    while(<MYFILE>){
447                        if( /$sdf_regex/ ){
448                            my $line           = defined $_ ? $_ : '';
449                            my $prj            = defined $3 ? $3 : '';
450                            my $file           = defined $4 ? $4 : '';
451                            my $type           = defined $6 ? $6 : '';
452                            my $gid            = defined $7 ? $7 : '';
453                            my $lid            = defined $8 ? $8 : '';
454                            my $lang           = defined $12 ? $12 : '';
455                            my $plattform      = defined $10 ? $10 : '';
456                            my $helpid         = defined $9 ? $9 : '';
457
458                            chomp( $line );
459
460                            if ( $lang eq $cur_lang ){
461                                # Overwrite fallback strings with collected strings
462                                #if( ( !has_two_sourcelanguages( $cur_lang) && $cur_lang eq "de" ) || $cur_lang ne "en-US" ){
463                                     $fallbackhashhash_ref->{ $cur_lang }{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } =  $line ;
464                                     #}
465
466                            }
467                        }
468                    }
469                }else { print STDERR "WARNING: Can't open file $currentfile"; }
470            }
471
472            foreach my $line ( keys( %{$fallbackhashhash_ref->{ $cur_lang } } )) {
473                if( #$cur_lang ne "de" &&
474                    $cur_lang ne "en-US" ){
475                    print ALLPARTICLES_MERGED ( $fallbackhashhash_ref->{ $cur_lang }{ $line }, "\n" );
476                }
477             }
478        }
479    } else {
480        foreach my $currentfile ( @sdfparticles ){
481            if ( open MYFILE , "< $currentfile" ) {
482                while( <MYFILE> ){
483                    print ALLPARTICLES_MERGED ( $_, "\n" );  # recheck de / en-US !
484                }
485            }
486            else { print STDERR "WARNING: Can't open file $currentfile"; }
487        }
488    }
489    close ALLPARTICLES_MERGED;
490
491    # Hash of array
492    my %output;
493    my @order;
494
495    ## Join both
496    if( $outputfile ){
497        if( open DESTFILE , "+> $outputfile" ){
498            if( !open LOCALIZEPARTICLE ,  "< $localizeSDF" ) { print STDERR "ERROR: Can't open file $localizeSDF\n"; }
499            if( !open ALLPARTICLES_MERGED , "< $particleSDF_merged" ) { print STDERR "ERROR: Can't open file $particleSDF_merged\n"; }
500
501            # Insert localize
502            my $extract_date="";
503            while ( <LOCALIZEPARTICLE> ){
504                if( /$sdf_regex/ ){
505                    my $leftpart       = defined $2 ? $2 : '';
506                    my $lang           = defined $12 ? $12 : '';
507                    my $rightpart      = defined $13 ? $13 : '';
508                    my $timestamp      = defined $18 ? $18 : '';
509
510                    my $prj            = defined $3 ? $3 : '';
511                    my $file           = defined $4 ? $4 : '';
512                    my $type           = defined $6 ? $6 : '';
513                    my $gid            = defined $7 ? $7 : '';
514                    my $lid            = defined $8 ? $8 : '';
515                    #my $lang           = defined $12 ? $12 : '';
516                    my $plattform      = defined $10 ? $10 : '';
517                    my $helpid         = defined $9 ? $9 : '';
518
519
520                    if( $use_default_date )
521                    {
522                        $extract_date = "$default_date\n" ;
523                    }
524                    elsif( $extract_date eq "" ) {
525                        $extract_date = $timestamp ;
526                        $extract_date =~ tr/\r\n//d;
527                        $extract_date .= "\n";
528                    }
529
530                    if( $bAll ){ print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date ; }
531                    else {
532                        foreach my $sLang ( keys( %{ $langhash_ref } ) ){
533                            if( $sLang=~ /all/i )                       {
534                                push @{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } ,  $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
535                                #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date;
536                            }
537                            #if( $sLang eq "de" && $lang eq "de" )       {
538                            #    push @{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } ,  $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
539                                #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date;
540                                #}
541                            if( $sLang eq "en-US" && $lang eq "en-US" ) {
542                                push @order , $prj.$gid.$lid.$file.$type.$plattform.$helpid;
543                                if( !$bFakeEnglish ){ push @{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } ,  $leftpart."\t".$lang."\t".$rightpart.$extract_date ; }
544                                #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date;
545                            }
546
547                        }
548                    }
549                }
550            }
551            # Insert particles
552            while ( <ALLPARTICLES_MERGED> ){
553                if( /$sdf_regex/ ){
554                    my $leftpart       = defined $2 ? $2 : '';
555                    my $prj            = defined $3 ? $3 : '';
556                    my $lang           = defined $12 ? $12 : '';
557                    my $rightpart      = defined $13 ? $13 : '';
558                    my $timestamp      = defined $18 ? $18 : '';
559
560                    #my $prj            = defined $3 ? $3 : '';
561                    my $file           = defined $4 ? $4 : '';
562                    my $type           = defined $6 ? $6 : '';
563                    my $gid            = defined $7 ? $7 : '';
564                    my $lid            = defined $8 ? $8 : '';
565                    #my $lang           = defined $12 ? $12 : '';
566                    my $plattform      = defined $10 ? $10 : '';
567                    my $helpid         = defined $9 ? $9 : '';
568
569
570                    if( $use_default_date )
571                    {
572                        $extract_date = "$default_date\n" ;
573                    }
574                    elsif( $extract_date eq "" )
575                    {
576                        $extract_date = $timestamp;
577                    }
578
579                    if( ! ( $prj =~ /binfilter/i ) ) {
580                        push @{ $output{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } } , $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
581                        #print DESTFILE $leftpart."\t".$lang."\t".$rightpart.$extract_date ;
582                    }
583                 }
584            }
585
586            # Write!
587            foreach my $curkey ( @order ){
588                foreach my $curlist ( $output{ $curkey } ){
589                    foreach my $line ( @{$curlist} ){
590                        print DESTFILE $line;
591                    }
592                }
593            }
594
595        }else { print STDERR "Can't open $outputfile";}
596    }
597    close DESTFILE;
598    close LOCALIZEPARTICLE;
599    close ALLPARTICLES_MERGED;
600    chdir $working_path;
601
602    #print STDOUT "DBG: \$localizeSDF $localizeSDF \$particleSDF_merged $particleSDF_merged\n";
603    unlink $localizeSDF , $particleSDF_merged ,  $my_localize_log;
604
605    #sort_outfile( $outputfile );
606    #remove_obsolete( $outputfile ) , if $bHasSourceLanguage ne "";
607    }
608
609#########################################################
610sub remove_obsolete{
611    my $outfile = shift;
612    my @lines;
613    my $enusleftpart;
614    my @good_lines;
615
616    print STDOUT "### Removing obsolete strings\n";
617
618    # Kick out all strings without en-US reference
619    if ( open ( SORTEDFILE , "< $outfile" ) ){
620        while( <SORTEDFILE> ){
621            if( /$sdf_regex/ ){
622                my $line           = defined $_ ? $_ : '';
623                my $language       = defined $12 ? $12 : '';
624                my $prj            = defined $3 ? $3 : '';
625                my $file           = defined $4 ? $4 : '';
626                my $type           = defined $6 ? $6 : '';
627                my $gid            = defined $7 ? $7 : '';
628                my $lid            = defined $8 ? $8 : '';
629                my $plattform      = defined $10 ? $10 : '';
630                my $helpid         = defined $9 ? $9 : '';
631
632                my $leftpart = $prj.$gid.$lid.$file.$type.$plattform.$helpid;
633
634                if( $language eq "en-US" ){                 # source string found, 1. entry
635                    $enusleftpart = $leftpart;
636                    push @good_lines , $line;
637                }else{
638                    if( !defined $enusleftpart or !defined $leftpart ){
639                        print STDERR "BADLINE: $line\n";
640                        print STDERR "\$enusleftpart = $enusleftpart\n";
641                        print STDERR "\$leftpart = $leftpart\n";
642                    }
643                    if( $enusleftpart eq $leftpart ){   # matching language
644                        push @good_lines , $line;
645                    }
646                    #else{
647                    #    print STDERR "OUT:  \$enusleftpart=$enusleftpart \$leftpart=$leftpart \$line=$line\n";
648                    #}
649                }
650            }
651        }
652        close SORTEDFILE;
653    } else { print STDERR "ERROR: Can't open file $outfile\n";}
654
655    # Write file
656    if ( open ( SORTEDFILE , "> $outfile" ) ){
657        foreach my $newline ( @good_lines ) {
658            print SORTEDFILE $newline;
659        }
660        close SORTEDFILE;
661    } else { print STDERR "ERROR: Can't open file $outfile\n";}
662
663}
664#########################################################
665sub sort_outfile{
666        my $outfile = shift;
667        print STDOUT "### Sorting ... $outfile ...";
668        my @lines;
669        my @sorted_lines;
670
671
672        #if ( open ( SORTEDFILE , "< $outputfile" ) ){
673        if ( open ( SORTEDFILE , "< $outfile" ) ){
674            my $line;
675            while ( <SORTEDFILE> ){
676                $line = $_;
677                if( $line =~ /^[^\#]/ ){
678                    push @lines , $line;
679                }
680            }
681            close SORTEDFILE;
682            @sorted_lines = sort {
683                my $xa_lang          = "";
684                my $xa_left_part    = "";
685                my $xa_right_part    = "";
686                my $xa_timestamp     = "";
687                my $xb_lang          = "";
688                my $xb_left_part    = "";
689                my $xb_right_part    = "";
690                my $xb_timestamp     = "";
691                my $xa               = "";
692                my $xb               = "";
693                my @alist;
694                my @blist;
695
696                if( $a=~ /$sdf_regex/ ){
697                    $xa_left_part       = defined $2 ? $2 : '';
698                    $xa_lang           = defined $12 ? $12 : '';
699                    $xa_right_part     = defined $13 ? $13 : '';
700                    $xa_left_part = remove_last_column( $xa_left_part );
701
702                }
703                if( $b=~ /$sdf_regex/ ){
704                    $xb_left_part       = defined $2 ? $2 : '';
705                    $xb_lang           = defined $12 ? $12 : '';
706                    $xb_right_part     = defined $13 ? $13 : '';
707                    $xb_left_part = remove_last_column( $xb_left_part );
708
709
710                }
711                if( (  $xa_left_part cmp $xb_left_part ) == 0 ){         # Left part equal
712                     if( ( $xa_lang cmp $xb_lang ) == 0 ){               # Lang equal
713                         return ( $xa_right_part cmp $xb_right_part );   # Right part compare
714                    }
715                    elsif( $xa_lang eq "en-US" ) { return -1; }        # en-US wins
716                    elsif( $xb_lang eq "en-US" ) { return 1;  }        # en-US wins
717                    else { return $xa_lang cmp $xb_lang; }             # lang compare
718                }
719                else {
720                    return $xa_left_part cmp $xb_left_part;        # Left part compare
721                }
722            } @lines;
723
724            if ( open ( SORTEDFILE , "> $outfile" ) ){
725                print SORTEDFILE get_license_header();
726                foreach my $newline ( @sorted_lines ) {
727                    print SORTEDFILE $newline;
728                    #print STDOUT $newline;
729                }
730            }
731            close SORTEDFILE;
732        } else { print STDERR "WARNING: Can't open file $outfile\n";}
733	print "done\n";
734
735}
736#########################################################
737sub remove_last_column{
738    my $string                  = shift;
739    my @alist = split ( "\t" , $string );
740    pop @alist;
741    return join( "\t" , @alist );
742}
743
744#########################################################
745sub rename_language{
746    my $fallbackhashhash_ref    = shift;
747    my $cur_fallback            = shift;
748    my $cur_lang                = shift;
749    my $line;
750
751    foreach my $key( keys ( %{ $fallbackhashhash_ref->{ $cur_fallback } } ) ){
752        $line = $fallbackhashhash_ref->{ $cur_fallback }{ $key };
753        if( $line =~ /$sdf_regex/ ){
754            my $leftpart       = defined $2 ? $2 : '';
755            my $lang           = defined $12 ? $12 : '';
756            my $rightpart      = defined $13 ? $13 : '';
757
758            $fallbackhashhash_ref->{ $cur_lang }{ $key } = $leftpart."\t".$cur_lang."\t".$rightpart;
759        }
760    }
761}
762
763############################################################
764sub remove_duplicates{
765    my $list_ref    = shift;
766    my %tmphash;
767    foreach my $key ( @{ $list_ref } ){ $tmphash{ $key } = '' ; }
768    @{$list_ref} = keys( %tmphash );
769}
770
771##############################################################
772sub fetch_fallback{
773    my $sdfparticleslist_ref   = shift;
774    my $localizeSDF            = shift;
775    my $langhash_ref           = shift;
776    my %fallbackhashhash;
777    my $cur_lang;
778    my @langlist;
779
780    foreach my $key ( keys ( %{ $langhash_ref } ) ){
781        $cur_lang = $langhash_ref->{ $key };
782        if ( $cur_lang ne "" ) {
783            push @langlist , $cur_lang;
784        }
785    }
786    remove_duplicates( \@langlist );
787    foreach  $cur_lang ( @langlist ){
788        if( $cur_lang eq "en-US" ){
789            read_fallbacks_from_source( $localizeSDF , $cur_lang , \%fallbackhashhash );
790        }
791    }
792
793    # remove de / en-US
794    my @tmplist;
795    foreach $cur_lang( @langlist ){
796        if(  $cur_lang ne "en-US" ){
797           push @tmplist , $cur_lang;
798
799        }
800    }
801    @langlist = @tmplist;
802    if ( $#langlist +1 ){
803        read_fallbacks_from_particles( $sdfparticleslist_ref , \@langlist , \%fallbackhashhash );
804
805    }
806    return (\%fallbackhashhash);
807}
808
809#########################################################
810sub write_file{
811
812    my $localizeFile = shift;
813    my $index_ref    = shift;
814
815    if( open DESTFILE , "+> $localizeFile" ){
816        foreach my $key( %{ $index_ref } ){
817            print DESTFILE ($index_ref->{ $key }, "\n" );
818        }
819        close DESTFILE;
820    }else {
821      print STDERR "Can't open/create '$localizeFile'";
822    }
823}
824
825#########################################################
826sub read_file{
827
828    my $sdffile         = shift;
829    my $langhash_ref    = shift;
830    my %block           = ();
831
832    open MYFILE , "< $sdffile"
833        or die "Can't open '$sdffile'\n";
834        while( <MYFILE>){
835          if( /$sdf_regex/ ){
836            my $line           = defined $_ ? $_ : '';
837            my $prj            = defined $3 ? $3 : '';
838            my $file           = defined $4 ? $4 : '';
839            my $type           = defined $6 ? $6 : '';
840            my $gid            = defined $7 ? $7 : '';
841            my $lid            = defined $8 ? $8 : '';
842            my $plattform      = defined $10 ? $10 : '';
843            my $lang           = defined $12 ? $12 : '';
844            my $helpid         = defined $9 ? $9 : '';
845
846            foreach my $isolang ( keys ( %{ $langhash_ref } ) ){
847                if( $isolang=~ /$lang/i || $isolang=~ /all/i ) { $block{$prj.$gid.$lid.$file.$type.$plattform.$helpid } =  $line ; }
848            }
849        }
850    }
851    return (\%block);
852}
853
854#########################################################
855sub read_fallbacks_from_particles{
856
857    my $sdfparticleslist_ref    = shift;
858    my $isolanglist_ref         = shift;
859    my $fallbackhashhash_ref    = shift;
860    my $block_ref;
861    foreach my $currentfile ( @{ $sdfparticleslist_ref } ){
862        if ( open MYFILE , "< $currentfile" ) {
863            while(<MYFILE>){
864                if( /$sdf_regex/ ){
865                    my $line           = defined $_ ? $_ : '';
866                    my $prj            = defined $3 ? $3 : '';
867                    my $file           = defined $4 ? $4 : '';
868                    my $type           = defined $6 ? $6 : '';
869                    my $gid            = defined $7 ? $7 : '';
870                    my $lid            = defined $8 ? $8 : '';
871                    my $lang           = defined $12 ? $12 : '';
872                    my $plattform      = defined $10 ? $10 : '';
873                    my $helpid         = defined $9 ? $9 : '';
874
875                    chomp( $line );
876
877                    foreach my $isolang ( @{$isolanglist_ref}  ){
878                        if( $isolang=~ /$lang/i ) {
879                            $fallbackhashhash_ref->{ $isolang }{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } =  $line ;
880                        }
881                    }
882                }
883            }
884       }else { print STDERR "WARNING: Can't open file $currentfile"; }
885    }
886}
887
888#########################################################
889sub read_fallbacks_from_source{
890
891    my $sdffile                 = shift;
892    my $isolang                 = shift;
893    my $fallbackhashhash_ref    = shift;
894    my $block_ref;
895    # read fallback for single file
896    open MYFILE , "< $sdffile"
897        or die "Can't open '$sdffile'\n";
898
899    while( <MYFILE>){
900          if( /$sdf_regex/ ){
901            my $line           = defined $_ ? $_ : '';
902            my $prj            = defined $3 ? $3 : '';
903            my $file           = defined $4 ? $4 : '';
904            my $type           = defined $6 ? $6 : '';
905            my $gid            = defined $7 ? $7 : '';
906            my $lid            = defined $8 ? $8 : '';
907            my $helpid         = defined $9 ? $9 : '';
908            my $lang           = defined $12 ? $12 : '';
909            my $plattform      = defined $10 ? $10 : '';
910
911            chomp( $line );
912            if( $isolang=~ /$lang/i ) { $fallbackhashhash_ref->{ $isolang }{ $prj.$gid.$lid.$file.$type.$plattform.$helpid } =  $line ;
913            }
914        }
915    }
916}
917
918#########################################################
919sub parseLanguages{
920
921    my $bAll;
922    my $bUseLocalize;
923    my $bHasSourceLanguage="";
924    my $bFakeEnglish="";
925    my %langhash;
926    my $iso="";
927    my $fallback="";
928
929    #### -l all
930    if(   $languages=~ /all/ ){
931        $bAll = "TRUE";
932        $bHasSourceLanguage = "TRUE";
933    }
934    ### -l fr=de,de
935    elsif( $languages=~ /.*,.*/ ){
936        my @tmpstr =  split "," , $languages;
937        for my $lang ( @tmpstr ){
938            if( $lang=~ /([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*)(=([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*))?/ ){
939                $iso        = $1;
940                $fallback   = $4;
941
942                if( ( $iso && $iso=~ /(en-US)/i )  || ( $fallback && $fallback=~ /(en-US)/i ) ) {
943                    $bUseLocalize = "TRUE";
944                }
945                if( ( $iso && $iso=~ /(en-US)/i ) ) {
946                    $bHasSourceLanguage = "TRUE";
947                }
948             if( $fallback ) { $langhash{ $iso } = $fallback;   }
949             else            { $langhash{ $iso } = "";          }
950            }
951        }
952    }
953    ### -l de
954    else{
955        if( $languages=~ /([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*)(=([a-zA-Z]{2,3}(-[a-zA-Z\-]*)*))?/ ){
956            $iso        = $1;
957            $fallback   = $4;
958
959            if( ( $iso && $iso=~ /(en-US)/i )  || ( $fallback && $fallback=~ /(en-US)/i ) ) {
960                $bUseLocalize = "TRUE";
961
962            }
963            if( ( $iso && $iso=~ /(en-US)/i )  ) {
964                $bHasSourceLanguage = "TRUE";
965            }
966
967             if( $fallback ) { $langhash{ $iso } = $fallback;   }
968             else            { $langhash{ $iso } = "";          }
969        }
970    }
971    # HACK en-US always needed!
972    if( !$bHasSourceLanguage ){
973        #$bHasSourceLanguage = "TRUE";
974        $bUseLocalize = "TRUE";
975        $bFakeEnglish = "TRUE";
976        $langhash{ "en-US" } = "";
977    }
978    return ( $bAll ,  $bUseLocalize , \%langhash , $bHasSourceLanguage, $bFakeEnglish);
979}
980
981#########################################################
982sub parse_options{
983
984    my $help;
985    my $merge;
986    my $extract;
987    my $success = GetOptions('f=s' => \$sdffile , 'l=s' => \$languages , 's=s' => \$srcpath ,  'h' => \$help , 'v' => \$bVerbose ,
988                             'm' => \$merge , 'e' => \$extract , 'x' => \$no_sort , 'd' => \$use_default_date , 'c' => \$create_dirs ,
989                             'n' => \$no_gsicheck , 'o' => \$force_ooo_module );
990    $outputfile = $sdffile;
991
992    #print STDOUT "DBG: lang = $languages\n";
993    if( !$srcpath ){
994        $srcpath = "$ENV{SRC_ROOT}";
995        if( !$srcpath ){
996	        print STDERR "No path to the source root found!\n\n";
997	        usage();
998            exit(1);
999        }
1000    }
1001    if( $help ){
1002        usage();
1003        exit(0);
1004    }
1005    if( !$success || $#ARGV > 1 || ( !$sdffile ) ){
1006        usage();
1007        exit(1);
1008    }
1009    if( $merge && $sdffile && ! ( -r $sdffile)){
1010        print STDERR "Can't open file '$sdffile'\n";
1011        exit(1);
1012    }
1013    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\-]*)*)?)*/ ) ){
1014        print STDERR "Please check the -l iso code\n";
1015        exit(1);
1016    }
1017    if( ( !$merge && !$extract ) || ( $merge && $extract ) ){ usage();exit( -1 );}
1018    if( $extract ){ $mode = "extract"; }
1019    else          { $mode = "merge";   }
1020}
1021#my $multi_localize_files    = ''; h
1022#my $module_to_merge         = ''; i
1023#my $sort_sdf_before         = ''; g
1024
1025#########################################################
1026sub usage{
1027
1028    print STDERR "Usage: localize.pl\n";
1029    print STDERR "Split or collect SDF files\n";
1030    print STDERR "           merge: -m -f <sdffile>    -l l1[=f1][,l2[=f2]][...] [ -s <sourceroot> ] [ -c ]\n";
1031    print STDERR "         extract: -e -f <outputfile> -l <lang> [ -s <sourceroot> ] [-d]\n";
1032    print STDERR "Options:\n";
1033    print STDERR "    -h              help\n";
1034    print STDERR "    -m              Merge mode\n";
1035    print STDERR "    -e              Extract mode\n";
1036    print STDERR "    -f <sdffile>    To split a big SDF file into particles\n";
1037    print STDERR "       <outputfile> To collect and join all particles to one big file\n";
1038    print STDERR "    -s <sourceroot> Path to the modules, if no \$SRC_ROOT is set\n";
1039    print STDERR "    -l ( all | <isocode> | <isocode>=fallback ) comma seperated languages\n";
1040    print STDERR "    -d              Use default date in extracted sdf file\n";
1041    print STDERR "    -c              Create needed directories\n";
1042    print STDERR "    -g              Sort sdf file before mergeing\n";
1043    print STDERR "    -h              File with localize.sdf's\n!";
1044    print STDERR "    -n              No gsicheck\n";
1045    print STDERR "    -i              Module to merge\n";
1046    print STDERR "    -o              force using ooo localization from the l10n module instead of l10n_so; \n";
1047    print STDERR "                    useful if the type can't be detected by the .svn tags; \n";
1048    print STDERR "    -v              Verbose\n";
1049    print STDERR "\nExample:\n";
1050    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";
1051    print STDERR "\nlocalize -m -l cs -f my.sdf\n( Merge cs translation into the sourcecode ) \n";
1052}
1053
1054