msxml3: Use a helper to get property values.
[wine/multimedia.git] / tools / wrc / po.c
blobe882a7cb853f603cab089439dfc5dbb43bf76c7b
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 static const char dnt[] = "do not translate";
507 po_message_t msg;
508 po_message_iterator_t iterator;
509 int codepage;
510 char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
512 if (!msgid->size) return;
514 id_buffer = id = convert_msgid_ascii( msgid, 1 );
515 context = get_message_context( &id );
516 if (context && strcmp(context, dnt) == 0)
518 /* This string should not be translated */
519 free( id_buffer );
520 return;
523 if (msgstr)
525 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
526 else codepage = get_language_codepage( 0, 0 );
527 assert( codepage != -1 );
528 str_buffer = str = convert_string_utf8( msgstr, codepage );
529 if (is_english( lang )) get_message_context( &str );
531 if (!(msg = find_message( po, id, context, &iterator )))
533 msg = po_message_create();
534 po_message_set_msgid( msg, id );
535 po_message_set_msgstr( msg, str ? str : "" );
536 if (context) po_message_set_msgctxt( msg, context );
537 po_message_insert( iterator, msg );
539 if (msgid->loc.file) po_message_add_filepos( msg, msgid->loc.file, msgid->loc.line );
540 po_message_iterator_free( iterator );
541 free( id_buffer );
542 free( str_buffer );
545 struct po_file_lang
547 struct list entry;
548 language_t lang;
549 po_file_t po;
552 static struct list po_file_langs = LIST_INIT( po_file_langs );
554 static po_file_t create_po_file(void)
556 po_file_t po;
557 po_message_t msg;
558 po_message_iterator_t iterator;
560 po = po_file_create();
561 iterator = po_message_iterator( po, NULL );
562 msg = po_message_create();
563 po_message_set_msgid( msg, "" );
564 po_message_set_msgstr( msg,
565 "Project-Id-Version: Wine\n"
566 "Report-Msgid-Bugs-To: http://bugs.winehq.org\n"
567 "POT-Creation-Date: N/A\n"
568 "PO-Revision-Date: N/A\n"
569 "Last-Translator: Automatically generated\n"
570 "Language-Team: none\n"
571 "MIME-Version: 1.0\n"
572 "Content-Type: text/plain; charset=UTF-8\n"
573 "Content-Transfer-Encoding: 8bit\n" );
574 po_message_insert( iterator, msg );
575 po_message_iterator_free( iterator );
576 return po;
579 static po_file_t get_po_file( const language_t *lang )
581 struct po_file_lang *po_file;
583 LIST_FOR_EACH_ENTRY( po_file, &po_file_langs, struct po_file_lang, entry )
584 if (po_file->lang.id == lang->id && po_file->lang.sub == lang->sub) return po_file->po;
586 /* create a new one */
587 po_file = xmalloc( sizeof(*po_file) );
588 po_file->lang = *lang;
589 po_file->po = create_po_file();
590 list_add_tail( &po_file_langs, &po_file->entry );
591 return po_file->po;
594 static char *get_po_file_name( const language_t *lang )
596 unsigned int i;
597 char name[40];
599 sprintf( name, "%02x-%02x", lang->id, lang->sub );
600 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
602 if (languages[i].id == lang->id && languages[i].sub == lang->sub)
604 strcpy( name, languages[i].name );
605 break;
608 strcat( name, ".po" );
609 return xstrdup( name );
612 static unsigned int flush_po_files( const char *output_name )
614 struct po_file_lang *po_file, *next;
615 unsigned int count = 0;
617 LIST_FOR_EACH_ENTRY_SAFE( po_file, next, &po_file_langs, struct po_file_lang, entry )
619 char *name = get_po_file_name( &po_file->lang );
620 if (output_name)
622 const char *p = strrchr( output_name, '/' );
623 if (p) p++;
624 else p = output_name;
625 if (!strcmp( p, name ))
627 po_file_write( po_file->po, name, &po_xerror_handler );
628 count++;
631 else /* no specified output name, output a file for every language found */
633 po_file_write( po_file->po, name, &po_xerror_handler );
634 count++;
635 fprintf( stderr, "created %s\n", name );
637 po_file_free( po_file->po );
638 list_remove( &po_file->entry );
639 free( po_file );
640 free( name );
642 return count;
645 static void add_pot_stringtable( po_file_t po, const resource_t *res )
647 const stringtable_t *stt = res->res.stt;
648 int i;
650 while (stt)
652 for (i = 0; i < stt->nentries; i++)
653 if (stt->entries[i].str) add_po_string( po, stt->entries[i].str, NULL, NULL );
654 stt = stt->next;
658 static void add_po_stringtable( const resource_t *english, const resource_t *res )
660 const stringtable_t *english_stt = english->res.stt;
661 const stringtable_t *stt = res->res.stt;
662 po_file_t po = get_po_file( stt->lvc.language );
663 int i;
665 while (english_stt && stt)
667 for (i = 0; i < stt->nentries; i++)
668 if (english_stt->entries[i].str && stt->entries[i].str)
669 add_po_string( po, english_stt->entries[i].str, stt->entries[i].str, stt->lvc.language );
670 stt = stt->next;
671 english_stt = english_stt->next;
675 static void add_pot_dialog_controls( po_file_t po, const control_t *ctrl )
677 while (ctrl)
679 if (control_has_title( ctrl )) add_po_string( po, ctrl->title->name.s_name, NULL, NULL );
680 ctrl = ctrl->next;
684 static void add_pot_dialog( po_file_t po, const resource_t *res )
686 const dialog_t *dlg = res->res.dlg;
688 if (dlg->title) add_po_string( po, dlg->title, NULL, NULL );
689 if (dlg->font) add_po_string( po, dlg->font->name, NULL, NULL );
690 add_pot_dialog_controls( po, dlg->controls );
693 static void add_po_dialog_controls( po_file_t po, const control_t *english_ctrl,
694 const control_t *ctrl, const language_t *lang )
696 while (english_ctrl && ctrl)
698 if (control_has_title( english_ctrl ) && control_has_title( ctrl ))
699 add_po_string( po, english_ctrl->title->name.s_name, ctrl->title->name.s_name, lang );
701 ctrl = ctrl->next;
702 english_ctrl = english_ctrl->next;
706 static void add_po_dialog( const resource_t *english, const resource_t *res )
708 const dialog_t *english_dlg = english->res.dlg;
709 const dialog_t *dlg = res->res.dlg;
710 po_file_t po = get_po_file( dlg->lvc.language );
712 if (english_dlg->title && dlg->title)
713 add_po_string( po, english_dlg->title, dlg->title, dlg->lvc.language );
714 if (english_dlg->font && dlg->font)
715 add_po_string( po, english_dlg->font->name, dlg->font->name, dlg->lvc.language );
716 add_po_dialog_controls( po, english_dlg->controls, dlg->controls, dlg->lvc.language );
719 static void add_pot_menu_items( po_file_t po, const menu_item_t *item )
721 while (item)
723 if (item->name) add_po_string( po, item->name, NULL, NULL );
724 if (item->popup) add_pot_menu_items( po, item->popup );
725 item = item->next;
729 static void add_pot_menu( po_file_t po, const resource_t *res )
731 add_pot_menu_items( po, res->res.men->items );
734 static void add_po_menu_items( po_file_t po, const menu_item_t *english_item,
735 const menu_item_t *item, const language_t *lang )
737 while (english_item && item)
739 if (english_item->name && item->name)
740 add_po_string( po, english_item->name, item->name, lang );
741 if (english_item->popup && item->popup)
742 add_po_menu_items( po, english_item->popup, item->popup, lang );
743 item = item->next;
744 english_item = english_item->next;
748 static void add_po_menu( const resource_t *english, const resource_t *res )
750 const menu_item_t *english_items = english->res.men->items;
751 const menu_item_t *items = res->res.men->items;
752 po_file_t po = get_po_file( res->res.men->lvc.language );
754 add_po_menu_items( po, english_items, items, res->res.men->lvc.language );
757 static resource_t *find_english_resource( resource_t *res )
759 resource_t *ptr;
761 for (ptr = resource_top; ptr; ptr = ptr->next)
763 if (ptr->type != res->type) continue;
764 if (!ptr->lan) continue;
765 if (!is_english( ptr->lan )) continue;
766 if (compare_name_id( ptr->name, res->name )) continue;
767 return ptr;
769 return NULL;
772 void write_pot_file( const char *outname )
774 resource_t *res;
775 po_file_t po = create_po_file();
777 for (res = resource_top; res; res = res->next)
779 if (!is_english( res->lan )) continue;
781 switch (res->type)
783 case res_acc: break; /* FIXME */
784 case res_dlg: add_pot_dialog( po, res ); break;
785 case res_men: add_pot_menu( po, res ); break;
786 case res_stt: add_pot_stringtable( po, res ); break;
787 case res_msg: break; /* FIXME */
788 default: break;
791 po_file_write( po, outname, &po_xerror_handler );
792 po_file_free( po );
795 void write_po_files( const char *outname )
797 resource_t *res, *english;
799 for (res = resource_top; res; res = res->next)
801 if (!(english = find_english_resource( res ))) continue;
802 switch (res->type)
804 case res_acc: break; /* FIXME */
805 case res_dlg: add_po_dialog( english, res ); break;
806 case res_men: add_po_menu( english, res ); break;
807 case res_stt: add_po_stringtable( english, res ); break;
808 case res_msg: break; /* FIXME */
809 default: break;
812 if (!flush_po_files( outname ))
814 if (outname) error( "No translations found for %s\n", outname );
815 else error( "No translations found\n" );
819 #endif /* HAVE_LIBGETTEXTPO */
821 static string_t *translate_string( po_file_t po, string_t *str, int *found )
823 string_t *new;
824 const char *transl;
825 int res;
826 char *buffer, *msgid, *context;
828 if (!str->size || !(buffer = convert_msgid_ascii( str, 0 )))
829 return convert_string( str, str_unicode, 1252 );
831 msgid = buffer;
832 context = get_message_context( &msgid );
833 transl = get_msgstr( po, msgid, context, found );
835 new = xmalloc( sizeof(*new) );
836 new->type = str_unicode;
837 new->size = wine_utf8_mbstowcs( 0, transl, strlen(transl), NULL, 0 );
838 new->str.wstr = xmalloc( (new->size+1) * sizeof(WCHAR) );
839 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, transl, strlen(transl), new->str.wstr, new->size );
840 if (res == -2)
841 error( "Invalid utf-8 character in string '%s'\n", transl );
842 new->str.wstr[new->size] = 0;
843 free( buffer );
844 return new;
847 static control_t *translate_controls( po_file_t po, control_t *ctrl, int *found )
849 control_t *new, *head = NULL, *tail = NULL;
851 while (ctrl)
853 new = xmalloc( sizeof(*new) );
854 *new = *ctrl;
855 if (control_has_title( ctrl ))
857 new->title = new_name_id();
858 *new->title = *ctrl->title;
859 new->title->name.s_name = translate_string( po, ctrl->title->name.s_name, found );
861 else new->title = dup_name_id( ctrl->title );
862 new->ctlclass = dup_name_id( ctrl->ctlclass );
863 if (tail) tail->next = new;
864 else head = new;
865 new->next = NULL;
866 new->prev = tail;
867 tail = new;
868 ctrl = ctrl->next;
870 return head;
873 static menu_item_t *translate_items( po_file_t po, menu_item_t *item, int *found )
875 menu_item_t *new, *head = NULL, *tail = NULL;
877 while (item)
879 new = xmalloc( sizeof(*new) );
880 *new = *item;
881 if (item->name) new->name = translate_string( po, item->name, found );
882 if (item->popup) new->popup = translate_items( po, item->popup, found );
883 if (tail) tail->next = new;
884 else head = new;
885 new->next = NULL;
886 new->prev = tail;
887 tail = new;
888 item = item->next;
890 return head;
893 static stringtable_t *translate_stringtable( po_file_t po, stringtable_t *stt,
894 language_t *lang, int *found )
896 stringtable_t *new, *head = NULL, *tail = NULL;
897 int i;
899 while (stt)
901 new = xmalloc( sizeof(*new) );
902 *new = *stt;
903 new->lvc.language = lang;
904 new->lvc.version = get_dup_version( lang );
905 new->entries = xmalloc( new->nentries * sizeof(*new->entries) );
906 memcpy( new->entries, stt->entries, new->nentries * sizeof(*new->entries) );
907 for (i = 0; i < stt->nentries; i++)
908 if (stt->entries[i].str)
909 new->entries[i].str = translate_string( po, stt->entries[i].str, found );
911 if (tail) tail->next = new;
912 else head = new;
913 new->next = NULL;
914 new->prev = tail;
915 tail = new;
916 stt = stt->next;
918 return head;
921 static void translate_dialog( po_file_t po, dialog_t *dlg, dialog_t *new, int *found )
923 if (dlg->title) new->title = translate_string( po, dlg->title, found );
924 if (dlg->font)
926 new->font = xmalloc( sizeof(*dlg->font) );
927 new->font = dlg->font;
928 new->font->name = translate_string( po, dlg->font->name, found );
930 new->controls = translate_controls( po, dlg->controls, found );
933 static void translate_resources( po_file_t po, language_t *lang )
935 resource_t *res;
937 for (res = resource_top; res; res = res->next)
939 resource_t *new = NULL;
940 int found = 0;
942 if (!is_english( res->lan )) continue;
944 switch (res->type)
946 case res_acc:
947 /* FIXME */
948 break;
949 case res_dlg:
950 new = dup_resource( res, lang );
951 translate_dialog( po, res->res.dlg, new->res.dlg, &found );
952 break;
953 case res_men:
954 new = dup_resource( res, lang );
955 new->res.men->items = translate_items( po, res->res.men->items, &found );
956 break;
957 case res_stt:
958 new = dup_resource( res, lang );
959 new->res.stt = translate_stringtable( po, res->res.stt, lang, &found );
960 break;
961 case res_msg:
962 /* FIXME */
963 break;
964 default:
965 break;
968 if (new && found)
970 if (new_tail) new_tail->next = new;
971 else new_top = new;
972 new->prev = new_tail;
973 new_tail = new;
978 void add_translations( const char *po_dir )
980 resource_t *res;
981 po_file_t po;
982 char buffer[256];
983 char *p, *tok, *name;
984 unsigned int i;
985 FILE *f;
987 /* first check if we have English resources to translate */
988 for (res = resource_top; res; res = res->next) if (is_english( res->lan )) break;
989 if (!res) return;
991 new_top = new_tail = NULL;
993 name = strmake( "%s/LINGUAS", po_dir );
994 if (!(f = fopen( name, "r" )))
996 free( name );
997 return;
999 free( name );
1000 while (fgets( buffer, sizeof(buffer), f ))
1002 if ((p = strchr( buffer, '#' ))) *p = 0;
1003 for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
1005 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
1006 if (!strcmp( tok, languages[i].name )) break;
1008 if (i == sizeof(languages)/sizeof(languages[0]))
1009 error( "unknown language '%s'\n", tok );
1011 name = strmake( "%s/%s.po", po_dir, tok );
1012 po = read_po_file( name );
1013 translate_resources( po, new_language(languages[i].id, languages[i].sub) );
1014 po_file_free( po );
1015 free( name );
1018 fclose( f );
1020 /* prepend the translated resources to the global list */
1021 if (new_tail)
1023 new_tail->next = resource_top;
1024 resource_top->prev = new_tail;
1025 resource_top = new_top;