oledb32: Add a 'fall through' comment.
[wine] / tools / wrc / po.c
1 /*
2  * Support for po files
3  *
4  * Copyright 2010 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 #include "wine/port.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #ifdef HAVE_LIBGETTEXTPO
31 #include <gettext-po.h>
32 #endif
33
34 #include "wrc.h"
35 #include "genres.h"
36 #include "newstruc.h"
37 #include "utils.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wingdi.h"
41 #include "winuser.h"
42 #include "wine/list.h"
43 #include "wine/unicode.h"
44
45 static resource_t *new_top, *new_tail;
46
47 struct mo_file
48 {
49     unsigned int magic;
50     unsigned int revision;
51     unsigned int count;
52     unsigned int msgid_off;
53     unsigned int msgstr_off;
54     /* ... rest of file data here */
55 };
56
57 static int is_english( const language_t *lan )
58 {
59     return lan->id == LANG_ENGLISH && lan->sub == SUBLANG_DEFAULT;
60 }
61
62 static int is_rtl_language( const language_t *lan )
63 {
64     return lan->id == LANG_ARABIC || lan->id == LANG_HEBREW || lan->id == LANG_PERSIAN;
65 }
66
67 static int uses_larger_font( const language_t *lan )
68 {
69     return lan->id == LANG_CHINESE || lan->id == LANG_JAPANESE || lan->id == LANG_KOREAN;
70 }
71
72 static version_t *get_dup_version( language_t *lang )
73 {
74     /* English "translations" take precedence over the original rc contents */
75     return new_version( is_english( lang ) ? 1 : -1 );
76 }
77
78 static name_id_t *dup_name_id( name_id_t *id )
79 {
80     name_id_t *new;
81
82     if (!id || id->type != name_str) return id;
83     new = new_name_id();
84     *new = *id;
85     new->name.s_name = convert_string( id->name.s_name, str_unicode, 1252 );
86     return new;
87 }
88
89 static char *convert_msgid_ascii( const string_t *str, int error_on_invalid_char )
90 {
91     int i;
92     string_t *newstr = convert_string( str, str_unicode, 1252 );
93     char *buffer = xmalloc( newstr->size + 1 );
94
95     for (i = 0; i < newstr->size; i++)
96     {
97         buffer[i] =  newstr->str.wstr[i];
98         if (newstr->str.wstr[i] >= 32 && newstr->str.wstr[i] <= 127) continue;
99         if (newstr->str.wstr[i] == '\t' || newstr->str.wstr[i] == '\n') continue;
100         if (error_on_invalid_char)
101         {
102             print_location( &newstr->loc );
103             error( "Invalid character %04x in source string\n", newstr->str.wstr[i] );
104         }
105         free( buffer);
106         free_string( newstr );
107         return NULL;
108     }
109     buffer[i] = 0;
110     free_string( newstr );
111     return buffer;
112 }
113
114 static char *get_message_context( char **msgid )
115 {
116     static const char magic[] = "#msgctxt#";
117     char *id, *context;
118
119     if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
120     context = *msgid + sizeof(magic) - 1;
121     if (!(id = strchr( context, '#' ))) return NULL;
122     *id = 0;
123     *msgid = id + 1;
124     return context;
125 }
126
127 static int control_has_title( const control_t *ctrl )
128 {
129     if (!ctrl->title) return 0;
130     if (ctrl->title->type != name_str) return 0;
131     /* check for text static control */
132     if (ctrl->ctlclass && ctrl->ctlclass->type == name_ord && ctrl->ctlclass->name.i_name == CT_STATIC)
133     {
134         switch (ctrl->style->or_mask & SS_TYPEMASK)
135         {
136         case SS_LEFT:
137         case SS_CENTER:
138         case SS_RIGHT:
139             return 1;
140         default:
141             return 0;
142         }
143     }
144     return 1;
145 }
146
147 static resource_t *dup_resource( resource_t *res, language_t *lang )
148 {
149     resource_t *new = xmalloc( sizeof(*new) );
150
151     *new = *res;
152     new->lan = lang;
153     new->next = new->prev = NULL;
154     new->name = dup_name_id( res->name );
155
156     switch (res->type)
157     {
158     case res_acc:
159         new->res.acc = xmalloc( sizeof(*(new)->res.acc) );
160         *new->res.acc = *res->res.acc;
161         new->res.acc->lvc.language = lang;
162         new->res.acc->lvc.version = get_dup_version( lang );
163         break;
164     case res_dlg:
165         new->res.dlg = xmalloc( sizeof(*(new)->res.dlg) );
166         *new->res.dlg = *res->res.dlg;
167         new->res.dlg->lvc.language = lang;
168         new->res.dlg->lvc.version = get_dup_version( lang );
169         break;
170     case res_men:
171         new->res.men = xmalloc( sizeof(*(new)->res.men) );
172         *new->res.men = *res->res.men;
173         new->res.men->lvc.language = lang;
174         new->res.men->lvc.version = get_dup_version( lang );
175         break;
176     case res_stt:
177         new->res.stt = xmalloc( sizeof(*(new)->res.stt) );
178         *new->res.stt = *res->res.stt;
179         new->res.stt->lvc.language = lang;
180         new->res.stt->lvc.version = get_dup_version( lang );
181         break;
182     default:
183         assert(0);
184     }
185     return new;
186 }
187
188 static const struct
189 {
190     unsigned int id, sub;
191     const char *name;
192 } languages[] =
193 {
194     { LANG_ARABIC,         SUBLANG_NEUTRAL,                     "ar" },
195     { LANG_ARABIC,         SUBLANG_ARABIC_SAUDI_ARABIA,         "ar_SA" },
196     { LANG_ARABIC,         SUBLANG_ARABIC_IRAQ,                 "ar_IQ" },
197     { LANG_ARABIC,         SUBLANG_ARABIC_EGYPT,                "ar_EG" },
198     { LANG_ARABIC,         SUBLANG_ARABIC_LIBYA,                "ar_LY" },
199     { LANG_ARABIC,         SUBLANG_ARABIC_ALGERIA,              "ar_DZ" },
200     { LANG_ARABIC,         SUBLANG_ARABIC_MOROCCO,              "ar_MA" },
201     { LANG_ARABIC,         SUBLANG_ARABIC_TUNISIA,              "ar_TN" },
202     { LANG_ARABIC,         SUBLANG_ARABIC_OMAN,                 "ar_OM" },
203     { LANG_ARABIC,         SUBLANG_ARABIC_YEMEN,                "ar_YE" },
204     { LANG_ARABIC,         SUBLANG_ARABIC_SYRIA,                "ar_SY" },
205     { LANG_ARABIC,         SUBLANG_ARABIC_JORDAN,               "ar_JO" },
206     { LANG_ARABIC,         SUBLANG_ARABIC_LEBANON,              "ar_LB" },
207     { LANG_ARABIC,         SUBLANG_ARABIC_KUWAIT,               "ar_KW" },
208     { LANG_ARABIC,         SUBLANG_ARABIC_UAE,                  "ar_AE" },
209     { LANG_ARABIC,         SUBLANG_ARABIC_BAHRAIN,              "ar_BH" },
210     { LANG_ARABIC,         SUBLANG_ARABIC_QATAR,                "ar_QA" },
211     { LANG_BULGARIAN,      SUBLANG_NEUTRAL,                     "bg" },
212     { LANG_BULGARIAN,      SUBLANG_BULGARIAN_BULGARIA,          "bg_BG" },
213     { LANG_CATALAN,        SUBLANG_NEUTRAL,                     "ca" },
214     { LANG_CATALAN,        SUBLANG_CATALAN_CATALAN,             "ca_ES" },
215     { LANG_CHINESE,        SUBLANG_NEUTRAL,                     "zh" },
216     { LANG_CHINESE,        SUBLANG_CHINESE_TRADITIONAL,         "zh_TW" },
217     { LANG_CHINESE,        SUBLANG_CHINESE_SIMPLIFIED,          "zh_CN" },
218     { LANG_CHINESE,        SUBLANG_CHINESE_HONGKONG,            "zh_HK" },
219     { LANG_CHINESE,        SUBLANG_CHINESE_SINGAPORE,           "zh_SG" },
220     { LANG_CHINESE,        SUBLANG_CHINESE_MACAU,               "zh_MO" },
221     { LANG_CZECH,          SUBLANG_NEUTRAL,                     "cs" },
222     { LANG_CZECH,          SUBLANG_CZECH_CZECH_REPUBLIC,        "cs_CZ" },
223     { LANG_DANISH,         SUBLANG_NEUTRAL,                     "da" },
224     { LANG_DANISH,         SUBLANG_DANISH_DENMARK,              "da_DK" },
225     { LANG_GERMAN,         SUBLANG_NEUTRAL,                     "de" },
226     { LANG_GERMAN,         SUBLANG_GERMAN,                      "de_DE" },
227     { LANG_GERMAN,         SUBLANG_GERMAN_SWISS,                "de_CH" },
228     { LANG_GERMAN,         SUBLANG_GERMAN_AUSTRIAN,             "de_AT" },
229     { LANG_GERMAN,         SUBLANG_GERMAN_LUXEMBOURG,           "de_LU" },
230     { LANG_GERMAN,         SUBLANG_GERMAN_LIECHTENSTEIN,        "de_LI" },
231     { LANG_GREEK,          SUBLANG_NEUTRAL,                     "el" },
232     { LANG_GREEK,          SUBLANG_GREEK_GREECE,                "el_GR" },
233     { LANG_ENGLISH,        SUBLANG_NEUTRAL,                     "en" },
234     { LANG_ENGLISH,        SUBLANG_ENGLISH_US,                  "en_US" },
235     { LANG_ENGLISH,        SUBLANG_ENGLISH_UK,                  "en_GB" },
236     { LANG_ENGLISH,        SUBLANG_ENGLISH_AUS,                 "en_AU" },
237     { LANG_ENGLISH,        SUBLANG_ENGLISH_CAN,                 "en_CA" },
238     { LANG_ENGLISH,        SUBLANG_ENGLISH_NZ,                  "en_NZ" },
239     { LANG_ENGLISH,        SUBLANG_ENGLISH_EIRE,                "en_IE" },
240     { LANG_ENGLISH,        SUBLANG_ENGLISH_SOUTH_AFRICA,        "en_ZA" },
241     { LANG_ENGLISH,        SUBLANG_ENGLISH_JAMAICA,             "en_JM" },
242     { LANG_ENGLISH,        SUBLANG_ENGLISH_CARIBBEAN,           "en_CB" },
243     { LANG_ENGLISH,        SUBLANG_ENGLISH_BELIZE,              "en_BZ" },
244     { LANG_ENGLISH,        SUBLANG_ENGLISH_TRINIDAD,            "en_TT" },
245     { LANG_ENGLISH,        SUBLANG_ENGLISH_ZIMBABWE,            "en_ZW" },
246     { LANG_ENGLISH,        SUBLANG_ENGLISH_PHILIPPINES,         "en_PH" },
247     { LANG_SPANISH,        SUBLANG_NEUTRAL,                     "es" },
248     { LANG_SPANISH,        SUBLANG_SPANISH,                     "es_ES" },
249     { LANG_SPANISH,        SUBLANG_SPANISH_MEXICAN,             "es_MX" },
250     { LANG_SPANISH,        SUBLANG_SPANISH_MODERN,              "es_ES_modern" },
251     { LANG_SPANISH,        SUBLANG_SPANISH_GUATEMALA,           "es_GT" },
252     { LANG_SPANISH,        SUBLANG_SPANISH_COSTA_RICA,          "es_CR" },
253     { LANG_SPANISH,        SUBLANG_SPANISH_PANAMA,              "es_PA" },
254     { LANG_SPANISH,        SUBLANG_SPANISH_DOMINICAN_REPUBLIC,  "es_DO" },
255     { LANG_SPANISH,        SUBLANG_SPANISH_VENEZUELA,           "es_VE" },
256     { LANG_SPANISH,        SUBLANG_SPANISH_COLOMBIA,            "es_CO" },
257     { LANG_SPANISH,        SUBLANG_SPANISH_PERU,                "es_PE" },
258     { LANG_SPANISH,        SUBLANG_SPANISH_ARGENTINA,           "es_AR" },
259     { LANG_SPANISH,        SUBLANG_SPANISH_ECUADOR,             "es_EC" },
260     { LANG_SPANISH,        SUBLANG_SPANISH_CHILE,               "es_CL" },
261     { LANG_SPANISH,        SUBLANG_SPANISH_URUGUAY,             "es_UY" },
262     { LANG_SPANISH,        SUBLANG_SPANISH_PARAGUAY,            "es_PY" },
263     { LANG_SPANISH,        SUBLANG_SPANISH_BOLIVIA,             "es_BO" },
264     { LANG_SPANISH,        SUBLANG_SPANISH_EL_SALVADOR,         "es_SV" },
265     { LANG_SPANISH,        SUBLANG_SPANISH_HONDURAS,            "es_HN" },
266     { LANG_SPANISH,        SUBLANG_SPANISH_NICARAGUA,           "es_NI" },
267     { LANG_SPANISH,        SUBLANG_SPANISH_PUERTO_RICO,         "es_PR" },
268     { LANG_FINNISH,        SUBLANG_NEUTRAL,                     "fi" },
269     { LANG_FINNISH,        SUBLANG_FINNISH_FINLAND,             "fi_FI" },
270     { LANG_FRENCH,         SUBLANG_NEUTRAL,                     "fr" },
271     { LANG_FRENCH,         SUBLANG_FRENCH,                      "fr_FR" },
272     { LANG_FRENCH,         SUBLANG_FRENCH_BELGIAN,              "fr_BE" },
273     { LANG_FRENCH,         SUBLANG_FRENCH_CANADIAN,             "fr_CA" },
274     { LANG_FRENCH,         SUBLANG_FRENCH_SWISS,                "fr_CH" },
275     { LANG_FRENCH,         SUBLANG_FRENCH_LUXEMBOURG,           "fr_LU" },
276     { LANG_FRENCH,         SUBLANG_FRENCH_MONACO,               "fr_MC" },
277     { LANG_HEBREW,         SUBLANG_NEUTRAL,                     "he" },
278     { LANG_HEBREW,         SUBLANG_HEBREW_ISRAEL,               "he_IL" },
279     { LANG_HUNGARIAN,      SUBLANG_NEUTRAL,                     "hu" },
280     { LANG_HUNGARIAN,      SUBLANG_HUNGARIAN_HUNGARY,           "hu_HU" },
281     { LANG_ICELANDIC,      SUBLANG_NEUTRAL,                     "is" },
282     { LANG_ICELANDIC,      SUBLANG_ICELANDIC_ICELAND,           "is_IS" },
283     { LANG_ITALIAN,        SUBLANG_NEUTRAL,                     "it" },
284     { LANG_ITALIAN,        SUBLANG_ITALIAN,                     "it_IT" },
285     { LANG_ITALIAN,        SUBLANG_ITALIAN_SWISS,               "it_CH" },
286     { LANG_JAPANESE,       SUBLANG_NEUTRAL,                     "ja" },
287     { LANG_JAPANESE,       SUBLANG_JAPANESE_JAPAN,              "ja_JP" },
288     { LANG_KOREAN,         SUBLANG_NEUTRAL,                     "ko" },
289     { LANG_KOREAN,         SUBLANG_KOREAN,                      "ko_KR" },
290     { LANG_DUTCH,          SUBLANG_NEUTRAL,                     "nl" },
291     { LANG_DUTCH,          SUBLANG_DUTCH,                       "nl_NL" },
292     { LANG_DUTCH,          SUBLANG_DUTCH_BELGIAN,               "nl_BE" },
293     { LANG_DUTCH,          SUBLANG_DUTCH_SURINAM,               "nl_SR" },
294     { LANG_NORWEGIAN,      SUBLANG_NORWEGIAN_BOKMAL,            "nb_NO" },
295     { LANG_NORWEGIAN,      SUBLANG_NORWEGIAN_NYNORSK,           "nn_NO" },
296     { LANG_POLISH,         SUBLANG_NEUTRAL,                     "pl" },
297     { LANG_POLISH,         SUBLANG_POLISH_POLAND,               "pl_PL" },
298     { LANG_PORTUGUESE,     SUBLANG_NEUTRAL,                     "pt" },
299     { LANG_PORTUGUESE,     SUBLANG_PORTUGUESE_BRAZILIAN,        "pt_BR" },
300     { LANG_PORTUGUESE,     SUBLANG_PORTUGUESE_PORTUGAL,         "pt_PT" },
301     { LANG_ROMANSH,        SUBLANG_NEUTRAL,                     "rm" },
302     { LANG_ROMANSH,        SUBLANG_ROMANSH_SWITZERLAND,         "rm_CH" },
303     { LANG_ROMANIAN,       SUBLANG_NEUTRAL,                     "ro" },
304     { LANG_ROMANIAN,       SUBLANG_ROMANIAN_ROMANIA,            "ro_RO" },
305     { LANG_RUSSIAN,        SUBLANG_NEUTRAL,                     "ru" },
306     { LANG_RUSSIAN,        SUBLANG_RUSSIAN_RUSSIA,              "ru_RU" },
307     { LANG_SERBIAN,        SUBLANG_NEUTRAL,                     "hr" },
308     { LANG_SERBIAN,        SUBLANG_SERBIAN_CROATIA,             "hr_HR" },
309     { LANG_SERBIAN,        SUBLANG_SERBIAN_LATIN,               "sr_RS@latin" },
310     { LANG_SERBIAN,        SUBLANG_SERBIAN_CYRILLIC,            "sr_RS@cyrillic" },
311     { LANG_SLOVAK,         SUBLANG_NEUTRAL,                     "sk" },
312     { LANG_SLOVAK,         SUBLANG_SLOVAK_SLOVAKIA,             "sk_SK" },
313     { LANG_ALBANIAN,       SUBLANG_NEUTRAL,                     "sq" },
314     { LANG_ALBANIAN,       SUBLANG_ALBANIAN_ALBANIA,            "sq_AL" },
315     { LANG_SWEDISH,        SUBLANG_NEUTRAL,                     "sv" },
316     { LANG_SWEDISH,        SUBLANG_SWEDISH_SWEDEN,              "sv_SE" },
317     { LANG_SWEDISH,        SUBLANG_SWEDISH_FINLAND,             "sv_FI" },
318     { LANG_THAI,           SUBLANG_NEUTRAL,                     "th" },
319     { LANG_THAI,           SUBLANG_THAI_THAILAND,               "th_TH" },
320     { LANG_TURKISH,        SUBLANG_NEUTRAL,                     "tr" },
321     { LANG_TURKISH,        SUBLANG_TURKISH_TURKEY,              "tr_TR" },
322     { LANG_URDU,           SUBLANG_NEUTRAL,                     "ur" },
323     { LANG_URDU,           SUBLANG_URDU_PAKISTAN,               "ur_PK" },
324     { LANG_INDONESIAN,     SUBLANG_NEUTRAL,                     "id" },
325     { LANG_INDONESIAN,     SUBLANG_INDONESIAN_INDONESIA,        "id_ID" },
326     { LANG_UKRAINIAN,      SUBLANG_NEUTRAL,                     "uk" },
327     { LANG_UKRAINIAN,      SUBLANG_UKRAINIAN_UKRAINE,           "uk_UA" },
328     { LANG_BELARUSIAN,     SUBLANG_NEUTRAL,                     "be" },
329     { LANG_BELARUSIAN,     SUBLANG_BELARUSIAN_BELARUS,          "be_BY" },
330     { LANG_SLOVENIAN,      SUBLANG_NEUTRAL,                     "sl" },
331     { LANG_SLOVENIAN,      SUBLANG_SLOVENIAN_SLOVENIA,          "sl_SI" },
332     { LANG_ESTONIAN,       SUBLANG_NEUTRAL,                     "et" },
333     { LANG_ESTONIAN,       SUBLANG_ESTONIAN_ESTONIA,            "et_EE" },
334     { LANG_LATVIAN,        SUBLANG_NEUTRAL,                     "lv" },
335     { LANG_LATVIAN,        SUBLANG_LATVIAN_LATVIA,              "lv_LV" },
336     { LANG_LITHUANIAN,     SUBLANG_NEUTRAL,                     "lt" },
337     { LANG_LITHUANIAN,     SUBLANG_LITHUANIAN_LITHUANIA,        "lt_LT" },
338     { LANG_PERSIAN,        SUBLANG_NEUTRAL,                     "fa" },
339     { LANG_PERSIAN,        SUBLANG_PERSIAN_IRAN,                "fa_IR" },
340     { LANG_ARMENIAN,       SUBLANG_NEUTRAL,                     "hy" },
341     { LANG_ARMENIAN,       SUBLANG_ARMENIAN_ARMENIA,            "hy_AM" },
342     { LANG_AZERI,          SUBLANG_NEUTRAL,                     "az" },
343     { LANG_AZERI,          SUBLANG_AZERI_LATIN,                 "az_AZ@latin" },
344     { LANG_AZERI,          SUBLANG_AZERI_CYRILLIC,              "az_AZ@cyrillic" },
345     { LANG_BASQUE,         SUBLANG_NEUTRAL,                     "eu" },
346     { LANG_BASQUE,         SUBLANG_BASQUE_BASQUE,               "eu_ES" },
347     { LANG_MACEDONIAN,     SUBLANG_NEUTRAL,                     "mk" },
348     { LANG_MACEDONIAN,     SUBLANG_MACEDONIAN_MACEDONIA,        "mk_MK" },
349     { LANG_AFRIKAANS,      SUBLANG_NEUTRAL,                     "af" },
350     { LANG_AFRIKAANS,      SUBLANG_AFRIKAANS_SOUTH_AFRICA,      "af_ZA" },
351     { LANG_GEORGIAN,       SUBLANG_NEUTRAL,                     "ka" },
352     { LANG_GEORGIAN,       SUBLANG_GEORGIAN_GEORGIA,            "ka_GE" },
353     { LANG_FAEROESE,       SUBLANG_NEUTRAL,                     "fo" },
354     { LANG_FAEROESE,       SUBLANG_FAEROESE_FAROE_ISLANDS,      "fo_FO" },
355     { LANG_HINDI,          SUBLANG_NEUTRAL,                     "hi" },
356     { LANG_HINDI,          SUBLANG_HINDI_INDIA,                 "hi_IN" },
357     { LANG_MALAY,          SUBLANG_NEUTRAL,                     "ms" },
358     { LANG_MALAY,          SUBLANG_MALAY_MALAYSIA,              "ms_MY" },
359     { LANG_MALAY,          SUBLANG_MALAY_BRUNEI_DARUSSALAM,     "ms_BN" },
360     { LANG_KAZAK,          SUBLANG_NEUTRAL,                     "kk" },
361     { LANG_KAZAK,          SUBLANG_KAZAK_KAZAKHSTAN,            "kk_KZ" },
362     { LANG_KYRGYZ,         SUBLANG_NEUTRAL,                     "ky" },
363     { LANG_KYRGYZ,         SUBLANG_KYRGYZ_KYRGYZSTAN,           "ky_KG" },
364     { LANG_SWAHILI,        SUBLANG_NEUTRAL,                     "sw" },
365     { LANG_SWAHILI,        SUBLANG_SWAHILI_KENYA,               "sw_KE" },
366     { LANG_UZBEK,          SUBLANG_NEUTRAL,                     "uz" },
367     { LANG_UZBEK,          SUBLANG_UZBEK_LATIN,                 "uz_UZ@latin" },
368     { LANG_UZBEK,          SUBLANG_UZBEK_CYRILLIC,              "uz_UZ@cyrillic" },
369     { LANG_TATAR,          SUBLANG_NEUTRAL,                     "tt" },
370     { LANG_TATAR,          SUBLANG_TATAR_RUSSIA,                "tt_TA" },
371     { LANG_PUNJABI,        SUBLANG_NEUTRAL,                     "pa" },
372     { LANG_PUNJABI,        SUBLANG_PUNJABI_INDIA,               "pa_IN" },
373     { LANG_GUJARATI,       SUBLANG_NEUTRAL,                     "gu" },
374     { LANG_GUJARATI,       SUBLANG_GUJARATI_INDIA,              "gu_IN" },
375     { LANG_ORIYA,          SUBLANG_NEUTRAL,                     "or" },
376     { LANG_ORIYA,          SUBLANG_ORIYA_INDIA,                 "or_IN" },
377     { LANG_TAMIL,          SUBLANG_NEUTRAL,                     "ta" },
378     { LANG_TAMIL,          SUBLANG_TAMIL_INDIA,                 "ta_IN" },
379     { LANG_TELUGU,         SUBLANG_NEUTRAL,                     "te" },
380     { LANG_TELUGU,         SUBLANG_TELUGU_INDIA,                "te_IN" },
381     { LANG_KANNADA,        SUBLANG_NEUTRAL,                     "kn" },
382     { LANG_KANNADA,        SUBLANG_KANNADA_INDIA,               "kn_IN" },
383     { LANG_MALAYALAM,      SUBLANG_NEUTRAL,                     "ml" },
384     { LANG_MALAYALAM,      SUBLANG_MALAYALAM_INDIA,             "ml_IN" },
385     { LANG_MARATHI,        SUBLANG_NEUTRAL,                     "mr" },
386     { LANG_MARATHI,        SUBLANG_MARATHI_INDIA,               "mr_IN" },
387     { LANG_SANSKRIT,       SUBLANG_NEUTRAL,                     "sa" },
388     { LANG_SANSKRIT,       SUBLANG_SANSKRIT_INDIA,              "sa_IN" },
389     { LANG_MONGOLIAN,      SUBLANG_NEUTRAL,                     "mn" },
390     { LANG_MONGOLIAN,      SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
391     { LANG_WELSH,          SUBLANG_NEUTRAL,                     "cy" },
392     { LANG_WELSH,          SUBLANG_WELSH_UNITED_KINGDOM,        "cy_GB" },
393     { LANG_GALICIAN,       SUBLANG_NEUTRAL,                     "gl" },
394     { LANG_GALICIAN,       SUBLANG_GALICIAN_GALICIAN,           "gl_ES" },
395     { LANG_KONKANI,        SUBLANG_NEUTRAL,                     "kok" },
396     { LANG_KONKANI,        SUBLANG_KONKANI_INDIA,               "kok_IN" },
397     { LANG_DIVEHI,         SUBLANG_NEUTRAL,                     "dv" },
398     { LANG_DIVEHI,         SUBLANG_DIVEHI_MALDIVES,             "dv_MV" },
399     { LANG_BRETON,         SUBLANG_NEUTRAL,                     "br" },
400     { LANG_BRETON,         SUBLANG_BRETON_FRANCE,               "br_FR" },
401
402 #ifdef LANG_ESPERANTO
403     { LANG_ESPERANTO,      SUBLANG_DEFAULT,                     "eo" },
404 #endif
405 #ifdef LANG_WALON
406     { LANG_WALON,          SUBLANG_NEUTRAL,                     "wa" },
407     { LANG_WALON,          SUBLANG_DEFAULT,                     "wa_BE" },
408 #endif
409 #ifdef LANG_CORNISH
410     { LANG_CORNISH,        SUBLANG_NEUTRAL,                     "kw" },
411     { LANG_CORNISH,        SUBLANG_DEFAULT,                     "kw_GB" },
412 #endif
413 #ifdef LANG_GAELIC
414     { LANG_GAELIC,         SUBLANG_NEUTRAL,                     "ga" },
415     { LANG_GAELIC,         SUBLANG_GAELIC,                      "ga_IE" },
416     { LANG_GAELIC,         SUBLANG_GAELIC_SCOTTISH,             "gd_GB" },
417     { LANG_GAELIC,         SUBLANG_GAELIC_MANX,                 "gv_GB" },
418 #endif
419 };
420
421 #ifndef HAVE_LIBGETTEXTPO
422
423 void write_pot_file( const char *outname )
424 {
425     error( "PO files not supported in this wrc build\n" );
426 }
427
428 void write_po_files( const char *outname )
429 {
430     error( "PO files not supported in this wrc build\n" );
431 }
432
433 #else  /* HAVE_LIBGETTEXTPO */
434
435 static void po_xerror( int severity, po_message_t message,
436                        const char *filename, size_t lineno, size_t column,
437                        int multiline_p, const char *message_text )
438 {
439     fprintf( stderr, "%s:%u:%u: %s\n",
440              filename, (unsigned int)lineno, (unsigned int)column, message_text );
441     if (severity) exit(1);
442 }
443
444 static void po_xerror2( int severity, po_message_t message1,
445                         const char *filename1, size_t lineno1, size_t column1,
446                         int multiline_p1, const char *message_text1,
447                         po_message_t message2,
448                         const char *filename2, size_t lineno2, size_t column2,
449                         int multiline_p2, const char *message_text2 )
450 {
451     fprintf( stderr, "%s:%u:%u: %s\n",
452              filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
453     fprintf( stderr, "%s:%u:%u: %s\n",
454              filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
455     if (severity) exit(1);
456 }
457
458 static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
459
460 static char *convert_string_utf8( const string_t *str, int codepage )
461 {
462     string_t *newstr = convert_string( str, str_unicode, codepage );
463     char *buffer = xmalloc( newstr->size * 4 + 1 );
464     int len = wine_utf8_wcstombs( 0, newstr->str.wstr, newstr->size, buffer, newstr->size * 4 );
465     buffer[len] = 0;
466     free_string( newstr );
467     return buffer;
468 }
469
470 static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
471                                   po_message_iterator_t *iterator )
472 {
473     po_message_t msg;
474     const char *context;
475
476     *iterator = po_message_iterator( po, NULL );
477     while ((msg = po_next_message( *iterator )))
478     {
479         if (strcmp( po_message_msgid( msg ), msgid )) continue;
480         if (!msgctxt) break;
481         if (!(context = po_message_msgctxt( msg ))) continue;
482         if (!strcmp( context, msgctxt )) break;
483     }
484     return msg;
485 }
486
487 static void add_po_string( po_file_t po, const string_t *msgid, const string_t *msgstr,
488                            const language_t *lang )
489 {
490     static const char dnt[] = "do not translate";
491     po_message_t msg;
492     po_message_iterator_t iterator;
493     int codepage;
494     char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
495
496     if (!msgid->size) return;
497
498     id_buffer = id = convert_msgid_ascii( msgid, 1 );
499     context = get_message_context( &id );
500     if (context && strcmp(context, dnt) == 0)
501     {
502         /* This string should not be translated */
503         free( id_buffer );
504         return;
505     }
506
507     if (msgstr)
508     {
509         if (lang) codepage = get_language_codepage( lang->id, lang->sub );
510         else codepage = get_language_codepage( 0, 0 );
511         assert( codepage != -1 );
512         str_buffer = str = convert_string_utf8( msgstr, codepage );
513         if (is_english( lang )) get_message_context( &str );
514     }
515     if (!(msg = find_message( po, id, context, &iterator )))
516     {
517         msg = po_message_create();
518         po_message_set_msgid( msg, id );
519         po_message_set_msgstr( msg, str ? str : "" );
520         if (context) po_message_set_msgctxt( msg, context );
521         po_message_insert( iterator, msg );
522     }
523     if (msgid->loc.file) po_message_add_filepos( msg, msgid->loc.file, msgid->loc.line );
524     po_message_iterator_free( iterator );
525     free( id_buffer );
526     free( str_buffer );
527 }
528
529 struct po_file_lang
530 {
531     struct list entry;
532     language_t  lang;
533     po_file_t   po;
534 };
535
536 static struct list po_file_langs = LIST_INIT( po_file_langs );
537
538 static po_file_t create_po_file(void)
539 {
540     po_file_t po;
541     po_message_t msg;
542     po_message_iterator_t iterator;
543
544     po = po_file_create();
545     iterator = po_message_iterator( po, NULL );
546     msg = po_message_create();
547     po_message_set_msgid( msg, "" );
548     po_message_set_msgstr( msg,
549                            "Project-Id-Version: Wine\n"
550                            "Report-Msgid-Bugs-To: http://bugs.winehq.org\n"
551                            "POT-Creation-Date: N/A\n"
552                            "PO-Revision-Date: N/A\n"
553                            "Last-Translator: Automatically generated\n"
554                            "Language-Team: none\n"
555                            "MIME-Version: 1.0\n"
556                            "Content-Type: text/plain; charset=UTF-8\n"
557                            "Content-Transfer-Encoding: 8bit\n" );
558     po_message_insert( iterator, msg );
559     po_message_iterator_free( iterator );
560     return po;
561 }
562
563 static po_file_t get_po_file( const language_t *lang )
564 {
565     struct po_file_lang *po_file;
566
567     LIST_FOR_EACH_ENTRY( po_file, &po_file_langs, struct po_file_lang, entry )
568         if (po_file->lang.id == lang->id && po_file->lang.sub == lang->sub) return po_file->po;
569
570     /* create a new one */
571     po_file = xmalloc( sizeof(*po_file) );
572     po_file->lang = *lang;
573     po_file->po = create_po_file();
574     list_add_tail( &po_file_langs, &po_file->entry );
575     return po_file->po;
576 }
577
578 static const char *get_language_name( const language_t *lang )
579 {
580     static char name[20];
581     unsigned int i;
582
583     for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
584         if (languages[i].id == lang->id && languages[i].sub == lang->sub)
585             return languages[i].name;
586
587     sprintf( name, "%02x-%02x", lang->id, lang->sub );
588     return name;
589 }
590
591 static char *get_po_file_name( const language_t *lang )
592 {
593     return strmake( "%s.po", get_language_name( lang ) );
594 }
595
596 static unsigned int flush_po_files( const char *output_name )
597 {
598     struct po_file_lang *po_file, *next;
599     unsigned int count = 0;
600
601     LIST_FOR_EACH_ENTRY_SAFE( po_file, next, &po_file_langs, struct po_file_lang, entry )
602     {
603         char *name = get_po_file_name( &po_file->lang );
604         if (output_name)
605         {
606             const char *p = strrchr( output_name, '/' );
607             if (p) p++;
608             else p = output_name;
609             if (!strcmp( p, name ))
610             {
611                 po_file_write( po_file->po, name, &po_xerror_handler );
612                 count++;
613             }
614         }
615         else  /* no specified output name, output a file for every language found */
616         {
617             po_file_write( po_file->po, name, &po_xerror_handler );
618             count++;
619             fprintf( stderr, "created %s\n", name );
620         }
621         po_file_free( po_file->po );
622         list_remove( &po_file->entry );
623         free( po_file );
624         free( name );
625     }
626     return count;
627 }
628
629 static void add_pot_stringtable( po_file_t po, const resource_t *res )
630 {
631     const stringtable_t *stt = res->res.stt;
632     int i;
633
634     while (stt)
635     {
636         for (i = 0; i < stt->nentries; i++)
637             if (stt->entries[i].str) add_po_string( po, stt->entries[i].str, NULL, NULL );
638         stt = stt->next;
639     }
640 }
641
642 static void add_po_stringtable( const resource_t *english, const resource_t *res )
643 {
644     const stringtable_t *english_stt = english->res.stt;
645     const stringtable_t *stt = res->res.stt;
646     po_file_t po = get_po_file( stt->lvc.language );
647     int i;
648
649     while (english_stt && stt)
650     {
651         for (i = 0; i < stt->nentries; i++)
652             if (english_stt->entries[i].str && stt->entries[i].str)
653                 add_po_string( po, english_stt->entries[i].str, stt->entries[i].str, stt->lvc.language );
654         stt = stt->next;
655         english_stt = english_stt->next;
656     }
657 }
658
659 static void add_pot_dialog_controls( po_file_t po, const control_t *ctrl )
660 {
661     while (ctrl)
662     {
663         if (control_has_title( ctrl )) add_po_string( po, ctrl->title->name.s_name, NULL, NULL );
664         ctrl = ctrl->next;
665     }
666 }
667
668 static void add_pot_dialog( po_file_t po, const resource_t *res )
669 {
670     const dialog_t *dlg = res->res.dlg;
671
672     if (dlg->title) add_po_string( po, dlg->title, NULL, NULL );
673     add_pot_dialog_controls( po, dlg->controls );
674 }
675
676 static void compare_dialogs( const dialog_t *english_dlg, const dialog_t *dlg )
677 {
678     const control_t *english_ctrl, *ctrl;
679     unsigned int style = 0, exstyle = 0, english_style = 0, english_exstyle = 0;
680     char *name;
681     char *title = english_dlg->title ? convert_msgid_ascii( english_dlg->title, 0 ) : xstrdup("??");
682
683     if (english_dlg->width != dlg->width || english_dlg->height != dlg->height)
684         warning( "%s: dialog %s doesn't have the same size (%d,%d vs %d,%d)\n",
685                  get_language_name( dlg->lvc.language ), title, dlg->width, dlg->height,
686                  english_dlg->width, english_dlg->height );
687
688     if (dlg->gotstyle) style = dlg->style->or_mask;
689     if (dlg->gotexstyle) exstyle = dlg->exstyle->or_mask;
690     if (english_dlg->gotstyle) english_style = english_dlg->style->or_mask;
691     if (english_dlg->gotexstyle) english_exstyle = english_dlg->exstyle->or_mask;
692     if (is_rtl_language( dlg->lvc.language )) english_exstyle |= WS_EX_LAYOUTRTL;
693
694     if (english_style != style)
695         warning( "%s: dialog %s doesn't have the same style (%08x vs %08x)\n",
696                  get_language_name( dlg->lvc.language ), title, style, english_style );
697     if (english_exstyle != exstyle)
698         warning( "%s: dialog %s doesn't have the same exstyle (%08x vs %08x)\n",
699                  get_language_name( dlg->lvc.language ), title, exstyle, english_exstyle );
700
701     if (english_dlg->font || dlg->font)
702     {
703         int size = 0, english_size = 0;
704         char *font = NULL, *english_font = NULL;
705
706         if (english_dlg->font)
707         {
708             english_font = convert_msgid_ascii( english_dlg->font->name, 0 );
709             english_size = english_dlg->font->size;
710         }
711         if (dlg->font)
712         {
713             font = convert_msgid_ascii( dlg->font->name, 0 );
714             size = dlg->font->size;
715         }
716         if (uses_larger_font( dlg->lvc.language )) english_size++;
717
718         if (!english_font || !font || strcasecmp( english_font, font ) || english_size != size)
719             warning( "%s: dialog %s doesn't have the same font (%s %u vs %s %u)\n",
720                      get_language_name(dlg->lvc.language), title,
721                      english_font ? english_font : "default", english_size,
722                      font ? font : "default", size );
723         free( font );
724         free( english_font );
725     }
726     english_ctrl = english_dlg->controls;
727     ctrl = dlg->controls;
728     for ( ; english_ctrl && ctrl; ctrl = ctrl->next, english_ctrl = english_ctrl->next )
729     {
730         if (control_has_title( english_ctrl ))
731             name = convert_msgid_ascii( english_ctrl->title->name.s_name, 0 );
732         else
733             name = strmake( "%d", ctrl->id );
734
735         if (english_ctrl->width != ctrl->width || english_ctrl->height != ctrl->height)
736             warning( "%s: dialog %s control %s doesn't have the same size (%d,%d vs %d,%d)\n",
737                      get_language_name( dlg->lvc.language ), title, name,
738                      ctrl->width, ctrl->height, english_ctrl->width, english_ctrl->height );
739         if (english_ctrl->x != ctrl->x || english_ctrl->y != ctrl->y)
740             warning( "%s: dialog %s control %s doesn't have the same position (%d,%d vs %d,%d)\n",
741                      get_language_name( dlg->lvc.language ), title, name,
742                      ctrl->x, ctrl->y, english_ctrl->x, english_ctrl->y );
743         free( name );
744     }
745     free( title );
746 }
747
748 static void add_po_dialog_controls( po_file_t po, const control_t *english_ctrl,
749                                     const control_t *ctrl, const language_t *lang )
750 {
751     while (english_ctrl && ctrl)
752     {
753         if (control_has_title( english_ctrl ) && control_has_title( ctrl ))
754             add_po_string( po, english_ctrl->title->name.s_name, ctrl->title->name.s_name, lang );
755
756         ctrl = ctrl->next;
757         english_ctrl = english_ctrl->next;
758     }
759 }
760
761 static void add_po_dialog( const resource_t *english, const resource_t *res )
762 {
763     const dialog_t *english_dlg = english->res.dlg;
764     const dialog_t *dlg = res->res.dlg;
765     po_file_t po = get_po_file( dlg->lvc.language );
766
767     compare_dialogs( english_dlg, dlg );
768
769     if (english_dlg->title && dlg->title)
770         add_po_string( po, english_dlg->title, dlg->title, dlg->lvc.language );
771     add_po_dialog_controls( po, english_dlg->controls, dlg->controls, dlg->lvc.language );
772 }
773
774 static void add_pot_menu_items( po_file_t po, const menu_item_t *item )
775 {
776     while (item)
777     {
778         if (item->name) add_po_string( po, item->name, NULL, NULL );
779         if (item->popup) add_pot_menu_items( po, item->popup );
780         item = item->next;
781     }
782 }
783
784 static void add_pot_menu( po_file_t po, const resource_t *res )
785 {
786     add_pot_menu_items( po, res->res.men->items );
787 }
788
789 static void add_po_menu_items( po_file_t po, const menu_item_t *english_item,
790                                const menu_item_t *item, const language_t *lang )
791 {
792     while (english_item && item)
793     {
794         if (english_item->name && item->name)
795             add_po_string( po, english_item->name, item->name, lang );
796         if (english_item->popup && item->popup)
797             add_po_menu_items( po, english_item->popup, item->popup, lang );
798         item = item->next;
799         english_item = english_item->next;
800     }
801 }
802
803 static void add_po_menu( const resource_t *english, const resource_t *res )
804 {
805     const menu_item_t *english_items = english->res.men->items;
806     const menu_item_t *items = res->res.men->items;
807     po_file_t po = get_po_file( res->res.men->lvc.language );
808
809     add_po_menu_items( po, english_items, items, res->res.men->lvc.language );
810 }
811
812 static int string_has_context( const string_t *str )
813 {
814     char *id, *id_buffer, *context;
815
816     id_buffer = id = convert_msgid_ascii( str, 1 );
817     context = get_message_context( &id );
818     free( id_buffer );
819     return context != NULL;
820 }
821
822 static void add_pot_accel( po_file_t po, const resource_t *res )
823 {
824     event_t *event = res->res.acc->events;
825
826     while (event)
827     {
828         /* accelerators without a context don't make sense in po files */
829         if (event->str && string_has_context( event->str ))
830             add_po_string( po, event->str, NULL, NULL );
831         event = event->next;
832     }
833 }
834
835 static void add_po_accel( const resource_t *english, const resource_t *res )
836 {
837     event_t *english_event = english->res.acc->events;
838     event_t *event = res->res.acc->events;
839     po_file_t po = get_po_file( res->res.acc->lvc.language );
840
841     while (english_event && event)
842     {
843         if (english_event->str && event->str && string_has_context( english_event->str ))
844             add_po_string( po, english_event->str, event->str, res->res.acc->lvc.language );
845         event = event->next;
846         english_event = english_event->next;
847     }
848 }
849
850 static resource_t *find_english_resource( resource_t *res )
851 {
852     resource_t *ptr;
853
854     for (ptr = resource_top; ptr; ptr = ptr->next)
855     {
856         if (ptr->type != res->type) continue;
857         if (!ptr->lan) continue;
858         if (!is_english( ptr->lan )) continue;
859         if (compare_name_id( ptr->name, res->name )) continue;
860         return ptr;
861     }
862     return NULL;
863 }
864
865 void write_pot_file( const char *outname )
866 {
867     resource_t *res;
868     po_file_t po = create_po_file();
869
870     for (res = resource_top; res; res = res->next)
871     {
872         if (!is_english( res->lan )) continue;
873
874         switch (res->type)
875         {
876         case res_acc: add_pot_accel( po, res ); break;
877         case res_dlg: add_pot_dialog( po, res ); break;
878         case res_men: add_pot_menu( po, res ); break;
879         case res_stt: add_pot_stringtable( po, res ); break;
880         case res_msg: break;  /* FIXME */
881         default: break;
882         }
883     }
884     po_file_write( po, outname, &po_xerror_handler );
885     po_file_free( po );
886 }
887
888 void write_po_files( const char *outname )
889 {
890     resource_t *res, *english;
891
892     for (res = resource_top; res; res = res->next)
893     {
894         if (!(english = find_english_resource( res ))) continue;
895         switch (res->type)
896         {
897         case res_acc: add_po_accel( english, res ); break;
898         case res_dlg: add_po_dialog( english, res ); break;
899         case res_men: add_po_menu( english, res ); break;
900         case res_stt: add_po_stringtable( english, res ); break;
901         case res_msg: break;  /* FIXME */
902         default: break;
903         }
904     }
905     if (!flush_po_files( outname ))
906     {
907         if (outname) error( "No translations found for %s\n", outname );
908         else error( "No translations found\n" );
909     }
910 }
911
912 #endif  /* HAVE_LIBGETTEXTPO */
913
914 static struct mo_file *mo_file;
915
916 static void byteswap( unsigned int *data, unsigned int count )
917 {
918     unsigned int i;
919
920     for (i = 0; i < count; i++)
921         data[i] = data[i] >> 24 | (data[i] >> 8 & 0xff00) | (data[i] << 8 & 0xff0000) | data[i] << 24;
922 }
923
924 static void load_mo_file( const char *name )
925 {
926     struct stat st;
927     int res, fd;
928
929     fd = open( name, O_RDONLY | O_BINARY );
930     if (fd == -1) fatal_perror( "Failed to open %s", name );
931     fstat( fd, &st );
932     mo_file = xmalloc( st.st_size );
933     res = read( fd, mo_file, st.st_size );
934     if (res == -1) fatal_perror( "Failed to read %s", name );
935     else if (res != st.st_size) error( "Failed to read %s\n", name );
936     close( fd );
937
938     /* sanity checks */
939
940     if (st.st_size < sizeof(*mo_file))
941         error( "%s is not a valid .mo file\n", name );
942     if (mo_file->magic == 0xde120495)
943         byteswap( &mo_file->revision, 4 );
944     else if (mo_file->magic != 0x950412de)
945         error( "%s is not a valid .mo file\n", name );
946     if ((mo_file->revision >> 16) > 1)
947         error( "%s: unsupported file version %x\n", name, mo_file->revision );
948     if (mo_file->msgid_off >= st.st_size ||
949         mo_file->msgstr_off >= st.st_size ||
950         st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count)
951         error( "%s: corrupted file\n", name );
952
953     if (mo_file->magic == 0xde120495)
954     {
955         byteswap( (unsigned int *)((char *)mo_file + mo_file->msgid_off), 2 * mo_file->count );
956         byteswap( (unsigned int *)((char *)mo_file + mo_file->msgstr_off), 2 * mo_file->count );
957     }
958 }
959
960 static void free_mo_file(void)
961 {
962     free( mo_file );
963     mo_file = NULL;
964 }
965
966 static inline const char *get_mo_msgid( int index )
967 {
968     const char *base = (const char *)mo_file;
969     const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgid_off);
970     return base + offsets[2 * index + 1];
971 }
972
973 static inline const char *get_mo_msgstr( int index )
974 {
975     const char *base = (const char *)mo_file;
976     const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgstr_off);
977     return base + offsets[2 * index + 1];
978 }
979
980 static const char *get_msgstr( const char *msgid, const char *context, int *found )
981 {
982     int pos, res, min, max;
983     const char *ret = msgid;
984     char *id = NULL;
985
986     if (!mo_file)  /* strings containing a context still need to be transformed */
987     {
988         if (context) (*found)++;
989         return ret;
990     }
991
992     if (context) id = strmake( "%s%c%s", context, 4, msgid );
993     min = 0;
994     max = mo_file->count - 1;
995     while (min <= max)
996     {
997         pos = (min + max) / 2;
998         res = strcmp( get_mo_msgid(pos), id ? id : msgid );
999         if (!res)
1000         {
1001             const char *str = get_mo_msgstr( pos );
1002             if (str[0])  /* ignore empty strings */
1003             {
1004                 ret = str;
1005                 (*found)++;
1006             }
1007             break;
1008         }
1009         if (res > 0) max = pos - 1;
1010         else min = pos + 1;
1011     }
1012     free( id );
1013     return ret;
1014 }
1015
1016 static string_t *translate_string( string_t *str, int *found )
1017 {
1018     string_t *new;
1019     const char *transl;
1020     int res;
1021     char *buffer, *msgid, *context;
1022
1023     if (!str->size || !(buffer = convert_msgid_ascii( str, 0 )))
1024         return convert_string( str, str_unicode, 1252 );
1025
1026     msgid = buffer;
1027     context = get_message_context( &msgid );
1028     transl = get_msgstr( msgid, context, found );
1029
1030     new = xmalloc( sizeof(*new) );
1031     new->type = str_unicode;
1032     new->size = wine_utf8_mbstowcs( 0, transl, strlen(transl), NULL, 0 );
1033     new->str.wstr = xmalloc( (new->size+1) * sizeof(WCHAR) );
1034     res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, transl, strlen(transl), new->str.wstr, new->size );
1035     if (res == -2)
1036         error( "Invalid utf-8 character in string '%s'\n", transl );
1037     new->str.wstr[new->size] = 0;
1038     free( buffer );
1039     return new;
1040 }
1041
1042 static control_t *translate_controls( control_t *ctrl, int *found )
1043 {
1044     control_t *new, *head = NULL, *tail = NULL;
1045
1046     while (ctrl)
1047     {
1048         new = xmalloc( sizeof(*new) );
1049         *new = *ctrl;
1050         if (control_has_title( ctrl ))
1051         {
1052             new->title = new_name_id();
1053             *new->title = *ctrl->title;
1054             new->title->name.s_name = translate_string( ctrl->title->name.s_name, found );
1055         }
1056         else new->title = dup_name_id( ctrl->title );
1057         new->ctlclass = dup_name_id( ctrl->ctlclass );
1058         if (tail) tail->next = new;
1059         else head = new;
1060         new->next = NULL;
1061         new->prev = tail;
1062         tail = new;
1063         ctrl = ctrl->next;
1064     }
1065     return head;
1066 }
1067
1068 static menu_item_t *translate_items( menu_item_t *item, int *found )
1069 {
1070     menu_item_t *new, *head = NULL, *tail = NULL;
1071
1072     while (item)
1073     {
1074         new = xmalloc( sizeof(*new) );
1075         *new = *item;
1076         if (item->name) new->name = translate_string( item->name, found );
1077         if (item->popup) new->popup = translate_items( item->popup, found );
1078         if (tail) tail->next = new;
1079         else head = new;
1080         new->next = NULL;
1081         new->prev = tail;
1082         tail = new;
1083         item = item->next;
1084     }
1085     return head;
1086 }
1087
1088 static stringtable_t *translate_stringtable( stringtable_t *stt, language_t *lang, int *found )
1089 {
1090     stringtable_t *new, *head = NULL, *tail = NULL;
1091     int i;
1092
1093     while (stt)
1094     {
1095         new = xmalloc( sizeof(*new) );
1096         *new = *stt;
1097         new->lvc.language = lang;
1098         new->lvc.version = get_dup_version( lang );
1099         new->entries = xmalloc( new->nentries * sizeof(*new->entries) );
1100         memcpy( new->entries, stt->entries, new->nentries * sizeof(*new->entries) );
1101         for (i = 0; i < stt->nentries; i++)
1102             if (stt->entries[i].str)
1103                 new->entries[i].str = translate_string( stt->entries[i].str, found );
1104
1105         if (tail) tail->next = new;
1106         else head = new;
1107         new->next = NULL;
1108         new->prev = tail;
1109         tail = new;
1110         stt = stt->next;
1111     }
1112     return head;
1113 }
1114
1115 static void translate_dialog( dialog_t *dlg, dialog_t *new, int *found )
1116 {
1117     if (dlg->title) new->title = translate_string( dlg->title, found );
1118     if (is_rtl_language( new->lvc.language ))
1119     {
1120         new->gotexstyle = TRUE;
1121         if (dlg->gotexstyle)
1122             new->exstyle = new_style( dlg->exstyle->or_mask | WS_EX_LAYOUTRTL, dlg->exstyle->and_mask );
1123         else
1124             new->exstyle = new_style( WS_EX_LAYOUTRTL, 0 );
1125     }
1126     if (dlg->font)
1127     {
1128         new->font = xmalloc( sizeof(*dlg->font) );
1129         *new->font = *dlg->font;
1130         if (uses_larger_font( new->lvc.language )) new->font->size++;
1131         new->font->name = convert_string( dlg->font->name, str_unicode, 1252 );
1132     }
1133     new->controls = translate_controls( dlg->controls, found );
1134 }
1135
1136 static event_t *translate_accel( accelerator_t *acc, accelerator_t *new, int *found )
1137 {
1138     event_t *event, *new_ev, *head = NULL, *tail = NULL;
1139
1140     event = acc->events;
1141     while (event)
1142     {
1143         new_ev = new_event();
1144         *new_ev = *event;
1145         if (event->str) new_ev->str = translate_string( event->str, found );
1146         if (tail) tail->next = new_ev;
1147         else head = new_ev;
1148         new_ev->next = NULL;
1149         new_ev->prev = tail;
1150         tail = new_ev;
1151         event = event->next;
1152     }
1153     return head;
1154 }
1155
1156 static void translate_resources( language_t *lang )
1157 {
1158     resource_t *res;
1159
1160     for (res = resource_top; res; res = res->next)
1161     {
1162         resource_t *new = NULL;
1163         int found = 0;
1164
1165         if (!is_english( res->lan )) continue;
1166
1167         switch (res->type)
1168         {
1169         case res_acc:
1170             new = dup_resource( res, lang );
1171             new->res.acc->events = translate_accel( res->res.acc, new->res.acc, &found );
1172             break;
1173         case res_dlg:
1174             new = dup_resource( res, lang );
1175             translate_dialog( res->res.dlg, new->res.dlg, &found );
1176             break;
1177         case res_men:
1178             new = dup_resource( res, lang );
1179             new->res.men->items = translate_items( res->res.men->items, &found );
1180             break;
1181         case res_stt:
1182             new = dup_resource( res, lang );
1183             new->res.stt = translate_stringtable( res->res.stt, lang, &found );
1184             break;
1185         case res_msg:
1186             /* FIXME */
1187             break;
1188         default:
1189             break;
1190         }
1191
1192         if (new && found)
1193         {
1194             if (new_tail) new_tail->next = new;
1195             else new_top = new;
1196             new->prev = new_tail;
1197             new_tail = new;
1198         }
1199     }
1200 }
1201
1202 void add_translations( const char *po_dir )
1203 {
1204     resource_t *res;
1205     char buffer[256];
1206     char *p, *tok, *name;
1207     unsigned int i;
1208     FILE *f;
1209
1210     /* first check if we have English resources to translate */
1211     for (res = resource_top; res; res = res->next) if (is_english( res->lan )) break;
1212     if (!res) return;
1213
1214     if (!po_dir)  /* run through the translation process to remove msg contexts */
1215     {
1216         translate_resources( new_language( LANG_ENGLISH, SUBLANG_DEFAULT ));
1217         goto done;
1218     }
1219
1220     new_top = new_tail = NULL;
1221
1222     name = strmake( "%s/LINGUAS", po_dir );
1223     if (!(f = fopen( name, "r" )))
1224     {
1225         free( name );
1226         return;
1227     }
1228     free( name );
1229     while (fgets( buffer, sizeof(buffer), f ))
1230     {
1231         if ((p = strchr( buffer, '#' ))) *p = 0;
1232         for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
1233         {
1234             for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
1235                 if (!strcmp( tok, languages[i].name )) break;
1236
1237             if (i == sizeof(languages)/sizeof(languages[0]))
1238                 error( "unknown language '%s'\n", tok );
1239
1240             name = strmake( "%s/%s.mo", po_dir, tok );
1241             load_mo_file( name );
1242             translate_resources( new_language(languages[i].id, languages[i].sub) );
1243             free_mo_file();
1244             free( name );
1245         }
1246     }
1247     fclose( f );
1248
1249 done:
1250     /* prepend the translated resources to the global list */
1251     if (new_tail)
1252     {
1253         new_tail->next = resource_top;
1254         resource_top->prev = new_tail;
1255         resource_top = new_top;
1256     }
1257 }