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/>.
24 print 'create_nsis_translations.pl translations installer tmp_dir
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
34 This is your nsis installer source file. You must include
35 in it the marker @INSERT_TRANSLATIONS@ before you use any
37 After that you can use the variable $(toBe) in your file.
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.
48 if (! $ARGV[0] || ! -f
$ARGV[0])
54 $translations = $ARGV[0];
59 if (! $ARGV[0] || ! -f
$ARGV[0])
65 $installer = $ARGV[0];
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"]
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";
174 # Create a hash of the keys to translate
175 open (MYFILE
, $translations);
178 if ($_ =~ /^Encoding=UTF-8/ || $_ =~ /^\s*$/ || $_ =~ /^\[Desktop Entry\]/ || $_ =~ /^#/)
182 elsif ($_ =~ /^(\w+)=(.*)/)
187 $value =~ s/["]/\$\\"/g;
188 $result{"$lang"}{"$key"} = "!define $key \"$value\"\n";
190 elsif ($_ =~ /^(\w+)\[([\w@]+)\]=(.*)/)
195 $value =~ s/["]/\$\\"/g;
196 $result{"$lang"}{"$key"} = "!define $key \"$value\"\n";
200 print "Found unrecognized line: '$_'\n";
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"
213 ;; English goes first because its the default. The rest are
214 ;; in alphabetical order (at least the strings actually displayed
216 !insertmacro MUI_LANGUAGE "English"
219 # The specific GCompris translation for the installer
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 ;--------------------------------
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
237 !define "${LABEL}" "${VALUE}"
238 !ifdef INSERT_DEFAULT
239 !warning "${LANG} lang file missing ${LABEL}, using default..."
244 !macro GCOMPRIS_MACRO_LANGSTRING_INSERT LABEL LANG
245 LangString "${LABEL}" "${LANG_${LANG}}" "${${LABEL}}"
249 !macro GCOMPRIS_MACRO_LANGUAGEFILE_BEGIN LANG
250 !define CUR_LANG "${LANG}"
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 .= '
276 $gcomprisLanguages .= '
277 !macro GCOMPRIS_MACRO_INCLUDE_LANGFILE LANG FILE
278 !insertmacro GCOMPRIS_MACRO_LANGUAGEFILE_BEGIN "${LANG}"
280 !insertmacro GCOMPRIS_MACRO_LANGUAGEFILE_END
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 /;
298 $gcomprisLanguages .= " !insertmacro GCOMPRIS_MACRO_INCLUDE_LANGFILE".
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}}
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)) {
332 foreach my $keyLocale (keys(%$text_locale)) {
333 # Fine, we found a translation
334 if ( $keyLocale eq $keyEn )
336 print DESC
"$result{$lang}{$keyLocale}";
342 # English keys are the reference.
343 # If not found they are inserted
346 # print DESC "$result{'en'}{$keyEn}";
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";
360 print "Ignoring language $lang because it is less than 50% translated ($found_key_count of $total_key_count).\n";
366 system("iconv -f UTF-8 -t $localeNames{$lang}[1] $tmp_dir/$lang.nsh.utf8 > $tmp_dir/$lang.nsh");
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 .= '
380 # We have all the data, let's replace it
381 my $gcomprisInstaller;
382 open (MYFILE
, $installer);
384 if ($_ =~ /\@INSERT_TRANSLATIONS\@/)
386 print "Processing \@INSERT_TRANSLATIONS\@\n";
387 $gcomprisInstaller .= $muiLanguages;
388 $gcomprisInstaller .= $gcomprisLanguages;
389 $gcomprisInstaller .= $selectTranslationFunction;
393 $gcomprisInstaller .= "$_";
398 # Rewrite the file with the replaced data
399 open (MYFILE
, ">$installer");
400 print MYFILE
"$gcomprisInstaller";