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
22 #include "wine/port.h"
30 #ifdef HAVE_LIBGETTEXTPO
31 #include <gettext-po.h>
42 #include "wine/list.h"
43 #include "wine/unicode.h"
45 static resource_t *new_top, *new_tail;
50 unsigned int revision;
52 unsigned int msgid_off;
53 unsigned int msgstr_off;
54 /* ... rest of file data here */
57 static int is_english( const language_t *lan )
59 return lan->id == LANG_ENGLISH && lan->sub == SUBLANG_DEFAULT;
62 static int is_rtl_language( const language_t *lan )
64 return lan->id == LANG_ARABIC || lan->id == LANG_HEBREW || lan->id == LANG_PERSIAN;
67 static int uses_larger_font( const language_t *lan )
69 return lan->id == LANG_CHINESE || lan->id == LANG_JAPANESE || lan->id == LANG_KOREAN;
72 static version_t *get_dup_version( language_t *lang )
74 /* English "translations" take precedence over the original rc contents */
75 return new_version( is_english( lang ) ? 1 : -1 );
78 static name_id_t *dup_name_id( name_id_t *id )
82 if (!id || id->type != name_str) return id;
85 new->name.s_name = convert_string( id->name.s_name, str_unicode, 1252 );
89 static char *convert_msgid_ascii( const string_t *str, int error_on_invalid_char )
92 string_t *newstr = convert_string( str, str_unicode, 1252 );
93 char *buffer = xmalloc( newstr->size + 1 );
95 for (i = 0; i < newstr->size; i++)
97 buffer[i] = newstr->str.wstr[i];
98 if (newstr->str.wstr[i] >= 32 && newstr->str.wstr[i] <= 127) continue;
99 if (newstr->str.wstr[i] == '\t' || newstr->str.wstr[i] == '\n') continue;
100 if (error_on_invalid_char)
102 print_location( &newstr->loc );
103 error( "Invalid character %04x in source string\n", newstr->str.wstr[i] );
106 free_string( newstr );
110 free_string( newstr );
114 static char *get_message_context( char **msgid )
116 static const char magic[] = "#msgctxt#";
119 if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
120 context = *msgid + sizeof(magic) - 1;
121 if (!(id = strchr( context, '#' ))) return NULL;
127 static int control_has_title( const control_t *ctrl )
129 if (!ctrl->title) return 0;
130 if (ctrl->title->type != name_str) return 0;
131 /* check for text static control */
132 if (ctrl->ctlclass && ctrl->ctlclass->type == name_ord && ctrl->ctlclass->name.i_name == CT_STATIC)
134 switch (ctrl->style->or_mask & SS_TYPEMASK)
147 static resource_t *dup_resource( resource_t *res, language_t *lang )
149 resource_t *new = xmalloc( sizeof(*new) );
153 new->next = new->prev = NULL;
154 new->name = dup_name_id( res->name );
159 new->res.acc = xmalloc( sizeof(*(new)->res.acc) );
160 *new->res.acc = *res->res.acc;
161 new->res.acc->lvc.language = lang;
162 new->res.acc->lvc.version = get_dup_version( lang );
165 new->res.dlg = xmalloc( sizeof(*(new)->res.dlg) );
166 *new->res.dlg = *res->res.dlg;
167 new->res.dlg->lvc.language = lang;
168 new->res.dlg->lvc.version = get_dup_version( lang );
171 new->res.men = xmalloc( sizeof(*(new)->res.men) );
172 *new->res.men = *res->res.men;
173 new->res.men->lvc.language = lang;
174 new->res.men->lvc.version = get_dup_version( lang );
177 new->res.stt = xmalloc( sizeof(*(new)->res.stt) );
178 *new->res.stt = *res->res.stt;
179 new->res.stt->lvc.language = lang;
180 new->res.stt->lvc.version = get_dup_version( lang );
190 unsigned int id, sub;
194 { LANG_ARABIC, SUBLANG_NEUTRAL, "ar" },
195 { LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA, "ar_SA" },
196 { LANG_ARABIC, SUBLANG_ARABIC_IRAQ, "ar_IQ" },
197 { LANG_ARABIC, SUBLANG_ARABIC_EGYPT, "ar_EG" },
198 { LANG_ARABIC, SUBLANG_ARABIC_LIBYA, "ar_LY" },
199 { LANG_ARABIC, SUBLANG_ARABIC_ALGERIA, "ar_DZ" },
200 { LANG_ARABIC, SUBLANG_ARABIC_MOROCCO, "ar_MA" },
201 { LANG_ARABIC, SUBLANG_ARABIC_TUNISIA, "ar_TN" },
202 { LANG_ARABIC, SUBLANG_ARABIC_OMAN, "ar_OM" },
203 { LANG_ARABIC, SUBLANG_ARABIC_YEMEN, "ar_YE" },
204 { LANG_ARABIC, SUBLANG_ARABIC_SYRIA, "ar_SY" },
205 { LANG_ARABIC, SUBLANG_ARABIC_JORDAN, "ar_JO" },
206 { LANG_ARABIC, SUBLANG_ARABIC_LEBANON, "ar_LB" },
207 { LANG_ARABIC, SUBLANG_ARABIC_KUWAIT, "ar_KW" },
208 { LANG_ARABIC, SUBLANG_ARABIC_UAE, "ar_AE" },
209 { LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN, "ar_BH" },
210 { LANG_ARABIC, SUBLANG_ARABIC_QATAR, "ar_QA" },
211 { LANG_BULGARIAN, SUBLANG_NEUTRAL, "bg" },
212 { LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA, "bg_BG" },
213 { LANG_CATALAN, SUBLANG_NEUTRAL, "ca" },
214 { LANG_CATALAN, SUBLANG_CATALAN_CATALAN, "ca_ES" },
215 { LANG_CHINESE, SUBLANG_NEUTRAL, "zh" },
216 { LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL, "zh_TW" },
217 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, "zh_CN" },
218 { LANG_CHINESE, SUBLANG_CHINESE_HONGKONG, "zh_HK" },
219 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, "zh_SG" },
220 { LANG_CHINESE, SUBLANG_CHINESE_MACAU, "zh_MO" },
221 { LANG_CZECH, SUBLANG_NEUTRAL, "cs" },
222 { LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC, "cs_CZ" },
223 { LANG_DANISH, SUBLANG_NEUTRAL, "da" },
224 { LANG_DANISH, SUBLANG_DANISH_DENMARK, "da_DK" },
225 { LANG_GERMAN, SUBLANG_NEUTRAL, "de" },
226 { LANG_GERMAN, SUBLANG_GERMAN, "de_DE" },
227 { LANG_GERMAN, SUBLANG_GERMAN_SWISS, "de_CH" },
228 { LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN, "de_AT" },
229 { LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG, "de_LU" },
230 { LANG_GERMAN, SUBLANG_GERMAN_LIECHTENSTEIN, "de_LI" },
231 { LANG_GREEK, SUBLANG_NEUTRAL, "el" },
232 { LANG_GREEK, SUBLANG_GREEK_GREECE, "el_GR" },
233 { LANG_ENGLISH, SUBLANG_NEUTRAL, "en" },
234 { LANG_ENGLISH, SUBLANG_ENGLISH_US, "en_US" },
235 { LANG_ENGLISH, SUBLANG_ENGLISH_UK, "en_GB" },
236 { LANG_ENGLISH, SUBLANG_ENGLISH_AUS, "en_AU" },
237 { LANG_ENGLISH, SUBLANG_ENGLISH_CAN, "en_CA" },
238 { LANG_ENGLISH, SUBLANG_ENGLISH_NZ, "en_NZ" },
239 { LANG_ENGLISH, SUBLANG_ENGLISH_EIRE, "en_IE" },
240 { LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA, "en_ZA" },
241 { LANG_ENGLISH, SUBLANG_ENGLISH_JAMAICA, "en_JM" },
242 { LANG_ENGLISH, SUBLANG_ENGLISH_CARIBBEAN, "en_CB" },
243 { LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE, "en_BZ" },
244 { LANG_ENGLISH, SUBLANG_ENGLISH_TRINIDAD, "en_TT" },
245 { LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE, "en_ZW" },
246 { LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES, "en_PH" },
247 { LANG_SPANISH, SUBLANG_NEUTRAL, "es" },
248 { LANG_SPANISH, SUBLANG_SPANISH, "es_ES" },
249 { LANG_SPANISH, SUBLANG_SPANISH_MEXICAN, "es_MX" },
250 { LANG_SPANISH, SUBLANG_SPANISH_MODERN, "es_ES_modern" },
251 { LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA, "es_GT" },
252 { LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA, "es_CR" },
253 { LANG_SPANISH, SUBLANG_SPANISH_PANAMA, "es_PA" },
254 { LANG_SPANISH, SUBLANG_SPANISH_DOMINICAN_REPUBLIC, "es_DO" },
255 { LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA, "es_VE" },
256 { LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA, "es_CO" },
257 { LANG_SPANISH, SUBLANG_SPANISH_PERU, "es_PE" },
258 { LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA, "es_AR" },
259 { LANG_SPANISH, SUBLANG_SPANISH_ECUADOR, "es_EC" },
260 { LANG_SPANISH, SUBLANG_SPANISH_CHILE, "es_CL" },
261 { LANG_SPANISH, SUBLANG_SPANISH_URUGUAY, "es_UY" },
262 { LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY, "es_PY" },
263 { LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA, "es_BO" },
264 { LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR, "es_SV" },
265 { LANG_SPANISH, SUBLANG_SPANISH_HONDURAS, "es_HN" },
266 { LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA, "es_NI" },
267 { LANG_SPANISH, SUBLANG_SPANISH_PUERTO_RICO, "es_PR" },
268 { LANG_FINNISH, SUBLANG_NEUTRAL, "fi" },
269 { LANG_FINNISH, SUBLANG_FINNISH_FINLAND, "fi_FI" },
270 { LANG_FRENCH, SUBLANG_NEUTRAL, "fr" },
271 { LANG_FRENCH, SUBLANG_FRENCH, "fr_FR" },
272 { LANG_FRENCH, SUBLANG_FRENCH_BELGIAN, "fr_BE" },
273 { LANG_FRENCH, SUBLANG_FRENCH_CANADIAN, "fr_CA" },
274 { LANG_FRENCH, SUBLANG_FRENCH_SWISS, "fr_CH" },
275 { LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG, "fr_LU" },
276 { LANG_FRENCH, SUBLANG_FRENCH_MONACO, "fr_MC" },
277 { LANG_HEBREW, SUBLANG_NEUTRAL, "he" },
278 { LANG_HEBREW, SUBLANG_HEBREW_ISRAEL, "he_IL" },
279 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, "hu" },
280 { LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY, "hu_HU" },
281 { LANG_ICELANDIC, SUBLANG_NEUTRAL, "is" },
282 { LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND, "is_IS" },
283 { LANG_ITALIAN, SUBLANG_NEUTRAL, "it" },
284 { LANG_ITALIAN, SUBLANG_ITALIAN, "it_IT" },
285 { LANG_ITALIAN, SUBLANG_ITALIAN_SWISS, "it_CH" },
286 { LANG_JAPANESE, SUBLANG_NEUTRAL, "ja" },
287 { LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN, "ja_JP" },
288 { LANG_KOREAN, SUBLANG_NEUTRAL, "ko" },
289 { LANG_KOREAN, SUBLANG_KOREAN, "ko_KR" },
290 { LANG_DUTCH, SUBLANG_NEUTRAL, "nl" },
291 { LANG_DUTCH, SUBLANG_DUTCH, "nl_NL" },
292 { LANG_DUTCH, SUBLANG_DUTCH_BELGIAN, "nl_BE" },
293 { LANG_DUTCH, SUBLANG_DUTCH_SURINAM, "nl_SR" },
294 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL, "nb_NO" },
295 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK, "nn_NO" },
296 { LANG_POLISH, SUBLANG_NEUTRAL, "pl" },
297 { LANG_POLISH, SUBLANG_POLISH_POLAND, "pl_PL" },
298 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, "pt" },
299 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN, "pt_BR" },
300 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_PORTUGAL, "pt_PT" },
301 { LANG_ROMANSH, SUBLANG_NEUTRAL, "rm" },
302 { LANG_ROMANSH, SUBLANG_ROMANSH_SWITZERLAND, "rm_CH" },
303 { LANG_ROMANIAN, SUBLANG_NEUTRAL, "ro" },
304 { LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA, "ro_RO" },
305 { LANG_RUSSIAN, SUBLANG_NEUTRAL, "ru" },
306 { LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA, "ru_RU" },
307 { LANG_SERBIAN, SUBLANG_NEUTRAL, "hr" },
308 { LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA, "hr_HR" },
309 { LANG_SERBIAN, SUBLANG_SERBIAN_LATIN, "sr_RS@latin" },
310 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, "sr_RS@cyrillic" },
311 { LANG_SLOVAK, SUBLANG_NEUTRAL, "sk" },
312 { LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA, "sk_SK" },
313 { LANG_ALBANIAN, SUBLANG_NEUTRAL, "sq" },
314 { LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA, "sq_AL" },
315 { LANG_SWEDISH, SUBLANG_NEUTRAL, "sv" },
316 { LANG_SWEDISH, SUBLANG_SWEDISH_SWEDEN, "sv_SE" },
317 { LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND, "sv_FI" },
318 { LANG_THAI, SUBLANG_NEUTRAL, "th" },
319 { LANG_THAI, SUBLANG_THAI_THAILAND, "th_TH" },
320 { LANG_TURKISH, SUBLANG_NEUTRAL, "tr" },
321 { LANG_TURKISH, SUBLANG_TURKISH_TURKEY, "tr_TR" },
322 { LANG_URDU, SUBLANG_NEUTRAL, "ur" },
323 { LANG_URDU, SUBLANG_URDU_PAKISTAN, "ur_PK" },
324 { LANG_INDONESIAN, SUBLANG_NEUTRAL, "id" },
325 { LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA, "id_ID" },
326 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, "uk" },
327 { LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE, "uk_UA" },
328 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, "be" },
329 { LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS, "be_BY" },
330 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, "sl" },
331 { LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA, "sl_SI" },
332 { LANG_ESTONIAN, SUBLANG_NEUTRAL, "et" },
333 { LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA, "et_EE" },
334 { LANG_LATVIAN, SUBLANG_NEUTRAL, "lv" },
335 { LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA, "lv_LV" },
336 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, "lt" },
337 { LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA, "lt_LT" },
338 { LANG_PERSIAN, SUBLANG_NEUTRAL, "fa" },
339 { LANG_PERSIAN, SUBLANG_PERSIAN_IRAN, "fa_IR" },
340 { LANG_ARMENIAN, SUBLANG_NEUTRAL, "hy" },
341 { LANG_ARMENIAN, SUBLANG_ARMENIAN_ARMENIA, "hy_AM" },
342 { LANG_AZERI, SUBLANG_NEUTRAL, "az" },
343 { LANG_AZERI, SUBLANG_AZERI_LATIN, "az_AZ@latin" },
344 { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, "az_AZ@cyrillic" },
345 { LANG_BASQUE, SUBLANG_NEUTRAL, "eu" },
346 { LANG_BASQUE, SUBLANG_BASQUE_BASQUE, "eu_ES" },
347 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, "mk" },
348 { LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA, "mk_MK" },
349 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, "af" },
350 { LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA, "af_ZA" },
351 { LANG_GEORGIAN, SUBLANG_NEUTRAL, "ka" },
352 { LANG_GEORGIAN, SUBLANG_GEORGIAN_GEORGIA, "ka_GE" },
353 { LANG_FAEROESE, SUBLANG_NEUTRAL, "fo" },
354 { LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS, "fo_FO" },
355 { LANG_HINDI, SUBLANG_NEUTRAL, "hi" },
356 { LANG_HINDI, SUBLANG_HINDI_INDIA, "hi_IN" },
357 { LANG_MALAY, SUBLANG_NEUTRAL, "ms" },
358 { LANG_MALAY, SUBLANG_MALAY_MALAYSIA, "ms_MY" },
359 { LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM, "ms_BN" },
360 { LANG_KAZAK, SUBLANG_NEUTRAL, "kk" },
361 { LANG_KAZAK, SUBLANG_KAZAK_KAZAKHSTAN, "kk_KZ" },
362 { LANG_KYRGYZ, SUBLANG_NEUTRAL, "ky" },
363 { LANG_KYRGYZ, SUBLANG_KYRGYZ_KYRGYZSTAN, "ky_KG" },
364 { LANG_SWAHILI, SUBLANG_NEUTRAL, "sw" },
365 { LANG_SWAHILI, SUBLANG_SWAHILI_KENYA, "sw_KE" },
366 { LANG_UZBEK, SUBLANG_NEUTRAL, "uz" },
367 { LANG_UZBEK, SUBLANG_UZBEK_LATIN, "uz_UZ@latin" },
368 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, "uz_UZ@cyrillic" },
369 { LANG_TATAR, SUBLANG_NEUTRAL, "tt" },
370 { LANG_TATAR, SUBLANG_TATAR_RUSSIA, "tt_TA" },
371 { LANG_PUNJABI, SUBLANG_NEUTRAL, "pa" },
372 { LANG_PUNJABI, SUBLANG_PUNJABI_INDIA, "pa_IN" },
373 { LANG_GUJARATI, SUBLANG_NEUTRAL, "gu" },
374 { LANG_GUJARATI, SUBLANG_GUJARATI_INDIA, "gu_IN" },
375 { LANG_ORIYA, SUBLANG_NEUTRAL, "or" },
376 { LANG_ORIYA, SUBLANG_ORIYA_INDIA, "or_IN" },
377 { LANG_TAMIL, SUBLANG_NEUTRAL, "ta" },
378 { LANG_TAMIL, SUBLANG_TAMIL_INDIA, "ta_IN" },
379 { LANG_TELUGU, SUBLANG_NEUTRAL, "te" },
380 { LANG_TELUGU, SUBLANG_TELUGU_INDIA, "te_IN" },
381 { LANG_KANNADA, SUBLANG_NEUTRAL, "kn" },
382 { LANG_KANNADA, SUBLANG_KANNADA_INDIA, "kn_IN" },
383 { LANG_MALAYALAM, SUBLANG_NEUTRAL, "ml" },
384 { LANG_MALAYALAM, SUBLANG_MALAYALAM_INDIA, "ml_IN" },
385 { LANG_MARATHI, SUBLANG_NEUTRAL, "mr" },
386 { LANG_MARATHI, SUBLANG_MARATHI_INDIA, "mr_IN" },
387 { LANG_SANSKRIT, SUBLANG_NEUTRAL, "sa" },
388 { LANG_SANSKRIT, SUBLANG_SANSKRIT_INDIA, "sa_IN" },
389 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, "mn" },
390 { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
391 { LANG_WELSH, SUBLANG_NEUTRAL, "cy" },
392 { LANG_WELSH, SUBLANG_WELSH_UNITED_KINGDOM, "cy_GB" },
393 { LANG_GALICIAN, SUBLANG_NEUTRAL, "gl" },
394 { LANG_GALICIAN, SUBLANG_GALICIAN_GALICIAN, "gl_ES" },
395 { LANG_KONKANI, SUBLANG_NEUTRAL, "kok" },
396 { LANG_KONKANI, SUBLANG_KONKANI_INDIA, "kok_IN" },
397 { LANG_DIVEHI, SUBLANG_NEUTRAL, "dv" },
398 { LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES, "dv_MV" },
399 { LANG_BRETON, SUBLANG_NEUTRAL, "br" },
400 { LANG_BRETON, SUBLANG_BRETON_FRANCE, "br_FR" },
402 #ifdef LANG_ESPERANTO
403 { LANG_ESPERANTO, SUBLANG_DEFAULT, "eo" },
406 { LANG_WALON, SUBLANG_NEUTRAL, "wa" },
407 { LANG_WALON, SUBLANG_DEFAULT, "wa_BE" },
410 { LANG_CORNISH, SUBLANG_NEUTRAL, "kw" },
411 { LANG_CORNISH, SUBLANG_DEFAULT, "kw_GB" },
414 { LANG_GAELIC, SUBLANG_NEUTRAL, "ga" },
415 { LANG_GAELIC, SUBLANG_GAELIC, "ga_IE" },
416 { LANG_GAELIC, SUBLANG_GAELIC_SCOTTISH, "gd_GB" },
417 { LANG_GAELIC, SUBLANG_GAELIC_MANX, "gv_GB" },
421 #ifndef HAVE_LIBGETTEXTPO
423 void write_pot_file( const char *outname )
425 error( "PO files not supported in this wrc build\n" );
428 void write_po_files( const char *outname )
430 error( "PO files not supported in this wrc build\n" );
433 #else /* HAVE_LIBGETTEXTPO */
435 static void po_xerror( int severity, po_message_t message,
436 const char *filename, size_t lineno, size_t column,
437 int multiline_p, const char *message_text )
439 fprintf( stderr, "%s:%u:%u: %s\n",
440 filename, (unsigned int)lineno, (unsigned int)column, message_text );
441 if (severity) exit(1);
444 static void po_xerror2( int severity, po_message_t message1,
445 const char *filename1, size_t lineno1, size_t column1,
446 int multiline_p1, const char *message_text1,
447 po_message_t message2,
448 const char *filename2, size_t lineno2, size_t column2,
449 int multiline_p2, const char *message_text2 )
451 fprintf( stderr, "%s:%u:%u: %s\n",
452 filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
453 fprintf( stderr, "%s:%u:%u: %s\n",
454 filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
455 if (severity) exit(1);
458 static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
460 static char *convert_string_utf8( const string_t *str, int codepage )
462 string_t *newstr = convert_string( str, str_unicode, codepage );
463 char *buffer = xmalloc( newstr->size * 4 + 1 );
464 int len = wine_utf8_wcstombs( 0, newstr->str.wstr, newstr->size, buffer, newstr->size * 4 );
466 free_string( newstr );
470 static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
471 po_message_iterator_t *iterator )
476 *iterator = po_message_iterator( po, NULL );
477 while ((msg = po_next_message( *iterator )))
479 if (strcmp( po_message_msgid( msg ), msgid )) continue;
481 if (!(context = po_message_msgctxt( msg ))) continue;
482 if (!strcmp( context, msgctxt )) break;
487 static void add_po_string( po_file_t po, const string_t *msgid, const string_t *msgstr,
488 const language_t *lang )
490 static const char dnt[] = "do not translate";
492 po_message_iterator_t iterator;
494 char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
496 if (!msgid->size) return;
498 id_buffer = id = convert_msgid_ascii( msgid, 1 );
499 context = get_message_context( &id );
500 if (context && strcmp(context, dnt) == 0)
502 /* This string should not be translated */
509 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
510 else codepage = get_language_codepage( 0, 0 );
511 assert( codepage != -1 );
512 str_buffer = str = convert_string_utf8( msgstr, codepage );
513 if (is_english( lang )) get_message_context( &str );
515 if (!(msg = find_message( po, id, context, &iterator )))
517 msg = po_message_create();
518 po_message_set_msgid( msg, id );
519 po_message_set_msgstr( msg, str ? str : "" );
520 if (context) po_message_set_msgctxt( msg, context );
521 po_message_insert( iterator, msg );
523 if (msgid->loc.file) po_message_add_filepos( msg, msgid->loc.file, msgid->loc.line );
524 po_message_iterator_free( iterator );
536 static struct list po_file_langs = LIST_INIT( po_file_langs );
538 static po_file_t create_po_file(void)
542 po_message_iterator_t iterator;
544 po = po_file_create();
545 iterator = po_message_iterator( po, NULL );
546 msg = po_message_create();
547 po_message_set_msgid( msg, "" );
548 po_message_set_msgstr( msg,
549 "Project-Id-Version: Wine\n"
550 "Report-Msgid-Bugs-To: http://bugs.winehq.org\n"
551 "POT-Creation-Date: N/A\n"
552 "PO-Revision-Date: N/A\n"
553 "Last-Translator: Automatically generated\n"
554 "Language-Team: none\n"
555 "MIME-Version: 1.0\n"
556 "Content-Type: text/plain; charset=UTF-8\n"
557 "Content-Transfer-Encoding: 8bit\n" );
558 po_message_insert( iterator, msg );
559 po_message_iterator_free( iterator );
563 static po_file_t get_po_file( const language_t *lang )
565 struct po_file_lang *po_file;
567 LIST_FOR_EACH_ENTRY( po_file, &po_file_langs, struct po_file_lang, entry )
568 if (po_file->lang.id == lang->id && po_file->lang.sub == lang->sub) return po_file->po;
570 /* create a new one */
571 po_file = xmalloc( sizeof(*po_file) );
572 po_file->lang = *lang;
573 po_file->po = create_po_file();
574 list_add_tail( &po_file_langs, &po_file->entry );
578 static const char *get_language_name( const language_t *lang )
580 static char name[20];
583 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
584 if (languages[i].id == lang->id && languages[i].sub == lang->sub)
585 return languages[i].name;
587 sprintf( name, "%02x-%02x", lang->id, lang->sub );
591 static char *get_po_file_name( const language_t *lang )
593 return strmake( "%s.po", get_language_name( lang ) );
596 static unsigned int flush_po_files( const char *output_name )
598 struct po_file_lang *po_file, *next;
599 unsigned int count = 0;
601 LIST_FOR_EACH_ENTRY_SAFE( po_file, next, &po_file_langs, struct po_file_lang, entry )
603 char *name = get_po_file_name( &po_file->lang );
606 const char *p = strrchr( output_name, '/' );
608 else p = output_name;
609 if (!strcmp( p, name ))
611 po_file_write( po_file->po, name, &po_xerror_handler );
615 else /* no specified output name, output a file for every language found */
617 po_file_write( po_file->po, name, &po_xerror_handler );
619 fprintf( stderr, "created %s\n", name );
621 po_file_free( po_file->po );
622 list_remove( &po_file->entry );
629 static void add_pot_stringtable( po_file_t po, const resource_t *res )
631 const stringtable_t *stt = res->res.stt;
636 for (i = 0; i < stt->nentries; i++)
637 if (stt->entries[i].str) add_po_string( po, stt->entries[i].str, NULL, NULL );
642 static void add_po_stringtable( const resource_t *english, const resource_t *res )
644 const stringtable_t *english_stt = english->res.stt;
645 const stringtable_t *stt = res->res.stt;
646 po_file_t po = get_po_file( stt->lvc.language );
649 while (english_stt && stt)
651 for (i = 0; i < stt->nentries; i++)
652 if (english_stt->entries[i].str && stt->entries[i].str)
653 add_po_string( po, english_stt->entries[i].str, stt->entries[i].str, stt->lvc.language );
655 english_stt = english_stt->next;
659 static void add_pot_dialog_controls( po_file_t po, const control_t *ctrl )
663 if (control_has_title( ctrl )) add_po_string( po, ctrl->title->name.s_name, NULL, NULL );
668 static void add_pot_dialog( po_file_t po, const resource_t *res )
670 const dialog_t *dlg = res->res.dlg;
672 if (dlg->title) add_po_string( po, dlg->title, NULL, NULL );
673 add_pot_dialog_controls( po, dlg->controls );
676 static void compare_dialogs( const dialog_t *english_dlg, const dialog_t *dlg )
678 const control_t *english_ctrl, *ctrl;
679 unsigned int style = 0, exstyle = 0, english_style = 0, english_exstyle = 0;
681 char *title = english_dlg->title ? convert_msgid_ascii( english_dlg->title, 0 ) : xstrdup("??");
683 if (english_dlg->width != dlg->width || english_dlg->height != dlg->height)
684 warning( "%s: dialog %s doesn't have the same size (%d,%d vs %d,%d)\n",
685 get_language_name( dlg->lvc.language ), title, dlg->width, dlg->height,
686 english_dlg->width, english_dlg->height );
688 if (dlg->gotstyle) style = dlg->style->or_mask;
689 if (dlg->gotexstyle) exstyle = dlg->exstyle->or_mask;
690 if (english_dlg->gotstyle) english_style = english_dlg->style->or_mask;
691 if (english_dlg->gotexstyle) english_exstyle = english_dlg->exstyle->or_mask;
692 if (is_rtl_language( dlg->lvc.language )) english_exstyle |= WS_EX_LAYOUTRTL;
694 if (english_style != style)
695 warning( "%s: dialog %s doesn't have the same style (%08x vs %08x)\n",
696 get_language_name( dlg->lvc.language ), title, style, english_style );
697 if (english_exstyle != exstyle)
698 warning( "%s: dialog %s doesn't have the same exstyle (%08x vs %08x)\n",
699 get_language_name( dlg->lvc.language ), title, exstyle, english_exstyle );
701 if (english_dlg->font || dlg->font)
703 int size = 0, english_size = 0;
704 char *font = NULL, *english_font = NULL;
706 if (english_dlg->font)
708 english_font = convert_msgid_ascii( english_dlg->font->name, 0 );
709 english_size = english_dlg->font->size;
713 font = convert_msgid_ascii( dlg->font->name, 0 );
714 size = dlg->font->size;
716 if (uses_larger_font( dlg->lvc.language )) english_size++;
718 if (!english_font || !font || strcasecmp( english_font, font ) || english_size != size)
719 warning( "%s: dialog %s doesn't have the same font (%s %u vs %s %u)\n",
720 get_language_name(dlg->lvc.language), title,
721 english_font ? english_font : "default", english_size,
722 font ? font : "default", size );
724 free( english_font );
726 english_ctrl = english_dlg->controls;
727 ctrl = dlg->controls;
728 for ( ; english_ctrl && ctrl; ctrl = ctrl->next, english_ctrl = english_ctrl->next )
730 if (control_has_title( english_ctrl ))
731 name = convert_msgid_ascii( english_ctrl->title->name.s_name, 0 );
733 name = strmake( "%d", ctrl->id );
735 if (english_ctrl->width != ctrl->width || english_ctrl->height != ctrl->height)
736 warning( "%s: dialog %s control %s doesn't have the same size (%d,%d vs %d,%d)\n",
737 get_language_name( dlg->lvc.language ), title, name,
738 ctrl->width, ctrl->height, english_ctrl->width, english_ctrl->height );
739 if (english_ctrl->x != ctrl->x || english_ctrl->y != ctrl->y)
740 warning( "%s: dialog %s control %s doesn't have the same position (%d,%d vs %d,%d)\n",
741 get_language_name( dlg->lvc.language ), title, name,
742 ctrl->x, ctrl->y, english_ctrl->x, english_ctrl->y );
748 static void add_po_dialog_controls( po_file_t po, const control_t *english_ctrl,
749 const control_t *ctrl, const language_t *lang )
751 while (english_ctrl && ctrl)
753 if (control_has_title( english_ctrl ) && control_has_title( ctrl ))
754 add_po_string( po, english_ctrl->title->name.s_name, ctrl->title->name.s_name, lang );
757 english_ctrl = english_ctrl->next;
761 static void add_po_dialog( const resource_t *english, const resource_t *res )
763 const dialog_t *english_dlg = english->res.dlg;
764 const dialog_t *dlg = res->res.dlg;
765 po_file_t po = get_po_file( dlg->lvc.language );
767 compare_dialogs( english_dlg, dlg );
769 if (english_dlg->title && dlg->title)
770 add_po_string( po, english_dlg->title, dlg->title, dlg->lvc.language );
771 add_po_dialog_controls( po, english_dlg->controls, dlg->controls, dlg->lvc.language );
774 static void add_pot_menu_items( po_file_t po, const menu_item_t *item )
778 if (item->name) add_po_string( po, item->name, NULL, NULL );
779 if (item->popup) add_pot_menu_items( po, item->popup );
784 static void add_pot_menu( po_file_t po, const resource_t *res )
786 add_pot_menu_items( po, res->res.men->items );
789 static void add_po_menu_items( po_file_t po, const menu_item_t *english_item,
790 const menu_item_t *item, const language_t *lang )
792 while (english_item && item)
794 if (english_item->name && item->name)
795 add_po_string( po, english_item->name, item->name, lang );
796 if (english_item->popup && item->popup)
797 add_po_menu_items( po, english_item->popup, item->popup, lang );
799 english_item = english_item->next;
803 static void add_po_menu( const resource_t *english, const resource_t *res )
805 const menu_item_t *english_items = english->res.men->items;
806 const menu_item_t *items = res->res.men->items;
807 po_file_t po = get_po_file( res->res.men->lvc.language );
809 add_po_menu_items( po, english_items, items, res->res.men->lvc.language );
812 static int string_has_context( const string_t *str )
814 char *id, *id_buffer, *context;
816 id_buffer = id = convert_msgid_ascii( str, 1 );
817 context = get_message_context( &id );
819 return context != NULL;
822 static void add_pot_accel( po_file_t po, const resource_t *res )
824 event_t *event = res->res.acc->events;
828 /* accelerators without a context don't make sense in po files */
829 if (event->str && string_has_context( event->str ))
830 add_po_string( po, event->str, NULL, NULL );
835 static void add_po_accel( const resource_t *english, const resource_t *res )
837 event_t *english_event = english->res.acc->events;
838 event_t *event = res->res.acc->events;
839 po_file_t po = get_po_file( res->res.acc->lvc.language );
841 while (english_event && event)
843 if (english_event->str && event->str && string_has_context( english_event->str ))
844 add_po_string( po, english_event->str, event->str, res->res.acc->lvc.language );
846 english_event = english_event->next;
850 static resource_t *find_english_resource( resource_t *res )
854 for (ptr = resource_top; ptr; ptr = ptr->next)
856 if (ptr->type != res->type) continue;
857 if (!ptr->lan) continue;
858 if (!is_english( ptr->lan )) continue;
859 if (compare_name_id( ptr->name, res->name )) continue;
865 void write_pot_file( const char *outname )
868 po_file_t po = create_po_file();
870 for (res = resource_top; res; res = res->next)
872 if (!is_english( res->lan )) continue;
876 case res_acc: add_pot_accel( po, res ); break;
877 case res_dlg: add_pot_dialog( po, res ); break;
878 case res_men: add_pot_menu( po, res ); break;
879 case res_stt: add_pot_stringtable( po, res ); break;
880 case res_msg: break; /* FIXME */
884 po_file_write( po, outname, &po_xerror_handler );
888 void write_po_files( const char *outname )
890 resource_t *res, *english;
892 for (res = resource_top; res; res = res->next)
894 if (!(english = find_english_resource( res ))) continue;
897 case res_acc: add_po_accel( english, res ); break;
898 case res_dlg: add_po_dialog( english, res ); break;
899 case res_men: add_po_menu( english, res ); break;
900 case res_stt: add_po_stringtable( english, res ); break;
901 case res_msg: break; /* FIXME */
905 if (!flush_po_files( outname ))
907 if (outname) error( "No translations found for %s\n", outname );
908 else error( "No translations found\n" );
912 #endif /* HAVE_LIBGETTEXTPO */
914 static struct mo_file *mo_file;
916 static void byteswap( unsigned int *data, unsigned int count )
920 for (i = 0; i < count; i++)
921 data[i] = data[i] >> 24 | (data[i] >> 8 & 0xff00) | (data[i] << 8 & 0xff0000) | data[i] << 24;
924 static void load_mo_file( const char *name )
929 fd = open( name, O_RDONLY | O_BINARY );
930 if (fd == -1) fatal_perror( "Failed to open %s", name );
932 mo_file = xmalloc( st.st_size );
933 res = read( fd, mo_file, st.st_size );
934 if (res == -1) fatal_perror( "Failed to read %s", name );
935 else if (res != st.st_size) error( "Failed to read %s\n", name );
940 if (st.st_size < sizeof(*mo_file))
941 error( "%s is not a valid .mo file\n", name );
942 if (mo_file->magic == 0xde120495)
943 byteswap( &mo_file->revision, 4 );
944 else if (mo_file->magic != 0x950412de)
945 error( "%s is not a valid .mo file\n", name );
946 if ((mo_file->revision >> 16) > 1)
947 error( "%s: unsupported file version %x\n", name, mo_file->revision );
948 if (mo_file->msgid_off >= st.st_size ||
949 mo_file->msgstr_off >= st.st_size ||
950 st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count)
951 error( "%s: corrupted file\n", name );
953 if (mo_file->magic == 0xde120495)
955 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgid_off), 2 * mo_file->count );
956 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgstr_off), 2 * mo_file->count );
960 static void free_mo_file(void)
966 static inline const char *get_mo_msgid( int index )
968 const char *base = (const char *)mo_file;
969 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgid_off);
970 return base + offsets[2 * index + 1];
973 static inline const char *get_mo_msgstr( int index )
975 const char *base = (const char *)mo_file;
976 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgstr_off);
977 return base + offsets[2 * index + 1];
980 static const char *get_msgstr( const char *msgid, const char *context, int *found )
982 int pos, res, min, max;
983 const char *ret = msgid;
986 if (!mo_file) /* strings containing a context still need to be transformed */
988 if (context) (*found)++;
992 if (context) id = strmake( "%s%c%s", context, 4, msgid );
994 max = mo_file->count - 1;
997 pos = (min + max) / 2;
998 res = strcmp( get_mo_msgid(pos), id ? id : msgid );
1001 const char *str = get_mo_msgstr( pos );
1002 if (str[0]) /* ignore empty strings */
1009 if (res > 0) max = pos - 1;
1016 static string_t *translate_string( string_t *str, int *found )
1021 char *buffer, *msgid, *context;
1023 if (!str->size || !(buffer = convert_msgid_ascii( str, 0 )))
1024 return convert_string( str, str_unicode, 1252 );
1027 context = get_message_context( &msgid );
1028 transl = get_msgstr( msgid, context, found );
1030 new = xmalloc( sizeof(*new) );
1031 new->type = str_unicode;
1032 new->size = wine_utf8_mbstowcs( 0, transl, strlen(transl), NULL, 0 );
1033 new->str.wstr = xmalloc( (new->size+1) * sizeof(WCHAR) );
1034 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, transl, strlen(transl), new->str.wstr, new->size );
1036 error( "Invalid utf-8 character in string '%s'\n", transl );
1037 new->str.wstr[new->size] = 0;
1042 static control_t *translate_controls( control_t *ctrl, int *found )
1044 control_t *new, *head = NULL, *tail = NULL;
1048 new = xmalloc( sizeof(*new) );
1050 if (control_has_title( ctrl ))
1052 new->title = new_name_id();
1053 *new->title = *ctrl->title;
1054 new->title->name.s_name = translate_string( ctrl->title->name.s_name, found );
1056 else new->title = dup_name_id( ctrl->title );
1057 new->ctlclass = dup_name_id( ctrl->ctlclass );
1058 if (tail) tail->next = new;
1068 static menu_item_t *translate_items( menu_item_t *item, int *found )
1070 menu_item_t *new, *head = NULL, *tail = NULL;
1074 new = xmalloc( sizeof(*new) );
1076 if (item->name) new->name = translate_string( item->name, found );
1077 if (item->popup) new->popup = translate_items( item->popup, found );
1078 if (tail) tail->next = new;
1088 static stringtable_t *translate_stringtable( stringtable_t *stt, language_t *lang, int *found )
1090 stringtable_t *new, *head = NULL, *tail = NULL;
1095 new = xmalloc( sizeof(*new) );
1097 new->lvc.language = lang;
1098 new->lvc.version = get_dup_version( lang );
1099 new->entries = xmalloc( new->nentries * sizeof(*new->entries) );
1100 memcpy( new->entries, stt->entries, new->nentries * sizeof(*new->entries) );
1101 for (i = 0; i < stt->nentries; i++)
1102 if (stt->entries[i].str)
1103 new->entries[i].str = translate_string( stt->entries[i].str, found );
1105 if (tail) tail->next = new;
1115 static void translate_dialog( dialog_t *dlg, dialog_t *new, int *found )
1117 if (dlg->title) new->title = translate_string( dlg->title, found );
1118 if (is_rtl_language( new->lvc.language ))
1120 new->gotexstyle = TRUE;
1121 if (dlg->gotexstyle)
1122 new->exstyle = new_style( dlg->exstyle->or_mask | WS_EX_LAYOUTRTL, dlg->exstyle->and_mask );
1124 new->exstyle = new_style( WS_EX_LAYOUTRTL, 0 );
1128 new->font = xmalloc( sizeof(*dlg->font) );
1129 *new->font = *dlg->font;
1130 if (uses_larger_font( new->lvc.language )) new->font->size++;
1131 new->font->name = convert_string( dlg->font->name, str_unicode, 1252 );
1133 new->controls = translate_controls( dlg->controls, found );
1136 static event_t *translate_accel( accelerator_t *acc, accelerator_t *new, int *found )
1138 event_t *event, *new_ev, *head = NULL, *tail = NULL;
1140 event = acc->events;
1143 new_ev = new_event();
1145 if (event->str) new_ev->str = translate_string( event->str, found );
1146 if (tail) tail->next = new_ev;
1148 new_ev->next = NULL;
1149 new_ev->prev = tail;
1151 event = event->next;
1156 static void translate_resources( language_t *lang )
1160 for (res = resource_top; res; res = res->next)
1162 resource_t *new = NULL;
1165 if (!is_english( res->lan )) continue;
1170 new = dup_resource( res, lang );
1171 new->res.acc->events = translate_accel( res->res.acc, new->res.acc, &found );
1174 new = dup_resource( res, lang );
1175 translate_dialog( res->res.dlg, new->res.dlg, &found );
1178 new = dup_resource( res, lang );
1179 new->res.men->items = translate_items( res->res.men->items, &found );
1182 new = dup_resource( res, lang );
1183 new->res.stt = translate_stringtable( res->res.stt, lang, &found );
1194 if (new_tail) new_tail->next = new;
1196 new->prev = new_tail;
1202 void add_translations( const char *po_dir )
1206 char *p, *tok, *name;
1210 /* first check if we have English resources to translate */
1211 for (res = resource_top; res; res = res->next) if (is_english( res->lan )) break;
1214 if (!po_dir) /* run through the translation process to remove msg contexts */
1216 translate_resources( new_language( LANG_ENGLISH, SUBLANG_DEFAULT ));
1220 new_top = new_tail = NULL;
1222 name = strmake( "%s/LINGUAS", po_dir );
1223 if (!(f = fopen( name, "r" )))
1229 while (fgets( buffer, sizeof(buffer), f ))
1231 if ((p = strchr( buffer, '#' ))) *p = 0;
1232 for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
1234 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
1235 if (!strcmp( tok, languages[i].name )) break;
1237 if (i == sizeof(languages)/sizeof(languages[0]))
1238 error( "unknown language '%s'\n", tok );
1240 name = strmake( "%s/%s.mo", po_dir, tok );
1241 load_mo_file( name );
1242 translate_resources( new_language(languages[i].id, languages[i].sub) );
1250 /* prepend the translated resources to the global list */
1253 new_tail->next = resource_top;
1254 resource_top->prev = new_tail;
1255 resource_top = new_top;