This repository has been archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
magicpo
executable file
·996 lines (881 loc) · 27.4 KB
/
magicpo
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
#!/usr/bin/perl
# MagicPO
# Assists in automated translations of gettext po-files
# Copyright (C) Eskild Hustvedt 2005, 2006, 2007, 2008
# Orignally forked from TransPO - copyright (C) 2003, 2004 Harald Thingelstad
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http:https://www.gnu.org/licenses/>.
use strict;
use warnings;
require 5.6.0; # Use of proper utf8 requires perl 5.6 or later
use strict;
use warnings;
use Getopt::Long; # Commandline parsing
use Carp;
use File::Basename; # Needed to find out our directory and name
use File::Copy; # We need to copy files
use utf8; # Allow the use of utf8 in the source code
use Cwd; # We need realpath();
use POSIX qw(locale_h); # We need setlocale();
use Fatal qw(open);
use constant {
true => 1,
false => 0,
# Mode definitions
# The dictionary mode. Classic MagicPO and default
MODE_DICT => 5,
# The database mode
MODE_DB => 6,
# The mode that creates a database
MODE_MKDB => 7,
};
# Used to locate our own modules
use FindBin;
use lib "$FindBin::RealBin/modules/";
# The PO-parser
use MagicPO::Parser;
# The dictionary loader
use MagicPO::DictLoader;
# The main worker
use MagicPO::Magic;
# See to that correct locale is set, manually (use encoding 'utf8' is broken).
{
my $locale = setlocale(LC_CTYPE);
$locale =~ s/\..+$//;
$locale .= '.UTF8';
setlocale(LC_CTYPE,$locale);
}
use locale; # Use POSIX locales for things like sorting
# Allow bundling of commandline parameters and
Getopt::Long::Configure ('bundling', 'prefix_pattern=(--|-)');
my $Version = '0.4'; # Version number
my (
$Dictionary, $BaseDictionary,
$ReformatPOFileOnly, $DebugMode,
$ReplaceString, $LastVerbLength,
$CurrentFile, $NewerFile, $OutFile,
$SourceDBFile, $NoLastTrans,
); # Declare variables
my $Verbose = true;
my $MarkFuzzy = true;
my $ReplaceFuzzy = false;
# Default mode is DICT
my $AppMode = MODE_DICT;
my $ChangeTest = ' ';
local $| = true;
my ($callP,$lastP) = (0,undef);
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Various smaller functions
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Purpose: Output pretty progression indicator
# Usage: progressed();
# - Outputs something every ten times it is called
sub progressed
{
return if not $Verbose;
if ($callP < 10)
{
$callP++;
return();
} else {
$callP = 0;
}
$lastP++;
local $| = false;
if(not defined($lastP) or $lastP == 1 or $lastP == 5){
print '-';
}elsif($lastP == 2 or $lastP == 6) {
print '\\';
}elsif($lastP == 3 or $lastP == 7) {
print '|';
}elsif($lastP == 4 or $lastP == 8) {
print '/';
}
$lastP = 0 if ($lastP == 8);
print "\b";
local $| = true;
return true;
}
# Purpose: Display a verbose status message
# Usage: vprint('Current status');
sub vprint {
return if not $Verbose;
print shift;
return true;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Small helper functions
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Purpose: Print formatted --help output
# Usage: PrintHelp("-shortoption", "--longoption", "description");
# Description will be reformatted to fit within a normal terminal
sub PrintHelp {
# The short option
my $short = shift,
# The long option
my $long = shift;
# The description
my $desc = shift;
# The generated description that will be printed in the end
my $GeneratedDesc;
# The current line of the description
my $currdesc = '';
# The maximum length any line can be
my $maxlen = 80;
# The length the options take up
my $optionlen = 20;
# Check if the short/long are LONGER than optionlen, if so, we need
# to do some additional magic to take up only $maxlen.
# The +1 here is because we always add a space between them, no matter what
if ((length($short) + length($long) + 1) > $optionlen)
{
$optionlen = length($short) + length($long) + 1;
}
# Split the description into lines
foreach my $part (split(/ /,$desc))
{
if(defined $GeneratedDesc)
{
if ((length($currdesc) + length($part) + 1 + 20) > $maxlen)
{
$GeneratedDesc .= "\n";
$currdesc = '';
}
else
{
$currdesc .= ' ';
$GeneratedDesc .= ' ';
}
}
$currdesc .= $part;
$GeneratedDesc .= $part;
}
# Something went wrong
die('Option mismatch') if not $GeneratedDesc;
# Print it all
foreach my $description (split(/\n/,$GeneratedDesc))
{
printf "%-4s %-15s %s\n", $short,$long,$description;
# Set short and long to '' to ensure we don't print the options twice
$short = '';$long = '';
}
# Succeed
return true;
}
# Purpose: Print the magicpo version number
# Usage: Version();
sub Version {
print "\nMagicPO version $Version\n";
return true;
}
# Purpose: Print our help
# Usage: Help();
sub Help {
my $Command = basename($0);
Version();
print "\n";
print "Usage: $Command [Options] --dict dictionary file1.po file2.po ...\n";
print " or : $Command [Options] --mkdb file.mdb source1.po source2.po source3.po\n";
print " or : $Command [Options] --dbfile file.mdb target.po target2.po ...\n\n";
PrintHelp('-h', '--help', 'Display this help screen');
PrintHelp('', '--string', 'Run replacements on the supplied string instead of a file');
PrintHelp('-f', '--dictfile X', 'Use the dictionary file X');
PrintHelp('-d', '--dict', 'Use the dictionary supplied. Use --dict list to list the possible parameters');
PrintHelp('-r', '--reformat', 'Don\'t actually replace anything, just write a reformatted file');
PrintHelp('', '--newer','Select the newer file for merging');
PrintHelp('', '--current','Select the current file for merging');
PrintHelp('-t', '--target', 'Output to the file supplied instead of back to the original file');
PrintHelp('-n', '--nomarkfuzzy', 'Don\'t mark changed strings as fuzzy');
PrintHelp('','--nolasttrans','Don\'t change "Last-Translator"');
PrintHelp('', '--debuginfo','Output some information useful for debugging');
PrintHelp('-s','--silent','Be silent');
PrintHelp('','--whatchanged','Lets you examine regular expressions. See the manpage.');
PrintHelp('','--replacefuzzy','Replace fuzzy strings with magicpo-translated strings when in merge mode.');
print "\n";
PrintHelp('','--mkdb X','Create a translation database for use with --dbfile (see the manpage).');
PrintHelp('','--dbfile X','Try to automatically translate the other files supplied on the commandline using the MagicPO translation database supplied to this argument.');
return true;
}
# Purpose: Get OS/distro version information
# Usage: print "OS: ",GetDistVer(),"\n";
sub GetDistVer {
# Try LSB first
my %LSB;
if (-e '/etc/lsb-release')
{
LoadConfigFile('/etc/lsb-release',\%LSB);
if(defined($LSB{DISTRIB_ID}) and $LSB{DISTRIB_ID} =~ /\S/ and defined($LSB{DISTRIB_RELEASE}) and $LSB{DISTRIB_RELEASE} =~ /\S/)
{
my $ret = '/etc/lsb-release: '.$LSB{DISTRIB_ID}.' '.$LSB{DISTRIB_RELEASE};
if(defined($LSB{DISTRIB_CODENAME}))
{
$ret .= ' ('.$LSB{DISTRIB_CODENAME}.')';
}
return($ret);
}
}
# GNU/Linux and BSD
foreach(qw/mandriva mandrakelinux mandrake fedora redhat red-hat ubuntu debian gentoo suse distro dist slackware freebsd openbsd netbsd dragonflybsd NULL/) {
if (-e "/etc/$_-release" or -e "/etc/$_-version" or -e "/etc/${_}_version" or $_ eq 'NULL') {
my ($DistVer, $File, $VERSION_FILE);
if(-e "/etc/$_-release") {
$File = "$_-release";
open($VERSION_FILE, '<', "/etc/$_-release") or next;
$DistVer = <$VERSION_FILE>;
} elsif (-e "/etc/$_-version") {
$File = "$_-version";
open($VERSION_FILE, '<', "/etc/$_-release") or next;
$DistVer = <$VERSION_FILE>;
} elsif (-e "/etc/${_}_version") {
$File = "${_}_version";
open($VERSION_FILE, '<', "/etc/${_}_version") or next;
$DistVer = <$VERSION_FILE>;
} elsif ($_ eq 'NULL') {
last if not -e '/etc/version';
$File = 'version';
open($VERSION_FILE, '<', '/etc/version') or next;
$DistVer = <$VERSION_FILE>;
}
close($VERSION_FILE) or warn("Failed to close filehandle for version input: $!\n");
chomp($DistVer);
return("/etc/$File: $DistVer");
}
}
# Didn't find anything yet. Get uname info
my ($sysname, $nodename, $release, $version, $machine) = POSIX::uname();
if ($sysname =~ /darwin/i) {
my $DarwinName;
my $DarwinOSVer;
# Darwin kernel, try to get OS X info.
if(InPath('sw_vers') and eval('use IPC::Open2;1')) {
if(open2(my $SW_VERS, my $NULL_IN, 'sw_vers')) {
while(<$SW_VERS>) {
chomp;
if (s/^ProductName:\s+//gi) {
$DarwinName = $_;
} elsif(s/^ProductVersion:\s+//) {
$DarwinOSVer = $_;
}
}
close($SW_VERS) or warn("Failed to close Open2 filehandle for sw_vers: $!\n");
close($NULL_IN) or warn("Failed to close Open2 (input) filehandle for sw_vers: $!\n");
}
}
if(defined($DarwinOSVer) and defined($DarwinName)) {
return("$DarwinName $DarwinOSVer ($machine)");
}
}
# Some distros set a LSB DISTRIB_ID but no version, try DISTRIB_ID
# along with the kernel info.
if ($LSB{DISTRIB_ID})
{
return($LSB{DISTRIB_ID}."/Unknown ($sysname $release $version $machine)");
}
return("Unknown ($sysname $release $version $machine)");
}
# Purpose: Load a configuration file
# Usage: LoadConfigFile(/FILE, \%ConfigHash, \%OptionRegexHash, OnlyValidOptions?);
# OptionRegeXhash can be available for only a select few of the config options
# or skipped completely (by replacing it by undef).
# If OnlyValidOptions is true it will cause LoadConfigFile to skip options not in
# the OptionRegexHash.
sub LoadConfigFile {
my ($File, $ConfigHash, $OptionRegex, $OnlyValidOptions) = @_;
open(my $CONFIG, '<', $File) or do {
DPIntWarn(sprintf('Unable to read the configuration settings from %s: %s', $File, $!));
return(0);
};
while(<$CONFIG>) {
next if m/^\s*(#.*)?$/;
next if not m/=/;
chomp;
my $Option = $_;
my $Value = $_;
$Option =~ s/^\s*(\w+)\s*=.*/$1/;
$Value =~ s/^\w+=\s*(.*)\s*/$1/;
if($OnlyValidOptions) {
if(not defined($OptionRegex->{$Option})) {
DPIntWarn("Unknown configuration option \"$Option\" (=$Value) in $File: Ignored.");
next;
}
}
if(not defined($Value)) {
DPIntWarn("Empty value for option $Option in $File");
}
if(defined($OptionRegex) and defined($OptionRegex->{$Option})) {
my $MustMatch = $OptionRegex->{$Option};
if (not $Value =~ /$MustMatch/) {
DPIntWarn("Invalid setting of $Option (=$Value) in the config file: Must match $OptionRegex->{Option}.");
next;
}
}
$ConfigHash->{$Option} = $Value;
}
close($CONFIG) or warn("Failed to close filehandle for $File: $!\n");;
return true;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Initialization functions
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Purpose: Get the MagicPO::DictLoader object for the dictionary file supplied
# Usage: get_dictloader(Dictionary);
sub get_dictloader
{
my $file = shift;
if ($Verbose)
{
my $VerboseDictName = $file;
my $basedir = Cwd::realpath($0);
$basedir = dirname($basedir);
$VerboseDictName =~ s#^$basedir(/dictionaries/?)?##g;
$VerboseDictName =~ s#^$ENV{HOME}#~#g;
vprint("Reading dictionary $VerboseDictName...");
}
my $dict = MagicPO::DictLoader->new($Dictionary,\&progressed);
if ($dict)
{
vprint("done (read $dict->{phrases} phrases)\n");
return $dict;
}
else
{
die("Fatal error while reading dictionary, giving up.\n");
}
}
# Purpose: Get the MagicPO::Magic object for the DictLoader supplied
# Usage: get_magic(DictLoader object, replaceFuzzy?);
# replaceFuzzy is a boolean that can be undef. If true then fuzzy strings
# will be replaced by MagicPO processed strings.
sub get_magic
{
my $loader = shift;
my $replaceFuzzy = shift;
my $object = shift;
# If it's a string and not ' ', look it up
if ($object and not $ChangeTest eq ' ' and $ChangeTest =~ /\D/)
{
$ChangeTest = $object->{Lookup}{$ChangeTest};
if(not defined $ChangeTest)
{
warn("Failed to locate --whatchanged string\n");
}
}
my $magic = MagicPO::Magic->new($loader,\&progressed,$replaceFuzzy,$ChangeTest);
return $magic;
}
# Purpose: Replace a single string supplied on the commandline
# Usage: ReplaceSingleString(STRING);
sub ReplaceSingleString {
my $Replace = shift;
my $dict = get_dictloader($Dictionary);
my $magic = get_magic($dict,$ReplaceFuzzy);
my $str = $magic->replaceString($Replace);
print "$str\n";
return true;
}
# Purpose: Start replacing files
# Usage: StartReplacement();
sub StartReplacement {
my $dict;
if(not $ReformatPOFileOnly) {
$dict = get_dictloader($Dictionary);
}
if ($CurrentFile or $NewerFile)
{
if(not $CurrentFile)
{
die("--newer is required along with --current\n");
}
elsif(not $NewerFile)
{
die("--current is required along with --newer\n");
}
if (@ARGV)
{
die("You can not supply additional files when using --newer and --current\n");
}
push(@ARGV,$NewerFile);
}
if ($OutFile)
{
if (@ARGV > 1)
{
die("You can only MagicPO one file at a time when using --target\n");
}
}
die "I need to know which file to run on\n" if not @ARGV;
foreach my $filename (@ARGV) { # For each file to be processed
if(not -e $filename)
{
warn("$filename: does not exist. Skipped.\n");
next;
}
if(not -r $filename and -w $filename)
{
warn("$filename: permissions are wrong. Needs to be at least 600. Skipped.\n");
next;
}
if (-d $filename)
{
warn("$filename: is a directory. Skipped.\n");
next;
}
if(not -f $filename)
{
warn("$filename: is not a regular file. Skipped.\n");
next;
}
vprint($filename.":\n");
vprint(' Parsing...');
my $object = MagicPO::Parser->new($filename,\&progressed,$NoLastTrans);
vprint("done\n");
my $older;
if ($CurrentFile)
{
$older = MagicPO::Parser->new($CurrentFile,\&progressed,$NoLastTrans);
}
vprint ' Performing magic...';
# If markfuzzy mode is on and we're not going to merge two files, set markdirtymdoe to fuzzy.
# This causes MagicPO::Parser to mark the strings we change as fuzzy (which we don't want to do
# in merge mode).
if ($MarkFuzzy and not $CurrentFile)
{
$object->markdirtymode('fuzzy');
}
else
{
$object->markdirtymode('normal');
}
my $magic;
my $isDirty;
if($ReformatPOFileOnly)
{
$isDirty = true;
}
else
{
$magic = get_magic($dict,$ReplaceFuzzy,$object);
$isDirty = $magic->replaceFile($object,$older); # replace strings.
}
if ($CurrentFile)
{
# If markfuzzy mode is on, then ensure we mark changed/new strings as fuzzy
if ($MarkFuzzy)
{
$older->markdirtymode('fuzzy');
}
$older->mergeobject($object,$ReplaceFuzzy);
$object = $older;
$filename = $CurrentFile;
}
if ($OutFile)
{
$filename = $OutFile;
}
vprint "done\n";
# Only write the file if it is dirty and we're not in any special mode.
if((not $CurrentFile) and (not $OutFile) and (not $isDirty) and (not $ReformatPOFileOnly))
{
vprint(" No changes made\n");
}
else
{
vprint(' Writing file to ');
if ($OutFile)
{
vprint($OutFile);
}
else
{
vprint($filename);
}
vprint('...');
if ($ReformatPOFileOnly)
{
# Mark it all as dirty
foreach(keys(%{$object->{PoFile}}))
{
progressed();
$object->markdirty($_);
}
}
$object->write($filename);
vprint("done\n");
}
}
return true;
}
# Purpose: Detect the dictionary file we want to use (sets $Dictionary or dies)
# Usage: DetectDictionary();
# FIXME: Global $Dictionary is ugly
sub DetectDictionary {
# Get the directory containing magicpo
my $basedir = Cwd::realpath($0);
$basedir = dirname($basedir);
# An array of dictionaries.
my @Dicts;
# If $BaseDictionary is set, use that dictionary.
if ($BaseDictionary) {
@Dicts = ($BaseDictionary, $ENV{HOME}.'/'.$BaseDictionary, $basedir.'/'.$BaseDictionary);
} else {
# If it isn't set, try all known ones.
warn("MagicPO warning: no --dict provided by user, using the first dictionary that can be found\n");
my @DictionaryFiles = qw(magicpo.dict magicpo-nn2nb.dict magicpo-da2nb.dict magicpo-nb2nn.dict magicpo-da2nn.dict);
foreach (@DictionaryFiles) {
push(@Dicts, $_);
push(@Dicts, $ENV{HOME}.'/'.$_);
push(@Dicts, $basedir.'/'.$_);
push(@Dicts, $basedir.'/dictionaries/'.$_);
}
}
# Detect the dictionary
foreach (@Dicts) {
if ( -r $_ ) {
$Dictionary = $_;
last;
}
}
# Die if the dictionary couldn't be found.
if(not $Dictionary)
{
if(not $BaseDictionary) {
die "\nUnable to detect any dictionary! Use --dict to select one.\n";
} else {
die "\nUnable to find the dictionary file \"$BaseDictionary\"\n";
}
}
return $Dictionary;
}
# Purpose: Main function in MODE_DICT
# Usage: main_dict();
sub main_dict
{
if (not $ReformatPOFileOnly) {
# Detect the location of the dictionary
DetectDictionary();
}
vprint "\n";
# Okay, if we got this far then start replacing stuff :)
if($ReplaceString) {
ReplaceSingleString($ReplaceString);
} else {
StartReplacement();
}
return true;
}
# Purpose: Main function in MODE_DB and MODE_MKDB
# Usage: main_transdb();
sub main_transdb
{
vprint 'Reading database...';
my $transdb = MagicPO::TransDB->new($SourceDBFile,\&progressed,$NoLastTrans);
vprint "done\n";
foreach(@ARGV)
{
if(not -e $_)
{
print "Ignoring file: '$_': $!\n";
next;
}
if ($AppMode == MODE_DB)
{
vprint "Translating $_...";
my $changed = $transdb->translate($_);
vprint "done - translated $changed string(s).\n";
}
elsif ($AppMode == MODE_MKDB)
{
vprint "Working on: $_\n";
$transdb->addpo($_);
}
else
{
die("In main_transdb() and AppMode is not MODE_DB nor MODE_MKDB ($AppMode)\n");
}
}
if ($AppMode == MODE_MKDB)
{
vprint "Writing database to $OutFile ...";
$transdb->writeDb($OutFile);
vprint "done\n";
}
return true;
}
# Purpose: Parse dictionary filename for --dict
# Usage: parseDictName(NAME);
sub parseDictName
{
my $name = shift;
# First create a list of the dictionaries
# Get the directory containing magicpo
my $basedir = Cwd::realpath($0);
$basedir = dirname($basedir);
# The list of shorthands
my %FileHash;
# Fallback list of files
my %FallbackFiles;
# An array of dictionaries.
my @Dicts = ($ENV{HOME},$basedir.'/dictionaries',$basedir,getcwd());
foreach my $dir(@Dicts)
{
foreach my $file (glob($dir.'/magicpo-*.dict'))
{
next if not -r $file;
# Now try to parse the name.
my $fname = $file;
$fname =~ s/.*magicpo-//i;
$fname =~ s/\.dict$//i;
$fname =~ s/-//g;
$fname =~ s/spraakvask/fix/gi;
$fname =~ s/vask/fix/gi;
$fname =~ s/(.)to(.)/${1}2${2}/;
if(not $FileHash{$fname})
{
$FileHash{$fname} = $file;
}
# Now add the full path to the fallback
if(not $FallbackFiles{$file})
{
# Yeah, setting it to the same value as the key
# is useless, but works.
$FallbackFiles{$file} = $file;
}
# Finally add the filename to the fallback
my $basefile = basename($file);
if(not $FallbackFiles{$basefile})
{
$FallbackFiles{$basefile} = $file;
}
}
}
if ($name eq 'list')
{
printf('%-10s %s'."\n",'Name:','File:');
foreach my $key(sort keys(%FileHash))
{
my $file = $FileHash{$key};
$file =~ s#^$basedir/dictionaries/?##;
$file =~ s#^$basedir/?##;
$file =~ s#^$ENV{HOME}/?#~#;
printf('%-10s %s'."\n",$key,$file);
}
exit(0);
}
if ($FileHash{$name})
{
return($FileHash{$name});
}
else
{
if ($FallbackFiles{$name})
{
return($FallbackFiles{$name});
}
die("Unknown file supplied to --dict, try --dict list\n\n");
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Commandline processing
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Display help and exit if we got no commandline options
Help() and exit 0 if not @ARGV;
# Parse commandline arguments
GetOptions (
'version' => sub { Version(); exit 0; },
'help|h' => sub { Help(); exit 0 },
'string=s' => \$ReplaceString,
'dictfile|f=s' => \$BaseDictionary,
'dict|d=s' => sub
{
$BaseDictionary = parseDictName($_[1]);
},
'verbose|v' => \$Verbose,
'silent|s' => sub {
$Verbose = false;
},
'r|removebr|reformat' => \$ReformatPOFileOnly,
'debug|D' => \$DebugMode,
'current=s' => \$CurrentFile,
'newer=s' => \$NewerFile,
't|target=s' => \$OutFile,
'n|nomarkfuzzy' => sub { $MarkFuzzy = false },
'nolasttrans' => \$NoLastTrans,
'replacefuzzy' => \$ReplaceFuzzy,
'whatchanged=s' => \$ChangeTest,
'debuginfo' => sub {
Version();
printf 'Perl version: %vd'."\n", $^V;
print 'OS: ', GetDistVer(), "\n";
eval('use Digest::MD5;');
my $md5 = Digest::MD5->new();
my $self = $0;
if(not -f $self)
{
$self = InPath($self);
}
open(my $f,'<',$self);
$md5->addfile($f);
my $digest = $md5->hexdigest;
close($f);
print 'MD5: '.$digest."\n";
exit(0);
},
# Dict mode arguments
'dbsource|dbfile=s' => sub {
shift;
$SourceDBFile = shift;
$AppMode = MODE_DB;
},
'mkdb=s' => sub {
shift;
$OutFile = shift;
$AppMode = MODE_MKDB;
},
) or die "See --help for more information\n";
# Check which app mode we are in and run the correct main function
if ($AppMode == MODE_DICT or $ReformatPOFileOnly)
{
main_dict();
}
elsif ($AppMode == MODE_DB || $AppMode == MODE_MKDB)
{
warn("MagicPO warning: Database mode is still somewat experimental, report any issues you might find.\n");
main_transdb();
}
else
{
die("Unknown AppMode: $AppMode");
}
__END__
=pod
=head1 NAME
MagicPO - automatically translate po-files
=head1 SYNOPSIS
B<magicpo> [I<OPTIONS>]
=head1 DESCRIPTION
B<MagicPO> is a utility that assists in automated translation of files in the
gettext PO format.
It can translate between similar languages (such as Norwegian Nynorsk and Norwegian Bokmaal,
or Norwegian Bokmaal and Danish) using a dictionary, or insert previous translations
from a database.
By default it uses a dictionary, is verbose and marks changed strings as fuzzy.
The latter can be supressed with the --silent and --nomarkfuzzy commandline arguments.
=head1 OPTIONS
=head2 GENERAL OPTIONS
=over
=item B<-h, --help>
Display the help screen
=item B<-s, --silent>
Be silent (don't output anything other than syntax errors)
=item B<--debuginfo>
Display various information useful for debugging.
=item B<-r, --reformat>
Don't perform any replacements or translations, just reformat the PO-file.
Overrides everything else.
=back
=head2 DICTIONARY OPTIONS
=over
=item B<-d, --dict> I<NAME>
Use the dictionary supplied. Try --dict list to view a list
of possible dictionaries. This searches through a preset list of directories
for dictionaries, and generates simple shorthand names for those.
The searchpath is as follows:
First it searches ~/, then the magicpo-dir/dictionaries, then magicpo-dir/
and finally ./
=item B<-f, --dictfile> I<FILE>
Use the dictionary I<FILE>
Using --dict is often easier, but this lets
you explicitly set the file to use.
=item B<-t, --target> I<FILE>
Write the translated po-file to I<FILE> instead of back to the original.
=item B<--newer>
Selects the newer file for merging. See MERGING.
=item B<--current>
Selects the current (older) file for merging. See MERGING.
=item B<--replacefuzzy>
Replace fuzzy strings in --current file with non-fuzzy strings from --newer
=item B<--nomarkfuzzy>
Don't mark changed strings as fuzzy.
=item B<--nolasttrans>
Don't change "Last-Translator" in PO files.
=item B<--whatchanged>
This lets you examine which regular expressions are changing a certain string. The parameter
is the string number in the file (the header is string number 1).
So if you want to see which dictionary entries keep messing up string number 5, supply
--whatchanged 5.
You can also use --whatchanged "String". The string must then be EXACTLY as it is in
the po-file. Also, this will not work when running on multiple files.
=back
=head2 DATABASE OPTIONS
When either of these options are used, all dictionary-related commandline options will be
ignored.
=over
=item B<--mkdb> I<FILE>
Create a translation database in I<FILE> for use with --dbfile.
This will create a single translation database comprised of all of the strings
in all of the PO files you supply on the commandline. If the database already
exists it will merely append to it. It ignores all fuzzy and untranslated strings
in source files.
For instance, one might create a translation database from a large svn project
where you know that all of the relevant po files will be named nn.po:
magicpo --mkdb nn-magicpo.db `find ~/svn -iname nn.po`
Or in a dir structure that already contains only po-files you want in the db:
magicpo --mkdb nn-magicpo.db `find ~/svn/nn -iname '*.po'`
=item B<--dbfile> I<FILE>
Translate all of the po-files supplied on the commandline using the translation
database in I<FILE>. These strings will not be marked as fuzzy at all, regardless
of settings.
=back
=head1 MERGING
MagicPO can merge one po-file with another. Say for example that you have dayplanner.po in
Norwegian Nynorsk for version 0.7 of Day Planner, but the Norwegian Bokmaal version is
currently only updated for version 0.6. MagicPO can translate the Nynorsk file and add the
new strings to the Bokmaal file, keeping all of the old Bokmaal strings intact.
To do this run MagicPO like this:
magicpo --current dayplanner_nb.po --newer dayplanner_nn.po
This will update dayplanner_nb.po to include the latest changes as per dayplanner_nn.po
=head1 SYNTAX ERRORS
MagicPO will display syntax error messages if it finds any. You are advised to check
that the output file does not have any removed data when these appear (MagicPO will
in most cases attempt to solve them).
If you find an invalid syntax error you should report this as a bug.
First verify manually in the ORIGINAL file that it is not a syntax error. Then
provide both the old and the new file, the output of magicpo --debuginfo and a
copy of all the error messages MagicPO displayed. See BUGS AND LIMITATIONS for information on
how to report.
=head1 EXIT STATUS
0 for success, any nonzero value for failure.
=head1 HISTORY
MagicPO was originally TransPO. A simple perl program written by Harald Thingelstad
to assist in automated translation. Eskild Hustvedt joined and became the primary
maintainer of TransPO around 2005. In 2006 Harald had decided he wanted to rewrite
TransPO in python, at this point Eskild forked it into MagicPO. Since then MagicPO
has seen a lot of improvements, such as proper support for PO-files with tags,
support for more advanced forms of PO-files and support for merging, and at the moment
contain very very little of the original TransPO code.
=head1 BUGS AND LIMITATIONS
=over
=item - Currently does not format po-files exactly the way msgmerge would.
=item - Doesn't handle more than three msgstr's per string.
=item - It is not possible to make it mark strings translated using the database
as fuzzy
=back
Probably others too. Send reports, with any relevant information and the output of magicpo --debuginfo
to zerodogg -a-t- zerodogg dot org
=head1 LICENSE AND COPYRIGHT
Copyright (C) Eskild Hustvedt 2005, 2006, 2007, 2008
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=head1 SEE ALSO
magicpo.dict(5) - Dictionary syntax