-
-
Notifications
You must be signed in to change notification settings - Fork 588
/
extracti18n.pl
343 lines (286 loc) · 9.21 KB
/
extracti18n.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/perl -w
# This file is part of Krita
#
# SPDX-FileCopyrightText: 2005 Sven Langkamp <[email protected]>
#
# SPDX-License-Identifier: GPL-2.0-or-later
use strict;
use warnings;
use Archive::Zip qw( :ERROR_CODES );
use Archive::Zip::MemberRead;
sub printi18n($$$$) {
# function that prints the actual output
my ($name, $filename, $filename2, $linenum) = @_;
chomp($name); # chomp out the null bytes at the end if relevant
$name =~ s/\0*//;
if ($name ne "")
{
if ($filename =~ /myb$/)
{
print "// i18n: Display name of resource, see context: MyPaint brush [path-to-file]/[resource-filename]\n";
}
else
{
print "// i18n: Display name of resource, see context: [path-to-resources]/[resource-type]/[resource-filename]\n";
}
if ($name =~ /^\w\)/)
{
print "// i18n: 'a)', 'b)' etc. in resource names are used to keep resources in a specific order ";
print "when Krita sorts them alphabetically. The order will be kept only using the original/untranslated names\n";
}
if ($name =~ /DITH\b/)
{
print "// i18n: DITH probably means 'dithering'\n";
}
if ($linenum > 0)
{
print "// i18n: file: ".$filename.":".$linenum."\n";
}
print "i18nc(\"".$filename."\",\"".$name."\");\n";
}
}
sub parseFromFilenameAuto($) {
# function that prettifies filenames without needing to define the extension
# because it figures out the extension on its own
my $name = $_[0];
my @extensions = split(/\./, $name);
my $extension = $extensions[$#extensions];
return parseFromFilename($name, $extension);
}
sub parseFromFilename($$) {
# function that prettifies filenames
# it extracts the filename from the full path, cuts off the extension and replaces '_ with ' '
my $name = $_[0];
my $extension = $_[1];
chomp($name);
my @path = split(/\//, $name);
$name = $path[$#path];
$name =~ s/_/ /g;
if( $extension ne "" ) {
$name =~ s/\.${extension}$//g;
}
return $name;
}
sub readGeneric($$$$) {
# function that reads all the lines from either the proper perl file handle
# or from the special buffer made out of the contents of the file in the bundle
my ($fh, $zipSpecialBuffer, $bufferref, $bytesnum) = @_;
my $response = undef;
my $buffer = ${$bufferref};
if(defined $fh)
{
$response = read($fh, $buffer, $bytesnum);
}
elsif(defined $zipSpecialBuffer)
{
my $where = $zipSpecialBuffer->{"pos"};
$buffer = unpack("x$where a${bytesnum}", $zipSpecialBuffer->{"buffer"});
$response = length($buffer);
$zipSpecialBuffer->{"pos"} = $zipSpecialBuffer->{"pos"} + $bytesnum;
}
${$bufferref} = $buffer;
return $response;
}
sub readAllLinesGeneric($$) {
# function that reads all the lines from either the proper perl file handle
# or from the MemberRead from the Archive::Zip module file handle
my ($fh, $ziph) = @_;
my @response = undef;
if(defined $fh)
{
@response = <$fh>;
}
elsif(defined $ziph)
{
my $i = 0;
@response = ();
while (defined(my $line = $ziph->getline()))
{
push(@response, $line);
$i += 1;
}
}
return @response;
}
sub readGbrBytesWise($$) {
# function that extracts a name from a gbr file
# it's in the function to allow easier error checking
# (early returning)
my ($fh, $ziph) = @_;
my $success = 1;
my ($bytes, $size, $version);
$success = readGeneric($fh, $ziph, \$bytes, 4) == 4;
return "" if not $success;
$size = unpack("N", $bytes);
$success = readGeneric($fh, $ziph, \$bytes, 4) == 4;
return "" if not $success;
$version = unpack("N", $bytes);
if( $version == 1 )
{
$success = readGeneric($fh, $ziph, \$bytes, 12) == 12;
return "" if not $success;
my $name;
$success = readGeneric($fh, $ziph, \$name, $size - 21) == $size - 21;
return "" if not $success;
return $name;
}
else
{
$success = readGeneric($fh, $ziph, \$bytes, 20) == 20;
return "" if not $success;
my $name;
$success = readGeneric($fh, $ziph, \$name, $size - 29) == $size - 29;
return "" if not $success;
return $name;
}
return "";
}
sub readZipSpecialBuffer($)
{
# this is a hack
# $ziph->read($buffer, $bytes) didn't work but ->getline() did
# this works for all binary files in the bundle that are supported at the moment
my ($ziph) = @_;
my $buffer = {};
$buffer->{"pos"} = 0;
my @array = readAllLinesGeneric(undef, $ziph);
$buffer->{"buffer"} = join("\n", @array);
return $buffer;
}
my @filenames = glob("./krita/data/gradients/*.ggr");
push( @filenames, glob("./krita/data/palettes/*.gpl"));
push( @filenames, glob("./krita/data/brushes/*.gih"));
push( @filenames, glob("./krita/data/brushes/*.gbr"));
push( @filenames, glob("./krita/data/brushes/*.svg"));
push( @filenames, glob("./krita/data/patterns/*.pat"));
push( @filenames, glob("./krita/data/patterns/*.png"));
push( @filenames, glob("./krita/data/paintoppresets/*.kpp"));
push( @filenames, glob("./krita/data/workspaces/*.kws"));
push( @filenames, glob("./krita/data/windowlayouts/*.kwl"));
push( @filenames, glob("./krita/data/gamutmasks/*.kgm"));
push( @filenames, glob("./plugins/paintops/mypaint/brushes/*.myb"));
push( @filenames, glob("./krita/data/symbols/*.svg"));
my %bundleForResource;
my %internalFilenameForResource;
# get the filename from the bundle
my @bundlenames = glob("./krita/data/bundles/*.bundle");
foreach my $bundlename (@bundlenames)
{
my $bundle = Archive::Zip->new();
unless ( $bundle->read( $bundlename ) == AZ_OK )
{
next;
}
my @memberNames = $bundle->memberNames();
foreach my $member (@memberNames)
{
unless ($member =~ /xml$/ or $member eq "mimetype" or $member eq "preview.png") {
my $newFilename = "$bundlename:$member";
push(@filenames, $newFilename);
$bundleForResource{$newFilename} = $bundle;
$internalFilenameForResource{$newFilename} = $member;
}
}
}
my $isZip = 0;
my $i = 0;
foreach my $filename (@filenames)
{
$i = $i + 1;
my $fh = undef;
my $ziph = undef;
unless ( open($fh, '<'.$filename) )
{
$fh = undef;
unless ($filename =~ /\.bundle/) {
next;
}
$ziph = Archive::Zip::MemberRead->new($bundleForResource{$filename}, $internalFilenameForResource{$filename});
unless (defined $ziph) {
next;
}
$isZip = 1;
}
if( $filename =~ /ggr/ || $filename =~ /gpl/ || $filename =~ /gih/ )
{
my @lines = readAllLinesGeneric($fh, $ziph);
if( $filename =~ /ggr/ || $filename =~ /gpl/ )
{
my @splitted = split(/: /, $lines[1]);
my $name = $splitted[1];
chomp($name);
$name =~ s/\t$//; # Trim trailing \t, fixes the name for "swatche.gpl"
printi18n($name, $filename, $filename, 2);
}
else
{
my $name = $lines[0];
chomp($name);
printi18n($name, $filename, $filename, 1);
}
}
elsif( $filename =~ /svg$/ )
{
my @lines = readAllLinesGeneric($fh, $ziph);
my $svg = join('', @lines);
my $name = "";
if( $svg =~ m:(<title.*?</title>):s )
{
my $titlesvg = $1;
if( $titlesvg =~ m:>(.*?)</title>:s ) #not perfect, but should usually do a good job, at least for existing libraries
{
$name = $1;
chomp($name);
$name =~ s/&/&/; # 'Pepper & Carrot Speech Bubbles' needs it
}
}
if ($name eq "")
{
$name = parseFromFilenameAuto($filename);
}
printi18n($name, $filename, $filename, -1);
}
elsif( $filename =~ /kpp$/ || $filename =~ /kws$/ || $filename =~ /kwl$/ || $filename =~ /kgm$/ || $filename =~ /jpg$/ || $filename =~ /myb$/ || $filename =~ /png$/ || $filename =~ /kse$/)
{
# all of Krita's default brush presets and other resources with abovementioned extensions
# are named the same way the file is called
# so there is no need to parse the file itself to find the name inside of it
my $extension = split(/\./, $filename);
my $name = parseFromFilenameAuto($filename);
printi18n($name, $filename, $filename, -1); # sadly, I'm not sure what the last number means exactly
}
elsif($filename =~ /gbr|pat/)
{
my $zipSpecialBuffer = undef;
if(defined($ziph)) {
$zipSpecialBuffer = readZipSpecialBuffer($ziph);
}
if( $filename =~ /gbr/ )
{
my $name = readGbrBytesWise($fh, $zipSpecialBuffer);
if($name eq "")
{
$name = parseFromFilenameAuto($filename);
}
printi18n($name, $filename, $filename, -1);
}
elsif( $filename =~ /pat$/ )
{
my $bytes;
my $name;
readGeneric($fh, $zipSpecialBuffer, \$bytes, 4);
my $size = unpack("N", $bytes);
readGeneric($fh, $zipSpecialBuffer, \$bytes, 20);
readGeneric($fh, $zipSpecialBuffer, \$name, $size - 25);
if( $name eq "" )
{
$name = parseFromFilenameAuto($filename);
}
printi18n($name, $filename, $filename, -1);
}
}
close($fh) if defined $fh;
}
# add "memory" resources that are defined in Krita's code (KoResourceServerProvider)
printi18n("0. Foreground to Background", "memory/gradients/Foreground to Background.svg", "memory/gradients/Foreground to Background.svg", -1);
printi18n("1. Foreground to Transparent", "memory/gradients/Foreground to Transparent.svg", "memory/gradients/Foreground to Transparent.svg", -1);