| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# PODNAME: tsv2tmx |
| 4 |
# ABSTRACT: Create a TMX from a TSV file |
| 5 |
|
| 6 |
use warnings; |
| 7 |
use strict; |
| 8 |
use Getopt::Long; |
| 9 |
use Pod::Usage; |
| 10 |
use utf8::all; |
| 11 |
|
| 12 |
### Option variables |
| 13 |
my $man = 0; |
| 14 |
my $help = 0; |
| 15 |
my ($sl, $tl); |
| 16 |
our $verbose = 1; |
| 17 |
my $header = 1; |
| 18 |
my $columns = "0,1"; |
| 19 |
my $fs = qr'\t'; |
| 20 |
#### ------ |
| 21 |
|
| 22 |
GetOptions ('help|h' => \$help, 'man' => \$man, |
| 23 |
"source|sl=s" => \$sl, |
| 24 |
"target|tl=s" => \$tl, |
| 25 |
"header" => \$header, |
| 26 |
"columns|c=s" => \$columns, |
| 27 |
"fs=s" => \$fs, |
| 28 |
"verbose|v" => \$verbose) |
| 29 |
or pod2usage(2); |
| 30 |
|
| 31 |
pod2usage(1) if $help; |
| 32 |
pod2usage(-exitval => 0, -verbose => 2) if $man; |
| 33 |
|
| 34 |
## |
| 35 |
sub strip { my $v=shift; |
| 36 |
$v =~ s/^\s*//; |
| 37 |
$v =~ s/\s*$//; |
| 38 |
$v; |
| 39 |
} |
| 40 |
|
| 41 |
our ($c1, $c2); |
| 42 |
if ($columns =~ /^(\d+),(\d+)$/) { |
| 43 |
($c1,$c2) = ($1,$2); |
| 44 |
} else { |
| 45 |
die "Columns definition should be a pair of integers. Ex: 1,2\n"; |
| 46 |
} |
| 47 |
|
| 48 |
if (!$sl || !$tl) { |
| 49 |
if ($header) { |
| 50 |
my ($l1, $l2) = readLine("find langs in header"); |
| 51 |
$sl = strip($l1) unless defined $sl; |
| 52 |
$tl = strip($l2) unless defined $tl; |
| 53 |
} else { |
| 54 |
die "No header, and one of the source or target languages not defined!\n"; |
| 55 |
} |
| 56 |
$header = 0; |
| 57 |
} |
| 58 |
|
| 59 |
readLine() if $header; |
| 60 |
|
| 61 |
use XML::TMX::Writer; |
| 62 |
my $tmx = XML::TMX::Writer->new(); |
| 63 |
$tmx->start_tmx(id => 'tsv2tmx'); |
| 64 |
|
| 65 |
my @r; |
| 66 |
while (@r = readLine()) { |
| 67 |
$tmx->add_tu($sl=>$r[0],$tl=>$r[1]); |
| 68 |
} |
| 69 |
|
| 70 |
$tmx->end_tmx(); |
| 71 |
|
| 72 |
sub _log { |
| 73 |
say STDERR @_ if $verbose; |
| 74 |
} |
| 75 |
|
| 76 |
sub readLine { |
| 77 |
my $isheader = shift || 0; |
| 78 |
my $line = <>; |
| 79 |
return () unless $line; |
| 80 |
|
| 81 |
chomp $line; |
| 82 |
$line =~ s{&((\w+;)?)}{ if ($1){ "&$1" } else { "&"} }ge; |
| 83 |
if ($line =~ /^\s*$/ ){ return readLine() } |
| 84 |
elsif ($line =~ /^#/ ){ return readLine() } |
| 85 |
else { |
| 86 |
my @fields = split /$fs/, $line; |
| 87 |
if ($isheader and scalar @fields <2 ){ |
| 88 |
die("Invalid header:$line\n") } |
| 89 |
my $sl = strip($fields[$c1] or ""); |
| 90 |
my $tl = strip($fields[$c2] or ""); |
| 91 |
|
| 92 |
if ($isheader){ |
| 93 |
if( $sl and $tl){ _log ("From header:$sl - $tl");} |
| 94 |
else { die("Invalid header: $line\n") } |
| 95 |
} |
| 96 |
if ($sl or $tl){ return ($sl,$tl) } |
| 97 |
else { return readLine() } |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
__END__ |
| 102 |
|
| 103 |
=pod |
| 104 |
|
| 105 |
=encoding utf-8 |
| 106 |
|
| 107 |
=head1 NAME |
| 108 |
|
| 109 |
tsv2tmx - Create a TMX from a TSV file |
| 110 |
|
| 111 |
=head1 VERSION |
| 112 |
|
| 113 |
version 0.39 |
| 114 |
|
| 115 |
=head1 SYNOPSIS |
| 116 |
|
| 117 |
tsv2tmx [options] file.tsv ... |
| 118 |
|
| 119 |
Options: |
| 120 |
--help brief help message |
| 121 |
--man full documentation |
| 122 |
--verbose | -v activated verbose mode |
| 123 |
--sl=EN --tl=PT describe source and target language names |
| 124 |
--header treat first line as a heading |
| 125 |
--columns=1,2 specify which columns to extract |
| 126 |
--fs='::' set field separator. (separated by '::') |
| 127 |
|
| 128 |
=head1 DESCRIPTION |
| 129 |
|
| 130 |
Useful to create translation memories from TSV files, that can be |
| 131 |
easily exported from spreadsheets or other applications. |
| 132 |
|
| 133 |
=head1 OPTIONS |
| 134 |
|
| 135 |
=over 8 |
| 136 |
|
| 137 |
=item B<--help> |
| 138 |
|
| 139 |
Print a brief help message and exits. |
| 140 |
|
| 141 |
=item B<--man> |
| 142 |
|
| 143 |
Prints the manual page and exits. |
| 144 |
|
| 145 |
=item B<--verbose> | B<-v> |
| 146 |
|
| 147 |
Activates the verbose mode. |
| 148 |
|
| 149 |
=item B<--sl> | B<--tl> |
| 150 |
|
| 151 |
Use these options to specify the names for the source and target |
| 152 |
languages. |
| 153 |
|
| 154 |
=item B<--header> |
| 155 |
|
| 156 |
By default this switch is on, and it means that the TSV file |
| 157 |
includes a first line with a heading. If no source or target |
| 158 |
language names are specified, the first line will be used to |
| 159 |
guess them. |
| 160 |
|
| 161 |
=item B<--columns=1,2> |
| 162 |
|
| 163 |
Specify which columns should be extracted. Needs to be a pair |
| 164 |
of integers, separated by a comma. Columns indexes start at 0. |
| 165 |
Default to C<0,1>. |
| 166 |
|
| 167 |
=back |
| 168 |
|
| 169 |
=head1 SEE ALSO |
| 170 |
|
| 171 |
XML::TMX |
| 172 |
|
| 173 |
=head1 AUTHORS |
| 174 |
|
| 175 |
=over 4 |
| 176 |
|
| 177 |
=item * |
| 178 |
|
| 179 |
Alberto Simões <[email protected]> |
| 180 |
|
| 181 |
=item * |
| 182 |
|
| 183 |
José João Almeida <[email protected]> |
| 184 |
|
| 185 |
=back |
| 186 |
|
| 187 |
=head1 COPYRIGHT AND LICENSE |
| 188 |
|
| 189 |
This software is copyright (c) 2010-2017 by Projeto Natura <[email protected]>. |
| 190 |
|
| 191 |
This is free software; you can redistribute it and/or modify it under |
| 192 |
the same terms as the Perl 5 programming language system itself. |
| 193 |
|
| 194 |
=cut |