Replace functions which called once with their bodies
[pidgin-git.git] / pidgin / win32 / nsis / create_nsis_translations.pl
blob189883099c020ebe2c4c2f353335ead9aa766a30
1 #!/usr/bin/perl
3 # create_nsis_translations.pl
5 # Copyright (C) 2000-2009 Bruno Coudoin
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 3 of the License, or
10 # (at your option) any later version.
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.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, see <http://www.gnu.org/licenses/>.
21 use strict;
23 sub usage {
24 print 'create_nsis_translations.pl translations installer tmp_dir
25 translations
26 This is an input file that contains all the
27 translated strings. If must be formated as a GNU/Linux
28 desktop file and contains multiple strings entry.
29 For example you must have:
30 toBe=To be or not to be
31 toBe[fr]=Etre ou ne pas etre
33 installer
34 This is your nsis installer source file. You must include
35 in it the marker @INSERT_TRANSLATIONS@ before you use any
36 translation string.
37 After that you can use the variable $(toBe) in your file.
39 tmp_dir
40 This is a directory in which temporary files needed for
41 the translation system.
42 It will be created if non existant.
43 You can remove it once you have created your installer.
47 my $translations;
48 if (! $ARGV[0] || ! -f $ARGV[0])
50 usage();
52 else
54 $translations = $ARGV[0];
57 shift;
58 my $installer;
59 if (! $ARGV[0] || ! -f $ARGV[0])
61 usage();
63 else
65 $installer = $ARGV[0];
68 shift;
69 my $tmp_dir;
70 if (! $ARGV[0] )
72 usage();
74 else
76 $tmp_dir = $ARGV[0];
78 if ( ! -d $tmp_dir )
80 mkdir $tmp_dir or die "ERROR: '$tmp_dir' $!\n";
84 print "Processing translation file '$translations'\n";
85 print " NSIS source file '$installer'\n";
86 print " Working dir '$tmp_dir'\n";
88 # Commented out locales that are not available in nsis
89 # Map value is ["NSISFilename", "Encoding", "LCID"]
90 my %localeNames = (
91 "af" => ["Afrikaans", "WINDOWS-1252", "1078"],
92 # "am" => ["Amharic", "UTF-8"],
93 "ar" => ["Arabic", "WINDOWS-1256", "1025"],
94 "be" => ["Belarusian", "WINDOWS-1251", "1059"],
95 "bg" => ["Bulgarian", "WINDOWS-1251", "1026"],
96 "bs" => ["Bosnian", "WINDOWS-1250", "5146"],
97 "br" => ["Breton", "WINDOWS-1252", "1150"],
98 "ca" => ["Catalan", "WINDOWS-1252", "1027"],
99 "cs" => ["Czech", "WINDOWS-1250", "1029"],
100 "cy" => ["Welsh", "WINDOWS-1252", "1160"],
101 "da" => ["Danish", "WINDOWS-1252", "1030"],
102 "de" => ["German", "WINDOWS-1252", "1031"],
103 # "dz" => ["Dzongkha", "UTF-8"],
104 "el" => ["Greek", "WINDOWS-1253", "1032"],
105 "en" => ["English", "WINDOWS-1252", "1033"],
106 "eo" => ["Esperanto", "WINDOWS-1252", "9998"],
107 "es" => ["Spanish", "WINDOWS-1252", "1034"],
108 "et" => ["Estonian", "WINDOWS-1257", "1061"],
109 "eu" => ["Basque", "WINDOWS-1252", "1069"],
110 #Some values in the farsi translation can't be represented in what iconv thinks is WINDOWS-1256, so we disable it
111 # "fa" => ["Farsi", "WINDOWS-1256", "1065"],
112 "fi" => ["Finnish", "WINDOWS-1252", "1035"],
113 "fr" => ["French", "WINDOWS-1252", "1036"],
114 "ga" => ["Irish", "WINDOWS-1252", "2108"],
115 "gl" => ["Galician", "WINDOWS-1252", "1110"],
116 # "gu" => ["Gujarati", "UTF-8"],
117 "he" => ["Hebrew", "WINDOWS-1255", "1037"],
118 # "hi" => ["Hindi", "UTF-8"],
119 "hr" => ["Croatian", "WINDOWS-1250", "1050"],
120 "hu" => ["Hungarian", "WINDOWS-1250", "1038"],
121 "id" => ["Indonesian", "WINDOWS-1252", "1057"],
122 "is" => ["Icelandic", "WINDOWS-1252", "15"], #This should be 1039!
123 "it" => ["Italian", "WINDOWS-1252", "1040"],
124 "ja" => ["Japanese", "CP932", "1041"],
125 # "ka" => ["Georgian", "UTF-8"],
126 "ko" => ["Korean", "CP949", "1042"],
127 "ku" => ["Kurdish", "WINDOWS-1254", "9999"],
128 "lb" => ["Luxembourgish", "WINDOWS-1252", "4103"],
129 "lt" => ["Lithuanian", "WINDOWS-1257", "1063"],
130 "lv" => ["Latvian", "WINDOWS-1257", "1062"],
131 "mk" => ["Macedonian", "WINDOWS-1251", "1071"],
132 # "ml" => ["Malayalam", "UTF-8"],
133 # "mr" => ["Marathi", "UTF-8"],
134 "mn" => ["Mongolian", "WINDOWS-1251", "1104"],
135 "ms" => ["Malay", "WINDOWS-1252", "1086"],
136 "nb" => ["Norwegian", "WINDOWS-1252", "1044"],
137 # "ne" => ["Nepal", "UTF-8"],
138 "nl" => ["Dutch", "WINDOWS-1252", "1043"],
139 "nn" => ["NorwegianNynorsk", "WINDOWS-1252", "2068"],
140 # "oc" => ["Occitan", "WINDOWS-1252"],
141 # "pa" => ["Punjabi", "UTF-8"],
142 "pl" => ["Polish", "WINDOWS-1250", "1045"],
143 "pt" => ["Portuguese", "WINDOWS-1252", "2070"],
144 "pt_BR" => ["PortugueseBR", "WINDOWS-1252", "1046"],
145 "ro" => ["Romanian", "WINDOWS-1250", "1048"],
146 "ru" => ["Russian", "WINDOWS-1251", "1049"],
147 # "rw" => ["Kinyarwanda", "UTF-8"],
148 "sk" => ["Slovak", "WINDOWS-1250", "1051"],
149 "sl" => ["Slovenian", "WINDOWS-1250", "1060"],
150 # "so" => ["Somali", "UTF-8"],
151 "sq" => ["Albanian", "WINDOWS-1252", "1052"],
152 "sr" => ["Serbian", "WINDOWS-1251", "3098"],
153 "sr\@latin" => ["SerbianLatin", "WINDOWS-1250", "2074"],
154 "sv" => ["Swedish", "WINDOWS-1252", "1053"],
155 # "ta" => ["Tamil", "UTF-8"],
156 "th" => ["Thai", "WINDOWS-874", "1054"],
157 "tr" => ["Turkish", "WINDOWS-1254", "1055"],
158 "uk" => ["Ukrainian", "WINDOWS-1251", "1058"],
159 "uz" => ["Uzbek", "WINDOWS-1252", "1091"],
160 # "ur" => ["Urdu", "UTF-8"],
161 # "vi" => ["Vietnamese", "WINDOWS-1258"],
162 # "wa" => ["Walloon", "WINDOWS-1252"],
163 "zh_CN" => ["SimpChinese", "WINDOWS-936", "2052"],
164 "zh_TW" => ["TradChinese", "CP950", "1028"],
167 my @localeKeys = keys(%localeNames);
169 # Create the holder for the results
170 # %result{"locale"}{"stringname"} = result line
171 print "Parsing nsis_translations.desktop\n";
172 my %result;
174 # Create a hash of the keys to translate
175 open (MYFILE, $translations);
176 while (<MYFILE>) {
177 chomp $_;
178 if ($_ =~ /^Encoding=UTF-8/ || $_ =~ /^\s*$/ || $_ =~ /^\[Desktop Entry\]/ || $_ =~ /^#/)
180 next;
182 elsif ($_ =~ /^(\w+)=(.*)/)
184 my $key = $1;
185 my $lang = "en";
186 my $value = $2;
187 $value =~ s/["]/\$\\"/g;
188 $result{"$lang"}{"$key"} = "!define $key \"$value\"\n";
190 elsif ($_ =~ /^(\w+)\[([\w@]+)\]=(.*)/)
192 my $key = $1;
193 my $lang = $2;
194 my $value = $3;
195 $value =~ s/["]/\$\\"/g;
196 $result{"$lang"}{"$key"} = "!define $key \"$value\"\n";
198 else
200 print "Found unrecognized line: '$_'\n";
203 close (MYFILE);
205 # Lets insert the default languages
206 # in the installer file which means replacing:
207 # @INSERTMACRO_MUI_LANGUAGE@
208 # By the list of locales:
209 # !insertmacro MUI_LANGUAGE "French"
211 my $muiLanguages;
212 $muiLanguages = '
213 ;; English goes first because its the default. The rest are
214 ;; in alphabetical order (at least the strings actually displayed
215 ;; will be).
216 !insertmacro MUI_LANGUAGE "English"
219 # The specific GCompris translation for the installer
220 # replacing:
221 # @GCOMPRIS_MACRO_INCLUDE_LANGFILE@
222 # By the list of locales:
223 # !insertmacro GCOMPRIS_MACRO_INCLUDE_LANGFILE "ALBANIAN" "${GCOMPRIS_NSIS_INCLUDE_PATH}\translations\albanian.nsh"
225 my $gcomprisLanguages;
227 $gcomprisLanguages .= '
228 ;--------------------------------
229 ;Translations
230 !define GCOMPRIS_DEFAULT_LANGFILE "${GCOMPRIS_NSIS_INCLUDE_PATH}\\translations\\en.nsh"
232 ;; Windows GCompris NSIS installer language macros
235 !macro GCOMPRIS_MACRO_DEFAULT_STRING LABEL VALUE
236 !ifndef "${LABEL}"
237 !define "${LABEL}" "${VALUE}"
238 !ifdef INSERT_DEFAULT
239 !warning "${LANG} lang file missing ${LABEL}, using default..."
240 !endif
241 !endif
242 !macroend
244 !macro GCOMPRIS_MACRO_LANGSTRING_INSERT LABEL LANG
245 LangString "${LABEL}" "${LANG_${LANG}}" "${${LABEL}}"
246 !undef "${LABEL}"
247 !macroend
249 !macro GCOMPRIS_MACRO_LANGUAGEFILE_BEGIN LANG
250 !define CUR_LANG "${LANG}"
251 !macroend
255 # GCOMPRIS_MACRO_LANGUAGEFILE_END
256 $gcomprisLanguages .= '
257 !macro GCOMPRIS_MACRO_LANGUAGEFILE_END
258 !define INSERT_DEFAULT
259 !include "${GCOMPRIS_DEFAULT_LANGFILE}"
260 !undef INSERT_DEFAULT
262 ; String labels should match those from the default language file.
265 my $text_en = $result{"en"};
266 foreach my $keyEn (keys(%$text_en)) {
267 $gcomprisLanguages .= " !insertmacro GCOMPRIS_MACRO_LANGSTRING_INSERT $keyEn \${CUR_LANG}";
268 $gcomprisLanguages .= "\n";
271 $gcomprisLanguages .= '
272 !undef CUR_LANG
273 !macroend
276 $gcomprisLanguages .= '
277 !macro GCOMPRIS_MACRO_INCLUDE_LANGFILE LANG FILE
278 !insertmacro GCOMPRIS_MACRO_LANGUAGEFILE_BEGIN "${LANG}"
279 !include "${FILE}"
280 !insertmacro GCOMPRIS_MACRO_LANGUAGEFILE_END
281 !macroend
285 # Create each nsh translation file
288 print "Creating the nsh default file\n";
289 open (DESC, ">$tmp_dir/en.nsh");
290 print DESC ";; Auto generated file by create_nsis_translations.pl\n";
291 foreach my $keyEn (keys(%$text_en)) {
292 my $line = $result{'en'}{$keyEn};
293 $line =~ s/!define /!insertmacro GCOMPRIS_MACRO_DEFAULT_STRING /;
294 print DESC $line;
296 close DESC;
298 $gcomprisLanguages .= " !insertmacro GCOMPRIS_MACRO_INCLUDE_LANGFILE".
299 " \"ENGLISH\"".
300 " \"\${GCOMPRIS_NSIS_INCLUDE_PATH}\\translations\\en.nsh\"\n";
302 my $selectTranslationFunction = '
303 !macro SELECT_TRANSLATION_SECTION LANG_NAME LANG_CODE
304 StrCmp "$LANGUAGE" "${LANG_${LANG_NAME}}" 0 end_${LANG_CODE}
305 !insertmacro SelectSection ${SecLang_${LANG_CODE}}
306 Goto done
307 end_${LANG_CODE}:
308 !macroend
309 ; Convert the current $LANGUAGE to a language code that we can use for translation mo selection
310 ; If there\'s a better way to do this, I\'d love to know it
311 !macro SELECT_TRANSLATION_FUNCTION
315 # Two pass are needed:
316 # - create the utf8 file
317 # - transform it to the proper windows locale
319 print "Creating the nsh locale files\n";
320 foreach my $lang (@localeKeys) {
321 if ( $lang eq "en" ) { next; }
322 open (DESC, ">$tmp_dir/$lang.nsh.utf8");
323 print DESC ";; Auto generated file by create_nsis_translations.pl\n";
324 print DESC ";; Code Page: $localeNames{$lang}[1]\n";
326 my $text_locale = $result{"$lang"};
327 my $total_key_count = 0;
328 my $found_key_count = 0;
329 foreach my $keyEn (keys(%$text_en)) {
330 my $found = 0;
331 $total_key_count++;
332 foreach my $keyLocale (keys(%$text_locale)) {
333 # Fine, we found a translation
334 if ( $keyLocale eq $keyEn )
336 print DESC "$result{$lang}{$keyLocale}";
337 $found = 1;
338 $found_key_count++;
339 last;
342 # English keys are the reference.
343 # If not found they are inserted
344 #if ( ! $found )
346 # print DESC "$result{'en'}{$keyEn}";
349 close DESC;
351 # If we have at least 50% of the keys found, include the language
352 if (($found_key_count * 1.0 / $total_key_count) >= 0.5) {
353 $muiLanguages .= " !insertmacro MUI_LANGUAGE \"$localeNames{$lang}[0]\"\n";
354 $gcomprisLanguages .= " !insertmacro GCOMPRIS_MACRO_INCLUDE_LANGFILE".
355 " \"". uc($localeNames{$lang}[0]) . "\"".
356 " \"\${GCOMPRIS_NSIS_INCLUDE_PATH}\\translations\\$lang.nsh\"\n";
357 $selectTranslationFunction .= " !insertmacro SELECT_TRANSLATION_SECTION".
358 " \"" . uc($localeNames{$lang}[0]) . "\" \"$lang\"\n";
359 } else {
360 print "Ignoring language $lang because it is less than 50% translated ($found_key_count of $total_key_count).\n";
361 next;
365 # iconv conversion
366 system("iconv -f UTF-8 -t $localeNames{$lang}[1] $tmp_dir/$lang.nsh.utf8 > $tmp_dir/$lang.nsh");
367 if ($? ne 0)
369 die("ERROR: Failed to run: iconv -f UTF-8 -t $localeNames{$lang}[1] $lang.nsh.utf8 > $lang.nsh\n");
371 #`rm $tmp_dir/$lang.nsh.utf8`;
375 $selectTranslationFunction .= '
376 done:
377 !macroend
380 # We have all the data, let's replace it
381 my $gcomprisInstaller;
382 open (MYFILE, $installer);
383 while (<MYFILE>) {
384 if ($_ =~ /\@INSERT_TRANSLATIONS\@/)
386 print "Processing \@INSERT_TRANSLATIONS\@\n";
387 $gcomprisInstaller .= $muiLanguages;
388 $gcomprisInstaller .= $gcomprisLanguages;
389 $gcomprisInstaller .= $selectTranslationFunction;
391 else
393 $gcomprisInstaller .= "$_";
396 close (MYFILE);
398 # Rewrite the file with the replaced data
399 open (MYFILE, ">$installer");
400 print MYFILE "$gcomprisInstaller";
401 close (MYFILE);