2013-05-01 14:41:25 +04:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# Copyright 2013 Vivek Dasmohapatra <vivek@collabora.co.uk>
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
2014-12-02 19:27:24 +03:00
|
|
|
#
|
2013-05-01 14:41:25 +04:00
|
|
|
# * The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
2014-12-02 19:27:24 +03:00
|
|
|
#
|
2013-05-01 14:41:25 +04:00
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
|
|
|
|
=head1
|
|
|
|
|
|
|
|
Filter the NetSurf combined messages (i10n) file according to language
|
|
|
|
and platform and generate output in a selection of formats for use
|
|
|
|
both internally within netsurf and externally for translation
|
|
|
|
services.
|
|
|
|
|
|
|
|
=cut
|
2012-03-25 14:26:02 +04:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
2013-05-01 14:41:25 +04:00
|
|
|
use Getopt::Long ();
|
|
|
|
use Fcntl qw( O_CREAT O_EXCL O_WRONLY O_APPEND O_RDONLY O_WRONLY );
|
|
|
|
|
2018-04-22 12:10:53 +03:00
|
|
|
use IO::Compress::Gzip;
|
|
|
|
|
2013-05-01 14:41:25 +04:00
|
|
|
use constant GETOPT_OPTS => qw( auto_abbrev no_getopt_compat bundling );
|
|
|
|
use constant GETOPT_SPEC =>
|
|
|
|
qw( output|o=s
|
|
|
|
input|i=s
|
|
|
|
lang|l=s
|
2014-12-02 19:27:24 +03:00
|
|
|
dlang|d=s
|
2013-05-01 14:41:25 +04:00
|
|
|
plat|platform|p=s
|
|
|
|
format|fmt|f=s
|
2014-12-02 19:27:24 +03:00
|
|
|
warning|W=s
|
2018-04-22 12:10:53 +03:00
|
|
|
gzip|z
|
2013-05-01 14:41:25 +04:00
|
|
|
help|h|? );
|
|
|
|
|
|
|
|
# default option values:
|
2014-12-02 19:27:24 +03:00
|
|
|
my %opt = qw( dlang en plat any format messages warning none );
|
2013-05-01 14:41:25 +04:00
|
|
|
|
|
|
|
sub input_stream ();
|
|
|
|
sub output_stream ();
|
|
|
|
sub formatter ();
|
2013-05-03 02:06:15 +04:00
|
|
|
sub static_section($);
|
2013-05-01 14:41:25 +04:00
|
|
|
sub usage ();
|
|
|
|
|
|
|
|
sub main ()
|
|
|
|
{
|
|
|
|
my $input;
|
|
|
|
my $output;
|
|
|
|
my $format;
|
|
|
|
my $header;
|
2013-05-03 02:06:15 +04:00
|
|
|
my $footer;
|
2013-05-01 14:41:25 +04:00
|
|
|
my $opt_ok;
|
|
|
|
|
|
|
|
# option parsing:
|
|
|
|
Getopt::Long::Configure( GETOPT_OPTS );
|
|
|
|
$opt_ok = Getopt::Long::GetOptions( \%opt, GETOPT_SPEC );
|
|
|
|
|
|
|
|
# allow input and output to be specified as non-option arguments:
|
|
|
|
if( @ARGV ) { $opt{input } ||= shift( @ARGV ) }
|
|
|
|
if( @ARGV ) { $opt{output} ||= shift( @ARGV ) }
|
|
|
|
|
|
|
|
# open the appropriate streams and get the formatter and headers:
|
|
|
|
if( $opt_ok )
|
|
|
|
{
|
|
|
|
$input = input_stream();
|
|
|
|
$output = output_stream();
|
|
|
|
$format = formatter();
|
2013-05-03 02:06:15 +04:00
|
|
|
$header = static_section('header');
|
|
|
|
$footer = static_section('footer');
|
2013-05-01 14:41:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# double check the options are sane (and we weren't asked for the help)
|
2014-12-02 19:27:24 +03:00
|
|
|
if( !$opt_ok || $opt{help} || $opt{lang} !~ /^[a-z]{2}$/ || $opt{dlang} !~ /^[a-z]{2}$/ )
|
2013-05-01 14:41:25 +04:00
|
|
|
{
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
# we are good to go:
|
|
|
|
print( $output $header );
|
|
|
|
|
2014-12-02 19:27:24 +03:00
|
|
|
my $cur_key;
|
|
|
|
|
|
|
|
my $dlang_key;
|
|
|
|
my $dlang_val;
|
|
|
|
|
|
|
|
my $tran_out = 1;
|
|
|
|
my $tran_val;
|
|
|
|
my $tran_key;
|
|
|
|
|
2013-05-01 14:41:25 +04:00
|
|
|
while (<$input>)
|
|
|
|
{
|
2014-12-02 19:27:24 +03:00
|
|
|
# skip comment and empty lines
|
2013-05-01 14:41:25 +04:00
|
|
|
/^#/ && next;
|
|
|
|
/^\s*$/ && next;
|
2014-12-02 19:27:24 +03:00
|
|
|
|
|
|
|
# only parsing things that look like message lines:
|
2013-05-01 14:41:25 +04:00
|
|
|
if( /^([a-z]{2}).([^.]+).([^:]+):(.*)/ )
|
|
|
|
{
|
|
|
|
my( $lang, $plat, $key, $val ) = ( $1, $2, $3, $4 );
|
|
|
|
|
2014-12-02 19:27:24 +03:00
|
|
|
# skip the line if it is not for our target platform
|
|
|
|
if( $opt{plat} ne 'any' &&
|
|
|
|
$opt{plat} ne $plat &&
|
|
|
|
'all' ne $plat )
|
2013-05-01 14:41:25 +04:00
|
|
|
{
|
2014-12-02 19:27:24 +03:00
|
|
|
next;
|
2013-05-01 14:41:25 +04:00
|
|
|
}
|
2014-12-02 19:27:24 +03:00
|
|
|
|
|
|
|
# On key change ensure a translation has been generated
|
|
|
|
if ($cur_key ne $key)
|
|
|
|
{
|
|
|
|
if ($tran_out == 0)
|
|
|
|
{
|
|
|
|
# No translaton for previous key
|
|
|
|
if ($cur_key eq $dlang_key)
|
|
|
|
{
|
|
|
|
print( $output $format->( $dlang_key, $dlang_val ) );
|
|
|
|
if( $opt{warning} eq "fb" )
|
|
|
|
{
|
|
|
|
warn( "warning: $dlang_key missing translation in $opt{lang} using $opt{dlang} instead" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# No translation and nothing in default language
|
|
|
|
warn( "warning: $dlang_key missing translation in $opt{lang} and no fallback in $opt{dlang}" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (($opt{dlang} ne $opt{lang} ) && ($tran_key eq $dlang_key) && ($tran_val eq $dlang_val))
|
|
|
|
{
|
|
|
|
if( $opt{warning} eq "dup" )
|
|
|
|
{
|
|
|
|
warn( "warning: $tran_key value in $opt{lang} is same as in default $opt{dlang}" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$cur_key = $key;
|
|
|
|
$tran_out = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# capture the key/value in the default language
|
|
|
|
if( $lang eq $opt{dlang} )
|
|
|
|
{
|
|
|
|
$dlang_key = $key;
|
|
|
|
$dlang_val = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
# output if its the target language
|
|
|
|
if( $lang eq $opt{lang} ) {
|
|
|
|
print( $output $format->( $key, $val ) );
|
|
|
|
$tran_out = 1;
|
|
|
|
$tran_val = $val;
|
|
|
|
$tran_key = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
warn( "Malformed entry: $_" );
|
|
|
|
}
|
2013-05-01 14:41:25 +04:00
|
|
|
}
|
2013-05-03 02:06:15 +04:00
|
|
|
|
|
|
|
print( $output $footer );
|
2013-05-01 14:41:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
main();
|
|
|
|
|
|
|
|
sub usage ()
|
|
|
|
{
|
|
|
|
my @fmt = map { s/::$//; $_ } keys(%{$::{'msgfmt::'}});
|
|
|
|
print(STDERR <<TXT );
|
|
|
|
usage:
|
2014-12-02 19:27:24 +03:00
|
|
|
$0 -l lang-code [-d def-lang-code] [-W warning] \
|
2020-06-15 02:04:44 +03:00
|
|
|
[-o output-file] [-i input-file] [-p platform] [-f format] [-z]
|
2013-05-01 14:41:25 +04:00
|
|
|
|
|
|
|
$0 -l lang-code ... [input-file [output-file]]
|
|
|
|
|
2014-12-02 19:27:24 +03:00
|
|
|
lang-code : en fr ko ... (no default)
|
|
|
|
def-lang-code : en fr ko ... (default 'en')
|
|
|
|
warning : none, all (default 'none')
|
|
|
|
platform : any gtk ami (default 'any')
|
|
|
|
format : @fmt (default 'messages')
|
|
|
|
input-file : defaults to standard input
|
|
|
|
output-file : defaults to standard output
|
2013-05-01 14:41:25 +04:00
|
|
|
TXT
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub input_stream ()
|
|
|
|
{
|
|
|
|
if( $opt{input} )
|
|
|
|
{
|
2014-12-02 19:27:24 +03:00
|
|
|
my $ifh;
|
2013-05-01 14:41:25 +04:00
|
|
|
|
2014-12-02 19:27:24 +03:00
|
|
|
sysopen( $ifh, $opt{input}, O_RDONLY ) ||
|
|
|
|
die( "$0: Failed to open input file $opt{input}: $!\n" );
|
2013-05-01 14:41:25 +04:00
|
|
|
|
2014-12-02 19:27:24 +03:00
|
|
|
return $ifh;
|
2013-05-01 14:41:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return \*STDIN;
|
|
|
|
}
|
|
|
|
|
2018-04-22 12:10:53 +03:00
|
|
|
sub underlying_output_stream ()
|
2013-05-01 14:41:25 +04:00
|
|
|
{
|
|
|
|
if( $opt{output} )
|
|
|
|
{
|
2014-12-02 19:27:24 +03:00
|
|
|
my $ofh;
|
2013-05-01 14:41:25 +04:00
|
|
|
|
2014-12-02 19:27:24 +03:00
|
|
|
sysopen( $ofh, $opt{output}, O_CREAT|O_EXCL|O_APPEND|O_WRONLY ) ||
|
|
|
|
die( "$0: Failed to open output file $opt{output}: $!\n" );
|
2013-05-01 14:41:25 +04:00
|
|
|
|
2014-12-02 19:27:24 +03:00
|
|
|
return $ofh;
|
2013-05-01 14:41:25 +04:00
|
|
|
}
|
2012-03-25 14:26:02 +04:00
|
|
|
|
2013-05-01 14:41:25 +04:00
|
|
|
return \*STDOUT;
|
|
|
|
}
|
|
|
|
|
2018-04-22 12:10:53 +03:00
|
|
|
sub output_stream ()
|
|
|
|
{
|
|
|
|
my $ofh = underlying_output_stream();
|
|
|
|
|
|
|
|
if( $opt{gzip} )
|
|
|
|
{
|
|
|
|
$ofh = new IO::Compress::Gzip( $ofh, AutoClose => 1, -Level => 9 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ofh;
|
|
|
|
}
|
|
|
|
|
2013-05-01 14:41:25 +04:00
|
|
|
sub formatter ()
|
|
|
|
{
|
|
|
|
my $name = $opt{format};
|
|
|
|
my $func = "msgfmt::$name"->UNIVERSAL::can("format");
|
|
|
|
|
|
|
|
return $func || die( "No handler found for format '$name'\n" );
|
|
|
|
}
|
|
|
|
|
2013-05-03 02:06:15 +04:00
|
|
|
sub static_section ($)
|
2013-05-01 14:41:25 +04:00
|
|
|
{
|
|
|
|
my $name = $opt{format};
|
2013-05-03 02:06:15 +04:00
|
|
|
my $sect = shift();
|
|
|
|
my $func = "msgfmt::$name"->UNIVERSAL::can( $sect );
|
2012-03-25 14:26:02 +04:00
|
|
|
|
2013-05-01 14:41:25 +04:00
|
|
|
return $func ? $func->() : "";
|
|
|
|
}
|
2012-03-25 14:26:02 +04:00
|
|
|
|
2013-05-01 14:41:25 +04:00
|
|
|
# format implementations:
|
|
|
|
{
|
|
|
|
package msgfmt::java;
|
2012-03-25 14:26:02 +04:00
|
|
|
|
2013-05-03 02:06:15 +04:00
|
|
|
sub escape { $_[0] =~ s/([:'\\])/\\$1/g; $_[0] }
|
|
|
|
sub format { return join(' = ', $_[0], escape( $_[1] ) ) . "\n" }
|
2013-05-01 14:41:25 +04:00
|
|
|
sub header { "# autogenerated from " . ($opt{input} || '-stdin-') . "\n" }
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-05-03 02:06:15 +04:00
|
|
|
package msgfmt::messages; # native netsurf format
|
2013-05-01 14:41:25 +04:00
|
|
|
|
2013-05-03 02:06:15 +04:00
|
|
|
sub format { return join( ":", @_ ) . "\n" }
|
2013-05-01 14:41:25 +04:00
|
|
|
sub header
|
|
|
|
{
|
2014-12-02 19:27:24 +03:00
|
|
|
my $in = $opt{input} || '-stdin-';
|
|
|
|
return <<TXT;
|
2013-05-01 14:41:25 +04:00
|
|
|
# This messages file is automatically generated from $in
|
|
|
|
# at build-time. Please go and edit that instead of this.\n
|
|
|
|
TXT
|
2012-03-25 14:26:02 +04:00
|
|
|
}
|
|
|
|
}
|
2013-05-01 14:41:25 +04:00
|
|
|
|
2013-05-03 02:06:15 +04:00
|
|
|
{
|
|
|
|
package msgfmt::transifex;
|
|
|
|
use base 'msgfmt::java';
|
|
|
|
|
|
|
|
# transifex has the following quirks:
|
|
|
|
# \ processing is buggy - they re-process every \\ as a \
|
|
|
|
# so \\n, instead or producing literal '\n', is interpreted as \ ^J
|
|
|
|
# Additionally, although the java properties format specifies
|
|
|
|
# that ' should be \ escaped, transifex does not allow/support this:
|
|
|
|
sub escape { $_[0] =~ s/(:|\\(?![abfnrtv]))/\\$1/g; $_[0] }
|
|
|
|
sub format { return join(' = ', $_[0], escape( $_[1] ) ) . "\n" }
|
|
|
|
}
|
|
|
|
|
2013-05-01 14:41:25 +04:00
|
|
|
########### YAML ###########
|
|
|
|
#{
|
|
|
|
# package msgfmt::yaml;
|
|
|
|
# use YAML qw(Dump Bless);
|
|
|
|
# print Dump %data;
|
|
|
|
#}
|
|
|
|
|
2013-05-03 02:06:15 +04:00
|
|
|
{
|
|
|
|
package msgfmt::android;
|
|
|
|
|
|
|
|
sub header { qq|<?xml version="1.0" encoding="utf-8"?>\n<resources>\n| }
|
|
|
|
sub footer { qq|</resources>| }
|
|
|
|
sub format
|
|
|
|
{
|
2014-12-02 19:27:24 +03:00
|
|
|
use HTML::Entities qw(encode_entities);
|
|
|
|
my $escaped = encode_entities( $_[1], '<>&"' );
|
|
|
|
qq| <string name="$_[0]">$escaped</string>\n|;
|
2013-05-03 02:06:15 +04:00
|
|
|
}
|
|
|
|
}
|