summaryrefslogtreecommitdiff
path: root/i18n-to-gettext.pl
blob: e2f5fedae5775432e568122df347172592032b37 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/perl -w

# Convert i18n texts to gettext
#
# Extracts all texts from the file i18n.c and creates language
# specific *.po files.
#
# Usage: i18n-to-gettext.pl
#
# See the main source file 'vdr.c' for copyright information and
# how to reach the author.
#
# $Id: i18n-to-gettext.pl 2.0 2007/11/04 10:57:36 kls Exp $

# How to convert an actual plugin:
#
# - If your code contains something like
#
#   const char *Text = "Some text";
#   ...
#   Function(tr(Text));
#
#   you need to enclose the text constant in trNOOP(...), as in
#
#   const char *Text = trNOOP("Some text");
#
#   The actual usage of the tr() macro remains unchanged.
#
# - If you have comments following the texts in your i18n.c file
#   that should be seen by the translators, move them from the
#   i18n.c file into the file where the text is actually used,
#   and mark them with the TRANSLATORS keyword, as in
#
#   // TRANSLATORS: note the plural!
#   Add(new cMenuEditIntItem( tr("Setup.EPG$Preferred languages"), ...
#
# - Fill in your email address in the line
#   $MSGIDBUGS = "" ...
#   below, so that users can reach you in case there is a problem
#   with a message id.
#
# - Run this Perl script from within your plugin's source directory.
#   This script will run 'xgettext' to extract all of your texts
#   into a file named <pluginname>.pot. After that it will extract
#   your existing i18n texts from the file i18n.c (or wherever you
#   have defined them) and generate *.po files for all languages
#   currently known by VDR.
#
# - Your Makefile will be automatically adjusted to allow for
#   building the *.mo files necessary for supporting gettext().
#   Please check carefully whether the automatic adjustments have
#   succeeded, though. The modified Makefile will be written to
#   Makefile.new, so you can call this script any numer of times
#   in case you need to do some fine tuning of your texts (especially
#   adding some trNOOP() in case the script reports "missing phrases".
#   Once you're done converting your plugin source to using gettext,
#   rename Makefile.new to Makefile.
#
# - You may want to remove the old internationalized texts from your
#   source. Usually this means removing the files i18n.[hc], stripping
#   i18n.o from the OBJS variable in the Makefile and removing all
#   '#include "i18n.h"' lines from the rest of your source files.
#   These steps are not done automatically.

# Check for the existence of a Makefile:

die "Makefile not found!" unless (-f "Makefile");

# Determine the plugin name:

$PLUGIN = `grep '^PLUGIN *=' Makefile | head -1` || die "can't find plugin name!";
$PLUGIN =~ s/.*= *(.*)\n/$1/;
die "can't find plugin name!" unless ($PLUGIN);

# Locate the file containing the texts:

$I18NFILE = "";
for ("i18n.c", "i18n.h", `ls *.c`) { # try i18n.[ch] explicitly first
    chomp($f = $_);
    if (-f $f && `grep tI18nPhrase $f`) {
       $I18NFILE = $f;
       last;
       }
    }
die "can't find internationalized texts!" unless ($I18NFILE);

# Plugin specific information:

$TITLE = "VDR plugin language source file";
$COPYRIGHT = "2007 Klaus Schmidinger <kls\@cadsoft.de>";
$PACKAGE = "VDR";
$VERSION = "1.5.7";
$MSGIDBUGS = "" || die "enter your email address here"; # escape the '@', as in "user\@domain.tld"
$LANGUAGETEAM = "<vdr\@linuxtv.org>";

# Gettext environment:

$XGETTEXT = "xgettext -C -cTRANSLATORS --no-wrap -F -k -ktr -ktrNOOP --msgid-bugs-address='$MSGIDBUGS'";
$PODIR = "po";
$POTFILE = "$PODIR/$PLUGIN.pot";

# Languages as known by VDR 1.5.6:

@LANGS = (
  "en_US",
  "de_DE",
  "sl_SI",
  "it_IT",
  "nl_NL",
  "pt_PT",
  "fr_FR",
  "nn_NO",
  "fi_FI",
  "pl_PL",
  "es_ES",
  "el_GR",
  "sv_SE",
  "ro_RO",
  "hu_HU",
  "ca_ES",
  "ru_RU",
  "hr_HR",
  "et_EE",
  "da_DK",
  "cs_CZ",
  "tr_TR"
  );

@CHARSETS = (
  "ISO-8859-15",
  "ISO-8859-15",
  "ISO-8859-2",
  "ISO-8859-15",
  "ISO-8859-15",
  "ISO-8859-1",
  "ISO-8859-1",
  "ISO-8859-1",
  "ISO-8859-15",
  "ISO-8859-2",
  "ISO-8859-15",
  "ISO-8859-7",
  "ISO-8859-1",
  "ISO-8859-2",
  "ISO-8859-2",
  "ISO-8859-1",
  "ISO-8859-5",
  "ISO-8859-2",
  "ISO-8859-13",
  "ISO-8859-15",
  "ISO-8859-2",
  "ISO-8859-9",
  );

@TRANSLATORS = (
  "Klaus Schmidinger <kls\@cadsoft.de>, 2000",
  "Klaus Schmidinger <kls\@cadsoft.de>, 2000",
  "Miha Setina <mihasetina\@softhome.net>, 2000; Matjaz Thaler <matjaz.thaler\@guest.arnes.si>, 2003",
  "Alberto Carraro <bertocar\@tin.it>, 2001; Antonio Ospite <ospite\@studenti.unina.it>, 2003; Sean Carlos <seanc\@libero.it>, 2005",
  "Arnold Niessen <niessen\@iae.nl> <arnold.niessen\@philips.com>, 2001; Hans Dingemans <hans.dingemans\@tacticalops.nl>, 2003; Maarten Wisse <Maarten.Wisse\@urz.uni-hd.de>, 2005",
  "Paulo Lopes <pmml\@netvita.pt>, 2001",
  "Jean-Claude Repetto <jc\@repetto.org>, 2001; Olivier Jacques <jacquesolivier\@hotmail.com>, 2003; Gregoire Favre <greg\@magma.unil.ch>, 2003; Nicolas Huillard <nhuillard\@e-dition.fr>, 2005",
  "Jørgen Tvedt <pjtvedt\@online.no>, 2001; Truls Slevigen <truls\@slevigen.no>, 2002",
  "Hannu Savolainen <hannu\@opensound.com>, 2002; Jaakko Hyvätti <jaakko\@hyvatti.iki.fi>, 2002; Niko Tarnanen <niko.tarnanen\@hut.fi>, 2003; Rolf Ahrenberg <rahrenbe\@cc.hut.fi>, 2003",
  "Michael Rakowski <mrak\@gmx.de>, 2002",
  "Ruben Nunez Francisco <ruben.nunez\@tang-it.com>, 2002",
  "Dimitrios Dimitrakos <mail\@dimitrios.de>, 2002",
  "Tomas Prybil <tomas\@prybil.se>, 2002; Jan Ekholm <chakie\@infa.abo.fi>, 2003",
  "Paul Lacatus <paul\@campina.iiruc.ro>, 2002; Lucian Muresan <lucianm\@users.sourceforge.net>, 2004",
  "Istvan Koenigsberger <istvnko\@hotmail.com>, 2002; Guido Josten <guido.josten\@t-online.de>, 2002",
  "Marc Rovira Vall <tm05462\@salleURL.edu>, 2003; Ramon Roca <ramon.roca\@xcombo.com>, 2003; Jordi Vilà <jvila\@tinet.org>, 2003",
  "Vyacheslav Dikonov <sdiconov\@mail.ru>, 2004",
  "Drazen Dupor <drazen.dupor\@dupor.com>, 2004; Dino Ravnic <dino.ravnic\@fer.hr>, 2004",
  "Arthur Konovalov <kasjas\@hot.ee>, 2004",
  "Mogens Elneff <mogens\@elneff.dk>, 2004",
  "Vladimír Bárta <vladimir.barta\@k2atmitec.cz>, 2006",
  "Oktay Yolgeçen <oktay_73\@yahoo.de>, 2007"
  );

@LASTTRANSLATOR = (
  "Klaus Schmidinger <kls\@cadsoft.de>",
  "Klaus Schmidinger <kls\@cadsoft.de>",
  "Matjaz Thaler <matjaz.thaler\@guest.arnes.si>",
  "Sean Carlos <seanc\@libero.it>",
  "Maarten Wisse <Maarten.Wisse\@urz.uni-hd.de>",
  "Paulo Lopes <pmml\@netvita.pt>",
  "Nicolas Huillard <nhuillard\@e-dition.fr>",
  "Truls Slevigen <truls\@slevigen.no>",
  "Rolf Ahrenberg <rahrenbe\@cc.hut.fi>",
  "Michael Rakowski <mrak\@gmx.de>",
  "Ruben Nunez Francisco <ruben.nunez\@tang-it.com>",
  "Dimitrios Dimitrakos <mail\@dimitrios.de>",
  "Tomas Prybil <tomas\@prybil.se>",
  "Lucian Muresan <lucianm\@users.sourceforge.net>",
  "Istvan Koenigsberger <istvnko\@hotmail.com>, Guido Josten <guido.josten\@t-online.de>",
  "Jordi Vilà <jvila\@tinet.org>",
  "Vyacheslav Dikonov <sdiconov\@mail.ru>",
  "Drazen Dupor <drazen.dupor\@dupor.com>",
  "Arthur Konovalov <kasjas\@hot.ee>",
  "Mogens Elneff <mogens\@elneff.dk>",
  "Vladimír Bárta <vladimir.barta\@k2atmitec.cz>",
  "Oktay Yolgeçen <oktay_73\@yahoo.de>"
  );

# Collect all translated texts:

open(F, $I18NFILE) || die "$I18NFILE: $!\n";
$InComment = 0;
while (<F>) {
      chomp;
      s/\t/ /g; # get rid of tabs
      s/  *$//; # get rid of trailing blanks
      s/^ *\/\/.*//; # remove comment lines
      s/ *\/\/.*//; # strip trailing comments
      s/\/\*.*\*\///g; # strip c comments
      if (/\/\*/) {
         $InComment = 1;
         s/\/\*.*$//; # remove start of comment
         }
      elsif (/\*\//) {
         $InComment = 0;
         s/^.*\*\///; # remove end of comment
         }
      elsif ($InComment) {
         next;
         }
      next if (/^ *$/); # skip empty lines
      next if (/#if/);
      next if (/#endif/);
      next unless ($found or $found = /const *tI18nPhrase .*{/); # sync on phrases
      next if (/const *tI18nPhrase .*{/); # skip sync line
      last if (/{ *NULL *}/); # stop after last phrase
      if (/{ *(.*),/) {
         $Original = $1;
         $Index = 0;
         }
      elsif (/}/) {
         }
      else {
         $Translated = $_;
         $Translated =~ s/ *(.*),/$1/;
         $Index++;
         $Map{$Original}[$Index] = $Translated;
         }
      }
close(F);

# Generate the .pot file:

(mkdir($PODIR) || die "$PODIR: $!\n") unless -d $PODIR;

system("$XGETTEXT -o $POTFILE *.c");

# Generate .po files for all languages:

open(POT, $POTFILE) || die "$POTFILE: $!\n";
@POT = <POT>;
close(POT);

for ($Index = 1; $LANGS[$Index]; $Index++) {

    $Authors = "# " . join("\n# ", split(/; */, $TRANSLATORS[$Index])) . "\n";

    $POFILE = "$PODIR/$LANGS[$Index].po";
    open(PO, ">$POFILE") || die "$POFILE: $!\n";
    $msgid = "";
    for (@POT) {
        next if (/^#, fuzzy$/); # skip the 'fuzzy' keyword
        $msgid = $1 if (/^msgid (".+")/);
        $date = $1 if (/POT-Creation-Date: (.*)\\n/);
        $s = $_;
        if (!$msgid) {
           $s =~ s/SOME DESCRIPTIVE TITLE/$TITLE/;
           $s =~ s/YEAR THE PACKAGE'S COPYRIGHT HOLDER/$COPYRIGHT/;
           $s =~ s/PACKAGE/$PACKAGE/;
           $s =~ s/VERSION/$VERSION/;
           $s = $Authors if (/FIRST AUTHOR/);
           $s =~ s/YEAR-MO-DA HO:MI\+ZONE/$date/;
           $s =~ s/FULL NAME <EMAIL\@ADDRESS>/$LASTTRANSLATOR[$Index]/;
           $s =~ s/LANGUAGE <LL\@li\.org>/$LANGUAGETEAM/;
           $s =~ s/CHARSET/$CHARSETS[$Index]/;
           }
        else {
           $s =~ s/""/$Map{$msgid}[$Index]/ if ($msgid && /^msgstr ""/ && defined $Map{$msgid}[$Index]);
           $Used{$msgid} = 1;
           }
        print PO $s;
        }
    close(PO);
    }

# Report missing phrases:

for (keys %Map) {
    push(@Missing, "$_\n") unless $Used{$_};
    }

if (@Missing) {
   print "Missing phrases (may need to use trNOOP):\n\n";
   print sort(@Missing);
   }

# Adjust the Makefile:

$MAKEI18N = q{### Internationalization (I18N):

PODIR     = po
LOCALEDIR = $(VDRDIR)/locale
I18Npo    = $(wildcard $(PODIR)/*.po)
I18Nmsgs  = $(addprefix $(LOCALEDIR)/, $(addsuffix /LC_MESSAGES/vdr-$(PLUGIN).mo, $(notdir $(foreach file, $(I18Npo), $(basename $(file))))))
I18Npot   = $(PODIR)/$(PLUGIN).pot

%.mo: %.po
	msgfmt -c -o $@ $<

$(I18Npot): $(wildcard *.c)
	xgettext -C -cTRANSLATORS --no-wrap --no-location -k -ktr -ktrNOOP --msgid-bugs-address='<} . $MSGIDBUGS . q{>' -o $@ $^

%.po: $(I18Npot)
	msgmerge -U --no-wrap --no-location --backup=none -q $@ $<
	@touch $@

$(I18Nmsgs): $(LOCALEDIR)/%/LC_MESSAGES/vdr-$(PLUGIN).mo: $(PODIR)/%.mo
	@mkdir -p $(dir $@)
	cp $< $@

.PHONY: i18n
i18n: $(I18Nmsgs)

};

open(OLD, "Makefile") || die "Makefile: $!";
open(NEW, ">Makefile.new") || die "Makefile.new: $!";
while (<OLD>) {
      chomp;
      if (/Targets:/) {
         print NEW $MAKEI18N;
         }
      elsif (/^all:/) {
         $_ .= " i18n";
         }
      elsif (/^clean:/) {
         $_ .= "\n\t\@-rm -f \$(PODIR)/*.mo \$(PODIR)/*.pot";
         }
      print NEW "$_\n";
      }
close(NEW);
close(OLD);