Root/
Source at commit 2567 created 9 years 5 months ago. By ifabio, Update Chameleon pkg Installer (Allow install on ESP). (Credits to Micky1979) | |
---|---|
1 | #!/usr/bin/perl -w␊ |
2 | ␊ |
3 | # Po4a::Wml.pm␊ |
4 | #␊ |
5 | # extract and translate translatable strings from a WML (web markup language) documents␊ |
6 | #␊ |
7 | # This program is free software; you can redistribute it and/or modify␊ |
8 | # it under the terms of the GNU General Public License as published by␊ |
9 | # the Free Software Foundation; either version 2 of the License, or␊ |
10 | # (at your option) any later version.␊ |
11 | #␊ |
12 | # This program is distributed in the hope that it will be useful,␊ |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of␊ |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␊ |
15 | # GNU General Public License for more details.␊ |
16 | #␊ |
17 | # You should have received a copy of the GNU General Public License␊ |
18 | # along with this program; if not, write to the Free Software␊ |
19 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.␊ |
20 | #␊ |
21 | ########################################################################␊ |
22 | ␊ |
23 | =encoding UTF-8␊ |
24 | ␊ |
25 | =head1 NAME␊ |
26 | ␊ |
27 | Locale::Po4a::Wml - convert WML (web markup language) documents from/to PO files␊ |
28 | ␊ |
29 | =head1 DESCRIPTION␊ |
30 | ␊ |
31 | The po4a (PO for anything) project goal is to ease translations (and more␊ |
32 | interestingly, the maintenance of translations) using gettext tools on␊ |
33 | areas where they were not expected like documentation.␊ |
34 | ␊ |
35 | Locale::Po4a::Wml is a module to help the translation of WML documents into␊ |
36 | other [human] languages. Do not mixup the WML we are speaking about here␊ |
37 | (web markup language) and the WAP crap used on cell phones.␊ |
38 | ␊ |
39 | Please note that this module relies upon the Locale::Po4a::Xhtml␊ |
40 | module, which also relies upon the Locale::Po4a::Xml module. This␊ |
41 | means that all tags for web page expressions are assumed to be written␊ |
42 | in the XHTML syntax.␊ |
43 | ␊ |
44 | =head1 OPTIONS ACCEPTED BY THIS MODULE␊ |
45 | ␊ |
46 | NONE.␊ |
47 | ␊ |
48 | =head1 STATUS OF THIS MODULE␊ |
49 | ␊ |
50 | This module works for some simple documents, but is still young.␊ |
51 | Currently, the biggest issue of the module is probably that it cannot␊ |
52 | handle documents that contain non-XML inline tags such as <email␊ |
53 | "foo@example.org">, which are often defined in the WML. Improvements␊ |
54 | will be added in the future releases.␊ |
55 | ␊ |
56 | =cut␊ |
57 | ␊ |
58 | package Locale::Po4a::Wml;␊ |
59 | ␊ |
60 | use 5.006;␊ |
61 | use strict;␊ |
62 | use warnings;␊ |
63 | ␊ |
64 | require Exporter;␊ |
65 | use vars qw(@ISA @EXPORT);␊ |
66 | @ISA = qw(Locale::Po4a::Xhtml);␊ |
67 | @EXPORT = qw();␊ |
68 | ␊ |
69 | use Locale::Po4a::Xhtml;␊ |
70 | use File::Temp;␊ |
71 | ␊ |
72 | sub initialize {␊ |
73 | my $self = shift;␊ |
74 | my %options = @_;␊ |
75 | ␊ |
76 | $self->SUPER::initialize(%options);␊ |
77 | ␊ |
78 | $self->treat_options;␊ |
79 | }␊ |
80 | ␊ |
81 | sub read {␊ |
82 | my ($self,$filename)=@_;␊ |
83 | my $tmp_filename;␊ |
84 | (undef,$tmp_filename)=File::Temp->tempfile("po4aXXXX",␊ |
85 | DIR => "/tmp",␊ |
86 | SUFFIX => ".xml",␊ |
87 | OPEN => 0,␊ |
88 | UNLINK => 0)␊ |
89 | or die wrap_msg(gettext("Can't create a temporary XML file: %s"), $!);␊ |
90 | my $file;␊ |
91 | open FILEIN,"$filename" or die "Cannot read $filename: $!\n";␊ |
92 | {␊ |
93 | $/ = undef;␊ |
94 | $file=<FILEIN>;␊ |
95 | }␊ |
96 | $/ = "\n";␊ |
97 | ␊ |
98 | # Mask perl cruft out of XML sight␊ |
99 | while ( ($file =~ m|^(.*?)<perl>(.*?)</perl>(.*?)$|ms)␊ |
100 | or ($file =~ m|^(.*?)<:(.*?):>(.*)$|ms)) {␊ |
101 | my ($pre,$in,$post) = ($1,$2,$3);␊ |
102 | $in =~ s/</PO4ALT/g;␊ |
103 | $in =~ s/>/PO4AGT/g;␊ |
104 | $file = "${pre}<!--PO4ABEGINPERL${in}PO4AENDPERL-->$post";␊ |
105 | }␊ |
106 | ␊ |
107 | # Mask mp4h cruft␊ |
108 | while ($file =~ s|^#(.*)$|<!--PO4ASHARPBEGIN$1PO4ASHARPEND-->|m) {␊ |
109 | my $line = $1;␊ |
110 | print STDERR "PROTECT HEADER: $line\n"␊ |
111 | if $self->debug();␊ |
112 | if ($line =~ m/title="([^"]*)"/) { #) {#"){␊ |
113 | warn "FIXME: We should translate the page title: $1\n";␊ |
114 | }␊ |
115 | }␊ |
116 | ␊ |
117 | # Validate define-tag tag's argument␊ |
118 | $file =~ s|(<define-tag\s+)([^\s>]+)|$1PO4ADUMMYATTR="$2"|g;␊ |
119 | ␊ |
120 | # Flush the result to disk␊ |
121 | open OUTFILE,">$tmp_filename";␊ |
122 | print OUTFILE $file;␊ |
123 | close INFILE;␊ |
124 | close OUTFILE or die "Cannot write $tmp_filename: $!\n";␊ |
125 | ␊ |
126 | push @{$self->{DOCXML}{infile}}, $tmp_filename;␊ |
127 | $self->{DOCWML}{$tmp_filename} = $filename;␊ |
128 | $self->Locale::Po4a::TransTractor::read($tmp_filename);␊ |
129 | unlink "$tmp_filename";␊ |
130 | }␊ |
131 | ␊ |
132 | sub parse {␊ |
133 | my $self = shift;␊ |
134 | ␊ |
135 | foreach my $filename (@{$self->{DOCXML}{infile}}) {␊ |
136 | $self->Locale::Po4a::Xml::parse_file($filename);␊ |
137 | my $org_filename = $self->{DOCWML}{$filename};␊ |
138 | ␊ |
139 | # Fix the references␊ |
140 | foreach my $msgid (keys %{$self->{TT}{po_out}{po}}) {␊ |
141 | $self->{TT}{po_out}{po}{$msgid}{'reference'} =~␊ |
142 | s|$filename(:\d+)|$org_filename$1|o;␊ |
143 | }␊ |
144 | ␊ |
145 | # Get the document back (undoing our WML masking)␊ |
146 | # FIXME: need to join the file first, and then split?␊ |
147 | my @doc_out;␊ |
148 | foreach my $line (@{$self->{TT}{doc_out}}) {␊ |
149 | $line =~ s/^<!--PO4ASHARPBEGIN(.*?)PO4ASHARPEND-->/#$1/mg;␊ |
150 | $line =~ s/<!--PO4ABEGINPERL(.*?)PO4AENDPERL-->/<:$1:>/sg;␊ |
151 | $line =~ s/(<define-tag\s+)PO4ADUMMYATTR="([^"]*)"/$1$2/g;␊ |
152 | $line =~ s/PO4ALT/</sg;␊ |
153 | $line =~ s/PO4AGT/>/sg;␊ |
154 | push @doc_out, $line;␊ |
155 | }␊ |
156 | $self->{TT}{doc_out} = \@doc_out;␊ |
157 | }␊ |
158 | }␊ |
159 | ␊ |
160 | 1;␊ |
161 | ␊ |
162 | =head1 AUTHORS␊ |
163 | ␊ |
164 | Martin Quinson (mquinson#debian.org)␊ |
165 | Noriada Kobayashi <nori1@dolphin.c.u-tokyo.ac.jp>␊ |
166 | ␊ |
167 | =head1 COPYRIGHT AND LICENSE␊ |
168 | ␊ |
169 | Copyright 2005 by SPI, inc.␊ |
170 | ␊ |
171 | This program is free software; you may redistribute it and/or modify it␊ |
172 | under the terms of GPL (see the COPYING file).␊ |
173 |