wrc: Don't include gettext-po.h if we don't have the corresponding library.
[wine/multimedia.git] / tools / wrc / po.c
blob3efd208cd47dc9ee548eb7c5b89bf70294754b10
1 /*
2 * Support for po files
4 * Copyright 2010 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #ifdef HAVE_LIBGETTEXTPO
30 #include <gettext-po.h>
31 #endif
33 #include "wrc.h"
34 #include "genres.h"
35 #include "newstruc.h"
36 #include "utils.h"
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "wine/list.h"
42 #include "wine/unicode.h"
44 static resource_t *new_top, *new_tail;
46 static int is_english( const language_t *lan )
48 return lan->id == LANG_ENGLISH && lan->sub == SUBLANG_DEFAULT;
51 static version_t *get_dup_version( language_t *lang )
53 /* English "translations" take precedence over the original rc contents */
54 return new_version( is_english( lang ) ? 1 : -1 );
57 static name_id_t *dup_name_id( name_id_t *id )
59 name_id_t *new;
61 if (!id || id->type != name_str) return id;
62 new = new_name_id();
63 *new = *id;
64 new->name.s_name = convert_string( id->name.s_name, str_unicode, 1252 );
65 return new;
68 static char *convert_msgid_ascii( const string_t *str, int error_on_invalid_char )
70 int i;
71 string_t *newstr = convert_string( str, str_unicode, 1252 );
72 char *buffer = xmalloc( newstr->size + 1 );
74 for (i = 0; i < newstr->size; i++)
76 buffer[i] = newstr->str.wstr[i];
77 if (newstr->str.wstr[i] >= 32 && newstr->str.wstr[i] <= 127) continue;
78 if (newstr->str.wstr[i] == '\t' || newstr->str.wstr[i] == '\n') continue;
79 if (error_on_invalid_char)
81 print_location( &newstr->loc );
82 error( "Invalid character %04x in source string\n", newstr->str.wstr[i] );
84 free( buffer);
85 free_string( newstr );
86 return NULL;
88 buffer[i] = 0;
89 free_string( newstr );
90 return buffer;
93 static char *get_message_context( char **msgid )
95 static const char magic[] = "#msgctxt#";
96 char *id, *context;
98 if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
99 context = *msgid + sizeof(magic) - 1;
100 if (!(id = strchr( context, '#' ))) return NULL;
101 *id = 0;
102 *msgid = id + 1;
103 return context;
106 static int control_has_title( const control_t *ctrl )
108 if (!ctrl->title) return 0;
109 if (ctrl->title->type != name_str) return 0;
110 /* check for text static control */
111 if (ctrl->ctlclass && ctrl->ctlclass->type == name_ord && ctrl->ctlclass->name.i_name == CT_STATIC)
113 switch (ctrl->style->or_mask & SS_TYPEMASK)
115 case SS_LEFT:
116 case SS_CENTER:
117 case SS_RIGHT:
118 return 1;
119 default:
120 return 0;
123 return 1;
126 static resource_t *dup_resource( resource_t *res, language_t *lang )
128 resource_t *new = xmalloc( sizeof(*new) );
130 *new = *res;
131 new->lan = lang;
132 new->next = new->prev = NULL;
133 new->name = dup_name_id( res->name );
135 switch (res->type)
137 case res_dlg:
138 new->res.dlg = xmalloc( sizeof(*(new)->res.dlg) );
139 *new->res.dlg = *res->res.dlg;
140 new->res.dlg->lvc.language = lang;
141 new->res.dlg->lvc.version = get_dup_version( lang );
142 break;
143 case res_men:
144 new->res.men = xmalloc( sizeof(*(new)->res.men) );
145 *new->res.men = *res->res.men;
146 new->res.men->lvc.language = lang;
147 new->res.men->lvc.version = get_dup_version( lang );
148 break;
149 case res_stt:
150 new->res.stt = xmalloc( sizeof(*(new)->res.stt) );
151 *new->res.stt = *res->res.stt;
152 new->res.stt->lvc.language = lang;
153 new->res.stt->lvc.version = get_dup_version( lang );
154 break;
155 default:
156 assert(0);
158 return new;
161 static const struct
163 unsigned int id, sub;
164 const char *name;
165 } languages[] =
167 { LANG_ARABIC, SUBLANG_NEUTRAL, "ar" },
168 { LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA, "ar_SA" },
169 { LANG_ARABIC, SUBLANG_ARABIC_IRAQ, "ar_IQ" },
170 { LANG_ARABIC, SUBLANG_ARABIC_EGYPT, "ar_EG" },
171 { LANG_ARABIC, SUBLANG_ARABIC_LIBYA, "ar_LY" },
172 { LANG_ARABIC, SUBLANG_ARABIC_ALGERIA, "ar_DZ" },
173 { LANG_ARABIC, SUBLANG_ARABIC_MOROCCO, "ar_MA" },
174 { LANG_ARABIC, SUBLANG_ARABIC_TUNISIA, "ar_TN" },
175 { LANG_ARABIC, SUBLANG_ARABIC_OMAN, "ar_OM" },
176 { LANG_ARABIC, SUBLANG_ARABIC_YEMEN, "ar_YE" },
177 { LANG_ARABIC, SUBLANG_ARABIC_SYRIA, "ar_SY" },
178 { LANG_ARABIC, SUBLANG_ARABIC_JORDAN, "ar_JO" },
179 { LANG_ARABIC, SUBLANG_ARABIC_LEBANON, "ar_LB" },
180 { LANG_ARABIC, SUBLANG_ARABIC_KUWAIT, "ar_KW" },
181 { LANG_ARABIC, SUBLANG_ARABIC_UAE, "ar_AE" },
182 { LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN, "ar_BH" },
183 { LANG_ARABIC, SUBLANG_ARABIC_QATAR, "ar_QA" },
184 { LANG_BULGARIAN, SUBLANG_NEUTRAL, "bg" },
185 { LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA, "bg_BG" },
186 { LANG_CATALAN, SUBLANG_NEUTRAL, "ca" },
187 { LANG_CATALAN, SUBLANG_CATALAN_CATALAN, "ca_ES" },
188 { LANG_CHINESE, SUBLANG_NEUTRAL, "zh" },
189 { LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL, "zh_TW" },
190 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, "zh_CN" },
191 { LANG_CHINESE, SUBLANG_CHINESE_HONGKONG, "zh_HK" },
192 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, "zh_SG" },
193 { LANG_CHINESE, SUBLANG_CHINESE_MACAU, "zh_MO" },
194 { LANG_CZECH, SUBLANG_NEUTRAL, "cs" },
195 { LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC, "cs_CZ" },
196 { LANG_DANISH, SUBLANG_NEUTRAL, "da" },
197 { LANG_DANISH, SUBLANG_DANISH_DENMARK, "da_DK" },
198 { LANG_GERMAN, SUBLANG_NEUTRAL, "de" },
199 { LANG_GERMAN, SUBLANG_GERMAN, "de_DE" },
200 { LANG_GERMAN, SUBLANG_GERMAN_SWISS, "de_CH" },
201 { LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN, "de_AT" },
202 { LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG, "de_LU" },
203 { LANG_GERMAN, SUBLANG_GERMAN_LIECHTENSTEIN, "de_LI" },
204 { LANG_GREEK, SUBLANG_NEUTRAL, "el" },
205 { LANG_GREEK, SUBLANG_GREEK_GREECE, "el_GR" },
206 { LANG_ENGLISH, SUBLANG_NEUTRAL, "en" },
207 { LANG_ENGLISH, SUBLANG_ENGLISH_US, "en_US" },
208 { LANG_ENGLISH, SUBLANG_ENGLISH_UK, "en_GB" },
209 { LANG_ENGLISH, SUBLANG_ENGLISH_AUS, "en_AU" },
210 { LANG_ENGLISH, SUBLANG_ENGLISH_CAN, "en_CA" },
211 { LANG_ENGLISH, SUBLANG_ENGLISH_NZ, "en_NZ" },
212 { LANG_ENGLISH, SUBLANG_ENGLISH_EIRE, "en_IE" },
213 { LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA, "en_ZA" },
214 { LANG_ENGLISH, SUBLANG_ENGLISH_JAMAICA, "en_JM" },
215 { LANG_ENGLISH, SUBLANG_ENGLISH_CARIBBEAN, "en_CB" },
216 { LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE, "en_BZ" },
217 { LANG_ENGLISH, SUBLANG_ENGLISH_TRINIDAD, "en_TT" },
218 { LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE, "en_ZW" },
219 { LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES, "en_PH" },
220 { LANG_SPANISH, SUBLANG_NEUTRAL, "es" },
221 { LANG_SPANISH, SUBLANG_SPANISH, "es_ES" },
222 { LANG_SPANISH, SUBLANG_SPANISH_MEXICAN, "es_MX" },
223 { LANG_SPANISH, SUBLANG_SPANISH_MODERN, "es_ES_modern" },
224 { LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA, "es_GT" },
225 { LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA, "es_CR" },
226 { LANG_SPANISH, SUBLANG_SPANISH_PANAMA, "es_PA" },
227 { LANG_SPANISH, SUBLANG_SPANISH_DOMINICAN_REPUBLIC, "es_DO" },
228 { LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA, "es_VE" },
229 { LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA, "es_CO" },
230 { LANG_SPANISH, SUBLANG_SPANISH_PERU, "es_PE" },
231 { LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA, "es_AR" },
232 { LANG_SPANISH, SUBLANG_SPANISH_ECUADOR, "es_EC" },
233 { LANG_SPANISH, SUBLANG_SPANISH_CHILE, "es_CL" },
234 { LANG_SPANISH, SUBLANG_SPANISH_URUGUAY, "es_UY" },
235 { LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY, "es_PY" },
236 { LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA, "es_BO" },
237 { LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR, "es_SV" },
238 { LANG_SPANISH, SUBLANG_SPANISH_HONDURAS, "es_HN" },
239 { LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA, "es_NI" },
240 { LANG_SPANISH, SUBLANG_SPANISH_PUERTO_RICO, "es_PR" },
241 { LANG_FINNISH, SUBLANG_NEUTRAL, "fi" },
242 { LANG_FINNISH, SUBLANG_FINNISH_FINLAND, "fi_FI" },
243 { LANG_FRENCH, SUBLANG_NEUTRAL, "fr" },
244 { LANG_FRENCH, SUBLANG_FRENCH, "fr_FR" },
245 { LANG_FRENCH, SUBLANG_FRENCH_BELGIAN, "fr_BE" },
246 { LANG_FRENCH, SUBLANG_FRENCH_CANADIAN, "fr_CA" },
247 { LANG_FRENCH, SUBLANG_FRENCH_SWISS, "fr_CH" },
248 { LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG, "fr_LU" },
249 { LANG_FRENCH, SUBLANG_FRENCH_MONACO, "fr_MC" },
250 { LANG_HEBREW, SUBLANG_NEUTRAL, "he" },
251 { LANG_HEBREW, SUBLANG_HEBREW_ISRAEL, "he_IL" },
252 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, "hu" },
253 { LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY, "hu_HU" },
254 { LANG_ICELANDIC, SUBLANG_NEUTRAL, "is" },
255 { LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND, "is_IS" },
256 { LANG_ITALIAN, SUBLANG_NEUTRAL, "it" },
257 { LANG_ITALIAN, SUBLANG_ITALIAN, "it_IT" },
258 { LANG_ITALIAN, SUBLANG_ITALIAN_SWISS, "it_CH" },
259 { LANG_JAPANESE, SUBLANG_NEUTRAL, "ja" },
260 { LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN, "ja_JP" },
261 { LANG_KOREAN, SUBLANG_NEUTRAL, "ko" },
262 { LANG_KOREAN, SUBLANG_KOREAN, "ko_KR" },
263 { LANG_DUTCH, SUBLANG_NEUTRAL, "nl" },
264 { LANG_DUTCH, SUBLANG_DUTCH, "nl_NL" },
265 { LANG_DUTCH, SUBLANG_DUTCH_BELGIAN, "nl_BE" },
266 { LANG_DUTCH, SUBLANG_DUTCH_SURINAM, "nl_SR" },
267 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL, "nb_NO" },
268 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK, "nn_NO" },
269 { LANG_POLISH, SUBLANG_NEUTRAL, "pl" },
270 { LANG_POLISH, SUBLANG_POLISH_POLAND, "pl_PL" },
271 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, "pt" },
272 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN, "pt_BR" },
273 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_PORTUGAL, "pt_PT" },
274 { LANG_ROMANSH, SUBLANG_NEUTRAL, "rm" },
275 { LANG_ROMANSH, SUBLANG_ROMANSH_SWITZERLAND, "rm_CH" },
276 { LANG_ROMANIAN, SUBLANG_NEUTRAL, "ro" },
277 { LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA, "ro_RO" },
278 { LANG_RUSSIAN, SUBLANG_NEUTRAL, "ru" },
279 { LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA, "ru_RU" },
280 { LANG_SERBIAN, SUBLANG_NEUTRAL, "hr" },
281 { LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA, "hr_HR" },
282 { LANG_SERBIAN, SUBLANG_SERBIAN_LATIN, "sr_RS@latin" },
283 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, "sr_RS@cyrillic" },
284 { LANG_SLOVAK, SUBLANG_NEUTRAL, "sk" },
285 { LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA, "sk_SK" },
286 { LANG_ALBANIAN, SUBLANG_NEUTRAL, "sq" },
287 { LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA, "sq_AL" },
288 { LANG_SWEDISH, SUBLANG_NEUTRAL, "sv" },
289 { LANG_SWEDISH, SUBLANG_SWEDISH_SWEDEN, "sv_SE" },
290 { LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND, "sv_FI" },
291 { LANG_THAI, SUBLANG_NEUTRAL, "th" },
292 { LANG_THAI, SUBLANG_THAI_THAILAND, "th_TH" },
293 { LANG_TURKISH, SUBLANG_NEUTRAL, "tr" },
294 { LANG_TURKISH, SUBLANG_TURKISH_TURKEY, "tr_TR" },
295 { LANG_URDU, SUBLANG_NEUTRAL, "ur" },
296 { LANG_URDU, SUBLANG_URDU_PAKISTAN, "ur_PK" },
297 { LANG_INDONESIAN, SUBLANG_NEUTRAL, "id" },
298 { LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA, "id_ID" },
299 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, "uk" },
300 { LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE, "uk_UA" },
301 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, "be" },
302 { LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS, "be_BY" },
303 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, "sl" },
304 { LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA, "sl_SI" },
305 { LANG_ESTONIAN, SUBLANG_NEUTRAL, "et" },
306 { LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA, "et_EE" },
307 { LANG_LATVIAN, SUBLANG_NEUTRAL, "lv" },
308 { LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA, "lv_LV" },
309 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, "lt" },
310 { LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA, "lt_LT" },
311 { LANG_PERSIAN, SUBLANG_NEUTRAL, "fa" },
312 { LANG_PERSIAN, SUBLANG_PERSIAN_IRAN, "fa_IR" },
313 { LANG_ARMENIAN, SUBLANG_NEUTRAL, "hy" },
314 { LANG_ARMENIAN, SUBLANG_ARMENIAN_ARMENIA, "hy_AM" },
315 { LANG_AZERI, SUBLANG_NEUTRAL, "az" },
316 { LANG_AZERI, SUBLANG_AZERI_LATIN, "az_AZ@latin" },
317 { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, "az_AZ@cyrillic" },
318 { LANG_BASQUE, SUBLANG_NEUTRAL, "eu" },
319 { LANG_BASQUE, SUBLANG_BASQUE_BASQUE, "eu_ES" },
320 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, "mk" },
321 { LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA, "mk_MK" },
322 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, "af" },
323 { LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA, "af_ZA" },
324 { LANG_GEORGIAN, SUBLANG_NEUTRAL, "ka" },
325 { LANG_GEORGIAN, SUBLANG_GEORGIAN_GEORGIA, "ka_GE" },
326 { LANG_FAEROESE, SUBLANG_NEUTRAL, "fo" },
327 { LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS, "fo_FO" },
328 { LANG_HINDI, SUBLANG_NEUTRAL, "hi" },
329 { LANG_HINDI, SUBLANG_HINDI_INDIA, "hi_IN" },
330 { LANG_MALAY, SUBLANG_NEUTRAL, "ms" },
331 { LANG_MALAY, SUBLANG_MALAY_MALAYSIA, "ms_MY" },
332 { LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM, "ms_BN" },
333 { LANG_KAZAK, SUBLANG_NEUTRAL, "kk" },
334 { LANG_KAZAK, SUBLANG_KAZAK_KAZAKHSTAN, "kk_KZ" },
335 { LANG_KYRGYZ, SUBLANG_NEUTRAL, "ky" },
336 { LANG_KYRGYZ, SUBLANG_KYRGYZ_KYRGYZSTAN, "ky_KG" },
337 { LANG_SWAHILI, SUBLANG_NEUTRAL, "sw" },
338 { LANG_SWAHILI, SUBLANG_SWAHILI_KENYA, "sw_KE" },
339 { LANG_UZBEK, SUBLANG_NEUTRAL, "uz" },
340 { LANG_UZBEK, SUBLANG_UZBEK_LATIN, "uz_UZ@latin" },
341 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, "uz_UZ@cyrillic" },
342 { LANG_TATAR, SUBLANG_NEUTRAL, "tt" },
343 { LANG_TATAR, SUBLANG_TATAR_RUSSIA, "tt_TA" },
344 { LANG_PUNJABI, SUBLANG_NEUTRAL, "pa" },
345 { LANG_PUNJABI, SUBLANG_PUNJABI_INDIA, "pa_IN" },
346 { LANG_GUJARATI, SUBLANG_NEUTRAL, "gu" },
347 { LANG_GUJARATI, SUBLANG_GUJARATI_INDIA, "gu_IN" },
348 { LANG_ORIYA, SUBLANG_NEUTRAL, "or" },
349 { LANG_ORIYA, SUBLANG_ORIYA_INDIA, "or_IN" },
350 { LANG_TAMIL, SUBLANG_NEUTRAL, "ta" },
351 { LANG_TAMIL, SUBLANG_TAMIL_INDIA, "ta_IN" },
352 { LANG_TELUGU, SUBLANG_NEUTRAL, "te" },
353 { LANG_TELUGU, SUBLANG_TELUGU_INDIA, "te_IN" },
354 { LANG_KANNADA, SUBLANG_NEUTRAL, "kn" },
355 { LANG_KANNADA, SUBLANG_KANNADA_INDIA, "kn_IN" },
356 { LANG_MALAYALAM, SUBLANG_NEUTRAL, "ml" },
357 { LANG_MALAYALAM, SUBLANG_MALAYALAM_INDIA, "ml_IN" },
358 { LANG_MARATHI, SUBLANG_NEUTRAL, "mr" },
359 { LANG_MARATHI, SUBLANG_MARATHI_INDIA, "mr_IN" },
360 { LANG_SANSKRIT, SUBLANG_NEUTRAL, "sa" },
361 { LANG_SANSKRIT, SUBLANG_SANSKRIT_INDIA, "sa_IN" },
362 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, "mn" },
363 { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
364 { LANG_WELSH, SUBLANG_NEUTRAL, "cy" },
365 { LANG_WELSH, SUBLANG_WELSH_UNITED_KINGDOM, "cy_GB" },
366 { LANG_GALICIAN, SUBLANG_NEUTRAL, "gl" },
367 { LANG_GALICIAN, SUBLANG_GALICIAN_GALICIAN, "gl_ES" },
368 { LANG_KONKANI, SUBLANG_NEUTRAL, "kok" },
369 { LANG_KONKANI, SUBLANG_KONKANI_INDIA, "kok_IN" },
370 { LANG_DIVEHI, SUBLANG_NEUTRAL, "dv" },
371 { LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES, "dv_MV" },
372 { LANG_BRETON, SUBLANG_NEUTRAL, "br" },
373 { LANG_BRETON, SUBLANG_BRETON_FRANCE, "br_FR" },
375 #ifdef LANG_ESPERANTO
376 { LANG_ESPERANTO, SUBLANG_DEFAULT, "eo" },
377 #endif
378 #ifdef LANG_WALON
379 { LANG_WALON, SUBLANG_NEUTRAL, "wa" },
380 { LANG_WALON, SUBLANG_DEFAULT, "wa_BE" },
381 #endif
382 #ifdef LANG_CORNISH
383 { LANG_CORNISH, SUBLANG_NEUTRAL, "kw" },
384 { LANG_CORNISH, SUBLANG_DEFAULT, "kw_GB" },
385 #endif
386 #ifdef LANG_GAELIC
387 { LANG_GAELIC, SUBLANG_NEUTRAL, "ga" },
388 { LANG_GAELIC, SUBLANG_GAELIC, "ga_IE" },
389 { LANG_GAELIC, SUBLANG_GAELIC_SCOTTISH, "gd_GB" },
390 { LANG_GAELIC, SUBLANG_GAELIC_MANX, "gv_GB" },
391 #endif
394 #ifndef HAVE_LIBGETTEXTPO
396 typedef void *po_file_t;
398 static const char *get_msgstr( po_file_t po, const char *msgid, const char *context, int *found )
400 if (context) (*found)++;
401 return msgid;
404 static po_file_t read_po_file( const char *name )
406 return NULL;
409 static void po_file_free ( po_file_t po )
413 void write_pot_file( const char *outname )
415 error( "PO files not supported in this wrc build\n" );
418 void write_po_files( const char *outname )
420 error( "PO files not supported in this wrc build\n" );
423 #else /* HAVE_LIBGETTEXTPO */
425 static void po_xerror( int severity, po_message_t message,
426 const char *filename, size_t lineno, size_t column,
427 int multiline_p, const char *message_text )
429 fprintf( stderr, "%s:%u:%u: %s\n",
430 filename, (unsigned int)lineno, (unsigned int)column, message_text );
431 if (severity) exit(1);
434 static void po_xerror2( int severity, po_message_t message1,
435 const char *filename1, size_t lineno1, size_t column1,
436 int multiline_p1, const char *message_text1,
437 po_message_t message2,
438 const char *filename2, size_t lineno2, size_t column2,
439 int multiline_p2, const char *message_text2 )
441 fprintf( stderr, "%s:%u:%u: %s\n",
442 filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
443 fprintf( stderr, "%s:%u:%u: %s\n",
444 filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
445 if (severity) exit(1);
448 static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
450 static char *convert_string_utf8( const string_t *str, int codepage )
452 string_t *newstr = convert_string( str, str_unicode, codepage );
453 char *buffer = xmalloc( newstr->size * 4 + 1 );
454 int len = wine_utf8_wcstombs( 0, newstr->str.wstr, newstr->size, buffer, newstr->size * 4 );
455 buffer[len] = 0;
456 free_string( newstr );
457 return buffer;
460 static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
461 po_message_iterator_t *iterator )
463 po_message_t msg;
464 const char *context;
466 *iterator = po_message_iterator( po, NULL );
467 while ((msg = po_next_message( *iterator )))
469 if (strcmp( po_message_msgid( msg ), msgid )) continue;
470 if (!msgctxt) break;
471 if (!(context = po_message_msgctxt( msg ))) continue;
472 if (!strcmp( context, msgctxt )) break;
474 return msg;
477 static const char *get_msgstr( po_file_t po, const char *msgid, const char *context, int *found )
479 const char *ret = msgid;
480 po_message_t msg;
481 po_message_iterator_t iterator;
483 msg = find_message( po, msgid, context, &iterator );
484 if (msg && !po_message_is_fuzzy( msg ))
486 ret = po_message_msgstr( msg );
487 if (!ret[0]) ret = msgid; /* ignore empty strings */
488 else (*found)++;
490 po_message_iterator_free( iterator );
491 return ret;
494 static po_file_t read_po_file( const char *name )
496 po_file_t po;
498 if (!(po = po_file_read( name, &po_xerror_handler )))
499 error( "cannot load po file '%s'\n", name );
500 return po;
503 static void add_po_string( po_file_t po, const string_t *msgid, const string_t *msgstr,
504 const language_t *lang )
506 po_message_t msg;
507 po_message_iterator_t iterator;
508 int codepage;
509 char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
511 if (!msgid->size) return;
513 id_buffer = id = convert_msgid_ascii( msgid, 1 );
514 context = get_message_context( &id );
516 if (msgstr)
518 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
519 else codepage = get_language_codepage( 0, 0 );
520 assert( codepage != -1 );
521 str_buffer = str = convert_string_utf8( msgstr, codepage );
522 if (is_english( lang )) get_message_context( &str );
524 if (!(msg = find_message( po, id, context, &iterator )))
526 msg = po_message_create();
527 po_message_set_msgid( msg, id );
528 po_message_set_msgstr( msg, str ? str : "" );
529 if (context) po_message_set_msgctxt( msg, context );
530 po_message_insert( iterator, msg );
532 if (msgid->loc.file) po_message_add_filepos( msg, msgid->loc.file, msgid->loc.line );
533 po_message_iterator_free( iterator );
534 free( id_buffer );
535 free( str_buffer );
538 struct po_file_lang
540 struct list entry;
541 language_t lang;
542 po_file_t po;
545 static struct list po_file_langs = LIST_INIT( po_file_langs );
547 static po_file_t create_po_file(void)
549 po_file_t po;
550 po_message_t msg;
551 po_message_iterator_t iterator;
553 po = po_file_create();
554 iterator = po_message_iterator( po, NULL );
555 msg = po_message_create();
556 po_message_set_msgid( msg, "" );
557 po_message_set_msgstr( msg,
558 "Project-Id-Version: Wine\n"
559 "Report-Msgid-Bugs-To: http://bugs.winehq.org\n"
560 "POT-Creation-Date: N/A\n"
561 "PO-Revision-Date: N/A\n"
562 "Last-Translator: Automatically generated\n"
563 "Language-Team: none\n"
564 "MIME-Version: 1.0\n"
565 "Content-Type: text/plain; charset=UTF-8\n"
566 "Content-Transfer-Encoding: 8bit\n" );
567 po_message_insert( iterator, msg );
568 po_message_iterator_free( iterator );
569 return po;
572 static po_file_t get_po_file( const language_t *lang )
574 struct po_file_lang *po_file;
576 LIST_FOR_EACH_ENTRY( po_file, &po_file_langs, struct po_file_lang, entry )
577 if (po_file->lang.id == lang->id && po_file->lang.sub == lang->sub) return po_file->po;
579 /* create a new one */
580 po_file = xmalloc( sizeof(*po_file) );
581 po_file->lang = *lang;
582 po_file->po = create_po_file();
583 list_add_tail( &po_file_langs, &po_file->entry );
584 return po_file->po;
587 static char *get_po_file_name( const language_t *lang )
589 unsigned int i;
590 char name[40];
592 sprintf( name, "%02x-%02x", lang->id, lang->sub );
593 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
595 if (languages[i].id == lang->id && languages[i].sub == lang->sub)
597 strcpy( name, languages[i].name );
598 break;
601 strcat( name, ".po" );
602 return xstrdup( name );
605 static unsigned int flush_po_files( const char *output_name )
607 struct po_file_lang *po_file, *next;
608 unsigned int count = 0;
610 LIST_FOR_EACH_ENTRY_SAFE( po_file, next, &po_file_langs, struct po_file_lang, entry )
612 char *name = get_po_file_name( &po_file->lang );
613 if (output_name)
615 const char *p = strrchr( output_name, '/' );
616 if (p) p++;
617 else p = output_name;
618 if (!strcmp( p, name ))
620 po_file_write( po_file->po, name, &po_xerror_handler );
621 count++;
624 else /* no specified output name, output a file for every language found */
626 po_file_write( po_file->po, name, &po_xerror_handler );
627 count++;
628 fprintf( stderr, "created %s\n", name );
630 po_file_free( po_file->po );
631 list_remove( &po_file->entry );
632 free( po_file );
633 free( name );
635 return count;
638 static void add_pot_stringtable( po_file_t po, const resource_t *res )
640 const stringtable_t *stt = res->res.stt;
641 int i;
643 while (stt)
645 for (i = 0; i < stt->nentries; i++)
646 if (stt->entries[i].str) add_po_string( po, stt->entries[i].str, NULL, NULL );
647 stt = stt->next;
651 static void add_po_stringtable( const resource_t *english, const resource_t *res )
653 const stringtable_t *english_stt = english->res.stt;
654 const stringtable_t *stt = res->res.stt;
655 po_file_t po = get_po_file( stt->lvc.language );
656 int i;
658 while (english_stt && stt)
660 for (i = 0; i < stt->nentries; i++)
661 if (english_stt->entries[i].str && stt->entries[i].str)
662 add_po_string( po, english_stt->entries[i].str, stt->entries[i].str, stt->lvc.language );
663 stt = stt->next;
664 english_stt = english_stt->next;
668 static void add_pot_dialog_controls( po_file_t po, const control_t *ctrl )
670 while (ctrl)
672 if (control_has_title( ctrl )) add_po_string( po, ctrl->title->name.s_name, NULL, NULL );
673 ctrl = ctrl->next;
677 static void add_pot_dialog( po_file_t po, const resource_t *res )
679 const dialog_t *dlg = res->res.dlg;
681 if (dlg->title) add_po_string( po, dlg->title, NULL, NULL );
682 if (dlg->font) add_po_string( po, dlg->font->name, NULL, NULL );
683 add_pot_dialog_controls( po, dlg->controls );
686 static void add_po_dialog_controls( po_file_t po, const control_t *english_ctrl,
687 const control_t *ctrl, const language_t *lang )
689 while (english_ctrl && ctrl)
691 if (control_has_title( english_ctrl ) && control_has_title( ctrl ))
692 add_po_string( po, english_ctrl->title->name.s_name, ctrl->title->name.s_name, lang );
694 ctrl = ctrl->next;
695 english_ctrl = english_ctrl->next;
699 static void add_po_dialog( const resource_t *english, const resource_t *res )
701 const dialog_t *english_dlg = english->res.dlg;
702 const dialog_t *dlg = res->res.dlg;
703 po_file_t po = get_po_file( dlg->lvc.language );
705 if (english_dlg->title && dlg->title)
706 add_po_string( po, english_dlg->title, dlg->title, dlg->lvc.language );
707 if (english_dlg->font && dlg->font)
708 add_po_string( po, english_dlg->font->name, dlg->font->name, dlg->lvc.language );
709 add_po_dialog_controls( po, english_dlg->controls, dlg->controls, dlg->lvc.language );
712 static void add_pot_menu_items( po_file_t po, const menu_item_t *item )
714 while (item)
716 if (item->name) add_po_string( po, item->name, NULL, NULL );
717 if (item->popup) add_pot_menu_items( po, item->popup );
718 item = item->next;
722 static void add_pot_menu( po_file_t po, const resource_t *res )
724 add_pot_menu_items( po, res->res.men->items );
727 static void add_po_menu_items( po_file_t po, const menu_item_t *english_item,
728 const menu_item_t *item, const language_t *lang )
730 while (english_item && item)
732 if (english_item->name && item->name)
733 add_po_string( po, english_item->name, item->name, lang );
734 if (english_item->popup && item->popup)
735 add_po_menu_items( po, english_item->popup, item->popup, lang );
736 item = item->next;
737 english_item = english_item->next;
741 static void add_po_menu( const resource_t *english, const resource_t *res )
743 const menu_item_t *english_items = english->res.men->items;
744 const menu_item_t *items = res->res.men->items;
745 po_file_t po = get_po_file( res->res.men->lvc.language );
747 add_po_menu_items( po, english_items, items, res->res.men->lvc.language );
750 static resource_t *find_english_resource( resource_t *res )
752 resource_t *ptr;
754 for (ptr = resource_top; ptr; ptr = ptr->next)
756 if (ptr->type != res->type) continue;
757 if (!ptr->lan) continue;
758 if (!is_english( ptr->lan )) continue;
759 if (compare_name_id( ptr->name, res->name )) continue;
760 return ptr;
762 return NULL;
765 void write_pot_file( const char *outname )
767 resource_t *res;
768 po_file_t po = create_po_file();
770 for (res = resource_top; res; res = res->next)
772 if (!is_english( res->lan )) continue;
774 switch (res->type)
776 case res_acc: break; /* FIXME */
777 case res_dlg: add_pot_dialog( po, res ); break;
778 case res_men: add_pot_menu( po, res ); break;
779 case res_stt: add_pot_stringtable( po, res ); break;
780 case res_msg: break; /* FIXME */
781 default: break;
784 po_file_write( po, outname, &po_xerror_handler );
785 po_file_free( po );
788 void write_po_files( const char *outname )
790 resource_t *res, *english;
792 for (res = resource_top; res; res = res->next)
794 if (!(english = find_english_resource( res ))) continue;
795 switch (res->type)
797 case res_acc: break; /* FIXME */
798 case res_dlg: add_po_dialog( english, res ); break;
799 case res_men: add_po_menu( english, res ); break;
800 case res_stt: add_po_stringtable( english, res ); break;
801 case res_msg: break; /* FIXME */
802 default: break;
805 if (!flush_po_files( outname ))
807 if (outname) error( "No translations found for %s\n", outname );
808 else error( "No translations found\n" );
812 #endif /* HAVE_LIBGETTEXTPO */
814 static string_t *translate_string( po_file_t po, string_t *str, int *found )
816 string_t *new;
817 const char *transl;
818 int res;
819 char *buffer, *msgid, *context;
821 if (!str->size || !(buffer = convert_msgid_ascii( str, 0 )))
822 return convert_string( str, str_unicode, 1252 );
824 msgid = buffer;
825 context = get_message_context( &msgid );
826 transl = get_msgstr( po, msgid, context, found );
828 new = xmalloc( sizeof(*new) );
829 new->type = str_unicode;
830 new->size = wine_utf8_mbstowcs( 0, transl, strlen(transl), NULL, 0 );
831 new->str.wstr = xmalloc( (new->size+1) * sizeof(WCHAR) );
832 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, transl, strlen(transl), new->str.wstr, new->size );
833 if (res == -2)
834 error( "Invalid utf-8 character in string '%s'\n", transl );
835 new->str.wstr[new->size] = 0;
836 free( buffer );
837 return new;
840 static control_t *translate_controls( po_file_t po, control_t *ctrl, int *found )
842 control_t *new, *head = NULL, *tail = NULL;
844 while (ctrl)
846 new = xmalloc( sizeof(*new) );
847 *new = *ctrl;
848 if (control_has_title( ctrl ))
850 new->title = new_name_id();
851 *new->title = *ctrl->title;
852 new->title->name.s_name = translate_string( po, ctrl->title->name.s_name, found );
854 else new->title = dup_name_id( ctrl->title );
855 new->ctlclass = dup_name_id( ctrl->ctlclass );
856 if (tail) tail->next = new;
857 else head = new;
858 new->next = NULL;
859 new->prev = tail;
860 tail = new;
861 ctrl = ctrl->next;
863 return head;
866 static menu_item_t *translate_items( po_file_t po, menu_item_t *item, int *found )
868 menu_item_t *new, *head = NULL, *tail = NULL;
870 while (item)
872 new = xmalloc( sizeof(*new) );
873 *new = *item;
874 if (item->name) new->name = translate_string( po, item->name, found );
875 if (item->popup) new->popup = translate_items( po, item->popup, found );
876 if (tail) tail->next = new;
877 else head = new;
878 new->next = NULL;
879 new->prev = tail;
880 tail = new;
881 item = item->next;
883 return head;
886 static stringtable_t *translate_stringtable( po_file_t po, stringtable_t *stt,
887 language_t *lang, int *found )
889 stringtable_t *new, *head = NULL, *tail = NULL;
890 int i;
892 while (stt)
894 new = xmalloc( sizeof(*new) );
895 *new = *stt;
896 new->lvc.language = lang;
897 new->lvc.version = get_dup_version( lang );
898 new->entries = xmalloc( new->nentries * sizeof(*new->entries) );
899 memcpy( new->entries, stt->entries, new->nentries * sizeof(*new->entries) );
900 for (i = 0; i < stt->nentries; i++)
901 if (stt->entries[i].str)
902 new->entries[i].str = translate_string( po, stt->entries[i].str, found );
904 if (tail) tail->next = new;
905 else head = new;
906 new->next = NULL;
907 new->prev = tail;
908 tail = new;
909 stt = stt->next;
911 return head;
914 static void translate_dialog( po_file_t po, dialog_t *dlg, dialog_t *new, int *found )
916 if (dlg->title) new->title = translate_string( po, dlg->title, found );
917 if (dlg->font)
919 new->font = xmalloc( sizeof(*dlg->font) );
920 new->font = dlg->font;
921 new->font->name = translate_string( po, dlg->font->name, found );
923 new->controls = translate_controls( po, dlg->controls, found );
926 static void translate_resources( po_file_t po, language_t *lang )
928 resource_t *res;
930 for (res = resource_top; res; res = res->next)
932 resource_t *new = NULL;
933 int found = 0;
935 if (!is_english( res->lan )) continue;
937 switch (res->type)
939 case res_acc:
940 /* FIXME */
941 break;
942 case res_dlg:
943 new = dup_resource( res, lang );
944 translate_dialog( po, res->res.dlg, new->res.dlg, &found );
945 break;
946 case res_men:
947 new = dup_resource( res, lang );
948 new->res.men->items = translate_items( po, res->res.men->items, &found );
949 break;
950 case res_stt:
951 new = dup_resource( res, lang );
952 new->res.stt = translate_stringtable( po, res->res.stt, lang, &found );
953 break;
954 case res_msg:
955 /* FIXME */
956 break;
957 default:
958 break;
961 if (new && found)
963 if (new_tail) new_tail->next = new;
964 else new_top = new;
965 new->prev = new_tail;
966 new_tail = new;
971 void add_translations( const char *po_dir )
973 resource_t *res;
974 po_file_t po;
975 char buffer[256];
976 char *p, *tok, *name;
977 unsigned int i;
978 FILE *f;
980 /* first check if we have English resources to translate */
981 for (res = resource_top; res; res = res->next) if (is_english( res->lan )) break;
982 if (!res) return;
984 new_top = new_tail = NULL;
986 name = strmake( "%s/LINGUAS", po_dir );
987 if (!(f = fopen( name, "r" )))
989 free( name );
990 return;
992 free( name );
993 while (fgets( buffer, sizeof(buffer), f ))
995 if ((p = strchr( buffer, '#' ))) *p = 0;
996 for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
998 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
999 if (!strcmp( tok, languages[i].name )) break;
1001 if (i == sizeof(languages)/sizeof(languages[0]))
1002 error( "unknown language '%s'\n", tok );
1004 name = strmake( "%s/%s.po", po_dir, tok );
1005 po = read_po_file( name );
1006 translate_resources( po, new_language(languages[i].id, languages[i].sub) );
1007 po_file_free( po );
1008 free( name );
1011 fclose( f );
1013 /* prepend the translated resources to the global list */
1014 if (new_tail)
1016 new_tail->next = resource_top;
1017 resource_top->prev = new_tail;
1018 resource_top = new_top;