wrc: Store the current po file in a global variable.
[wine/multimedia.git] / tools / wrc / po.c
blobfd55ba4bb8ad2921639b7673de73217db2f2096b
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 static const char *get_msgstr( const char *msgid, const char *context, int *found )
398 if (context) (*found)++;
399 return msgid;
402 static void load_po_file( const char *name )
406 static void free_po_file(void)
410 void write_pot_file( const char *outname )
412 error( "PO files not supported in this wrc build\n" );
415 void write_po_files( const char *outname )
417 error( "PO files not supported in this wrc build\n" );
420 #else /* HAVE_LIBGETTEXTPO */
422 static po_file_t po_file;
424 static void po_xerror( int severity, po_message_t message,
425 const char *filename, size_t lineno, size_t column,
426 int multiline_p, const char *message_text )
428 fprintf( stderr, "%s:%u:%u: %s\n",
429 filename, (unsigned int)lineno, (unsigned int)column, message_text );
430 if (severity) exit(1);
433 static void po_xerror2( int severity, po_message_t message1,
434 const char *filename1, size_t lineno1, size_t column1,
435 int multiline_p1, const char *message_text1,
436 po_message_t message2,
437 const char *filename2, size_t lineno2, size_t column2,
438 int multiline_p2, const char *message_text2 )
440 fprintf( stderr, "%s:%u:%u: %s\n",
441 filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
442 fprintf( stderr, "%s:%u:%u: %s\n",
443 filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
444 if (severity) exit(1);
447 static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
449 static char *convert_string_utf8( const string_t *str, int codepage )
451 string_t *newstr = convert_string( str, str_unicode, codepage );
452 char *buffer = xmalloc( newstr->size * 4 + 1 );
453 int len = wine_utf8_wcstombs( 0, newstr->str.wstr, newstr->size, buffer, newstr->size * 4 );
454 buffer[len] = 0;
455 free_string( newstr );
456 return buffer;
459 static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
460 po_message_iterator_t *iterator )
462 po_message_t msg;
463 const char *context;
465 *iterator = po_message_iterator( po, NULL );
466 while ((msg = po_next_message( *iterator )))
468 if (strcmp( po_message_msgid( msg ), msgid )) continue;
469 if (!msgctxt) break;
470 if (!(context = po_message_msgctxt( msg ))) continue;
471 if (!strcmp( context, msgctxt )) break;
473 return msg;
476 static const char *get_msgstr( const char *msgid, const char *context, int *found )
478 const char *ret = msgid;
479 po_message_t msg;
480 po_message_iterator_t iterator;
482 msg = find_message( po_file, msgid, context, &iterator );
483 if (msg && !po_message_is_fuzzy( msg ))
485 ret = po_message_msgstr( msg );
486 if (!ret[0]) ret = msgid; /* ignore empty strings */
487 else (*found)++;
489 po_message_iterator_free( iterator );
490 return ret;
493 static void load_po_file( const char *name )
495 if (!(po_file = po_file_read( name, &po_xerror_handler )))
496 error( "cannot load po file '%s'\n", name );
499 static void free_po_file(void)
501 po_file_free( po_file );
502 po_file = NULL;
505 static void add_po_string( po_file_t po, const string_t *msgid, const string_t *msgstr,
506 const language_t *lang )
508 static const char dnt[] = "do not translate";
509 po_message_t msg;
510 po_message_iterator_t iterator;
511 int codepage;
512 char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
514 if (!msgid->size) return;
516 id_buffer = id = convert_msgid_ascii( msgid, 1 );
517 context = get_message_context( &id );
518 if (context && strcmp(context, dnt) == 0)
520 /* This string should not be translated */
521 free( id_buffer );
522 return;
525 if (msgstr)
527 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
528 else codepage = get_language_codepage( 0, 0 );
529 assert( codepage != -1 );
530 str_buffer = str = convert_string_utf8( msgstr, codepage );
531 if (is_english( lang )) get_message_context( &str );
533 if (!(msg = find_message( po, id, context, &iterator )))
535 msg = po_message_create();
536 po_message_set_msgid( msg, id );
537 po_message_set_msgstr( msg, str ? str : "" );
538 if (context) po_message_set_msgctxt( msg, context );
539 po_message_insert( iterator, msg );
541 if (msgid->loc.file) po_message_add_filepos( msg, msgid->loc.file, msgid->loc.line );
542 po_message_iterator_free( iterator );
543 free( id_buffer );
544 free( str_buffer );
547 struct po_file_lang
549 struct list entry;
550 language_t lang;
551 po_file_t po;
554 static struct list po_file_langs = LIST_INIT( po_file_langs );
556 static po_file_t create_po_file(void)
558 po_file_t po;
559 po_message_t msg;
560 po_message_iterator_t iterator;
562 po = po_file_create();
563 iterator = po_message_iterator( po, NULL );
564 msg = po_message_create();
565 po_message_set_msgid( msg, "" );
566 po_message_set_msgstr( msg,
567 "Project-Id-Version: Wine\n"
568 "Report-Msgid-Bugs-To: http://bugs.winehq.org\n"
569 "POT-Creation-Date: N/A\n"
570 "PO-Revision-Date: N/A\n"
571 "Last-Translator: Automatically generated\n"
572 "Language-Team: none\n"
573 "MIME-Version: 1.0\n"
574 "Content-Type: text/plain; charset=UTF-8\n"
575 "Content-Transfer-Encoding: 8bit\n" );
576 po_message_insert( iterator, msg );
577 po_message_iterator_free( iterator );
578 return po;
581 static po_file_t get_po_file( const language_t *lang )
583 struct po_file_lang *po_file;
585 LIST_FOR_EACH_ENTRY( po_file, &po_file_langs, struct po_file_lang, entry )
586 if (po_file->lang.id == lang->id && po_file->lang.sub == lang->sub) return po_file->po;
588 /* create a new one */
589 po_file = xmalloc( sizeof(*po_file) );
590 po_file->lang = *lang;
591 po_file->po = create_po_file();
592 list_add_tail( &po_file_langs, &po_file->entry );
593 return po_file->po;
596 static char *get_po_file_name( const language_t *lang )
598 unsigned int i;
599 char name[40];
601 sprintf( name, "%02x-%02x", lang->id, lang->sub );
602 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
604 if (languages[i].id == lang->id && languages[i].sub == lang->sub)
606 strcpy( name, languages[i].name );
607 break;
610 strcat( name, ".po" );
611 return xstrdup( name );
614 static unsigned int flush_po_files( const char *output_name )
616 struct po_file_lang *po_file, *next;
617 unsigned int count = 0;
619 LIST_FOR_EACH_ENTRY_SAFE( po_file, next, &po_file_langs, struct po_file_lang, entry )
621 char *name = get_po_file_name( &po_file->lang );
622 if (output_name)
624 const char *p = strrchr( output_name, '/' );
625 if (p) p++;
626 else p = output_name;
627 if (!strcmp( p, name ))
629 po_file_write( po_file->po, name, &po_xerror_handler );
630 count++;
633 else /* no specified output name, output a file for every language found */
635 po_file_write( po_file->po, name, &po_xerror_handler );
636 count++;
637 fprintf( stderr, "created %s\n", name );
639 po_file_free( po_file->po );
640 list_remove( &po_file->entry );
641 free( po_file );
642 free( name );
644 return count;
647 static void add_pot_stringtable( po_file_t po, const resource_t *res )
649 const stringtable_t *stt = res->res.stt;
650 int i;
652 while (stt)
654 for (i = 0; i < stt->nentries; i++)
655 if (stt->entries[i].str) add_po_string( po, stt->entries[i].str, NULL, NULL );
656 stt = stt->next;
660 static void add_po_stringtable( const resource_t *english, const resource_t *res )
662 const stringtable_t *english_stt = english->res.stt;
663 const stringtable_t *stt = res->res.stt;
664 po_file_t po = get_po_file( stt->lvc.language );
665 int i;
667 while (english_stt && stt)
669 for (i = 0; i < stt->nentries; i++)
670 if (english_stt->entries[i].str && stt->entries[i].str)
671 add_po_string( po, english_stt->entries[i].str, stt->entries[i].str, stt->lvc.language );
672 stt = stt->next;
673 english_stt = english_stt->next;
677 static void add_pot_dialog_controls( po_file_t po, const control_t *ctrl )
679 while (ctrl)
681 if (control_has_title( ctrl )) add_po_string( po, ctrl->title->name.s_name, NULL, NULL );
682 ctrl = ctrl->next;
686 static void add_pot_dialog( po_file_t po, const resource_t *res )
688 const dialog_t *dlg = res->res.dlg;
690 if (dlg->title) add_po_string( po, dlg->title, NULL, NULL );
691 if (dlg->font) add_po_string( po, dlg->font->name, NULL, NULL );
692 add_pot_dialog_controls( po, dlg->controls );
695 static void add_po_dialog_controls( po_file_t po, const control_t *english_ctrl,
696 const control_t *ctrl, const language_t *lang )
698 while (english_ctrl && ctrl)
700 if (control_has_title( english_ctrl ) && control_has_title( ctrl ))
701 add_po_string( po, english_ctrl->title->name.s_name, ctrl->title->name.s_name, lang );
703 ctrl = ctrl->next;
704 english_ctrl = english_ctrl->next;
708 static void add_po_dialog( const resource_t *english, const resource_t *res )
710 const dialog_t *english_dlg = english->res.dlg;
711 const dialog_t *dlg = res->res.dlg;
712 po_file_t po = get_po_file( dlg->lvc.language );
714 if (english_dlg->title && dlg->title)
715 add_po_string( po, english_dlg->title, dlg->title, dlg->lvc.language );
716 if (english_dlg->font && dlg->font)
717 add_po_string( po, english_dlg->font->name, dlg->font->name, dlg->lvc.language );
718 add_po_dialog_controls( po, english_dlg->controls, dlg->controls, dlg->lvc.language );
721 static void add_pot_menu_items( po_file_t po, const menu_item_t *item )
723 while (item)
725 if (item->name) add_po_string( po, item->name, NULL, NULL );
726 if (item->popup) add_pot_menu_items( po, item->popup );
727 item = item->next;
731 static void add_pot_menu( po_file_t po, const resource_t *res )
733 add_pot_menu_items( po, res->res.men->items );
736 static void add_po_menu_items( po_file_t po, const menu_item_t *english_item,
737 const menu_item_t *item, const language_t *lang )
739 while (english_item && item)
741 if (english_item->name && item->name)
742 add_po_string( po, english_item->name, item->name, lang );
743 if (english_item->popup && item->popup)
744 add_po_menu_items( po, english_item->popup, item->popup, lang );
745 item = item->next;
746 english_item = english_item->next;
750 static void add_po_menu( const resource_t *english, const resource_t *res )
752 const menu_item_t *english_items = english->res.men->items;
753 const menu_item_t *items = res->res.men->items;
754 po_file_t po = get_po_file( res->res.men->lvc.language );
756 add_po_menu_items( po, english_items, items, res->res.men->lvc.language );
759 static resource_t *find_english_resource( resource_t *res )
761 resource_t *ptr;
763 for (ptr = resource_top; ptr; ptr = ptr->next)
765 if (ptr->type != res->type) continue;
766 if (!ptr->lan) continue;
767 if (!is_english( ptr->lan )) continue;
768 if (compare_name_id( ptr->name, res->name )) continue;
769 return ptr;
771 return NULL;
774 void write_pot_file( const char *outname )
776 resource_t *res;
777 po_file_t po = create_po_file();
779 for (res = resource_top; res; res = res->next)
781 if (!is_english( res->lan )) continue;
783 switch (res->type)
785 case res_acc: break; /* FIXME */
786 case res_dlg: add_pot_dialog( po, res ); break;
787 case res_men: add_pot_menu( po, res ); break;
788 case res_stt: add_pot_stringtable( po, res ); break;
789 case res_msg: break; /* FIXME */
790 default: break;
793 po_file_write( po, outname, &po_xerror_handler );
794 po_file_free( po );
797 void write_po_files( const char *outname )
799 resource_t *res, *english;
801 for (res = resource_top; res; res = res->next)
803 if (!(english = find_english_resource( res ))) continue;
804 switch (res->type)
806 case res_acc: break; /* FIXME */
807 case res_dlg: add_po_dialog( english, res ); break;
808 case res_men: add_po_menu( english, res ); break;
809 case res_stt: add_po_stringtable( english, res ); break;
810 case res_msg: break; /* FIXME */
811 default: break;
814 if (!flush_po_files( outname ))
816 if (outname) error( "No translations found for %s\n", outname );
817 else error( "No translations found\n" );
821 #endif /* HAVE_LIBGETTEXTPO */
823 static string_t *translate_string( string_t *str, int *found )
825 string_t *new;
826 const char *transl;
827 int res;
828 char *buffer, *msgid, *context;
830 if (!str->size || !(buffer = convert_msgid_ascii( str, 0 )))
831 return convert_string( str, str_unicode, 1252 );
833 msgid = buffer;
834 context = get_message_context( &msgid );
835 transl = get_msgstr( msgid, context, found );
837 new = xmalloc( sizeof(*new) );
838 new->type = str_unicode;
839 new->size = wine_utf8_mbstowcs( 0, transl, strlen(transl), NULL, 0 );
840 new->str.wstr = xmalloc( (new->size+1) * sizeof(WCHAR) );
841 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, transl, strlen(transl), new->str.wstr, new->size );
842 if (res == -2)
843 error( "Invalid utf-8 character in string '%s'\n", transl );
844 new->str.wstr[new->size] = 0;
845 free( buffer );
846 return new;
849 static control_t *translate_controls( control_t *ctrl, int *found )
851 control_t *new, *head = NULL, *tail = NULL;
853 while (ctrl)
855 new = xmalloc( sizeof(*new) );
856 *new = *ctrl;
857 if (control_has_title( ctrl ))
859 new->title = new_name_id();
860 *new->title = *ctrl->title;
861 new->title->name.s_name = translate_string( ctrl->title->name.s_name, found );
863 else new->title = dup_name_id( ctrl->title );
864 new->ctlclass = dup_name_id( ctrl->ctlclass );
865 if (tail) tail->next = new;
866 else head = new;
867 new->next = NULL;
868 new->prev = tail;
869 tail = new;
870 ctrl = ctrl->next;
872 return head;
875 static menu_item_t *translate_items( menu_item_t *item, int *found )
877 menu_item_t *new, *head = NULL, *tail = NULL;
879 while (item)
881 new = xmalloc( sizeof(*new) );
882 *new = *item;
883 if (item->name) new->name = translate_string( item->name, found );
884 if (item->popup) new->popup = translate_items( item->popup, found );
885 if (tail) tail->next = new;
886 else head = new;
887 new->next = NULL;
888 new->prev = tail;
889 tail = new;
890 item = item->next;
892 return head;
895 static stringtable_t *translate_stringtable( stringtable_t *stt, language_t *lang, int *found )
897 stringtable_t *new, *head = NULL, *tail = NULL;
898 int i;
900 while (stt)
902 new = xmalloc( sizeof(*new) );
903 *new = *stt;
904 new->lvc.language = lang;
905 new->lvc.version = get_dup_version( lang );
906 new->entries = xmalloc( new->nentries * sizeof(*new->entries) );
907 memcpy( new->entries, stt->entries, new->nentries * sizeof(*new->entries) );
908 for (i = 0; i < stt->nentries; i++)
909 if (stt->entries[i].str)
910 new->entries[i].str = translate_string( stt->entries[i].str, found );
912 if (tail) tail->next = new;
913 else head = new;
914 new->next = NULL;
915 new->prev = tail;
916 tail = new;
917 stt = stt->next;
919 return head;
922 static void translate_dialog( dialog_t *dlg, dialog_t *new, int *found )
924 if (dlg->title) new->title = translate_string( dlg->title, found );
925 if (dlg->font)
927 new->font = xmalloc( sizeof(*dlg->font) );
928 new->font = dlg->font;
929 new->font->name = translate_string( dlg->font->name, found );
931 new->controls = translate_controls( dlg->controls, found );
934 static void translate_resources( language_t *lang )
936 resource_t *res;
938 for (res = resource_top; res; res = res->next)
940 resource_t *new = NULL;
941 int found = 0;
943 if (!is_english( res->lan )) continue;
945 switch (res->type)
947 case res_acc:
948 /* FIXME */
949 break;
950 case res_dlg:
951 new = dup_resource( res, lang );
952 translate_dialog( res->res.dlg, new->res.dlg, &found );
953 break;
954 case res_men:
955 new = dup_resource( res, lang );
956 new->res.men->items = translate_items( res->res.men->items, &found );
957 break;
958 case res_stt:
959 new = dup_resource( res, lang );
960 new->res.stt = translate_stringtable( res->res.stt, lang, &found );
961 break;
962 case res_msg:
963 /* FIXME */
964 break;
965 default:
966 break;
969 if (new && found)
971 if (new_tail) new_tail->next = new;
972 else new_top = new;
973 new->prev = new_tail;
974 new_tail = new;
979 void add_translations( const char *po_dir )
981 resource_t *res;
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 load_po_file( name );
1013 translate_resources( new_language(languages[i].id, languages[i].sub) );
1014 free_po_file();
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;