dmsynth: Add error handling to the synth GetPortCaps method.
[wine.git] / tools / wrc / po.c
blobcc4d8624182f1f2d577ca9b4b22d12c40a1b8727
1 /*
2 * Support for po files
4 * Copyright 2010 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #ifdef HAVE_LIBGETTEXTPO
30 #include <gettext-po.h>
31 #endif
33 #include "../tools.h"
34 #include "wrc.h"
35 #include "newstruc.h"
36 #include "utils.h"
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "wine/list.h"
43 static resource_t *new_top, *new_tail;
45 struct mo_file
47 unsigned int magic;
48 unsigned int revision;
49 unsigned int count;
50 unsigned int msgid_off;
51 unsigned int msgstr_off;
52 /* ... rest of file data here */
55 static int is_english( const language_t *lan )
57 return lan->id == LANG_ENGLISH && lan->sub == SUBLANG_DEFAULT;
60 static int is_rtl_language( const language_t *lan )
62 return lan->id == LANG_ARABIC || lan->id == LANG_HEBREW || lan->id == LANG_PERSIAN;
65 static int uses_larger_font( const language_t *lan )
67 return lan->id == LANG_CHINESE || lan->id == LANG_JAPANESE || lan->id == LANG_KOREAN;
70 static int get_default_sublang( const language_t *lan )
72 if (lan->sub != SUBLANG_NEUTRAL)
73 return lan->sub;
75 switch (lan->id)
77 case LANG_SPANISH:
78 return SUBLANG_SPANISH_MODERN;
79 case LANG_CHINESE:
80 return SUBLANG_CHINESE_SIMPLIFIED;
81 default:
82 return SUBLANG_DEFAULT;
86 static version_t *get_dup_version( language_t *lang )
88 /* English "translations" take precedence over the original rc contents */
89 return new_version( is_english( lang ) ? 1 : -1 );
92 static name_id_t *dup_name_id( name_id_t *id )
94 name_id_t *new;
96 if (!id || id->type != name_str) return id;
97 new = new_name_id();
98 *new = *id;
99 new->name.s_name = convert_string_unicode( id->name.s_name, 1252 );
100 return new;
103 static char *convert_msgid_ascii( const string_t *str, int error_on_invalid_char )
105 int i;
106 char *buffer = xmalloc( str->size + 1 );
108 for (i = 0; i < str->size; i++)
110 WCHAR ch = (str->type == str_unicode ? str->str.wstr[i] : (unsigned char)str->str.cstr[i]);
111 buffer[i] = ch;
112 if (ch >= 32 && ch <= 127) continue;
113 if (ch == '\t' || ch == '\n') continue;
114 if (error_on_invalid_char)
116 print_location( &str->loc );
117 error( "Invalid character %04x in source string\n", ch );
119 free( buffer);
120 return NULL;
122 buffer[i] = 0;
123 return buffer;
126 static char *get_message_context( char **msgid )
128 static const char magic[] = "#msgctxt#";
129 char *id, *context;
131 if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
132 context = *msgid + sizeof(magic) - 1;
133 if (!(id = strchr( context, '#' ))) return NULL;
134 *id = 0;
135 *msgid = id + 1;
136 return context;
139 static int control_has_title( const control_t *ctrl )
141 if (!ctrl->title) return FALSE;
142 if (ctrl->title->type != name_str) return FALSE;
143 /* check for text static control */
144 if (ctrl->ctlclass && ctrl->ctlclass->type == name_ord && ctrl->ctlclass->name.i_name == CT_STATIC)
146 switch (ctrl->style->or_mask & SS_TYPEMASK)
148 case SS_LEFT:
149 case SS_CENTER:
150 case SS_RIGHT:
151 return TRUE;
152 default:
153 return FALSE;
156 return TRUE;
159 static resource_t *dup_resource( resource_t *res, language_t *lang )
161 resource_t *new = xmalloc( sizeof(*new) );
163 *new = *res;
164 new->lan = lang;
165 new->next = new->prev = NULL;
166 new->name = dup_name_id( res->name );
168 switch (res->type)
170 case res_acc:
171 new->res.acc = xmalloc( sizeof(*(new)->res.acc) );
172 *new->res.acc = *res->res.acc;
173 new->res.acc->lvc.language = lang;
174 new->res.acc->lvc.version = get_dup_version( lang );
175 break;
176 case res_dlg:
177 new->res.dlg = xmalloc( sizeof(*(new)->res.dlg) );
178 *new->res.dlg = *res->res.dlg;
179 new->res.dlg->lvc.language = lang;
180 new->res.dlg->lvc.version = get_dup_version( lang );
181 break;
182 case res_men:
183 new->res.men = xmalloc( sizeof(*(new)->res.men) );
184 *new->res.men = *res->res.men;
185 new->res.men->lvc.language = lang;
186 new->res.men->lvc.version = get_dup_version( lang );
187 break;
188 case res_stt:
189 new->res.stt = xmalloc( sizeof(*(new)->res.stt) );
190 *new->res.stt = *res->res.stt;
191 new->res.stt->lvc.language = lang;
192 new->res.stt->lvc.version = get_dup_version( lang );
193 break;
194 case res_ver:
195 new->res.ver = xmalloc( sizeof(*(new)->res.ver) );
196 *new->res.ver = *res->res.ver;
197 new->res.ver->lvc.language = lang;
198 new->res.ver->lvc.version = get_dup_version( lang );
199 break;
200 default:
201 assert(0);
203 return new;
206 static const struct
208 unsigned int id, sub;
209 const char *name;
210 } languages[] =
212 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, "af" },
213 { LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA, "af_ZA" },
214 { LANG_ALBANIAN, SUBLANG_NEUTRAL, "sq" },
215 { LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA, "sq_AL" },
216 { LANG_AMHARIC, SUBLANG_NEUTRAL, "am" },
217 { LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA, "am_ET" },
218 { LANG_ARABIC, SUBLANG_NEUTRAL, "ar" },
219 { LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA, "ar_SA" },
220 { LANG_ARABIC, SUBLANG_ARABIC_IRAQ, "ar_IQ" },
221 { LANG_ARABIC, SUBLANG_ARABIC_EGYPT, "ar_EG" },
222 { LANG_ARABIC, SUBLANG_ARABIC_LIBYA, "ar_LY" },
223 { LANG_ARABIC, SUBLANG_ARABIC_ALGERIA, "ar_DZ" },
224 { LANG_ARABIC, SUBLANG_ARABIC_MOROCCO, "ar_MA" },
225 { LANG_ARABIC, SUBLANG_ARABIC_TUNISIA, "ar_TN" },
226 { LANG_ARABIC, SUBLANG_ARABIC_OMAN, "ar_OM" },
227 { LANG_ARABIC, SUBLANG_ARABIC_YEMEN, "ar_YE" },
228 { LANG_ARABIC, SUBLANG_ARABIC_SYRIA, "ar_SY" },
229 { LANG_ARABIC, SUBLANG_ARABIC_JORDAN, "ar_JO" },
230 { LANG_ARABIC, SUBLANG_ARABIC_LEBANON, "ar_LB" },
231 { LANG_ARABIC, SUBLANG_ARABIC_KUWAIT, "ar_KW" },
232 { LANG_ARABIC, SUBLANG_ARABIC_UAE, "ar_AE" },
233 { LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN, "ar_BH" },
234 { LANG_ARABIC, SUBLANG_ARABIC_QATAR, "ar_QA" },
235 { LANG_ARMENIAN, SUBLANG_NEUTRAL, "hy" },
236 { LANG_ARMENIAN, SUBLANG_ARMENIAN_ARMENIA, "hy_AM" },
237 { LANG_ASSAMESE, SUBLANG_NEUTRAL, "as" },
238 { LANG_ASSAMESE, SUBLANG_ASSAMESE_INDIA, "as_IN" },
239 { LANG_ASTURIAN, SUBLANG_NEUTRAL, "ast" },
240 { LANG_ASTURIAN, SUBLANG_DEFAULT, "ast_ES" },
241 { LANG_AZERBAIJANI, SUBLANG_NEUTRAL, "az" },
242 { LANG_AZERBAIJANI, SUBLANG_AZERBAIJANI_AZERBAIJAN_LATIN,"az_AZ@latin" },
243 { LANG_AZERBAIJANI, SUBLANG_AZERBAIJANI_AZERBAIJAN_CYRILLIC, "az_AZ@cyrillic" },
244 { LANG_BASQUE, SUBLANG_NEUTRAL, "eu" },
245 { LANG_BASQUE, SUBLANG_BASQUE_BASQUE, "eu_ES" },
246 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, "be" },
247 { LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS, "be_BY" },
248 { LANG_BENGALI, SUBLANG_NEUTRAL, "bn" },
249 { LANG_BENGALI, SUBLANG_BENGALI_INDIA, "bn_IN" },
250 { LANG_BENGALI, SUBLANG_BENGALI_BANGLADESH, "bn_BD" },
251 { LANG_BRETON, SUBLANG_NEUTRAL, "br" },
252 { LANG_BRETON, SUBLANG_BRETON_FRANCE, "br_FR" },
253 { LANG_BULGARIAN, SUBLANG_NEUTRAL, "bg" },
254 { LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA, "bg_BG" },
255 { LANG_CATALAN, SUBLANG_NEUTRAL, "ca" },
256 { LANG_CATALAN, SUBLANG_CATALAN_CATALAN, "ca_ES" },
257 { LANG_CHINESE, SUBLANG_NEUTRAL, "zh" },
258 { LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL, "zh_TW" },
259 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, "zh_CN" },
260 { LANG_CHINESE, SUBLANG_CHINESE_HONGKONG, "zh_HK" },
261 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, "zh_SG" },
262 { LANG_CHINESE, SUBLANG_CHINESE_MACAU, "zh_MO" },
263 { LANG_CZECH, SUBLANG_NEUTRAL, "cs" },
264 { LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC, "cs_CZ" },
265 { LANG_DANISH, SUBLANG_NEUTRAL, "da" },
266 { LANG_DANISH, SUBLANG_DANISH_DENMARK, "da_DK" },
267 { LANG_DIVEHI, SUBLANG_NEUTRAL, "dv" },
268 { LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES, "dv_MV" },
269 { LANG_DUTCH, SUBLANG_NEUTRAL, "nl" },
270 { LANG_DUTCH, SUBLANG_DUTCH, "nl_NL" },
271 { LANG_DUTCH, SUBLANG_DUTCH_BELGIAN, "nl_BE" },
272 { LANG_DUTCH, SUBLANG_DUTCH_SURINAM, "nl_SR" },
273 { LANG_ENGLISH, SUBLANG_NEUTRAL, "en" },
274 { LANG_ENGLISH, SUBLANG_ENGLISH_US, "en_US" },
275 { LANG_ENGLISH, SUBLANG_ENGLISH_UK, "en_GB" },
276 { LANG_ENGLISH, SUBLANG_ENGLISH_AUS, "en_AU" },
277 { LANG_ENGLISH, SUBLANG_ENGLISH_CAN, "en_CA" },
278 { LANG_ENGLISH, SUBLANG_ENGLISH_NZ, "en_NZ" },
279 { LANG_ENGLISH, SUBLANG_ENGLISH_EIRE, "en_IE" },
280 { LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA, "en_ZA" },
281 { LANG_ENGLISH, SUBLANG_ENGLISH_JAMAICA, "en_JM" },
282 { LANG_ENGLISH, SUBLANG_ENGLISH_CARIBBEAN, "en_CB" },
283 { LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE, "en_BZ" },
284 { LANG_ENGLISH, SUBLANG_ENGLISH_TRINIDAD, "en_TT" },
285 { LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE, "en_ZW" },
286 { LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES, "en_PH" },
287 { LANG_ENGLISH, SUBLANG_ENGLISH_INDIA, "en_IN" },
288 { LANG_ENGLISH, SUBLANG_ENGLISH_MALAYSIA, "en_MY" },
289 { LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE, "en_SG" },
290 { LANG_ESTONIAN, SUBLANG_NEUTRAL, "et" },
291 { LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA, "et_EE" },
292 { LANG_FAEROESE, SUBLANG_NEUTRAL, "fo" },
293 { LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS, "fo_FO" },
294 { LANG_FILIPINO, SUBLANG_NEUTRAL, "fil" },
295 { LANG_FILIPINO, SUBLANG_FILIPINO_PHILIPPINES, "fil_PH" },
296 { LANG_FINNISH, SUBLANG_NEUTRAL, "fi" },
297 { LANG_FINNISH, SUBLANG_FINNISH_FINLAND, "fi_FI" },
298 { LANG_FRENCH, SUBLANG_NEUTRAL, "fr" },
299 { LANG_FRENCH, SUBLANG_FRENCH, "fr_FR" },
300 { LANG_FRENCH, SUBLANG_FRENCH_BELGIAN, "fr_BE" },
301 { LANG_FRENCH, SUBLANG_FRENCH_CANADIAN, "fr_CA" },
302 { LANG_FRENCH, SUBLANG_FRENCH_SWISS, "fr_CH" },
303 { LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG, "fr_LU" },
304 { LANG_FRENCH, SUBLANG_FRENCH_MONACO, "fr_MC" },
305 { LANG_GALICIAN, SUBLANG_NEUTRAL, "gl" },
306 { LANG_GALICIAN, SUBLANG_GALICIAN_GALICIAN, "gl_ES" },
307 { LANG_GEORGIAN, SUBLANG_NEUTRAL, "ka" },
308 { LANG_GEORGIAN, SUBLANG_GEORGIAN_GEORGIA, "ka_GE" },
309 { LANG_GERMAN, SUBLANG_NEUTRAL, "de" },
310 { LANG_GERMAN, SUBLANG_GERMAN, "de_DE" },
311 { LANG_GERMAN, SUBLANG_GERMAN_SWISS, "de_CH" },
312 { LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN, "de_AT" },
313 { LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG, "de_LU" },
314 { LANG_GERMAN, SUBLANG_GERMAN_LIECHTENSTEIN, "de_LI" },
315 { LANG_GREEK, SUBLANG_NEUTRAL, "el" },
316 { LANG_GREEK, SUBLANG_GREEK_GREECE, "el_GR" },
317 { LANG_GUJARATI, SUBLANG_NEUTRAL, "gu" },
318 { LANG_GUJARATI, SUBLANG_GUJARATI_INDIA, "gu_IN" },
319 { LANG_HAUSA, SUBLANG_NEUTRAL, "ha" },
320 { LANG_HAUSA, SUBLANG_HAUSA_NIGERIA, "ha_NG" },
321 { LANG_HAWAIIAN, SUBLANG_NEUTRAL, "haw" },
322 { LANG_HAWAIIAN, SUBLANG_HAWAIIAN_US, "haw_US" },
323 { LANG_HEBREW, SUBLANG_NEUTRAL, "he" },
324 { LANG_HEBREW, SUBLANG_HEBREW_ISRAEL, "he_IL" },
325 { LANG_HINDI, SUBLANG_NEUTRAL, "hi" },
326 { LANG_HINDI, SUBLANG_HINDI_INDIA, "hi_IN" },
327 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, "hu" },
328 { LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY, "hu_HU" },
329 { LANG_ICELANDIC, SUBLANG_NEUTRAL, "is" },
330 { LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND, "is_IS" },
331 { LANG_IGBO, SUBLANG_NEUTRAL, "ig" },
332 { LANG_IGBO, SUBLANG_IGBO_NIGERIA, "ig_NG" },
333 { LANG_INDONESIAN, SUBLANG_NEUTRAL, "id" },
334 { LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA, "id_ID" },
335 { LANG_INUKTITUT, SUBLANG_NEUTRAL, "iu" },
336 { LANG_INUKTITUT, SUBLANG_INUKTITUT_CANADA, "iu_CA" },
337 { LANG_IRISH, SUBLANG_NEUTRAL, "ga" },
338 { LANG_IRISH, SUBLANG_IRISH_IRELAND, "ga_IE" },
339 { LANG_ITALIAN, SUBLANG_NEUTRAL, "it" },
340 { LANG_ITALIAN, SUBLANG_ITALIAN, "it_IT" },
341 { LANG_ITALIAN, SUBLANG_ITALIAN_SWISS, "it_CH" },
342 { LANG_JAPANESE, SUBLANG_NEUTRAL, "ja" },
343 { LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN, "ja_JP" },
344 { LANG_KANNADA, SUBLANG_NEUTRAL, "kn" },
345 { LANG_KANNADA, SUBLANG_KANNADA_INDIA, "kn_IN" },
346 { LANG_KAZAK, SUBLANG_NEUTRAL, "kk" },
347 { LANG_KAZAK, SUBLANG_KAZAK_KAZAKHSTAN, "kk_KZ" },
348 { LANG_KHMER, SUBLANG_NEUTRAL, "km" },
349 { LANG_KHMER, SUBLANG_KHMER_CAMBODIA, "km_KH" },
350 { LANG_KINYARWANDA, SUBLANG_NEUTRAL, "rw" },
351 { LANG_KINYARWANDA, SUBLANG_KINYARWANDA_RWANDA, "rw_RW" },
352 { LANG_KONKANI, SUBLANG_NEUTRAL, "kok" },
353 { LANG_KONKANI, SUBLANG_KONKANI_INDIA, "kok_IN" },
354 { LANG_KOREAN, SUBLANG_NEUTRAL, "ko" },
355 { LANG_KOREAN, SUBLANG_KOREAN, "ko_KR" },
356 { LANG_KYRGYZ, SUBLANG_NEUTRAL, "ky" },
357 { LANG_KYRGYZ, SUBLANG_KYRGYZ_KYRGYZSTAN, "ky_KG" },
358 { LANG_LAO, SUBLANG_NEUTRAL, "lo" },
359 { LANG_LAO, SUBLANG_LAO_LAO, "lo_LA" },
360 { LANG_LATVIAN, SUBLANG_NEUTRAL, "lv" },
361 { LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA, "lv_LV" },
362 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, "lt" },
363 { LANG_LITHUANIAN, SUBLANG_LITHUANIAN, "lt_LT" },
364 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, "mk" },
365 { LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA, "mk_MK" },
366 { LANG_MALAY, SUBLANG_NEUTRAL, "ms" },
367 { LANG_MALAY, SUBLANG_MALAY_MALAYSIA, "ms_MY" },
368 { LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM, "ms_BN" },
369 { LANG_MALAYALAM, SUBLANG_NEUTRAL, "ml" },
370 { LANG_MALAYALAM, SUBLANG_MALAYALAM_INDIA, "ml_IN" },
371 { LANG_MALTESE, SUBLANG_NEUTRAL, "mt" },
372 { LANG_MALTESE, SUBLANG_MALTESE_MALTA, "mt_MT" },
373 { LANG_MARATHI, SUBLANG_NEUTRAL, "mr" },
374 { LANG_MARATHI, SUBLANG_MARATHI_INDIA, "mr_IN" },
375 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, "mn" },
376 { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
377 { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC, "mn_CN" },
378 { LANG_NEPALI, SUBLANG_NEUTRAL, "ne" },
379 { LANG_NEPALI, SUBLANG_NEPALI_NEPAL, "ne_NP" },
380 { LANG_NEPALI, SUBLANG_NEPALI_INDIA, "ne_IN" },
381 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL, "nb_NO" },
382 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK, "nn_NO" },
383 { LANG_ODIA, SUBLANG_NEUTRAL, "or" },
384 { LANG_ODIA, SUBLANG_ODIA_INDIA, "or_IN" },
385 { LANG_PASHTO, SUBLANG_NEUTRAL, "ps" },
386 { LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN, "ps_AF" },
387 { LANG_PERSIAN, SUBLANG_NEUTRAL, "fa" },
388 { LANG_PERSIAN, SUBLANG_PERSIAN_IRAN, "fa_IR" },
389 { LANG_POLISH, SUBLANG_NEUTRAL, "pl" },
390 { LANG_POLISH, SUBLANG_POLISH_POLAND, "pl_PL" },
391 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, "pt" },
392 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN, "pt_BR" },
393 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_PORTUGAL, "pt_PT" },
394 { LANG_PUNJABI, SUBLANG_NEUTRAL, "pa" },
395 { LANG_PUNJABI, SUBLANG_PUNJABI_INDIA, "pa_IN" },
396 { LANG_PUNJABI, SUBLANG_PUNJABI_PAKISTAN, "pa_PK" },
397 { LANG_ROMANIAN, SUBLANG_NEUTRAL, "ro" },
398 { LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA, "ro_RO" },
399 { LANG_ROMANSH, SUBLANG_NEUTRAL, "rm" },
400 { LANG_ROMANSH, SUBLANG_ROMANSH_SWITZERLAND, "rm_CH" },
401 { LANG_RUSSIAN, SUBLANG_NEUTRAL, "ru" },
402 { LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA, "ru_RU" },
403 { LANG_SAMI, SUBLANG_NEUTRAL, "se" },
404 { LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY, "se_NO" },
405 { LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN, "se_SE" },
406 { LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND, "se_FI" },
407 { LANG_SANSKRIT, SUBLANG_NEUTRAL, "sa" },
408 { LANG_SANSKRIT, SUBLANG_SANSKRIT_INDIA, "sa_IN" },
409 { LANG_SCOTTISH_GAELIC,SUBLANG_NEUTRAL, "gd" },
410 { LANG_SCOTTISH_GAELIC,SUBLANG_SCOTTISH_GAELIC, "gd_GB" },
411 /* LANG_SERBIAN/LANG_CROATIAN/LANG_BOSNIAN are the same */
412 { LANG_SERBIAN, SUBLANG_NEUTRAL, "hr" },
413 { LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA, "hr_HR" },
414 { LANG_SERBIAN, SUBLANG_SERBIAN_LATIN, "sr_RS@latin" },
415 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, "sr_RS@cyrillic" },
416 { LANG_SERBIAN, SUBLANG_CROATIAN_BOSNIA_HERZEGOVINA_LATIN, "hr_BA@latin" },
417 { LANG_SERBIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN, "bs_BA@latin" },
418 { LANG_SERBIAN, SUBLANG_SERBIAN_BOSNIA_HERZEGOVINA_LATIN, "sr_BA@latin" },
419 { LANG_SERBIAN, SUBLANG_SERBIAN_BOSNIA_HERZEGOVINA_CYRILLIC, "sr_BA@cyrillic" },
420 { LANG_SERBIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC, "bs_BA@cyrillic" },
421 { LANG_SERBIAN, SUBLANG_SERBIAN_SERBIA_LATIN, "sr_RS@latin" },
422 { LANG_SERBIAN, SUBLANG_SERBIAN_SERBIA_CYRILLIC, "sr_RS@cyrillic" },
423 { LANG_SERBIAN, SUBLANG_SERBIAN_MONTENEGRO_LATIN, "sr_ME@latin" },
424 { LANG_SERBIAN, SUBLANG_SERBIAN_MONTENEGRO_CYRILLIC, "sr_ME@cyrillic" },
425 { LANG_SINHALESE, SUBLANG_NEUTRAL, "si" },
426 { LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA, "si_LK" },
427 { LANG_SLOVAK, SUBLANG_NEUTRAL, "sk" },
428 { LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA, "sk_SK" },
429 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, "sl" },
430 { LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA, "sl_SI" },
431 { LANG_SOTHO, SUBLANG_NEUTRAL, "nso" },
432 { LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA, "nso_ZA" },
433 { LANG_SPANISH, SUBLANG_NEUTRAL, "es" },
434 { LANG_SPANISH, SUBLANG_SPANISH, "es_ES" },
435 { LANG_SPANISH, SUBLANG_SPANISH_MEXICAN, "es_MX" },
436 { LANG_SPANISH, SUBLANG_SPANISH_MODERN, "es_ES_modern" },
437 { LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA, "es_GT" },
438 { LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA, "es_CR" },
439 { LANG_SPANISH, SUBLANG_SPANISH_PANAMA, "es_PA" },
440 { LANG_SPANISH, SUBLANG_SPANISH_DOMINICAN_REPUBLIC, "es_DO" },
441 { LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA, "es_VE" },
442 { LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA, "es_CO" },
443 { LANG_SPANISH, SUBLANG_SPANISH_PERU, "es_PE" },
444 { LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA, "es_AR" },
445 { LANG_SPANISH, SUBLANG_SPANISH_ECUADOR, "es_EC" },
446 { LANG_SPANISH, SUBLANG_SPANISH_CHILE, "es_CL" },
447 { LANG_SPANISH, SUBLANG_SPANISH_URUGUAY, "es_UY" },
448 { LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY, "es_PY" },
449 { LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA, "es_BO" },
450 { LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR, "es_SV" },
451 { LANG_SPANISH, SUBLANG_SPANISH_HONDURAS, "es_HN" },
452 { LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA, "es_NI" },
453 { LANG_SPANISH, SUBLANG_SPANISH_PUERTO_RICO, "es_PR" },
454 { LANG_SPANISH, SUBLANG_SPANISH_US, "es_US" },
455 { LANG_SWAHILI, SUBLANG_NEUTRAL, "sw" },
456 { LANG_SWAHILI, SUBLANG_SWAHILI_KENYA, "sw_KE" },
457 { LANG_SWEDISH, SUBLANG_NEUTRAL, "sv" },
458 { LANG_SWEDISH, SUBLANG_SWEDISH_SWEDEN, "sv_SE" },
459 { LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND, "sv_FI" },
460 { LANG_SYRIAC, SUBLANG_NEUTRAL, "syr" },
461 { LANG_SYRIAC, SUBLANG_SYRIAC_SYRIA, "syr_SY" },
462 { LANG_TAJIK, SUBLANG_NEUTRAL, "tg" },
463 { LANG_TAJIK, SUBLANG_TAJIK_TAJIKISTAN, "tg_TJ" },
464 { LANG_TAMIL, SUBLANG_NEUTRAL, "ta" },
465 { LANG_TAMIL, SUBLANG_TAMIL_INDIA, "ta_IN" },
466 { LANG_TATAR, SUBLANG_NEUTRAL, "tt" },
467 { LANG_TATAR, SUBLANG_TATAR_RUSSIA, "tt_TA" },
468 { LANG_TELUGU, SUBLANG_NEUTRAL, "te" },
469 { LANG_TELUGU, SUBLANG_TELUGU_INDIA, "te_IN" },
470 { LANG_THAI, SUBLANG_NEUTRAL, "th" },
471 { LANG_THAI, SUBLANG_THAI_THAILAND, "th_TH" },
472 { LANG_TIGRINYA, SUBLANG_NEUTRAL, "ti" },
473 { LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA, "ti_ET" },
474 { LANG_TIGRINYA, SUBLANG_TIGRINYA_ERITREA, "ti_ER" },
475 { LANG_TSWANA, SUBLANG_NEUTRAL, "tn" },
476 { LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA, "tn_ZA" },
477 { LANG_TURKISH, SUBLANG_NEUTRAL, "tr" },
478 { LANG_TURKISH, SUBLANG_TURKISH_TURKEY, "tr_TR" },
479 { LANG_UIGHUR, SUBLANG_NEUTRAL, "ug" },
480 { LANG_UIGHUR, SUBLANG_UIGHUR_PRC, "ug_CN" },
481 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, "uk" },
482 { LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE, "uk_UA" },
483 { LANG_URDU, SUBLANG_NEUTRAL, "ur" },
484 { LANG_URDU, SUBLANG_URDU_PAKISTAN, "ur_PK" },
485 { LANG_URDU, SUBLANG_URDU_INDIA, "ur_IN" },
486 { LANG_UZBEK, SUBLANG_NEUTRAL, "uz" },
487 { LANG_UZBEK, SUBLANG_UZBEK_LATIN, "uz_UZ@latin" },
488 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, "uz_UZ@cyrillic" },
489 { LANG_VIETNAMESE, SUBLANG_NEUTRAL, "vi" },
490 { LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM, "vi_VN" },
491 { LANG_WELSH, SUBLANG_NEUTRAL, "cy" },
492 { LANG_WELSH, SUBLANG_WELSH_UNITED_KINGDOM, "cy_GB" },
493 { LANG_WOLOF, SUBLANG_NEUTRAL, "wo" },
494 { LANG_WOLOF, SUBLANG_WOLOF_SENEGAL, "wo_SN" },
495 { LANG_XHOSA, SUBLANG_NEUTRAL, "xh" },
496 { LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA, "xh_ZA" },
497 { LANG_YORUBA, SUBLANG_NEUTRAL, "yo" },
498 { LANG_YORUBA, SUBLANG_YORUBA_NIGERIA, "yo_NG" },
499 { LANG_ZULU, SUBLANG_NEUTRAL, "zu" },
500 { LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA, "zu_ZA" },
502 #ifdef LANG_ESPERANTO
503 { LANG_ESPERANTO, SUBLANG_DEFAULT, "eo" },
504 #endif
505 #ifdef LANG_WALON
506 { LANG_WALON, SUBLANG_NEUTRAL, "wa" },
507 { LANG_WALON, SUBLANG_DEFAULT, "wa_BE" },
508 #endif
509 #ifdef LANG_CORNISH
510 { LANG_CORNISH, SUBLANG_NEUTRAL, "kw" },
511 { LANG_CORNISH, SUBLANG_DEFAULT, "kw_GB" },
512 #endif
513 #ifdef LANG_MANX_GAELIC
514 { LANG_MANX_GAELIC, SUBLANG_MANX_GAELIC, "gv_GB" },
515 #endif
518 #ifndef HAVE_LIBGETTEXTPO
520 void write_pot_file( const char *outname )
522 error( "PO files not supported in this wrc build\n" );
525 void write_po_files( const char *outname )
527 error( "PO files not supported in this wrc build\n" );
530 #else /* HAVE_LIBGETTEXTPO */
532 static void po_xerror( int severity, po_message_t message,
533 const char *filename, size_t lineno, size_t column,
534 int multiline_p, const char *message_text )
536 fprintf( stderr, "%s:%u:%u: %s\n",
537 filename, (unsigned int)lineno, (unsigned int)column, message_text );
538 if (severity) exit(1);
541 static void po_xerror2( int severity, po_message_t message1,
542 const char *filename1, size_t lineno1, size_t column1,
543 int multiline_p1, const char *message_text1,
544 po_message_t message2,
545 const char *filename2, size_t lineno2, size_t column2,
546 int multiline_p2, const char *message_text2 )
548 fprintf( stderr, "%s:%u:%u: %s\n",
549 filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
550 fprintf( stderr, "%s:%u:%u: %s\n",
551 filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
552 if (severity) exit(1);
555 static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
557 static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
558 po_message_iterator_t *iterator )
560 po_message_t msg;
561 const char *context;
563 *iterator = po_message_iterator( po, NULL );
564 while ((msg = po_next_message( *iterator )))
566 if (strcmp( po_message_msgid( msg ), msgid )) continue;
567 if (!msgctxt) break;
568 if (!(context = po_message_msgctxt( msg ))) continue;
569 if (!strcmp( context, msgctxt )) break;
571 return msg;
574 static void add_po_string( po_file_t po, const string_t *msgid, const string_t *msgstr,
575 const language_t *lang )
577 static const char dnt[] = "do not translate";
578 po_message_t msg;
579 po_message_iterator_t iterator;
580 int codepage;
581 char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
583 if (!msgid->size) return;
585 id_buffer = id = convert_msgid_ascii( msgid, 1 );
586 context = get_message_context( &id );
587 if (context && strcmp(context, dnt) == 0)
589 /* This string should not be translated */
590 free( id_buffer );
591 return;
594 if (msgstr)
596 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
597 else codepage = get_language_codepage( 0, 0 );
598 assert( codepage != -1 );
599 str = str_buffer = convert_string_utf8( msgstr, codepage );
600 if (is_english( lang )) get_message_context( &str );
602 if (!(msg = find_message( po, id, context, &iterator )))
604 msg = po_message_create();
605 po_message_set_msgid( msg, id );
606 po_message_set_msgstr( msg, str ? str : "" );
607 if (context) po_message_set_msgctxt( msg, context );
608 po_message_insert( iterator, msg );
610 if (msgid->loc.file) po_message_add_filepos( msg, msgid->loc.file, msgid->loc.line );
611 po_message_iterator_free( iterator );
612 free( id_buffer );
613 free( str_buffer );
616 struct po_file_lang
618 struct list entry;
619 language_t lang;
620 po_file_t po;
623 static struct list po_file_langs = LIST_INIT( po_file_langs );
625 static po_file_t create_po_file(void)
627 po_file_t po;
628 po_message_t msg;
629 po_message_iterator_t iterator;
631 po = po_file_create();
632 iterator = po_message_iterator( po, NULL );
633 msg = po_message_create();
634 po_message_set_msgid( msg, "" );
635 po_message_set_msgstr( msg,
636 "Project-Id-Version: Wine\n"
637 "Report-Msgid-Bugs-To: https://bugs.winehq.org\n"
638 "POT-Creation-Date: N/A\n"
639 "PO-Revision-Date: N/A\n"
640 "Last-Translator: Automatically generated\n"
641 "Language-Team: none\n"
642 "MIME-Version: 1.0\n"
643 "Content-Type: text/plain; charset=UTF-8\n"
644 "Content-Transfer-Encoding: 8bit\n" );
645 po_message_insert( iterator, msg );
646 po_message_iterator_free( iterator );
647 return po;
650 static po_file_t get_po_file( const language_t *lang )
652 struct po_file_lang *po_file;
654 LIST_FOR_EACH_ENTRY( po_file, &po_file_langs, struct po_file_lang, entry )
655 if (po_file->lang.id == lang->id && po_file->lang.sub == lang->sub) return po_file->po;
657 /* create a new one */
658 po_file = xmalloc( sizeof(*po_file) );
659 po_file->lang = *lang;
660 po_file->po = create_po_file();
661 list_add_tail( &po_file_langs, &po_file->entry );
662 return po_file->po;
665 static const char *get_language_name( const language_t *lang )
667 static char name[20];
668 unsigned int i;
670 for (i = 0; i < ARRAY_SIZE(languages); i++)
671 if (languages[i].id == lang->id && languages[i].sub == lang->sub)
672 return languages[i].name;
674 sprintf( name, "%02x-%02x", lang->id, lang->sub );
675 return name;
678 static char *get_po_file_name( const language_t *lang )
680 return strmake( "%s.po", get_language_name( lang ) );
683 static unsigned int flush_po_files( const char *output_name )
685 struct po_file_lang *po_file, *next;
686 unsigned int count = 0;
688 LIST_FOR_EACH_ENTRY_SAFE( po_file, next, &po_file_langs, struct po_file_lang, entry )
690 char *name = get_po_file_name( &po_file->lang );
691 if (output_name)
693 if (!strcmp( get_basename(output_name), name ))
695 po_file_write( po_file->po, name, &po_xerror_handler );
696 count++;
699 else /* no specified output name, output a file for every language found */
701 po_file_write( po_file->po, name, &po_xerror_handler );
702 count++;
703 fprintf( stderr, "created %s\n", name );
705 po_file_free( po_file->po );
706 list_remove( &po_file->entry );
707 free( po_file );
708 free( name );
710 return count;
713 static void add_pot_stringtable( po_file_t po, const resource_t *res )
715 const stringtable_t *stt = res->res.stt;
716 int i;
718 while (stt)
720 for (i = 0; i < stt->nentries; i++)
721 if (stt->entries[i].str) add_po_string( po, stt->entries[i].str, NULL, NULL );
722 stt = stt->next;
726 static void add_po_stringtable( const resource_t *english, const resource_t *res )
728 const stringtable_t *english_stt = english->res.stt;
729 const stringtable_t *stt = res->res.stt;
730 po_file_t po = get_po_file( stt->lvc.language );
731 int i;
733 while (english_stt && stt)
735 for (i = 0; i < stt->nentries; i++)
736 if (english_stt->entries[i].str && stt->entries[i].str)
737 add_po_string( po, english_stt->entries[i].str, stt->entries[i].str, stt->lvc.language );
738 stt = stt->next;
739 english_stt = english_stt->next;
743 static void add_pot_dialog_controls( po_file_t po, const control_t *ctrl )
745 while (ctrl)
747 if (control_has_title( ctrl )) add_po_string( po, ctrl->title->name.s_name, NULL, NULL );
748 ctrl = ctrl->next;
752 static void add_pot_dialog( po_file_t po, const resource_t *res )
754 const dialog_t *dlg = res->res.dlg;
756 if (dlg->title) add_po_string( po, dlg->title, NULL, NULL );
757 add_pot_dialog_controls( po, dlg->controls );
760 static void compare_dialogs( const dialog_t *english_dlg, const dialog_t *dlg )
762 const control_t *english_ctrl, *ctrl;
763 unsigned int style = 0, exstyle = 0, english_style = 0, english_exstyle = 0;
764 char *name;
765 char *title = english_dlg->title ? convert_msgid_ascii( english_dlg->title, 0 ) : xstrdup("??");
767 if (english_dlg->width != dlg->width || english_dlg->height != dlg->height)
768 warning( "%s: dialog %s doesn't have the same size (%d,%d vs %d,%d)\n",
769 get_language_name( dlg->lvc.language ), title, dlg->width, dlg->height,
770 english_dlg->width, english_dlg->height );
772 if (dlg->gotstyle) style = dlg->style->or_mask;
773 if (dlg->gotexstyle) exstyle = dlg->exstyle->or_mask;
774 if (english_dlg->gotstyle) english_style = english_dlg->style->or_mask;
775 if (english_dlg->gotexstyle) english_exstyle = english_dlg->exstyle->or_mask;
776 if (is_rtl_language( dlg->lvc.language )) english_exstyle |= WS_EX_LAYOUTRTL;
778 if (english_style != style)
779 warning( "%s: dialog %s doesn't have the same style (%08x vs %08x)\n",
780 get_language_name( dlg->lvc.language ), title, style, english_style );
781 if (english_exstyle != exstyle)
782 warning( "%s: dialog %s doesn't have the same exstyle (%08x vs %08x)\n",
783 get_language_name( dlg->lvc.language ), title, exstyle, english_exstyle );
785 if (english_dlg->font || dlg->font)
787 int size = 0, english_size = 0;
788 char *font = NULL, *english_font = NULL;
790 if (english_dlg->font)
792 english_font = convert_msgid_ascii( english_dlg->font->name, 0 );
793 english_size = english_dlg->font->size;
795 if (dlg->font)
797 font = convert_msgid_ascii( dlg->font->name, 0 );
798 size = dlg->font->size;
800 if (uses_larger_font( dlg->lvc.language )) english_size++;
802 if (!english_font || !font || strcasecmp( english_font, font ) || english_size != size)
803 warning( "%s: dialog %s doesn't have the same font (%s %u vs %s %u)\n",
804 get_language_name(dlg->lvc.language), title,
805 english_font ? english_font : "default", english_size,
806 font ? font : "default", size );
807 free( font );
808 free( english_font );
810 english_ctrl = english_dlg->controls;
811 ctrl = dlg->controls;
812 for ( ; english_ctrl && ctrl; ctrl = ctrl->next, english_ctrl = english_ctrl->next )
814 if (control_has_title( english_ctrl ))
815 name = convert_msgid_ascii( english_ctrl->title->name.s_name, 0 );
816 else
817 name = strmake( "%d", ctrl->id );
819 if (english_ctrl->width != ctrl->width || english_ctrl->height != ctrl->height)
820 warning( "%s: dialog %s control %s doesn't have the same size (%d,%d vs %d,%d)\n",
821 get_language_name( dlg->lvc.language ), title, name,
822 ctrl->width, ctrl->height, english_ctrl->width, english_ctrl->height );
823 if (english_ctrl->x != ctrl->x || english_ctrl->y != ctrl->y)
824 warning( "%s: dialog %s control %s doesn't have the same position (%d,%d vs %d,%d)\n",
825 get_language_name( dlg->lvc.language ), title, name,
826 ctrl->x, ctrl->y, english_ctrl->x, english_ctrl->y );
827 free( name );
829 free( title );
832 static void add_po_dialog_controls( po_file_t po, const control_t *english_ctrl,
833 const control_t *ctrl, const language_t *lang )
835 while (english_ctrl && ctrl)
837 if (control_has_title( english_ctrl ) && control_has_title( ctrl ))
838 add_po_string( po, english_ctrl->title->name.s_name, ctrl->title->name.s_name, lang );
840 ctrl = ctrl->next;
841 english_ctrl = english_ctrl->next;
845 static void add_po_dialog( const resource_t *english, const resource_t *res )
847 const dialog_t *english_dlg = english->res.dlg;
848 const dialog_t *dlg = res->res.dlg;
849 po_file_t po = get_po_file( dlg->lvc.language );
851 compare_dialogs( english_dlg, dlg );
853 if (english_dlg->title && dlg->title)
854 add_po_string( po, english_dlg->title, dlg->title, dlg->lvc.language );
855 add_po_dialog_controls( po, english_dlg->controls, dlg->controls, dlg->lvc.language );
858 static void add_pot_menu_items( po_file_t po, const menu_item_t *item )
860 while (item)
862 if (item->name) add_po_string( po, item->name, NULL, NULL );
863 if (item->popup) add_pot_menu_items( po, item->popup );
864 item = item->next;
868 static void add_pot_menu( po_file_t po, const resource_t *res )
870 add_pot_menu_items( po, res->res.men->items );
873 static void add_po_menu_items( po_file_t po, const menu_item_t *english_item,
874 const menu_item_t *item, const language_t *lang )
876 while (english_item && item)
878 if (english_item->name && item->name)
879 add_po_string( po, english_item->name, item->name, lang );
880 if (english_item->popup && item->popup)
881 add_po_menu_items( po, english_item->popup, item->popup, lang );
882 item = item->next;
883 english_item = english_item->next;
887 static void add_po_menu( const resource_t *english, const resource_t *res )
889 const menu_item_t *english_items = english->res.men->items;
890 const menu_item_t *items = res->res.men->items;
891 po_file_t po = get_po_file( res->res.men->lvc.language );
893 add_po_menu_items( po, english_items, items, res->res.men->lvc.language );
896 static int string_has_context( const string_t *str )
898 char *id, *id_buffer, *context;
900 id_buffer = id = convert_msgid_ascii( str, 1 );
901 context = get_message_context( &id );
902 free( id_buffer );
903 return context != NULL;
906 static void add_pot_accel( po_file_t po, const resource_t *res )
908 event_t *event = res->res.acc->events;
910 while (event)
912 /* accelerators without a context don't make sense in po files */
913 if (event->str && string_has_context( event->str ))
914 add_po_string( po, event->str, NULL, NULL );
915 event = event->next;
919 static void add_po_accel( const resource_t *english, const resource_t *res )
921 event_t *english_event = english->res.acc->events;
922 event_t *event = res->res.acc->events;
923 po_file_t po = get_po_file( res->res.acc->lvc.language );
925 while (english_event && event)
927 if (english_event->str && event->str && string_has_context( english_event->str ))
928 add_po_string( po, english_event->str, event->str, res->res.acc->lvc.language );
929 event = event->next;
930 english_event = english_event->next;
934 static ver_block_t *get_version_langcharset_block( ver_block_t *block )
936 ver_block_t *stringfileinfo = NULL;
937 char *translation = NULL;
938 ver_value_t *val;
940 for (; block; block = block->next)
942 char *name;
943 name = convert_msgid_ascii( block->name, 0 );
944 if (!strcasecmp( name, "stringfileinfo" ))
945 stringfileinfo = block;
946 else if (!strcasecmp( name, "varfileinfo" ))
948 for (val = block->values; val; val = val->next)
950 char *key = convert_msgid_ascii( val->key, 0 );
951 if (val->type == val_words &&
952 !strcasecmp( key, "Translation" ) &&
953 val->value.words->nwords >= 2)
954 translation = strmake( "%04x%04x",
955 val->value.words->words[0],
956 val->value.words->words[1] );
957 free( key );
960 free( name );
963 if (!stringfileinfo || !translation) return NULL;
965 for (val = stringfileinfo->values; val; val = val->next)
967 char *block_name;
968 if (val->type != val_block) continue;
969 block_name = convert_msgid_ascii( val->value.block->name, 0 );
970 if (!strcasecmp( block_name, translation ))
972 free( block_name );
973 free( translation );
974 return val->value.block;
976 free( block_name );
978 free( translation );
979 return NULL;
982 static int version_value_needs_translation( const ver_value_t *val )
984 int ret;
985 char *key;
987 if (val->type != val_str) return 0;
988 if (!(key = convert_msgid_ascii( val->key, 0 ))) return 0;
990 /* most values contain version numbers or file names, only translate a few specific ones */
991 ret = (!strcasecmp( key, "FileDescription" ) || !strcasecmp( key, "ProductName" ));
993 free( key );
994 return ret;
997 static void add_pot_versioninfo( po_file_t po, const resource_t *res )
999 ver_value_t *val;
1000 ver_block_t *langcharset = get_version_langcharset_block( res->res.ver->blocks );
1002 if (!langcharset) return;
1003 for (val = langcharset->values; val; val = val->next)
1004 if (version_value_needs_translation( val ))
1005 add_po_string( po, val->value.str, NULL, NULL );
1008 static void add_po_versioninfo( const resource_t *english, const resource_t *res )
1010 const ver_block_t *langcharset = get_version_langcharset_block( res->res.ver->blocks );
1011 const ver_block_t *english_langcharset = get_version_langcharset_block( english->res.ver->blocks );
1012 ver_value_t *val, *english_val;
1013 po_file_t po = get_po_file( res->res.ver->lvc.language );
1015 if (!langcharset && !english_langcharset) return;
1016 val = langcharset->values;
1017 english_val = english_langcharset->values;
1018 while (english_val && val)
1020 if (val->type == val_str)
1021 add_po_string( po, english_val->value.str, val->value.str, res->res.ver->lvc.language );
1022 val = val->next;
1023 english_val = english_val->next;
1027 static resource_t *find_english_resource( resource_t *res )
1029 resource_t *ptr;
1031 for (ptr = resource_top; ptr; ptr = ptr->next)
1033 if (ptr->type != res->type) continue;
1034 if (!ptr->lan) continue;
1035 if (!is_english( ptr->lan )) continue;
1036 if (compare_name_id( ptr->name, res->name )) continue;
1037 return ptr;
1039 return NULL;
1042 void write_pot_file( const char *outname )
1044 resource_t *res;
1045 po_file_t po = create_po_file();
1047 for (res = resource_top; res; res = res->next)
1049 if (!is_english( res->lan )) continue;
1051 switch (res->type)
1053 case res_acc: add_pot_accel( po, res ); break;
1054 case res_dlg: add_pot_dialog( po, res ); break;
1055 case res_men: add_pot_menu( po, res ); break;
1056 case res_stt: add_pot_stringtable( po, res ); break;
1057 case res_ver: add_pot_versioninfo( po, res ); break;
1058 case res_msg: break; /* FIXME */
1059 default: break;
1062 po_file_write( po, outname, &po_xerror_handler );
1063 po_file_free( po );
1066 void write_po_files( const char *outname )
1068 resource_t *res, *english;
1070 for (res = resource_top; res; res = res->next)
1072 if (!(english = find_english_resource( res ))) continue;
1073 switch (res->type)
1075 case res_acc: add_po_accel( english, res ); break;
1076 case res_dlg: add_po_dialog( english, res ); break;
1077 case res_men: add_po_menu( english, res ); break;
1078 case res_stt: add_po_stringtable( english, res ); break;
1079 case res_ver: add_po_versioninfo( english, res ); break;
1080 case res_msg: break; /* FIXME */
1081 default: break;
1084 if (!flush_po_files( outname ))
1086 if (outname) error( "No translations found for %s\n", outname );
1087 else error( "No translations found\n" );
1091 #endif /* HAVE_LIBGETTEXTPO */
1093 static struct mo_file *mo_file;
1095 static void byteswap( unsigned int *data, unsigned int count )
1097 unsigned int i;
1099 for (i = 0; i < count; i++)
1100 data[i] = data[i] >> 24 | (data[i] >> 8 & 0xff00) | (data[i] << 8 & 0xff0000) | data[i] << 24;
1103 static void load_mo_file( const char *name )
1105 size_t size;
1107 if (!(mo_file = read_file( name, &size ))) fatal_perror( "Failed to read %s", name );
1109 /* sanity checks */
1111 if (size < sizeof(*mo_file))
1112 error( "%s is not a valid .mo file\n", name );
1113 if (mo_file->magic == 0xde120495)
1114 byteswap( &mo_file->revision, 4 );
1115 else if (mo_file->magic != 0x950412de)
1116 error( "%s is not a valid .mo file\n", name );
1117 if ((mo_file->revision >> 16) > 1)
1118 error( "%s: unsupported file version %x\n", name, mo_file->revision );
1119 if (mo_file->msgid_off >= size ||
1120 mo_file->msgstr_off >= size ||
1121 size < sizeof(*mo_file) + 2 * 8 * mo_file->count)
1122 error( "%s: corrupted file\n", name );
1124 if (mo_file->magic == 0xde120495)
1126 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgid_off), 2 * mo_file->count );
1127 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgstr_off), 2 * mo_file->count );
1131 static void free_mo_file(void)
1133 free( mo_file );
1134 mo_file = NULL;
1137 static inline const char *get_mo_msgid( int index )
1139 const char *base = (const char *)mo_file;
1140 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgid_off);
1141 return base + offsets[2 * index + 1];
1144 static inline const char *get_mo_msgstr( int index )
1146 const char *base = (const char *)mo_file;
1147 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgstr_off);
1148 return base + offsets[2 * index + 1];
1151 static const char *get_msgstr( const char *msgid, const char *context, int *found )
1153 int pos, res, min, max;
1154 const char *ret = msgid;
1155 char *id = NULL;
1157 if (!mo_file) /* strings containing a context still need to be transformed */
1159 if (context) (*found)++;
1160 return ret;
1163 if (context) id = strmake( "%s%c%s", context, 4, msgid );
1164 min = 0;
1165 max = mo_file->count - 1;
1166 while (min <= max)
1168 pos = (min + max) / 2;
1169 res = strcmp( get_mo_msgid(pos), id ? id : msgid );
1170 if (!res)
1172 const char *str = get_mo_msgstr( pos );
1173 if (str[0]) /* ignore empty strings */
1175 ret = str;
1176 (*found)++;
1178 break;
1180 if (res > 0) max = pos - 1;
1181 else min = pos + 1;
1183 free( id );
1184 return ret;
1187 static string_t *translate_string( string_t *str, int *found )
1189 string_t ustr, *new;
1190 const char *transl;
1191 char *buffer, *msgid, *context;
1193 if (!str->size || !(buffer = convert_msgid_ascii( str, 0 )))
1194 return convert_string_unicode( str, 1252 );
1196 msgid = buffer;
1197 context = get_message_context( &msgid );
1198 transl = get_msgstr( msgid, context, found );
1200 ustr.type = str_char;
1201 ustr.size = strlen( transl );
1202 ustr.str.cstr = (char *)transl;
1203 ustr.loc = str->loc;
1205 new = convert_string_unicode( &ustr, CP_UTF8 );
1206 free( buffer );
1207 return new;
1210 static control_t *translate_controls( control_t *ctrl, int *found )
1212 control_t *new, *head = NULL, *tail = NULL;
1214 while (ctrl)
1216 new = xmalloc( sizeof(*new) );
1217 *new = *ctrl;
1218 if (control_has_title( ctrl ))
1220 new->title = new_name_id();
1221 *new->title = *ctrl->title;
1222 new->title->name.s_name = translate_string( ctrl->title->name.s_name, found );
1224 else new->title = dup_name_id( ctrl->title );
1225 new->ctlclass = dup_name_id( ctrl->ctlclass );
1226 if (tail) tail->next = new;
1227 else head = new;
1228 new->next = NULL;
1229 new->prev = tail;
1230 tail = new;
1231 ctrl = ctrl->next;
1233 return head;
1236 static menu_item_t *translate_items( menu_item_t *item, int *found )
1238 menu_item_t *new, *head = NULL, *tail = NULL;
1240 while (item)
1242 new = xmalloc( sizeof(*new) );
1243 *new = *item;
1244 if (item->name) new->name = translate_string( item->name, found );
1245 if (item->popup) new->popup = translate_items( item->popup, found );
1246 if (tail) tail->next = new;
1247 else head = new;
1248 new->next = NULL;
1249 new->prev = tail;
1250 tail = new;
1251 item = item->next;
1253 return head;
1256 static stringtable_t *translate_stringtable( stringtable_t *stt, language_t *lang, int *found )
1258 stringtable_t *new, *head = NULL, *tail = NULL;
1259 int i;
1261 while (stt)
1263 new = xmalloc( sizeof(*new) );
1264 *new = *stt;
1265 new->lvc.language = lang;
1266 new->lvc.version = get_dup_version( lang );
1267 new->entries = xmalloc( new->nentries * sizeof(*new->entries) );
1268 memcpy( new->entries, stt->entries, new->nentries * sizeof(*new->entries) );
1269 for (i = 0; i < stt->nentries; i++)
1270 if (stt->entries[i].str)
1271 new->entries[i].str = translate_string( stt->entries[i].str, found );
1273 if (tail) tail->next = new;
1274 else head = new;
1275 new->next = NULL;
1276 new->prev = tail;
1277 tail = new;
1278 stt = stt->next;
1280 return head;
1283 static void translate_dialog( dialog_t *dlg, dialog_t *new, int *found )
1285 if (dlg->title) new->title = translate_string( dlg->title, found );
1286 if (is_rtl_language( new->lvc.language ))
1288 new->gotexstyle = TRUE;
1289 if (dlg->gotexstyle)
1290 new->exstyle = new_style( dlg->exstyle->or_mask | WS_EX_LAYOUTRTL, dlg->exstyle->and_mask );
1291 else
1292 new->exstyle = new_style( WS_EX_LAYOUTRTL, 0 );
1294 if (dlg->font)
1296 new->font = xmalloc( sizeof(*dlg->font) );
1297 *new->font = *dlg->font;
1298 if (uses_larger_font( new->lvc.language )) new->font->size++;
1299 new->font->name = convert_string_unicode( dlg->font->name, 1252 );
1301 new->controls = translate_controls( dlg->controls, found );
1304 static event_t *translate_accel( accelerator_t *acc, accelerator_t *new, int *found )
1306 event_t *event, *new_ev, *head = NULL, *tail = NULL;
1308 event = acc->events;
1309 while (event)
1311 new_ev = new_event();
1312 *new_ev = *event;
1313 if (event->str) new_ev->str = translate_string( event->str, found );
1314 if (tail) tail->next = new_ev;
1315 else head = new_ev;
1316 new_ev->next = NULL;
1317 new_ev->prev = tail;
1318 tail = new_ev;
1319 event = event->next;
1321 return head;
1324 static ver_value_t *translate_langcharset_values( ver_value_t *val, language_t *lang, int *found )
1326 ver_value_t *new_val, *head = NULL, *tail = NULL;
1327 while (val)
1329 new_val = new_ver_value();
1330 *new_val = *val;
1331 if (val->type == val_str)
1332 new_val->value.str = translate_string( val->value.str, found );
1333 if (tail) tail->next = new_val;
1334 else head = new_val;
1335 new_val->next = NULL;
1336 new_val->prev = tail;
1337 tail = new_val;
1338 val = val->next;
1340 return head;
1343 static ver_value_t *translate_stringfileinfo( ver_value_t *val, language_t *lang, int *found )
1345 int i;
1346 ver_value_t *new_val, *head = NULL, *tail = NULL;
1347 const char *english_block_name[2] = { "040904b0", "040904e4" };
1348 char *block_name[2];
1349 int langid = MAKELANGID( lang->id, get_default_sublang( lang ) );
1351 block_name[0] = strmake( "%04x%04x", langid, 1200 );
1352 block_name[1] = strmake( "%04x%04x", langid, get_language_codepage( lang->id, lang->sub ) );
1354 while (val)
1356 new_val = new_ver_value();
1357 *new_val = *val;
1358 if (val->type == val_block)
1360 ver_block_t *blk, *blk_head = NULL, *blk_tail = NULL;
1361 for (blk = val->value.block; blk; blk = blk->next)
1363 ver_block_t *new_blk;
1364 char *name;
1365 new_blk = new_ver_block();
1366 *new_blk = *blk;
1367 name = convert_msgid_ascii( blk->name, 0 );
1368 for (i = 0; i < ARRAY_SIZE(block_name); i++)
1370 if (!strcasecmp( name, english_block_name[i] ))
1372 string_t str;
1373 str.type = str_char;
1374 str.size = strlen( block_name[i] ) + 1;
1375 str.str.cstr = block_name[i];
1376 str.loc = blk->name->loc;
1377 new_blk->name = convert_string_unicode( &str, CP_UTF8 );
1378 new_blk->values = translate_langcharset_values( blk->values, lang, found );
1381 free( name );
1382 if (blk_tail) blk_tail->next = new_blk;
1383 else blk_head = new_blk;
1384 new_blk->next = NULL;
1385 new_blk->prev = blk_tail;
1386 blk_tail = new_blk;
1388 new_val->value.block = blk_head;
1390 if (tail) tail->next = new_val;
1391 else head = new_val;
1392 new_val->next = NULL;
1393 new_val->prev = tail;
1394 tail = new_val;
1395 val = val->next;
1398 for (i = 0; i < ARRAY_SIZE(block_name); i++)
1399 free( block_name[i] );
1400 return head;
1403 static ver_value_t *translate_varfileinfo( ver_value_t *val, language_t *lang )
1405 ver_value_t *new_val, *head = NULL, *tail = NULL;
1407 while (val)
1409 new_val = new_ver_value();
1410 *new_val = *val;
1411 if (val->type == val_words)
1413 char *key = convert_msgid_ascii( val->key, 0 );
1414 if (!strcasecmp( key, "Translation" ) &&
1415 val->value.words->nwords == 2 &&
1416 val->value.words->words[0] == MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ))
1418 ver_words_t *new_words;
1419 int langid, codepage;
1420 langid = MAKELANGID( lang->id, get_default_sublang( lang ) );
1421 new_words = new_ver_words( langid );
1422 if (val->value.words->words[1] == 1200)
1423 codepage = 1200;
1424 else
1425 codepage = get_language_codepage( lang->id, lang->sub );
1426 new_val->value.words = add_ver_words( new_words, codepage );
1428 free( key );
1430 if (tail) tail->next = new_val;
1431 else head = new_val;
1432 new_val->next = NULL;
1433 new_val->prev = tail;
1434 tail = new_val;
1435 val = val->next;
1437 return head;
1440 static ver_block_t *translate_versioninfo( ver_block_t *blk, language_t *lang, int *found )
1442 ver_block_t *new_blk, *head = NULL, *tail = NULL;
1443 char *name;
1445 while (blk)
1447 new_blk = new_ver_block();
1448 *new_blk = *blk;
1449 name = convert_msgid_ascii( blk->name, 0 );
1450 if (!strcasecmp( name, "stringfileinfo" ))
1451 new_blk->values = translate_stringfileinfo( blk->values, lang, found );
1452 else if (!strcasecmp( name, "varfileinfo" ))
1453 new_blk->values = translate_varfileinfo( blk->values, lang );
1454 free(name);
1455 if (tail) tail->next = new_blk;
1456 else head = new_blk;
1457 new_blk->next = NULL;
1458 new_blk->prev = tail;
1459 tail = new_blk;
1460 blk = blk->next;
1462 return head;
1465 static void translate_resources( language_t *lang )
1467 resource_t *res;
1469 for (res = resource_top; res; res = res->next)
1471 resource_t *new = NULL;
1472 int found = 0;
1474 if (!is_english( res->lan )) continue;
1476 switch (res->type)
1478 case res_acc:
1479 new = dup_resource( res, lang );
1480 new->res.acc->events = translate_accel( res->res.acc, new->res.acc, &found );
1481 break;
1482 case res_dlg:
1483 new = dup_resource( res, lang );
1484 translate_dialog( res->res.dlg, new->res.dlg, &found );
1485 break;
1486 case res_men:
1487 new = dup_resource( res, lang );
1488 new->res.men->items = translate_items( res->res.men->items, &found );
1489 break;
1490 case res_stt:
1491 new = dup_resource( res, lang );
1492 new->res.stt = translate_stringtable( res->res.stt, lang, &found );
1493 break;
1494 case res_ver:
1495 new = dup_resource( res, lang );
1496 new->res.ver->blocks = translate_versioninfo( res->res.ver->blocks, lang, &found );
1497 break;
1498 case res_msg:
1499 /* FIXME */
1500 break;
1501 default:
1502 break;
1505 if (new && found)
1507 if (new_tail) new_tail->next = new;
1508 else new_top = new;
1509 new->prev = new_tail;
1510 new_tail = new;
1515 void add_translations( const char *po_dir )
1517 resource_t *res;
1518 char buffer[256];
1519 char *p, *tok, *name;
1520 unsigned int i;
1521 FILE *f;
1523 /* first check if we have English resources to translate */
1524 for (res = resource_top; res; res = res->next) if (is_english( res->lan )) break;
1525 if (!res) return;
1527 if (!po_dir) /* run through the translation process to remove msg contexts */
1529 translate_resources( new_language( LANG_ENGLISH, SUBLANG_DEFAULT ));
1530 goto done;
1533 new_top = new_tail = NULL;
1535 name = strmake( "%s/LINGUAS", po_dir );
1536 if (!(f = fopen( name, "r" )))
1538 free( name );
1539 return;
1541 free( name );
1542 while (fgets( buffer, sizeof(buffer), f ))
1544 if ((p = strchr( buffer, '#' ))) *p = 0;
1545 for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
1547 for (i = 0; i < ARRAY_SIZE(languages); i++)
1548 if (!strcmp( tok, languages[i].name )) break;
1550 if (i == ARRAY_SIZE(languages))
1551 error( "unknown language '%s'\n", tok );
1553 name = strmake( "%s/%s.mo", po_dir, tok );
1554 load_mo_file( name );
1555 translate_resources( new_language(languages[i].id, languages[i].sub) );
1556 free_mo_file();
1557 free( name );
1560 fclose( f );
1562 done:
1563 /* prepend the translated resources to the global list */
1564 if (new_tail)
1566 new_tail->next = resource_top;
1567 resource_top->prev = new_tail;
1568 resource_top = new_top;