~ubuntu-branches/ubuntu/oneiric/kdesdk/oneiric-updates

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
#! /usr/bin/env perl

# Copyright (c) 2010 Volker Krause <vkrause@kde.org>
# based on extractrc by:
# Copyright (c) 2004 Richard Evans <rich@ridas.com>

sub usage
{
  warn <<"EOF";

extractqml [flags] filenames

This script extracts messages from QML files writes on standard output
(usually redirected to rc.cpp) the equivalent i18n() calls so that
xgettext can parse them.

--ignore-no-input : Do not warn if there were no filenames specified
--help|?          : Display this summary

EOF

  exit;
}

###########################################################################################

use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;

###########################################################################################
# Add options here as necessary - perldoc Getopt::Long for details on GetOptions

GetOptions ( "ignore-no-input" => \my $opt_ignore_no_input,
             "help|?"      => \&usage );

unless( @ARGV )
{
  warn "No filename specified" unless $opt_ignore_no_input;
  exit;
}

###########################################################################################

# We check for lines like:
#
#        vvvv
#    KDE.i18n( "single message", 2, test, "another string" )
#              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# match the first expression to know the function (i18n, i18nc, i18np, i18ncp)
# match the second expression to know the arguments for that function
#
# NOTE: this is a "hackish" solution that works if the function calls we
# want to # match are regular enough.  If more complicated patterns start to
# happen maybe we need to find another solution.

my $double_string = qr/ (?: \" (?: \\ \" | .)* \") /x;
my $single_string = qr/ (?: ' (?: \\ ' | .)* ') /x;
my $string = qr/ (?: $double_string | $single_string) /x;
my $simple_expression = qr/ [^,\)]* [^,\s\)] /x;
my $arguments = qr/ $string (?: \s* , \s* (?: $string | $simple_expression ) )* /x;

my $match_i18n = qr/ KDE \. ( i18nc?p? ) \s* \( \s* ( $arguments ) \s* \) /x;


for my $file_name ( @ARGV )
{
  my $fh;

  unless ( open $fh, "<", $file_name )
  {
    # warn "Failed to open: '$file_name': $!";
    next;
  }

  # Trick to read the entire file contents
  my $contents = do { local $/; <$fh> };

  while ( $contents =~ m/$match_i18n/gx ) {
    print "$1($2);\n";
  }

  close $fh or warn "Failed to close: '$file_name': $!";
}