gdi32/tests: Add tests for 1-bpp DDB pattern brushes.
[wine] / tools / wmc / po.c
1 /*
2  * Support for po files
3  *
4  * Copyright 2010, 2011 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
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_GETTEXT_PO_H
30 #include <gettext-po.h>
31 #endif
32
33 #include "wmc.h"
34 #include "utils.h"
35 #include "lang.h"
36 #include "write.h"
37 #include "windef.h"
38
39 #ifdef HAVE_LIBGETTEXTPO
40
41 static const struct
42 {
43     unsigned int id, sub;
44     const char *name;
45 } languages[] =
46 {
47     { LANG_ARABIC,         SUBLANG_NEUTRAL,                     "ar" },
48     { LANG_ARABIC,         SUBLANG_ARABIC_SAUDI_ARABIA,         "ar_SA" },
49     { LANG_ARABIC,         SUBLANG_ARABIC_IRAQ,                 "ar_IQ" },
50     { LANG_ARABIC,         SUBLANG_ARABIC_EGYPT,                "ar_EG" },
51     { LANG_ARABIC,         SUBLANG_ARABIC_LIBYA,                "ar_LY" },
52     { LANG_ARABIC,         SUBLANG_ARABIC_ALGERIA,              "ar_DZ" },
53     { LANG_ARABIC,         SUBLANG_ARABIC_MOROCCO,              "ar_MA" },
54     { LANG_ARABIC,         SUBLANG_ARABIC_TUNISIA,              "ar_TN" },
55     { LANG_ARABIC,         SUBLANG_ARABIC_OMAN,                 "ar_OM" },
56     { LANG_ARABIC,         SUBLANG_ARABIC_YEMEN,                "ar_YE" },
57     { LANG_ARABIC,         SUBLANG_ARABIC_SYRIA,                "ar_SY" },
58     { LANG_ARABIC,         SUBLANG_ARABIC_JORDAN,               "ar_JO" },
59     { LANG_ARABIC,         SUBLANG_ARABIC_LEBANON,              "ar_LB" },
60     { LANG_ARABIC,         SUBLANG_ARABIC_KUWAIT,               "ar_KW" },
61     { LANG_ARABIC,         SUBLANG_ARABIC_UAE,                  "ar_AE" },
62     { LANG_ARABIC,         SUBLANG_ARABIC_BAHRAIN,              "ar_BH" },
63     { LANG_ARABIC,         SUBLANG_ARABIC_QATAR,                "ar_QA" },
64     { LANG_BULGARIAN,      SUBLANG_NEUTRAL,                     "bg" },
65     { LANG_BULGARIAN,      SUBLANG_BULGARIAN_BULGARIA,          "bg_BG" },
66     { LANG_CATALAN,        SUBLANG_NEUTRAL,                     "ca" },
67     { LANG_CATALAN,        SUBLANG_CATALAN_CATALAN,             "ca_ES" },
68     { LANG_CHINESE,        SUBLANG_NEUTRAL,                     "zh" },
69     { LANG_CHINESE,        SUBLANG_CHINESE_TRADITIONAL,         "zh_TW" },
70     { LANG_CHINESE,        SUBLANG_CHINESE_SIMPLIFIED,          "zh_CN" },
71     { LANG_CHINESE,        SUBLANG_CHINESE_HONGKONG,            "zh_HK" },
72     { LANG_CHINESE,        SUBLANG_CHINESE_SINGAPORE,           "zh_SG" },
73     { LANG_CHINESE,        SUBLANG_CHINESE_MACAU,               "zh_MO" },
74     { LANG_CZECH,          SUBLANG_NEUTRAL,                     "cs" },
75     { LANG_CZECH,          SUBLANG_CZECH_CZECH_REPUBLIC,        "cs_CZ" },
76     { LANG_DANISH,         SUBLANG_NEUTRAL,                     "da" },
77     { LANG_DANISH,         SUBLANG_DANISH_DENMARK,              "da_DK" },
78     { LANG_GERMAN,         SUBLANG_NEUTRAL,                     "de" },
79     { LANG_GERMAN,         SUBLANG_GERMAN,                      "de_DE" },
80     { LANG_GERMAN,         SUBLANG_GERMAN_SWISS,                "de_CH" },
81     { LANG_GERMAN,         SUBLANG_GERMAN_AUSTRIAN,             "de_AT" },
82     { LANG_GERMAN,         SUBLANG_GERMAN_LUXEMBOURG,           "de_LU" },
83     { LANG_GERMAN,         SUBLANG_GERMAN_LIECHTENSTEIN,        "de_LI" },
84     { LANG_GREEK,          SUBLANG_NEUTRAL,                     "el" },
85     { LANG_GREEK,          SUBLANG_GREEK_GREECE,                "el_GR" },
86     { LANG_ENGLISH,        SUBLANG_NEUTRAL,                     "en" },
87     { LANG_ENGLISH,        SUBLANG_ENGLISH_US,                  "en_US" },
88     { LANG_ENGLISH,        SUBLANG_ENGLISH_UK,                  "en_GB" },
89     { LANG_ENGLISH,        SUBLANG_ENGLISH_AUS,                 "en_AU" },
90     { LANG_ENGLISH,        SUBLANG_ENGLISH_CAN,                 "en_CA" },
91     { LANG_ENGLISH,        SUBLANG_ENGLISH_NZ,                  "en_NZ" },
92     { LANG_ENGLISH,        SUBLANG_ENGLISH_EIRE,                "en_IE" },
93     { LANG_ENGLISH,        SUBLANG_ENGLISH_SOUTH_AFRICA,        "en_ZA" },
94     { LANG_ENGLISH,        SUBLANG_ENGLISH_JAMAICA,             "en_JM" },
95     { LANG_ENGLISH,        SUBLANG_ENGLISH_CARIBBEAN,           "en_CB" },
96     { LANG_ENGLISH,        SUBLANG_ENGLISH_BELIZE,              "en_BZ" },
97     { LANG_ENGLISH,        SUBLANG_ENGLISH_TRINIDAD,            "en_TT" },
98     { LANG_ENGLISH,        SUBLANG_ENGLISH_ZIMBABWE,            "en_ZW" },
99     { LANG_ENGLISH,        SUBLANG_ENGLISH_PHILIPPINES,         "en_PH" },
100     { LANG_SPANISH,        SUBLANG_NEUTRAL,                     "es" },
101     { LANG_SPANISH,        SUBLANG_SPANISH,                     "es_ES" },
102     { LANG_SPANISH,        SUBLANG_SPANISH_MEXICAN,             "es_MX" },
103     { LANG_SPANISH,        SUBLANG_SPANISH_MODERN,              "es_ES_modern" },
104     { LANG_SPANISH,        SUBLANG_SPANISH_GUATEMALA,           "es_GT" },
105     { LANG_SPANISH,        SUBLANG_SPANISH_COSTA_RICA,          "es_CR" },
106     { LANG_SPANISH,        SUBLANG_SPANISH_PANAMA,              "es_PA" },
107     { LANG_SPANISH,        SUBLANG_SPANISH_DOMINICAN_REPUBLIC,  "es_DO" },
108     { LANG_SPANISH,        SUBLANG_SPANISH_VENEZUELA,           "es_VE" },
109     { LANG_SPANISH,        SUBLANG_SPANISH_COLOMBIA,            "es_CO" },
110     { LANG_SPANISH,        SUBLANG_SPANISH_PERU,                "es_PE" },
111     { LANG_SPANISH,        SUBLANG_SPANISH_ARGENTINA,           "es_AR" },
112     { LANG_SPANISH,        SUBLANG_SPANISH_ECUADOR,             "es_EC" },
113     { LANG_SPANISH,        SUBLANG_SPANISH_CHILE,               "es_CL" },
114     { LANG_SPANISH,        SUBLANG_SPANISH_URUGUAY,             "es_UY" },
115     { LANG_SPANISH,        SUBLANG_SPANISH_PARAGUAY,            "es_PY" },
116     { LANG_SPANISH,        SUBLANG_SPANISH_BOLIVIA,             "es_BO" },
117     { LANG_SPANISH,        SUBLANG_SPANISH_EL_SALVADOR,         "es_SV" },
118     { LANG_SPANISH,        SUBLANG_SPANISH_HONDURAS,            "es_HN" },
119     { LANG_SPANISH,        SUBLANG_SPANISH_NICARAGUA,           "es_NI" },
120     { LANG_SPANISH,        SUBLANG_SPANISH_PUERTO_RICO,         "es_PR" },
121     { LANG_FINNISH,        SUBLANG_NEUTRAL,                     "fi" },
122     { LANG_FINNISH,        SUBLANG_FINNISH_FINLAND,             "fi_FI" },
123     { LANG_FRENCH,         SUBLANG_NEUTRAL,                     "fr" },
124     { LANG_FRENCH,         SUBLANG_FRENCH,                      "fr_FR" },
125     { LANG_FRENCH,         SUBLANG_FRENCH_BELGIAN,              "fr_BE" },
126     { LANG_FRENCH,         SUBLANG_FRENCH_CANADIAN,             "fr_CA" },
127     { LANG_FRENCH,         SUBLANG_FRENCH_SWISS,                "fr_CH" },
128     { LANG_FRENCH,         SUBLANG_FRENCH_LUXEMBOURG,           "fr_LU" },
129     { LANG_FRENCH,         SUBLANG_FRENCH_MONACO,               "fr_MC" },
130     { LANG_HEBREW,         SUBLANG_NEUTRAL,                     "he" },
131     { LANG_HEBREW,         SUBLANG_HEBREW_ISRAEL,               "he_IL" },
132     { LANG_HUNGARIAN,      SUBLANG_NEUTRAL,                     "hu" },
133     { LANG_HUNGARIAN,      SUBLANG_HUNGARIAN_HUNGARY,           "hu_HU" },
134     { LANG_ICELANDIC,      SUBLANG_NEUTRAL,                     "is" },
135     { LANG_ICELANDIC,      SUBLANG_ICELANDIC_ICELAND,           "is_IS" },
136     { LANG_ITALIAN,        SUBLANG_NEUTRAL,                     "it" },
137     { LANG_ITALIAN,        SUBLANG_ITALIAN,                     "it_IT" },
138     { LANG_ITALIAN,        SUBLANG_ITALIAN_SWISS,               "it_CH" },
139     { LANG_JAPANESE,       SUBLANG_NEUTRAL,                     "ja" },
140     { LANG_JAPANESE,       SUBLANG_JAPANESE_JAPAN,              "ja_JP" },
141     { LANG_KOREAN,         SUBLANG_NEUTRAL,                     "ko" },
142     { LANG_KOREAN,         SUBLANG_KOREAN,                      "ko_KR" },
143     { LANG_DUTCH,          SUBLANG_NEUTRAL,                     "nl" },
144     { LANG_DUTCH,          SUBLANG_DUTCH,                       "nl_NL" },
145     { LANG_DUTCH,          SUBLANG_DUTCH_BELGIAN,               "nl_BE" },
146     { LANG_DUTCH,          SUBLANG_DUTCH_SURINAM,               "nl_SR" },
147     { LANG_NORWEGIAN,      SUBLANG_NORWEGIAN_BOKMAL,            "nb_NO" },
148     { LANG_NORWEGIAN,      SUBLANG_NORWEGIAN_NYNORSK,           "nn_NO" },
149     { LANG_POLISH,         SUBLANG_NEUTRAL,                     "pl" },
150     { LANG_POLISH,         SUBLANG_POLISH_POLAND,               "pl_PL" },
151     { LANG_PORTUGUESE,     SUBLANG_NEUTRAL,                     "pt" },
152     { LANG_PORTUGUESE,     SUBLANG_PORTUGUESE_BRAZILIAN,        "pt_BR" },
153     { LANG_PORTUGUESE,     SUBLANG_PORTUGUESE_PORTUGAL,         "pt_PT" },
154     { LANG_ROMANSH,        SUBLANG_NEUTRAL,                     "rm" },
155     { LANG_ROMANSH,        SUBLANG_ROMANSH_SWITZERLAND,         "rm_CH" },
156     { LANG_ROMANIAN,       SUBLANG_NEUTRAL,                     "ro" },
157     { LANG_ROMANIAN,       SUBLANG_ROMANIAN_ROMANIA,            "ro_RO" },
158     { LANG_RUSSIAN,        SUBLANG_NEUTRAL,                     "ru" },
159     { LANG_RUSSIAN,        SUBLANG_RUSSIAN_RUSSIA,              "ru_RU" },
160     { LANG_SERBIAN,        SUBLANG_NEUTRAL,                     "hr" },
161     { LANG_SERBIAN,        SUBLANG_SERBIAN_CROATIA,             "hr_HR" },
162     { LANG_SERBIAN,        SUBLANG_SERBIAN_LATIN,               "sr_RS@latin" },
163     { LANG_SERBIAN,        SUBLANG_SERBIAN_CYRILLIC,            "sr_RS@cyrillic" },
164     { LANG_SLOVAK,         SUBLANG_NEUTRAL,                     "sk" },
165     { LANG_SLOVAK,         SUBLANG_SLOVAK_SLOVAKIA,             "sk_SK" },
166     { LANG_ALBANIAN,       SUBLANG_NEUTRAL,                     "sq" },
167     { LANG_ALBANIAN,       SUBLANG_ALBANIAN_ALBANIA,            "sq_AL" },
168     { LANG_SWEDISH,        SUBLANG_NEUTRAL,                     "sv" },
169     { LANG_SWEDISH,        SUBLANG_SWEDISH_SWEDEN,              "sv_SE" },
170     { LANG_SWEDISH,        SUBLANG_SWEDISH_FINLAND,             "sv_FI" },
171     { LANG_THAI,           SUBLANG_NEUTRAL,                     "th" },
172     { LANG_THAI,           SUBLANG_THAI_THAILAND,               "th_TH" },
173     { LANG_TURKISH,        SUBLANG_NEUTRAL,                     "tr" },
174     { LANG_TURKISH,        SUBLANG_TURKISH_TURKEY,              "tr_TR" },
175     { LANG_URDU,           SUBLANG_NEUTRAL,                     "ur" },
176     { LANG_URDU,           SUBLANG_URDU_PAKISTAN,               "ur_PK" },
177     { LANG_INDONESIAN,     SUBLANG_NEUTRAL,                     "id" },
178     { LANG_INDONESIAN,     SUBLANG_INDONESIAN_INDONESIA,        "id_ID" },
179     { LANG_UKRAINIAN,      SUBLANG_NEUTRAL,                     "uk" },
180     { LANG_UKRAINIAN,      SUBLANG_UKRAINIAN_UKRAINE,           "uk_UA" },
181     { LANG_BELARUSIAN,     SUBLANG_NEUTRAL,                     "be" },
182     { LANG_BELARUSIAN,     SUBLANG_BELARUSIAN_BELARUS,          "be_BY" },
183     { LANG_SLOVENIAN,      SUBLANG_NEUTRAL,                     "sl" },
184     { LANG_SLOVENIAN,      SUBLANG_SLOVENIAN_SLOVENIA,          "sl_SI" },
185     { LANG_ESTONIAN,       SUBLANG_NEUTRAL,                     "et" },
186     { LANG_ESTONIAN,       SUBLANG_ESTONIAN_ESTONIA,            "et_EE" },
187     { LANG_LATVIAN,        SUBLANG_NEUTRAL,                     "lv" },
188     { LANG_LATVIAN,        SUBLANG_LATVIAN_LATVIA,              "lv_LV" },
189     { LANG_LITHUANIAN,     SUBLANG_NEUTRAL,                     "lt" },
190     { LANG_LITHUANIAN,     SUBLANG_LITHUANIAN_LITHUANIA,        "lt_LT" },
191     { LANG_PERSIAN,        SUBLANG_NEUTRAL,                     "fa" },
192     { LANG_PERSIAN,        SUBLANG_PERSIAN_IRAN,                "fa_IR" },
193     { LANG_ARMENIAN,       SUBLANG_NEUTRAL,                     "hy" },
194     { LANG_ARMENIAN,       SUBLANG_ARMENIAN_ARMENIA,            "hy_AM" },
195     { LANG_AZERI,          SUBLANG_NEUTRAL,                     "az" },
196     { LANG_AZERI,          SUBLANG_AZERI_LATIN,                 "az_AZ@latin" },
197     { LANG_AZERI,          SUBLANG_AZERI_CYRILLIC,              "az_AZ@cyrillic" },
198     { LANG_BASQUE,         SUBLANG_NEUTRAL,                     "eu" },
199     { LANG_BASQUE,         SUBLANG_BASQUE_BASQUE,               "eu_ES" },
200     { LANG_MACEDONIAN,     SUBLANG_NEUTRAL,                     "mk" },
201     { LANG_MACEDONIAN,     SUBLANG_MACEDONIAN_MACEDONIA,        "mk_MK" },
202     { LANG_AFRIKAANS,      SUBLANG_NEUTRAL,                     "af" },
203     { LANG_AFRIKAANS,      SUBLANG_AFRIKAANS_SOUTH_AFRICA,      "af_ZA" },
204     { LANG_GEORGIAN,       SUBLANG_NEUTRAL,                     "ka" },
205     { LANG_GEORGIAN,       SUBLANG_GEORGIAN_GEORGIA,            "ka_GE" },
206     { LANG_FAEROESE,       SUBLANG_NEUTRAL,                     "fo" },
207     { LANG_FAEROESE,       SUBLANG_FAEROESE_FAROE_ISLANDS,      "fo_FO" },
208     { LANG_HINDI,          SUBLANG_NEUTRAL,                     "hi" },
209     { LANG_HINDI,          SUBLANG_HINDI_INDIA,                 "hi_IN" },
210     { LANG_MALAY,          SUBLANG_NEUTRAL,                     "ms" },
211     { LANG_MALAY,          SUBLANG_MALAY_MALAYSIA,              "ms_MY" },
212     { LANG_MALAY,          SUBLANG_MALAY_BRUNEI_DARUSSALAM,     "ms_BN" },
213     { LANG_KAZAK,          SUBLANG_NEUTRAL,                     "kk" },
214     { LANG_KAZAK,          SUBLANG_KAZAK_KAZAKHSTAN,            "kk_KZ" },
215     { LANG_KYRGYZ,         SUBLANG_NEUTRAL,                     "ky" },
216     { LANG_KYRGYZ,         SUBLANG_KYRGYZ_KYRGYZSTAN,           "ky_KG" },
217     { LANG_SWAHILI,        SUBLANG_NEUTRAL,                     "sw" },
218     { LANG_SWAHILI,        SUBLANG_SWAHILI_KENYA,               "sw_KE" },
219     { LANG_UZBEK,          SUBLANG_NEUTRAL,                     "uz" },
220     { LANG_UZBEK,          SUBLANG_UZBEK_LATIN,                 "uz_UZ@latin" },
221     { LANG_UZBEK,          SUBLANG_UZBEK_CYRILLIC,              "uz_UZ@cyrillic" },
222     { LANG_TATAR,          SUBLANG_NEUTRAL,                     "tt" },
223     { LANG_TATAR,          SUBLANG_TATAR_RUSSIA,                "tt_TA" },
224     { LANG_PUNJABI,        SUBLANG_NEUTRAL,                     "pa" },
225     { LANG_PUNJABI,        SUBLANG_PUNJABI_INDIA,               "pa_IN" },
226     { LANG_GUJARATI,       SUBLANG_NEUTRAL,                     "gu" },
227     { LANG_GUJARATI,       SUBLANG_GUJARATI_INDIA,              "gu_IN" },
228     { LANG_ORIYA,          SUBLANG_NEUTRAL,                     "or" },
229     { LANG_ORIYA,          SUBLANG_ORIYA_INDIA,                 "or_IN" },
230     { LANG_TAMIL,          SUBLANG_NEUTRAL,                     "ta" },
231     { LANG_TAMIL,          SUBLANG_TAMIL_INDIA,                 "ta_IN" },
232     { LANG_TELUGU,         SUBLANG_NEUTRAL,                     "te" },
233     { LANG_TELUGU,         SUBLANG_TELUGU_INDIA,                "te_IN" },
234     { LANG_KANNADA,        SUBLANG_NEUTRAL,                     "kn" },
235     { LANG_KANNADA,        SUBLANG_KANNADA_INDIA,               "kn_IN" },
236     { LANG_MALAYALAM,      SUBLANG_NEUTRAL,                     "ml" },
237     { LANG_MALAYALAM,      SUBLANG_MALAYALAM_INDIA,             "ml_IN" },
238     { LANG_MARATHI,        SUBLANG_NEUTRAL,                     "mr" },
239     { LANG_MARATHI,        SUBLANG_MARATHI_INDIA,               "mr_IN" },
240     { LANG_SANSKRIT,       SUBLANG_NEUTRAL,                     "sa" },
241     { LANG_SANSKRIT,       SUBLANG_SANSKRIT_INDIA,              "sa_IN" },
242     { LANG_MONGOLIAN,      SUBLANG_NEUTRAL,                     "mn" },
243     { LANG_MONGOLIAN,      SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
244     { LANG_WELSH,          SUBLANG_NEUTRAL,                     "cy" },
245     { LANG_WELSH,          SUBLANG_WELSH_UNITED_KINGDOM,        "cy_GB" },
246     { LANG_GALICIAN,       SUBLANG_NEUTRAL,                     "gl" },
247     { LANG_GALICIAN,       SUBLANG_GALICIAN_GALICIAN,           "gl_ES" },
248     { LANG_KONKANI,        SUBLANG_NEUTRAL,                     "kok" },
249     { LANG_KONKANI,        SUBLANG_KONKANI_INDIA,               "kok_IN" },
250     { LANG_DIVEHI,         SUBLANG_NEUTRAL,                     "dv" },
251     { LANG_DIVEHI,         SUBLANG_DIVEHI_MALDIVES,             "dv_MV" },
252     { LANG_BRETON,         SUBLANG_NEUTRAL,                     "br" },
253     { LANG_BRETON,         SUBLANG_BRETON_FRANCE,               "br_FR" },
254
255 #ifdef LANG_ESPERANTO
256     { LANG_ESPERANTO,      SUBLANG_DEFAULT,                     "eo" },
257 #endif
258 #ifdef LANG_WALON
259     { LANG_WALON,          SUBLANG_NEUTRAL,                     "wa" },
260     { LANG_WALON,          SUBLANG_DEFAULT,                     "wa_BE" },
261 #endif
262 #ifdef LANG_CORNISH
263     { LANG_CORNISH,        SUBLANG_NEUTRAL,                     "kw" },
264     { LANG_CORNISH,        SUBLANG_DEFAULT,                     "kw_GB" },
265 #endif
266 #ifdef LANG_GAELIC
267     { LANG_GAELIC,         SUBLANG_NEUTRAL,                     "ga" },
268     { LANG_GAELIC,         SUBLANG_GAELIC,                      "ga_IE" },
269     { LANG_GAELIC,         SUBLANG_GAELIC_SCOTTISH,             "gd_GB" },
270     { LANG_GAELIC,         SUBLANG_GAELIC_MANX,                 "gv_GB" },
271 #endif
272 };
273
274 static void po_xerror( int severity, po_message_t message,
275                        const char *filename, size_t lineno, size_t column,
276                        int multiline_p, const char *message_text )
277 {
278     fprintf( stderr, "%s:%u:%u: %s\n",
279              filename, (unsigned int)lineno, (unsigned int)column, message_text );
280     if (severity) exit(1);
281 }
282
283 static void po_xerror2( int severity, po_message_t message1,
284                         const char *filename1, size_t lineno1, size_t column1,
285                         int multiline_p1, const char *message_text1,
286                         po_message_t message2,
287                         const char *filename2, size_t lineno2, size_t column2,
288                         int multiline_p2, const char *message_text2 )
289 {
290     fprintf( stderr, "%s:%u:%u: %s\n",
291              filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
292     fprintf( stderr, "%s:%u:%u: %s\n",
293              filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
294     if (severity) exit(1);
295 }
296
297 static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
298
299 static int is_english( int lan )
300 {
301     return lan == MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
302 }
303
304 static char *convert_string_utf8( const lanmsg_t *msg )
305 {
306     char *buffer = xmalloc( msg->len * 4 + 1 );
307     int len = wine_utf8_wcstombs( 0, msg->msg, msg->len, buffer, msg->len * 4 );
308     buffer[len] = 0;
309     return buffer;
310 }
311
312 static char *convert_msgid_ascii( const lanmsg_t *msg, int error_on_invalid_char )
313 {
314     int i;
315     char *buffer = xmalloc( msg->len * 4 + 1 );
316
317     for (i = 0; i < msg->len; i++)
318     {
319         buffer[i] = msg->msg[i];
320         if (!msg->msg[i]) break;
321         if (msg->msg[i] >= 32 && msg->msg[i] <= 127) continue;
322         if (msg->msg[i] == '\t' || msg->msg[i] == '\n') continue;
323         if (error_on_invalid_char)
324         {
325             fprintf( stderr, "%s:%d: ", msg->file, msg->line );
326             error( "Invalid character %04x in source string\n", msg->msg[i] );
327         }
328         free( buffer );
329         return NULL;
330     }
331     buffer[i] = 0;
332     return buffer;
333 }
334
335 static char *get_message_context( char **msgid )
336 {
337     static const char magic[] = "#msgctxt#";
338     char *id, *context;
339
340     if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
341     context = *msgid + sizeof(magic) - 1;
342     if (!(id = strchr( context, '#' ))) return NULL;
343     *id = 0;
344     *msgid = id + 1;
345     return context;
346 }
347
348 static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
349                                   po_message_iterator_t *iterator )
350 {
351     po_message_t msg;
352     const char *context;
353
354     *iterator = po_message_iterator( po, NULL );
355     while ((msg = po_next_message( *iterator )))
356     {
357         if (strcmp( po_message_msgid( msg ), msgid )) continue;
358         if (!msgctxt) break;
359         if (!(context = po_message_msgctxt( msg ))) continue;
360         if (!strcmp( context, msgctxt )) break;
361     }
362     return msg;
363 }
364
365 static void add_po_string( po_file_t po, const lanmsg_t *msgid, const lanmsg_t *msgstr )
366 {
367     po_message_t msg;
368     po_message_iterator_t iterator;
369     char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
370
371     if (msgid->len <= 1) return;
372
373     id_buffer = id = convert_msgid_ascii( msgid, 1 );
374     context = get_message_context( &id );
375
376     if (msgstr)
377     {
378         str_buffer = str = convert_string_utf8( msgstr );
379         if (is_english( msgstr->lan )) get_message_context( &str );
380     }
381     if (!(msg = find_message( po, id, context, &iterator )))
382     {
383         msg = po_message_create();
384         po_message_set_msgid( msg, id );
385         po_message_set_msgstr( msg, str ? str : "" );
386         if (context) po_message_set_msgctxt( msg, context );
387         po_message_insert( iterator, msg );
388     }
389     if (msgid->file) po_message_add_filepos( msg, msgid->file, msgid->line );
390     po_message_iterator_free( iterator );
391     free( id_buffer );
392     free( str_buffer );
393 }
394
395 static po_file_t create_po_file(void)
396 {
397     po_file_t po;
398     po_message_t msg;
399     po_message_iterator_t iterator;
400
401     po = po_file_create();
402     iterator = po_message_iterator( po, NULL );
403     msg = po_message_create();
404     po_message_set_msgid( msg, "" );
405     po_message_set_msgstr( msg,
406                            "Project-Id-Version: Wine\n"
407                            "Report-Msgid-Bugs-To: http://bugs.winehq.org\n"
408                            "POT-Creation-Date: N/A\n"
409                            "PO-Revision-Date: N/A\n"
410                            "Last-Translator: Automatically generated\n"
411                            "Language-Team: none\n"
412                            "MIME-Version: 1.0\n"
413                            "Content-Type: text/plain; charset=UTF-8\n"
414                            "Content-Transfer-Encoding: 8bit\n" );
415     po_message_insert( iterator, msg );
416     po_message_iterator_free( iterator );
417     return po;
418 }
419
420 void write_pot_file( const char *outname )
421 {
422     int i, j;
423     lan_blk_t *lbp;
424     po_file_t po = create_po_file();
425
426     for (lbp = lanblockhead; lbp; lbp = lbp->next)
427     {
428         if (!is_english( lbp->lan )) continue;
429         for (i = 0; i < lbp->nblk; i++)
430         {
431             block_t *blk = &lbp->blks[i];
432             for (j = 0; j < blk->nmsg; j++) add_po_string( po, blk->msgs[j], NULL );
433         }
434     }
435     po_file_write( po, outname, &po_xerror_handler );
436     po_file_free( po );
437 }
438
439 static lan_blk_t *new_top, *new_tail;
440
441 static lanmsg_t *translate_string( po_file_t po, lanmsg_t *str, int lang, int *found )
442 {
443     po_message_t msg;
444     po_message_iterator_t iterator;
445     lanmsg_t *new;
446     const char *transl;
447     int res;
448     char *buffer, *msgid, *context;
449
450     if (str->len <= 1 || !(buffer = convert_msgid_ascii( str, 0 ))) return str;
451
452     msgid = buffer;
453     context = get_message_context( &msgid );
454     msg = find_message( po, msgid, context, &iterator );
455     po_message_iterator_free( iterator );
456
457     if (msg && !po_message_is_fuzzy( msg ))
458     {
459         transl = po_message_msgstr( msg );
460         if (!transl[0]) transl = msgid;  /* ignore empty strings */
461         else (*found)++;
462     }
463     else transl = msgid;
464
465     new = xmalloc( sizeof(*new) );
466     new->lan  = lang;
467     new->cp   = 0;  /* FIXME */
468     new->file = str->file;
469     new->line = str->line;
470     new->len  = wine_utf8_mbstowcs( 0, transl, strlen(transl) + 1, NULL, 0 );
471     new->msg  = xmalloc( new->len * sizeof(WCHAR) );
472     res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, transl, strlen(transl) + 1, new->msg, new->len );
473     if (res == -2)
474         error( "Invalid utf-8 character in string '%s'\n", transl );
475     free( buffer );
476     return new;
477 }
478
479 static void translate_block( po_file_t po, block_t *blk, block_t *new, int lang, int *found )
480 {
481     int i;
482
483     new->idlo = blk->idlo;
484     new->idhi = blk->idhi;
485     new->size = 0;
486     new->msgs = xmalloc( blk->nmsg * sizeof(*new->msgs) );
487     new->nmsg = blk->nmsg;
488     for (i = 0; i < blk->nmsg; i++)
489     {
490         new->msgs[i] = translate_string( po, blk->msgs[i], lang, found );
491         new->size += ((2 * new->msgs[i]->len + 3) & ~3) + 4;
492     }
493 }
494
495 static void translate_messages( po_file_t po, int lang )
496 {
497     int i, found;
498     lan_blk_t *lbp, *new;
499
500     for (lbp = lanblockhead; lbp; lbp = lbp->next)
501     {
502         if (!is_english( lbp->lan )) continue;
503         found = 0;
504         new = xmalloc( sizeof(*new) );
505         /* English "translations" take precedence over the original contents */
506         new->version = is_english( lang ) ? 1 : -1;
507         new->lan = lang;
508         new->blks = xmalloc( lbp->nblk * sizeof(*new->blks) );
509         new->nblk = lbp->nblk;
510
511         for (i = 0; i < lbp->nblk; i++)
512             translate_block( po, &lbp->blks[i], &new->blks[i], lang, &found );
513         if (found)
514         {
515             if (new_tail) new_tail->next = new;
516             else new_top = new;
517             new->prev = new_tail;
518             new_tail = new;
519         }
520         else
521         {
522             free( new->blks );
523             free( new );
524         }
525     }
526 }
527
528 void add_translations( const char *po_dir )
529 {
530     lan_blk_t *lbp;
531     po_file_t po;
532     char buffer[256];
533     char *p, *tok, *name;
534     unsigned int i;
535     FILE *f;
536
537     /* first check if we have English resources to translate */
538     for (lbp = lanblockhead; lbp; lbp = lbp->next) if (is_english( lbp->lan )) break;
539     if (!lbp) return;
540
541     new_top = new_tail = NULL;
542
543     name = strmake( "%s/LINGUAS", po_dir );
544     if (!(f = fopen( name, "r" ))) return;
545     free( name );
546     while (fgets( buffer, sizeof(buffer), f ))
547     {
548         if ((p = strchr( buffer, '#' ))) *p = 0;
549         for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
550         {
551             for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
552                 if (!strcmp( tok, languages[i].name )) break;
553
554             if (i == sizeof(languages)/sizeof(languages[0]))
555                 error( "unknown language '%s'\n", tok );
556
557             name = strmake( "%s/%s.po", po_dir, tok );
558             if (!(po = po_file_read( name, &po_xerror_handler )))
559                 error( "cannot load po file for language '%s'\n", tok );
560             translate_messages( po, MAKELANGID(languages[i].id, languages[i].sub) );
561             po_file_free( po );
562             free( name );
563         }
564     }
565     fclose( f );
566
567     /* prepend the translated messages to the global list */
568     if (new_tail)
569     {
570         new_tail->next = lanblockhead;
571         lanblockhead->prev = new_tail;
572         lanblockhead = new_top;
573     }
574 }
575
576 #else  /* HAVE_LIBGETTEXTPO */
577
578 void write_pot_file( const char *outname )
579 {
580     error( "PO files not supported in this wmc build\n" );
581 }
582
583 void add_translations( const char *po_dir )
584 {
585 }
586
587 #endif