~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/I18N/Extract.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# See bottom of file for license and copyright information
 
2
 
 
3
=begin TML
 
4
 
 
5
---+ package Foswiki::I18N::Extract
 
6
 
 
7
Support translatable strings extraction from Foswiki topics and templates.
 
8
Depends on Locale::Maketext::Extract (part of CPAN::Locale::Maketext::Lexicon).
 
9
 
 
10
=cut
 
11
 
 
12
package Foswiki::I18N::Extract;
 
13
 
 
14
use strict;
 
15
 
 
16
use vars qw( $initialised $initError );
 
17
 
 
18
BEGIN {
 
19
    eval "use base 'Locale::Maketext::Extract'";
 
20
    $initError   = $@;
 
21
    $initialised = !$initError;
 
22
}
 
23
 
 
24
##########################################################
 
25
 
 
26
=begin TML
 
27
 
 
28
---++ ClassMethod new ( $session ) -> $extract
 
29
 
 
30
Constructor. Creates a fresh new Extract object. A $session object, instance of
 
31
the Foswiki class, is optional: if it's available, it'll be used for printing
 
32
warnings.
 
33
 
 
34
=cut
 
35
 
 
36
sub new {
 
37
    my $class   = shift;
 
38
    my $session = shift;
 
39
 
 
40
    unless ($initialised) {
 
41
        $session->logger->log('warning', $initError) if $session;
 
42
        return undef;
 
43
    }
 
44
 
 
45
    my $self = new Locale::Maketext::Extract;
 
46
    $self->{session} = $session;
 
47
    return bless( $self, $class );
 
48
}
 
49
 
 
50
=begin TML
 
51
 
 
52
---++ ObjectMethod extract ( $file , $text )
 
53
 
 
54
Extract the strings from =$text=,m using =$file= as the name of the current
 
55
file being read (for comments in PO file, for example). Overrides the base
 
56
class method but calls it so the base behavior is preserved.
 
57
 
 
58
As in base class, extracted strings are just stored in the =$self='s internal
 
59
table for further use (e.g. creating/updating a PO file). Nothing is returned.
 
60
 
 
61
=cut
 
62
 
 
63
sub extract {
 
64
    my $self = shift;
 
65
    my $file = shift;
 
66
    local $_ = shift;
 
67
 
 
68
    # do existing extraction
 
69
    $self->SUPER::extract( $file, $_ );
 
70
 
 
71
    my $line;
 
72
    my $doublequoted = '"(\\\"|[^"])*"';
 
73
 
 
74
    # Foswiki's %MAKETEXT{...}% into topics and templates :
 
75
    $line = 1;
 
76
    pos($_) = 0;
 
77
    my @_lines = split( /\n/, $_ );
 
78
    foreach (@_lines) {
 
79
        while (m/%MAKETEXT\{\s*(string=)?($doublequoted)/gm) {
 
80
            my $str = substr( $2, 1, -1 );
 
81
            $str =~ s/\\"/"/g;
 
82
            $self->add_entry( $str, [ $file, $line, '' ] );
 
83
        }
 
84
        $line++;
 
85
    }
 
86
 
 
87
 # Foswiki's %MAKETEXT{...}% inside a search format would look like this:
 
88
 # %SEARCH{... format=" ... $percntMAKETEXT{\"...\" args=\"\"}$percnt ..." ...}%
 
89
 #
 
90
 # XXX: the regex down there matches a sequence formed be an escaped double
 
91
 # quote (\"), followed by characters that are not doublequotes OR
 
92
 # double-escaped doublequotes (\\\"), and terminated with another escaped
 
93
 # double-quote.
 
94
 #
 
95
 # SMELL: although here we can extract properly the string, %SEARCH{...}%
 
96
 # won't convert (\\\") inside format into (") as we do here. So it's best
 
97
 # to avoid trying to put doublequotes inside a MAKETEXT that is inside
 
98
 # a %SEARCH{...}% format.
 
99
    $line = 1;
 
100
    pos($_) = 0;
 
101
    my @_lines = split( /\n/, $_ );
 
102
    foreach (@_lines) {
 
103
        while (m/\$percntMAKETEXT\{\s*(string=)?(\\"(\\\\\\"|[^"])*\\")/gm) {
 
104
 
 
105
            # remove the enclosing [\"]'s:
 
106
            my $str = substr( $2, 2, -2 );
 
107
 
 
108
            # remove escaped stuff:
 
109
            $str =~ s/\\\\"/"/g;
 
110
            $str =~ s/\\"/"/g;
 
111
 
 
112
            # collect the string:
 
113
            $self->add_entry( $str, [ $file, $line, '' ] );
 
114
        }
 
115
        $line++;
 
116
    }
 
117
}
 
118
 
 
119
1;
 
120
__DATA__
 
121
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
122
#
 
123
# Copyright (C) 2008-2009 Foswiki Contributors. All Rights Reserved.
 
124
# Foswiki Contributors are listed in the AUTHORS file in the root
 
125
# of this distribution. NOTE: Please extend that file, not this notice.
 
126
#
 
127
# Additional copyrights apply to some or all of the code in this
 
128
# file as follows:
 
129
#
 
130
# Copyright (C) 2005-2007 TWiki Contributors. All Rights Reserved.
 
131
# TWiki Contributors are listed in the AUTHORS file in the root
 
132
# of this distribution. NOTE: Please extend that file, not this notice.
 
133
#
 
134
# This program is free software; you can redistribute it and/or
 
135
# modify it under the terms of the GNU General Public License
 
136
# as published by the Free Software Foundation; either version 2
 
137
# of the License, or (at your option) any later version. For
 
138
# more details read LICENSE in the root of this distribution.
 
139
#
 
140
# This program is distributed in the hope that it will be useful,
 
141
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
142
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
143
#
 
144
# As per the GPL, removal of this notice is prohibited.