Branch libreoffice-5-0-4
[LibreOffice.git] / solenv / bin / desktop-translate.pl
blob2b7b88263fb845c3575f8ab4be4287d5de81841f
2 eval 'exec perl -wS $0 ${1+"$@"}'
3 if 0;
6 # This file is part of the LibreOffice project.
8 # This Source Code Form is subject to the terms of the Mozilla Public
9 # License, v. 2.0. If a copy of the MPL was not distributed with this
10 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 # This file incorporates work covered by the following license notice:
14 # Licensed to the Apache Software Foundation (ASF) under one or more
15 # contributor license agreements. See the NOTICE file distributed
16 # with this work for additional information regarding copyright
17 # ownership. The ASF licenses this file to you under the Apache
18 # License, Version 2.0 (the "License"); you may not use this file
19 # except in compliance with the License. You may obtain a copy of
20 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
24 # Translates multiple .desktop files at once with strings from .ulf
25 # files; if you add new translateable .ulf files please add them to
26 # l10ntools/source/localize.cxx
29 my ($prefix, $ext, $key);
30 my $productname = "LibreOffice";
31 my $workdir = ".";
32 my $template_dir;
34 while ($_ = $ARGV[0], /^-/) {
35 shift;
36 last if /^--$/;
37 if (/^-p/) {
38 $productname = $ARGV[0];
39 shift;
41 if (/^-d/) {
42 $workdir = $ARGV[0];
43 shift;
45 if (/^--key/) {
46 $key = $ARGV[0];
47 shift;
49 if (/^--prefix/) {
50 $prefix = $ARGV[0];
51 shift;
53 if (/^--ext/) {
54 $ext = $ARGV[0];
55 shift;
57 if (/^--template-dir/) {
58 $template_dir = $ARGV[0];
59 shift;
63 if (!defined $template_dir) {
64 $template_dir = "$workdir/$prefix";
67 # hack for unity section
68 my $outkey = $key;
69 if ( $outkey eq "UnityQuicklist" ) {
70 $outkey = "Name";
73 my %templates;
75 # open input file
76 unless (open(SOURCE, $ARGV[0])) {
77 die "Can't open $ARGV[0] file: $!\n";
80 # currently read template
81 my $template;
83 # read ulf file
84 while (<SOURCE>) {
85 my $line = $_;
87 if ( "[" eq substr($line, 0, 1) ) {
88 $template = substr($line, 1, index($line,"]")-1);
89 my %entry;
90 # For every section in the specified ulf file there should exist
91 # a template file in $workdir ..
92 $entry{'outfile'} = "$template_dir$template.$ext";
93 my %translations;
94 $entry{'translations'} = \%translations;
95 $templates{$template} = \%entry;
96 } else {
97 # split locale = "value" into 2 strings
98 my ($locale, $value) = split(' = ', $line);
100 if ( $locale ne $line ) {
101 # replace en-US with en
102 $locale=~s/en-US/en/;
104 # use just anything inside the ""
105 $value = substr($value, index($value, "\"") + 1, rindex($value, "\"") - 1);
107 # replace resource placeholder
108 $value=~s/%PRODUCTNAME/$productname/g;
110 $locale=~s/-/_/;
112 $templates{$template}->{'translations'}->{$locale} = $value;
117 close(SOURCE);
119 my $processed = 0;
120 # process templates
121 foreach $template (keys %templates) {
122 my $outfile = $templates{$template}->{'outfile'};
124 # open the template file - ignore sections for which no
125 # templates exist
126 if (open(TEMPLATE, $outfile)) {
127 $processed++;
128 } elsif ($ext eq 'str') { # string files processed one by one
129 next;
130 } else {
131 die "Warning: No template found for item '$template' : '$outfile' : '$_': $!\n";
134 # open output file
135 unless (open(OUTFILE, "> $outfile.tmp")) {
136 print STDERR "Can't create output file $outfile.tmp: $!\n";
137 exit -1;
140 # emit the template to the output file
141 while (<TEMPLATE>) {
142 my $keyline = $_;
143 $keyline =~ s/^$key/$outkey/;
144 print OUTFILE $keyline;
145 if (/$key/) {
146 my $translations = $templates{$template}->{'translations'};
147 foreach my $locale (keys %{$translations}) {
148 my $value = $translations->{$locale};
149 # print "locale is $locale\n";
150 # print "value is $value\n";
151 if ($value) {
152 if ($ext eq "desktop" || $ext eq "str") {
153 print OUTFILE "$outkey\[$locale\]=$value\n";
154 } else {
155 print OUTFILE "\t\[$locale\]$outkey=$value\n";
162 close(TEMPLATE);
164 if (close(OUTFILE)) {
165 system "mv -f $outfile.tmp $outfile\n";
169 if ($ext eq 'str' && $processed == 0) {
170 die "Warning: No matching templates processed";