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