xref: /aoo42x/main/sysui/desktop/share/translate.pl (revision cdf0e10c)
1*cdf0e10cSrcweir:
2*cdf0e10cSrcweireval 'exec perl -wS $0 ${1+"$@"}'
3*cdf0e10cSrcweir	if 0;
4*cdf0e10cSrcweir
5*cdf0e10cSrcweir#*************************************************************************
6*cdf0e10cSrcweir#
7*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8*cdf0e10cSrcweir#
9*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
10*cdf0e10cSrcweir#
11*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
12*cdf0e10cSrcweir#
13*cdf0e10cSrcweir# This file is part of OpenOffice.org.
14*cdf0e10cSrcweir#
15*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
16*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
17*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
18*cdf0e10cSrcweir#
19*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
20*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
21*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
23*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
24*cdf0e10cSrcweir#
25*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
26*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
27*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
28*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
29*cdf0e10cSrcweir#
30*cdf0e10cSrcweir#*************************************************************************
31*cdf0e10cSrcweir
32*cdf0e10cSrcweir
33*cdf0e10cSrcweir#*********************************************************************
34*cdf0e10cSrcweir#
35*cdf0e10cSrcweir# main
36*cdf0e10cSrcweir#
37*cdf0e10cSrcweir
38*cdf0e10cSrcweirmy ($prefix, $ext, $key);
39*cdf0e10cSrcweir$productname = "OpenOffice.org";
40*cdf0e10cSrcweir$workdir = ".";
41*cdf0e10cSrcweir
42*cdf0e10cSrcweirwhile ($_ = $ARGV[0], /^-/) {
43*cdf0e10cSrcweir    shift;
44*cdf0e10cSrcweir    last if /^--$/;
45*cdf0e10cSrcweir    if (/^-p/) {
46*cdf0e10cSrcweir        $productname = $ARGV[0];
47*cdf0e10cSrcweir        shift;
48*cdf0e10cSrcweir    }
49*cdf0e10cSrcweir    if (/^-d/) {
50*cdf0e10cSrcweir        $workdir = $ARGV[0];
51*cdf0e10cSrcweir        shift;
52*cdf0e10cSrcweir    }
53*cdf0e10cSrcweir    if (/^--key/) {
54*cdf0e10cSrcweir        $key = $ARGV[0];
55*cdf0e10cSrcweir        shift;
56*cdf0e10cSrcweir    }
57*cdf0e10cSrcweir    if (/^--prefix/) {
58*cdf0e10cSrcweir        $prefix = $ARGV[0];
59*cdf0e10cSrcweir        shift;
60*cdf0e10cSrcweir    }
61*cdf0e10cSrcweir    if (/^--ext/) {
62*cdf0e10cSrcweir        $ext = $ARGV[0];
63*cdf0e10cSrcweir        shift;
64*cdf0e10cSrcweir    }
65*cdf0e10cSrcweir}
66*cdf0e10cSrcweir
67*cdf0e10cSrcweir# open input file
68*cdf0e10cSrcweirunless (open(SOURCE, $ARGV[0])) {
69*cdf0e10cSrcweir    print STDERR "Can't open $ARGV[0] file: $!\n";
70*cdf0e10cSrcweir    return;
71*cdf0e10cSrcweir}
72*cdf0e10cSrcweir
73*cdf0e10cSrcweir
74*cdf0e10cSrcweir# For every section in the specified ulf file there should exist
75*cdf0e10cSrcweir# a template file in $workdir ..
76*cdf0e10cSrcweirwhile (<SOURCE>) {
77*cdf0e10cSrcweir    $line = $_;
78*cdf0e10cSrcweir
79*cdf0e10cSrcweir    if ( "[" eq substr($line, 0, 1) ) {
80*cdf0e10cSrcweir        # Pass the tail of the template to the output file
81*cdf0e10cSrcweir        while (<TEMPLATE>) {
82*cdf0e10cSrcweir            print OUTFILE;
83*cdf0e10cSrcweir        }
84*cdf0e10cSrcweir
85*cdf0e10cSrcweir        close(TEMPLATE);
86*cdf0e10cSrcweir
87*cdf0e10cSrcweir        if (close(OUTFILE)) {
88*cdf0e10cSrcweir            system "mv -f $outfile.tmp $outfile\n";
89*cdf0e10cSrcweir        }
90*cdf0e10cSrcweir
91*cdf0e10cSrcweir        $_ = substr($line, 1, index($line,"]")-1);
92*cdf0e10cSrcweir        $outfile = "$workdir/$prefix$_.$ext";
93*cdf0e10cSrcweir
94*cdf0e10cSrcweir        # open the template file - ignore sections for which no
95*cdf0e10cSrcweir        # templates exist
96*cdf0e10cSrcweir        unless(open(TEMPLATE, $outfile)) {
97*cdf0e10cSrcweir            print STDERR "Warning: No template found for item $_: $outfile: $!\n";
98*cdf0e10cSrcweir            next;
99*cdf0e10cSrcweir        }
100*cdf0e10cSrcweir
101*cdf0e10cSrcweir    	# open output file
102*cdf0e10cSrcweir        unless (open(OUTFILE, "> $outfile.tmp")) {
103*cdf0e10cSrcweir            print STDERR "Can't create output file $outfile.tmp: $!\n";
104*cdf0e10cSrcweir    	    exit -1;
105*cdf0e10cSrcweir        }
106*cdf0e10cSrcweir
107*cdf0e10cSrcweir        # Pass the head of the template to the output file
108*cdf0e10cSrcweirKEY:    while (<TEMPLATE>) {
109*cdf0e10cSrcweir            print OUTFILE;
110*cdf0e10cSrcweir            last KEY if (/$key/);
111*cdf0e10cSrcweir        }
112*cdf0e10cSrcweir
113*cdf0e10cSrcweir    } else {
114*cdf0e10cSrcweir        # split locale = "value" into 2 strings
115*cdf0e10cSrcweir        ($locale, $value) = split(' = ', $line);
116*cdf0e10cSrcweir
117*cdf0e10cSrcweir        if ( $locale ne $line ) {
118*cdf0e10cSrcweir            # replace en-US with en
119*cdf0e10cSrcweir            $locale=~s/en-US/en/;
120*cdf0e10cSrcweir
121*cdf0e10cSrcweir            # use just anything inside the ""
122*cdf0e10cSrcweir            $value = substr($value, index($value, "\"") + 1, rindex($value, "\"") - 1);
123*cdf0e10cSrcweir
124*cdf0e10cSrcweir            # replace resource placeholder
125*cdf0e10cSrcweir            $value=~s/%PRODUCTNAME/$productname/g;
126*cdf0e10cSrcweir
127*cdf0e10cSrcweir            $locale=~s/-/_/;
128*cdf0e10cSrcweir            if ($ext eq "desktop") {
129*cdf0e10cSrcweir                print OUTFILE "$key\[$locale\]=$value\n";
130*cdf0e10cSrcweir            } else {
131*cdf0e10cSrcweir                print OUTFILE  "\t\[$locale\]$key=$value\n";
132*cdf0e10cSrcweir            }
133*cdf0e10cSrcweir        }
134*cdf0e10cSrcweir    }
135*cdf0e10cSrcweir}
136*cdf0e10cSrcweir
137*cdf0e10cSrcweirwhile (<TEMPLATE>) {
138*cdf0e10cSrcweir    print OUTFILE;
139*cdf0e10cSrcweir}
140*cdf0e10cSrcweir
141*cdf0e10cSrcweirif (close(OUTFILE)) {
142*cdf0e10cSrcweir    system "mv -f $outfile.tmp $outfile\n";
143*cdf0e10cSrcweir}
144*cdf0e10cSrcweir
145*cdf0e10cSrcweirclose(TEMPLATE);
146