Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tools/create_nsis_translations.pl
blob: 5c494788935fd0eac51cb6531d89981a72b8e90e (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
#
# create_nsis_translations.pl
#
# Copyright (C) 2000-2009 Bruno Coudoin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#

use strict;

sub usage {
    print 'create_nsis_translations.pl translations installer tmp_dir
  translations
     This is an input file that contains all the
     translated strings. If must be formated as a GNU/Linux
     desktop file and contains multiple strings entry.
     For example you must have:
     toBe=To be or not to be
     toBe[fr]=Etre ou ne pas etre

  installer
     This is your nsis installer source file. You must include
     in it the marker @INSERT_TRANSLATIONS@ before you use any
     translation string.
     After that you can use the variable $(toBe) in your file.

  tmp_dir
     This is a directory in which temporary files needed for
     the translation system.
     It will be created if non existant.
     You can remove it once you have created your installer.
';
}

my $translations;
if (! $ARGV[0] || ! -f $ARGV[0])
{
    usage();
}
else
{
    $translations = $ARGV[0];
}

shift;
my $installer;
if (! $ARGV[0] || ! -f $ARGV[0])
{
    usage();
}
else
{
    $installer = $ARGV[0];
}

shift;
my $tmp_dir;
if (! $ARGV[0] )
{
    usage();
}
else
{
    $tmp_dir = $ARGV[0];

    if ( ! -d $tmp_dir )
    {
	mkdir $tmp_dir or die "ERROR: '$tmp_dir' $!\n";
    }
}

print "Processing translation file '$translations'\n";
print "          NSIS source file  '$installer'\n";
print "                Working dir '$tmp_dir'\n";

# Commented out locales that are not available in nsis
my %localeNames = (
  "af" =>	["Afrikaans", "UTF-8"],
#  "am" =>	["Amharic", "UTF-8"],
  "ar" =>	["Arabic", "WINDOWS-1256"],
  "bg" =>	["Bulgarian", "WINDOWS-1251"],
  "br" =>	["Breton", "WINDOWS-1252"],
  "ca" =>	["Catalan", "WINDOWS-1252"],
  "cs" =>	["Czech", "WINDOWS-1252"],
  "da" =>	["Danish", "WINDOWS-1252"],
  "de" =>	["German", "WINDOWS-1252"],
#  "dz" =>	["Dzongkha", "UTF-8"],
  "el" =>	["Greek", "WINDOWS-1253"],
  "en" =>	["English", "WINDOWS-1252"],
  "es" =>	["Spanish", "WINDOWS-1252"],
  "eu" =>	["Basque", "WINDOWS-1252"],
#  "fa" =>	["Persian", "UTF-8"],
  "fi" =>	["Finnish", "WINDOWS-1252"],
  "fr" =>	["French", "WINDOWS-1252"],
  "ga" =>	["Irish", "WINDOWS-1252"],
#  "gu" =>	["Gujarati", "UTF-8"],
  "he" =>	["Hebrew", "WINDOWS-1255"],
#  "hi" =>	["Hindi", "UTF-8"],
  "hr" =>	["Croatian", "WINDOWS-1250"],
  "hu" =>	["Hungarian", "WINDOWS-1250"],
  "id" =>	["Indonesian", "WINDOWS-1252"],
  "it" =>	["Italian", "WINDOWS-1252"],
  "ja" =>	["Japanese", "CP932"],
#  "ka" =>	["Georgian", "UTF-8"],
  "ko" =>	["Korean", "MSCP949"],
  "lt" =>	["Lithuanian", "WINDOWS-1257"],
  "mk" =>	["Macedonian", "WINDOWS-1251"],
#  "ml" =>	["Malayalam", "UTF-8"],
#  "mr" =>	["Marathi", "UTF-8"],
  "ms" =>	["Malay", "WINDOWS-1252"],
  "nb" =>	["Norwegian", "WINDOWS-1252"],
#  "ne" =>	["Nepal", "UTF-8"],
  "nl" =>	["Dutch", "WINDOWS-1252"],
  "nn" =>	["NorwegianNynorsk", "UTF-8"],
#  "oc" =>	["Occitan", "WINDOWS-1252"],
#  "pa" =>	["Punjabi", "UTF-8"],
  "pl" =>	["Polish", "WINDOWS-1250"],
  "pt" =>	["Portuguese", "WINDOWS-1252"],
  "pt_BR" =>	["PortugueseBR", "WINDOWS-1252"],
  "ro" =>	["Romanian", "WINDOWS-1250"],
  "ru" =>	["Russian", "WINDOWS-1251"],
 # "rw" =>	["Kinyarwanda", "UTF-8"],
  "sk" =>	["Slovak", "WINDOWS-1250"],
  "sl" =>	["Slovenian", "WINDOWS-1250"],
#  "so" =>	["Somali", "UTF-8"],
  "sq" =>	["Albanian", "WINDOWS-1252"],
  "sr" =>	["Serbian", "WINDOWS-1250"],
  "sv" =>	["Swedish", "WINDOWS-1252"],
#  "ta" =>	["Tamil", "UTF-8"],
  "th" =>	["Thai", "WINDOWS-874"],
  "tr" =>	["Turkish", "WINDOWS-1254"],
  "uk" =>	["Ukrainian", "WINDOWS-1251"],
#  "ur" =>	["Urdu", "UTF-8"],
#  "vi" =>	["Vietnamese", "WINDOWS-1258"],
#  "wa" =>	["Walloon", "WINDOWS-1252"],
  "zh" =>	["SimpChinese", "WINDOWS-936"],
  "zh" =>	["TradChinese", "CP950"],
);

my @localeKeys = keys(%localeNames);

# Create the holder for the results
# %result{"locale"}{"stringname"} = result line
print "Parsing nsis_translations.desktop\n";
my %result;

# Create a hash of the keys to translate
open (MYFILE, $translations);
while (<MYFILE>) {
    chomp $_;
    if ($_ =~ /Encoding=UTF-8/)
    {
	next;
    }
    elsif ($_ =~ /^(\w+)=(.*)/)
    {
	my $line = "!define $1 \"$2\"\n";
	$result{"en"}{"$1"} = $line;
    }
    elsif ($_ =~ /^(\w+)\[(\w+)\]=(.*)/)
    {
	my $line = "!define $1 \"$3\"\n";
	$result{"$2"}{"$1"} = $line;
    }
}
close (MYFILE);

# Lets insert the default languages
# in the installer file which means replacing:
#   @INSERTMACRO_MUI_LANGUAGE@
# By the list of locales:
#   !insertmacro MUI_LANGUAGE "French"

my $muiLanguages;
$muiLanguages = '
  ;; English goes first because its the default. The rest are
  ;; in alphabetical order (at least the strings actually displayed
  ;; will be).
  !insertmacro MUI_LANGUAGE "English"
';

foreach my $lang (@localeKeys) {
    if ( $lang eq "en" ) { next; }
    $muiLanguages .= "  !insertmacro MUI_LANGUAGE \"$localeNames{$lang}[0]\"\n";
}

# The specific GCompris translation for the installer
# replacing:
#   @GCOMPRIS_MACRO_INCLUDE_LANGFILE@
# By the list of locales:
#   !insertmacro GCOMPRIS_MACRO_INCLUDE_LANGFILE "ALBANIAN" "${GCOMPRIS_NSIS_INCLUDE_PATH}\translations\albanian.nsh"

my $gcomprisLanguages;

$gcomprisLanguages .= '
;--------------------------------
;Translations
  !define GCOMPRIS_DEFAULT_LANGFILE "${GCOMPRIS_NSIS_INCLUDE_PATH}\\translations\\en.nsh"
;;
;; Windows GCompris NSIS installer language macros
;;

!macro GCOMPRIS_MACRO_DEFAULT_STRING LABEL VALUE
  !ifndef "${LABEL}"
    !define "${LABEL}" "${VALUE}"
    !ifdef INSERT_DEFAULT
      !warning "${LANG} lang file mising ${LABEL}, using default.."
    !endif
  !endif
!macroend

!macro GCOMPRIS_MACRO_LANGSTRING_INSERT LABEL LANG
  LangString "${LABEL}" "${LANG_${LANG}}" "${${LABEL}}"
  !undef "${LABEL}"
!macroend

!macro GCOMPRIS_MACRO_LANGUAGEFILE_BEGIN LANG
  !define CUR_LANG "${LANG}"
!macroend
';


# GCOMPRIS_MACRO_LANGUAGEFILE_END
$gcomprisLanguages .= '
!macro GCOMPRIS_MACRO_LANGUAGEFILE_END
  !define INSERT_DEFAULT
  !include "${GCOMPRIS_DEFAULT_LANGFILE}"
  !undef INSERT_DEFAULT

  ; String labels should match those from the default language file.
';

my $text_en = $result{"en"};
foreach my $keyEn (keys(%$text_en)) {
    $gcomprisLanguages .= "  !insertmacro GCOMPRIS_MACRO_LANGSTRING_INSERT $keyEn \${CUR_LANG}";
    $gcomprisLanguages .= "\n";
}

$gcomprisLanguages .= '
  !undef CUR_LANG
!macroend
';

$gcomprisLanguages .= '
!macro GCOMPRIS_MACRO_INCLUDE_LANGFILE LANG FILE
  !insertmacro GCOMPRIS_MACRO_LANGUAGEFILE_BEGIN "${LANG}"
  !include "${FILE}"
  !insertmacro GCOMPRIS_MACRO_LANGUAGEFILE_END
!macroend
';

foreach my $lang (@localeKeys) {
    $gcomprisLanguages .= "  !insertmacro GCOMPRIS_MACRO_INCLUDE_LANGFILE".
     " \"$localeNames{$lang}[0]\"".
     " \"\${GCOMPRIS_NSIS_INCLUDE_PATH}\\translations\\$lang.nsh\"\n";
}

# We have all the data, let's replace it
my $gcomprisInstaller;
open (MYFILE, $installer);
while (<MYFILE>) {
    if ($_ =~ /\@INSERT_TRANSLATIONS\@/)
    {
	print "Processing \@INSERT_TRANSLATIONS\@\n";
	$gcomprisInstaller .= $muiLanguages;
	$gcomprisInstaller .= $gcomprisLanguages;
    }
    else
    {
	$gcomprisInstaller .= "$_";
    }
}
close (MYFILE);

# Rewrite the file with the replaced data
open (MYFILE, ">$installer");
print MYFILE "$gcomprisInstaller";
close (MYFILE);

#
# Create each nsh translation file
#

print "Creating the nsh default file\n";
open (DESC, ">$tmp_dir/en.nsh");
print DESC ";; Auto generated file by create_nsis_translations.pl\n";
foreach my $keyEn (keys(%$text_en)) {
    my $line = $result{'en'}{$keyEn};
    $line =~ s/!define /!insertmacro GCOMPRIS_MACRO_DEFAULT_STRING /;
    print DESC $line;
}
close DESC;

#
# Two pass are needed:
# - create the utf8 file
# - transform it to the proper windows locale
#
print "Creating the nsh locale files\n";
foreach my $lang (@localeKeys) {
    if ( $lang eq "en" ) { next; }
    open (DESC, ">$tmp_dir/$lang.nsh.utf8");
    print DESC ";; Auto generated file by create_nsis_translations.pl\n";
    print DESC ";; Code Page: $localeNames{$lang}[1]\n";

    my $text_locale = $result{"$lang"};
    foreach my $keyEn (keys(%$text_en)) {
	my $found = 0;
	foreach my $keyLocale (keys(%$text_locale)) {
	    # Fine, we found a translation
	    if ( $keyLocale eq $keyEn )
	    {
		print DESC "$result{$lang}{$keyLocale}";
		$found = 1;
		last;
	    }
	}
	# English keys are the reference.
	# If not found they are inserted
	if ( ! $found )
	{
	    print DESC "$result{'en'}{$keyEn}";
	}
    }
    close DESC;

    # iconv conversion
    system("iconv -f UTF-8 -t $localeNames{$lang}[1] $tmp_dir/$lang.nsh.utf8 -o $tmp_dir/$lang.nsh");
    if ($? ne 0)
    {
	print("ERROR: Failed to run: iconv -f UTF-8 -t $localeNames{$lang}[1] $lang.nsh.utf8 -o $lang.nsh\n");
    }
    `rm $tmp_dir/$lang.nsh.utf8`;

}