1
# See bottom of file for license and copyright information
5
---+ package Foswiki::I18N::Extract
7
Support translatable strings extraction from Foswiki topics and templates.
8
Depends on Locale::Maketext::Extract (part of CPAN::Locale::Maketext::Lexicon).
12
package Foswiki::I18N::Extract;
16
use vars qw( $initialised $initError );
19
eval "use base 'Locale::Maketext::Extract'";
21
$initialised = !$initError;
24
##########################################################
28
---++ ClassMethod new ( $session ) -> $extract
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
40
unless ($initialised) {
41
$session->logger->log('warning', $initError) if $session;
45
my $self = new Locale::Maketext::Extract;
46
$self->{session} = $session;
47
return bless( $self, $class );
52
---++ ObjectMethod extract ( $file , $text )
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.
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.
68
# do existing extraction
69
$self->SUPER::extract( $file, $_ );
72
my $doublequoted = '"(\\\"|[^"])*"';
74
# Foswiki's %MAKETEXT{...}% into topics and templates :
77
my @_lines = split( /\n/, $_ );
79
while (m/%MAKETEXT\{\s*(string=)?($doublequoted)/gm) {
80
my $str = substr( $2, 1, -1 );
82
$self->add_entry( $str, [ $file, $line, '' ] );
87
# Foswiki's %MAKETEXT{...}% inside a search format would look like this:
88
# %SEARCH{... format=" ... $percntMAKETEXT{\"...\" args=\"\"}$percnt ..." ...}%
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
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.
101
my @_lines = split( /\n/, $_ );
103
while (m/\$percntMAKETEXT\{\s*(string=)?(\\"(\\\\\\"|[^"])*\\")/gm) {
105
# remove the enclosing [\"]'s:
106
my $str = substr( $2, 2, -2 );
108
# remove escaped stuff:
112
# collect the string:
113
$self->add_entry( $str, [ $file, $line, '' ] );
121
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
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.
127
# Additional copyrights apply to some or all of the code in this
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.
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.
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.
144
# As per the GPL, removal of this notice is prohibited.