msvcr100: Add _NonReentrantPPLLock@details class stub.
[wine.git] / tools / wrc / po.c
blobc72a422f9446332dba70990fa9593a794f3a758e
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"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #ifdef HAVE_LIBGETTEXTPO
31 #include <gettext-po.h>
32 #endif
34 #include "wrc.h"
35 #include "genres.h"
36 #include "newstruc.h"
37 #include "utils.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wingdi.h"
41 #include "winuser.h"
42 #include "wine/list.h"
44 static resource_t *new_top, *new_tail;
46 struct mo_file
48 unsigned int magic;
49 unsigned int revision;
50 unsigned int count;
51 unsigned int msgid_off;
52 unsigned int msgstr_off;
53 /* ... rest of file data here */
56 static BOOL is_english( const language_t *lan )
58 return lan->id == LANG_ENGLISH && lan->sub == SUBLANG_DEFAULT;
61 static BOOL is_rtl_language( const language_t *lan )
63 return lan->id == LANG_ARABIC || lan->id == LANG_HEBREW || lan->id == LANG_PERSIAN;
66 static BOOL uses_larger_font( const language_t *lan )
68 return lan->id == LANG_CHINESE || lan->id == LANG_JAPANESE || lan->id == LANG_KOREAN;
71 static WORD get_default_sublang( const language_t *lan )
73 if (lan->sub != SUBLANG_NEUTRAL)
74 return lan->sub;
76 switch (lan->id)
78 case LANG_SPANISH:
79 return SUBLANG_SPANISH_MODERN;
80 case LANG_CHINESE:
81 return SUBLANG_CHINESE_SIMPLIFIED;
82 default:
83 return SUBLANG_DEFAULT;
87 static version_t *get_dup_version( language_t *lang )
89 /* English "translations" take precedence over the original rc contents */
90 return new_version( is_english( lang ) ? 1 : -1 );
93 static name_id_t *dup_name_id( name_id_t *id )
95 name_id_t *new;
97 if (!id || id->type != name_str) return id;
98 new = new_name_id();
99 *new = *id;
100 new->name.s_name = convert_string( id->name.s_name, str_unicode, 1252 );
101 return new;
104 static char *convert_msgid_ascii( const string_t *str, int error_on_invalid_char )
106 int i;
107 string_t *newstr = convert_string( str, str_unicode, 1252 );
108 char *buffer = xmalloc( newstr->size + 1 );
110 for (i = 0; i < newstr->size; i++)
112 buffer[i] = newstr->str.wstr[i];
113 if (newstr->str.wstr[i] >= 32 && newstr->str.wstr[i] <= 127) continue;
114 if (newstr->str.wstr[i] == '\t' || newstr->str.wstr[i] == '\n') continue;
115 if (error_on_invalid_char)
117 print_location( &newstr->loc );
118 error( "Invalid character %04x in source string\n", newstr->str.wstr[i] );
120 free( buffer);
121 free_string( newstr );
122 return NULL;
124 buffer[i] = 0;
125 free_string( newstr );
126 return buffer;
129 static char *get_message_context( char **msgid )
131 static const char magic[] = "#msgctxt#";
132 char *id, *context;
134 if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
135 context = *msgid + sizeof(magic) - 1;
136 if (!(id = strchr( context, '#' ))) return NULL;
137 *id = 0;
138 *msgid = id + 1;
139 return context;
142 static BOOL control_has_title( const control_t *ctrl )
144 if (!ctrl->title) return FALSE;
145 if (ctrl->title->type != name_str) return FALSE;
146 /* check for text static control */
147 if (ctrl->ctlclass && ctrl->ctlclass->type == name_ord && ctrl->ctlclass->name.i_name == CT_STATIC)
149 switch (ctrl->style->or_mask & SS_TYPEMASK)
151 case SS_LEFT:
152 case SS_CENTER:
153 case SS_RIGHT:
154 return TRUE;
155 default:
156 return FALSE;
159 return TRUE;
162 static resource_t *dup_resource( resource_t *res, language_t *lang )
164 resource_t *new = xmalloc( sizeof(*new) );
166 *new = *res;
167 new->lan = lang;
168 new->next = new->prev = NULL;
169 new->name = dup_name_id( res->name );
171 switch (res->type)
173 case res_acc:
174 new->res.acc = xmalloc( sizeof(*(new)->res.acc) );
175 *new->res.acc = *res->res.acc;
176 new->res.acc->lvc.language = lang;
177 new->res.acc->lvc.version = get_dup_version( lang );
178 break;
179 case res_dlg:
180 new->res.dlg = xmalloc( sizeof(*(new)->res.dlg) );
181 *new->res.dlg = *res->res.dlg;
182 new->res.dlg->lvc.language = lang;
183 new->res.dlg->lvc.version = get_dup_version( lang );
184 break;
185 case res_men:
186 new->res.men = xmalloc( sizeof(*(new)->res.men) );
187 *new->res.men = *res->res.men;
188 new->res.men->lvc.language = lang;
189 new->res.men->lvc.version = get_dup_version( lang );
190 break;
191 case res_stt:
192 new->res.stt = xmalloc( sizeof(*(new)->res.stt) );
193 *new->res.stt = *res->res.stt;
194 new->res.stt->lvc.language = lang;
195 new->res.stt->lvc.version = get_dup_version( lang );
196 break;
197 case res_ver:
198 new->res.ver = xmalloc( sizeof(*(new)->res.ver) );
199 *new->res.ver = *res->res.ver;
200 new->res.ver->lvc.language = lang;
201 new->res.ver->lvc.version = get_dup_version( lang );
202 break;
203 default:
204 assert(0);
206 return new;
209 static const struct
211 unsigned int id, sub;
212 const char *name;
213 } languages[] =
215 { LANG_ARABIC, SUBLANG_NEUTRAL, "ar" },
216 { LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA, "ar_SA" },
217 { LANG_ARABIC, SUBLANG_ARABIC_IRAQ, "ar_IQ" },
218 { LANG_ARABIC, SUBLANG_ARABIC_EGYPT, "ar_EG" },
219 { LANG_ARABIC, SUBLANG_ARABIC_LIBYA, "ar_LY" },
220 { LANG_ARABIC, SUBLANG_ARABIC_ALGERIA, "ar_DZ" },
221 { LANG_ARABIC, SUBLANG_ARABIC_MOROCCO, "ar_MA" },
222 { LANG_ARABIC, SUBLANG_ARABIC_TUNISIA, "ar_TN" },
223 { LANG_ARABIC, SUBLANG_ARABIC_OMAN, "ar_OM" },
224 { LANG_ARABIC, SUBLANG_ARABIC_YEMEN, "ar_YE" },
225 { LANG_ARABIC, SUBLANG_ARABIC_SYRIA, "ar_SY" },
226 { LANG_ARABIC, SUBLANG_ARABIC_JORDAN, "ar_JO" },
227 { LANG_ARABIC, SUBLANG_ARABIC_LEBANON, "ar_LB" },
228 { LANG_ARABIC, SUBLANG_ARABIC_KUWAIT, "ar_KW" },
229 { LANG_ARABIC, SUBLANG_ARABIC_UAE, "ar_AE" },
230 { LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN, "ar_BH" },
231 { LANG_ARABIC, SUBLANG_ARABIC_QATAR, "ar_QA" },
232 { LANG_BULGARIAN, SUBLANG_NEUTRAL, "bg" },
233 { LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA, "bg_BG" },
234 { LANG_CATALAN, SUBLANG_NEUTRAL, "ca" },
235 { LANG_CATALAN, SUBLANG_CATALAN_CATALAN, "ca_ES" },
236 { LANG_CHINESE, SUBLANG_NEUTRAL, "zh" },
237 { LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL, "zh_TW" },
238 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, "zh_CN" },
239 { LANG_CHINESE, SUBLANG_CHINESE_HONGKONG, "zh_HK" },
240 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, "zh_SG" },
241 { LANG_CHINESE, SUBLANG_CHINESE_MACAU, "zh_MO" },
242 { LANG_CZECH, SUBLANG_NEUTRAL, "cs" },
243 { LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC, "cs_CZ" },
244 { LANG_DANISH, SUBLANG_NEUTRAL, "da" },
245 { LANG_DANISH, SUBLANG_DANISH_DENMARK, "da_DK" },
246 { LANG_GERMAN, SUBLANG_NEUTRAL, "de" },
247 { LANG_GERMAN, SUBLANG_GERMAN, "de_DE" },
248 { LANG_GERMAN, SUBLANG_GERMAN_SWISS, "de_CH" },
249 { LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN, "de_AT" },
250 { LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG, "de_LU" },
251 { LANG_GERMAN, SUBLANG_GERMAN_LIECHTENSTEIN, "de_LI" },
252 { LANG_GREEK, SUBLANG_NEUTRAL, "el" },
253 { LANG_GREEK, SUBLANG_GREEK_GREECE, "el_GR" },
254 { LANG_ENGLISH, SUBLANG_NEUTRAL, "en" },
255 { LANG_ENGLISH, SUBLANG_ENGLISH_US, "en_US" },
256 { LANG_ENGLISH, SUBLANG_ENGLISH_UK, "en_GB" },
257 { LANG_ENGLISH, SUBLANG_ENGLISH_AUS, "en_AU" },
258 { LANG_ENGLISH, SUBLANG_ENGLISH_CAN, "en_CA" },
259 { LANG_ENGLISH, SUBLANG_ENGLISH_NZ, "en_NZ" },
260 { LANG_ENGLISH, SUBLANG_ENGLISH_EIRE, "en_IE" },
261 { LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA, "en_ZA" },
262 { LANG_ENGLISH, SUBLANG_ENGLISH_JAMAICA, "en_JM" },
263 { LANG_ENGLISH, SUBLANG_ENGLISH_CARIBBEAN, "en_CB" },
264 { LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE, "en_BZ" },
265 { LANG_ENGLISH, SUBLANG_ENGLISH_TRINIDAD, "en_TT" },
266 { LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE, "en_ZW" },
267 { LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES, "en_PH" },
268 { LANG_SPANISH, SUBLANG_NEUTRAL, "es" },
269 { LANG_SPANISH, SUBLANG_SPANISH, "es_ES" },
270 { LANG_SPANISH, SUBLANG_SPANISH_MEXICAN, "es_MX" },
271 { LANG_SPANISH, SUBLANG_SPANISH_MODERN, "es_ES_modern" },
272 { LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA, "es_GT" },
273 { LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA, "es_CR" },
274 { LANG_SPANISH, SUBLANG_SPANISH_PANAMA, "es_PA" },
275 { LANG_SPANISH, SUBLANG_SPANISH_DOMINICAN_REPUBLIC, "es_DO" },
276 { LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA, "es_VE" },
277 { LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA, "es_CO" },
278 { LANG_SPANISH, SUBLANG_SPANISH_PERU, "es_PE" },
279 { LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA, "es_AR" },
280 { LANG_SPANISH, SUBLANG_SPANISH_ECUADOR, "es_EC" },
281 { LANG_SPANISH, SUBLANG_SPANISH_CHILE, "es_CL" },
282 { LANG_SPANISH, SUBLANG_SPANISH_URUGUAY, "es_UY" },
283 { LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY, "es_PY" },
284 { LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA, "es_BO" },
285 { LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR, "es_SV" },
286 { LANG_SPANISH, SUBLANG_SPANISH_HONDURAS, "es_HN" },
287 { LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA, "es_NI" },
288 { LANG_SPANISH, SUBLANG_SPANISH_PUERTO_RICO, "es_PR" },
289 { LANG_FINNISH, SUBLANG_NEUTRAL, "fi" },
290 { LANG_FINNISH, SUBLANG_FINNISH_FINLAND, "fi_FI" },
291 { LANG_FRENCH, SUBLANG_NEUTRAL, "fr" },
292 { LANG_FRENCH, SUBLANG_FRENCH, "fr_FR" },
293 { LANG_FRENCH, SUBLANG_FRENCH_BELGIAN, "fr_BE" },
294 { LANG_FRENCH, SUBLANG_FRENCH_CANADIAN, "fr_CA" },
295 { LANG_FRENCH, SUBLANG_FRENCH_SWISS, "fr_CH" },
296 { LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG, "fr_LU" },
297 { LANG_FRENCH, SUBLANG_FRENCH_MONACO, "fr_MC" },
298 { LANG_HEBREW, SUBLANG_NEUTRAL, "he" },
299 { LANG_HEBREW, SUBLANG_HEBREW_ISRAEL, "he_IL" },
300 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, "hu" },
301 { LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY, "hu_HU" },
302 { LANG_ICELANDIC, SUBLANG_NEUTRAL, "is" },
303 { LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND, "is_IS" },
304 { LANG_IRISH, SUBLANG_NEUTRAL, "ga" },
305 { LANG_IRISH, SUBLANG_IRISH_IRELAND, "ga_IE" },
306 { LANG_ITALIAN, SUBLANG_NEUTRAL, "it" },
307 { LANG_ITALIAN, SUBLANG_ITALIAN, "it_IT" },
308 { LANG_ITALIAN, SUBLANG_ITALIAN_SWISS, "it_CH" },
309 { LANG_JAPANESE, SUBLANG_NEUTRAL, "ja" },
310 { LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN, "ja_JP" },
311 { LANG_KOREAN, SUBLANG_NEUTRAL, "ko" },
312 { LANG_KOREAN, SUBLANG_KOREAN, "ko_KR" },
313 { LANG_DUTCH, SUBLANG_NEUTRAL, "nl" },
314 { LANG_DUTCH, SUBLANG_DUTCH, "nl_NL" },
315 { LANG_DUTCH, SUBLANG_DUTCH_BELGIAN, "nl_BE" },
316 { LANG_DUTCH, SUBLANG_DUTCH_SURINAM, "nl_SR" },
317 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL, "nb_NO" },
318 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK, "nn_NO" },
319 { LANG_POLISH, SUBLANG_NEUTRAL, "pl" },
320 { LANG_POLISH, SUBLANG_POLISH_POLAND, "pl_PL" },
321 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, "pt" },
322 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN, "pt_BR" },
323 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_PORTUGAL, "pt_PT" },
324 { LANG_ROMANSH, SUBLANG_NEUTRAL, "rm" },
325 { LANG_ROMANSH, SUBLANG_ROMANSH_SWITZERLAND, "rm_CH" },
326 { LANG_ROMANIAN, SUBLANG_NEUTRAL, "ro" },
327 { LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA, "ro_RO" },
328 { LANG_RUSSIAN, SUBLANG_NEUTRAL, "ru" },
329 { LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA, "ru_RU" },
330 { LANG_SCOTTISH_GAELIC,SUBLANG_NEUTRAL, "gd" },
331 { LANG_SCOTTISH_GAELIC,SUBLANG_SCOTTISH_GAELIC, "gd_GB" },
332 { LANG_SERBIAN, SUBLANG_NEUTRAL, "hr" },
333 { LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA, "hr_HR" },
334 { LANG_SERBIAN, SUBLANG_SERBIAN_LATIN, "sr_RS@latin" },
335 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, "sr_RS@cyrillic" },
336 { LANG_SLOVAK, SUBLANG_NEUTRAL, "sk" },
337 { LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA, "sk_SK" },
338 { LANG_ALBANIAN, SUBLANG_NEUTRAL, "sq" },
339 { LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA, "sq_AL" },
340 { LANG_SWEDISH, SUBLANG_NEUTRAL, "sv" },
341 { LANG_SWEDISH, SUBLANG_SWEDISH_SWEDEN, "sv_SE" },
342 { LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND, "sv_FI" },
343 { LANG_THAI, SUBLANG_NEUTRAL, "th" },
344 { LANG_THAI, SUBLANG_THAI_THAILAND, "th_TH" },
345 { LANG_TURKISH, SUBLANG_NEUTRAL, "tr" },
346 { LANG_TURKISH, SUBLANG_TURKISH_TURKEY, "tr_TR" },
347 { LANG_URDU, SUBLANG_NEUTRAL, "ur" },
348 { LANG_URDU, SUBLANG_URDU_PAKISTAN, "ur_PK" },
349 { LANG_INDONESIAN, SUBLANG_NEUTRAL, "id" },
350 { LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA, "id_ID" },
351 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, "uk" },
352 { LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE, "uk_UA" },
353 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, "be" },
354 { LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS, "be_BY" },
355 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, "sl" },
356 { LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA, "sl_SI" },
357 { LANG_ESTONIAN, SUBLANG_NEUTRAL, "et" },
358 { LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA, "et_EE" },
359 { LANG_LATVIAN, SUBLANG_NEUTRAL, "lv" },
360 { LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA, "lv_LV" },
361 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, "lt" },
362 { LANG_LITHUANIAN, SUBLANG_LITHUANIAN, "lt_LT" },
363 { LANG_PERSIAN, SUBLANG_NEUTRAL, "fa" },
364 { LANG_PERSIAN, SUBLANG_PERSIAN_IRAN, "fa_IR" },
365 { LANG_ARMENIAN, SUBLANG_NEUTRAL, "hy" },
366 { LANG_ARMENIAN, SUBLANG_ARMENIAN_ARMENIA, "hy_AM" },
367 { LANG_AZERI, SUBLANG_NEUTRAL, "az" },
368 { LANG_AZERI, SUBLANG_AZERI_LATIN, "az_AZ@latin" },
369 { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, "az_AZ@cyrillic" },
370 { LANG_BASQUE, SUBLANG_NEUTRAL, "eu" },
371 { LANG_BASQUE, SUBLANG_BASQUE_BASQUE, "eu_ES" },
372 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, "mk" },
373 { LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA, "mk_MK" },
374 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, "af" },
375 { LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA, "af_ZA" },
376 { LANG_GEORGIAN, SUBLANG_NEUTRAL, "ka" },
377 { LANG_GEORGIAN, SUBLANG_GEORGIAN_GEORGIA, "ka_GE" },
378 { LANG_FAEROESE, SUBLANG_NEUTRAL, "fo" },
379 { LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS, "fo_FO" },
380 { LANG_HINDI, SUBLANG_NEUTRAL, "hi" },
381 { LANG_HINDI, SUBLANG_HINDI_INDIA, "hi_IN" },
382 { LANG_MALAY, SUBLANG_NEUTRAL, "ms" },
383 { LANG_MALAY, SUBLANG_MALAY_MALAYSIA, "ms_MY" },
384 { LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM, "ms_BN" },
385 { LANG_KAZAK, SUBLANG_NEUTRAL, "kk" },
386 { LANG_KAZAK, SUBLANG_KAZAK_KAZAKHSTAN, "kk_KZ" },
387 { LANG_KYRGYZ, SUBLANG_NEUTRAL, "ky" },
388 { LANG_KYRGYZ, SUBLANG_KYRGYZ_KYRGYZSTAN, "ky_KG" },
389 { LANG_SWAHILI, SUBLANG_NEUTRAL, "sw" },
390 { LANG_SWAHILI, SUBLANG_SWAHILI_KENYA, "sw_KE" },
391 { LANG_UZBEK, SUBLANG_NEUTRAL, "uz" },
392 { LANG_UZBEK, SUBLANG_UZBEK_LATIN, "uz_UZ@latin" },
393 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, "uz_UZ@cyrillic" },
394 { LANG_TATAR, SUBLANG_NEUTRAL, "tt" },
395 { LANG_TATAR, SUBLANG_TATAR_RUSSIA, "tt_TA" },
396 { LANG_PUNJABI, SUBLANG_NEUTRAL, "pa" },
397 { LANG_PUNJABI, SUBLANG_PUNJABI_INDIA, "pa_IN" },
398 { LANG_GUJARATI, SUBLANG_NEUTRAL, "gu" },
399 { LANG_GUJARATI, SUBLANG_GUJARATI_INDIA, "gu_IN" },
400 { LANG_ORIYA, SUBLANG_NEUTRAL, "or" },
401 { LANG_ORIYA, SUBLANG_ORIYA_INDIA, "or_IN" },
402 { LANG_TAMIL, SUBLANG_NEUTRAL, "ta" },
403 { LANG_TAMIL, SUBLANG_TAMIL_INDIA, "ta_IN" },
404 { LANG_TELUGU, SUBLANG_NEUTRAL, "te" },
405 { LANG_TELUGU, SUBLANG_TELUGU_INDIA, "te_IN" },
406 { LANG_KANNADA, SUBLANG_NEUTRAL, "kn" },
407 { LANG_KANNADA, SUBLANG_KANNADA_INDIA, "kn_IN" },
408 { LANG_MALAYALAM, SUBLANG_NEUTRAL, "ml" },
409 { LANG_MALAYALAM, SUBLANG_MALAYALAM_INDIA, "ml_IN" },
410 { LANG_MARATHI, SUBLANG_NEUTRAL, "mr" },
411 { LANG_MARATHI, SUBLANG_MARATHI_INDIA, "mr_IN" },
412 { LANG_SANSKRIT, SUBLANG_NEUTRAL, "sa" },
413 { LANG_SANSKRIT, SUBLANG_SANSKRIT_INDIA, "sa_IN" },
414 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, "mn" },
415 { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
416 { LANG_WELSH, SUBLANG_NEUTRAL, "cy" },
417 { LANG_WELSH, SUBLANG_WELSH_UNITED_KINGDOM, "cy_GB" },
418 { LANG_GALICIAN, SUBLANG_NEUTRAL, "gl" },
419 { LANG_GALICIAN, SUBLANG_GALICIAN_GALICIAN, "gl_ES" },
420 { LANG_KONKANI, SUBLANG_NEUTRAL, "kok" },
421 { LANG_KONKANI, SUBLANG_KONKANI_INDIA, "kok_IN" },
422 { LANG_DIVEHI, SUBLANG_NEUTRAL, "dv" },
423 { LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES, "dv_MV" },
424 { LANG_BRETON, SUBLANG_NEUTRAL, "br" },
425 { LANG_BRETON, SUBLANG_BRETON_FRANCE, "br_FR" },
427 #ifdef LANG_ESPERANTO
428 { LANG_ESPERANTO, SUBLANG_DEFAULT, "eo" },
429 #endif
430 #ifdef LANG_WALON
431 { LANG_WALON, SUBLANG_NEUTRAL, "wa" },
432 { LANG_WALON, SUBLANG_DEFAULT, "wa_BE" },
433 #endif
434 #ifdef LANG_CORNISH
435 { LANG_CORNISH, SUBLANG_NEUTRAL, "kw" },
436 { LANG_CORNISH, SUBLANG_DEFAULT, "kw_GB" },
437 #endif
438 #ifdef LANG_MANX_GAELIC
439 { LANG_MANX_GAELIC, SUBLANG_MANX_GAELIC, "gv_GB" },
440 #endif
443 #ifndef HAVE_LIBGETTEXTPO
445 void write_pot_file( const char *outname )
447 error( "PO files not supported in this wrc build\n" );
450 void write_po_files( const char *outname )
452 error( "PO files not supported in this wrc build\n" );
455 #else /* HAVE_LIBGETTEXTPO */
457 static void po_xerror( int severity, po_message_t message,
458 const char *filename, size_t lineno, size_t column,
459 int multiline_p, const char *message_text )
461 fprintf( stderr, "%s:%u:%u: %s\n",
462 filename, (unsigned int)lineno, (unsigned int)column, message_text );
463 if (severity) exit(1);
466 static void po_xerror2( int severity, po_message_t message1,
467 const char *filename1, size_t lineno1, size_t column1,
468 int multiline_p1, const char *message_text1,
469 po_message_t message2,
470 const char *filename2, size_t lineno2, size_t column2,
471 int multiline_p2, const char *message_text2 )
473 fprintf( stderr, "%s:%u:%u: %s\n",
474 filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
475 fprintf( stderr, "%s:%u:%u: %s\n",
476 filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
477 if (severity) exit(1);
480 static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
482 static char *convert_string_utf8( const string_t *str, int codepage )
484 string_t *newstr = convert_string( str, str_unicode, codepage );
485 char *buffer = xmalloc( newstr->size * 4 + 1 );
486 int len = wine_utf8_wcstombs( 0, newstr->str.wstr, newstr->size, buffer, newstr->size * 4 );
487 buffer[len] = 0;
488 free_string( newstr );
489 return buffer;
492 static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
493 po_message_iterator_t *iterator )
495 po_message_t msg;
496 const char *context;
498 *iterator = po_message_iterator( po, NULL );
499 while ((msg = po_next_message( *iterator )))
501 if (strcmp( po_message_msgid( msg ), msgid )) continue;
502 if (!msgctxt) break;
503 if (!(context = po_message_msgctxt( msg ))) continue;
504 if (!strcmp( context, msgctxt )) break;
506 return msg;
509 static void add_po_string( po_file_t po, const string_t *msgid, const string_t *msgstr,
510 const language_t *lang )
512 static const char dnt[] = "do not translate";
513 po_message_t msg;
514 po_message_iterator_t iterator;
515 int codepage;
516 char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
518 if (!msgid->size) return;
520 id_buffer = id = convert_msgid_ascii( msgid, 1 );
521 context = get_message_context( &id );
522 if (context && strcmp(context, dnt) == 0)
524 /* This string should not be translated */
525 free( id_buffer );
526 return;
529 if (msgstr)
531 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
532 else codepage = get_language_codepage( 0, 0 );
533 assert( codepage != -1 );
534 str_buffer = str = convert_string_utf8( msgstr, codepage );
535 if (is_english( lang )) get_message_context( &str );
537 if (!(msg = find_message( po, id, context, &iterator )))
539 msg = po_message_create();
540 po_message_set_msgid( msg, id );
541 po_message_set_msgstr( msg, str ? str : "" );
542 if (context) po_message_set_msgctxt( msg, context );
543 po_message_insert( iterator, msg );
545 if (msgid->loc.file) po_message_add_filepos( msg, msgid->loc.file, msgid->loc.line );
546 po_message_iterator_free( iterator );
547 free( id_buffer );
548 free( str_buffer );
551 struct po_file_lang
553 struct list entry;
554 language_t lang;
555 po_file_t po;
558 static struct list po_file_langs = LIST_INIT( po_file_langs );
560 static po_file_t create_po_file(void)
562 po_file_t po;
563 po_message_t msg;
564 po_message_iterator_t iterator;
566 po = po_file_create();
567 iterator = po_message_iterator( po, NULL );
568 msg = po_message_create();
569 po_message_set_msgid( msg, "" );
570 po_message_set_msgstr( msg,
571 "Project-Id-Version: Wine\n"
572 "Report-Msgid-Bugs-To: https://bugs.winehq.org\n"
573 "POT-Creation-Date: N/A\n"
574 "PO-Revision-Date: N/A\n"
575 "Last-Translator: Automatically generated\n"
576 "Language-Team: none\n"
577 "MIME-Version: 1.0\n"
578 "Content-Type: text/plain; charset=UTF-8\n"
579 "Content-Transfer-Encoding: 8bit\n" );
580 po_message_insert( iterator, msg );
581 po_message_iterator_free( iterator );
582 return po;
585 static po_file_t get_po_file( const language_t *lang )
587 struct po_file_lang *po_file;
589 LIST_FOR_EACH_ENTRY( po_file, &po_file_langs, struct po_file_lang, entry )
590 if (po_file->lang.id == lang->id && po_file->lang.sub == lang->sub) return po_file->po;
592 /* create a new one */
593 po_file = xmalloc( sizeof(*po_file) );
594 po_file->lang = *lang;
595 po_file->po = create_po_file();
596 list_add_tail( &po_file_langs, &po_file->entry );
597 return po_file->po;
600 static const char *get_language_name( const language_t *lang )
602 static char name[20];
603 unsigned int i;
605 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
606 if (languages[i].id == lang->id && languages[i].sub == lang->sub)
607 return languages[i].name;
609 sprintf( name, "%02x-%02x", lang->id, lang->sub );
610 return name;
613 static char *get_po_file_name( const language_t *lang )
615 return strmake( "%s.po", get_language_name( lang ) );
618 static unsigned int flush_po_files( const char *output_name )
620 struct po_file_lang *po_file, *next;
621 unsigned int count = 0;
623 LIST_FOR_EACH_ENTRY_SAFE( po_file, next, &po_file_langs, struct po_file_lang, entry )
625 char *name = get_po_file_name( &po_file->lang );
626 if (output_name)
628 const char *p = strrchr( output_name, '/' );
629 if (p) p++;
630 else p = output_name;
631 if (!strcmp( p, name ))
633 po_file_write( po_file->po, name, &po_xerror_handler );
634 count++;
637 else /* no specified output name, output a file for every language found */
639 po_file_write( po_file->po, name, &po_xerror_handler );
640 count++;
641 fprintf( stderr, "created %s\n", name );
643 po_file_free( po_file->po );
644 list_remove( &po_file->entry );
645 free( po_file );
646 free( name );
648 return count;
651 static void add_pot_stringtable( po_file_t po, const resource_t *res )
653 const stringtable_t *stt = res->res.stt;
654 int i;
656 while (stt)
658 for (i = 0; i < stt->nentries; i++)
659 if (stt->entries[i].str) add_po_string( po, stt->entries[i].str, NULL, NULL );
660 stt = stt->next;
664 static void add_po_stringtable( const resource_t *english, const resource_t *res )
666 const stringtable_t *english_stt = english->res.stt;
667 const stringtable_t *stt = res->res.stt;
668 po_file_t po = get_po_file( stt->lvc.language );
669 int i;
671 while (english_stt && stt)
673 for (i = 0; i < stt->nentries; i++)
674 if (english_stt->entries[i].str && stt->entries[i].str)
675 add_po_string( po, english_stt->entries[i].str, stt->entries[i].str, stt->lvc.language );
676 stt = stt->next;
677 english_stt = english_stt->next;
681 static void add_pot_dialog_controls( po_file_t po, const control_t *ctrl )
683 while (ctrl)
685 if (control_has_title( ctrl )) add_po_string( po, ctrl->title->name.s_name, NULL, NULL );
686 ctrl = ctrl->next;
690 static void add_pot_dialog( po_file_t po, const resource_t *res )
692 const dialog_t *dlg = res->res.dlg;
694 if (dlg->title) add_po_string( po, dlg->title, NULL, NULL );
695 add_pot_dialog_controls( po, dlg->controls );
698 static void compare_dialogs( const dialog_t *english_dlg, const dialog_t *dlg )
700 const control_t *english_ctrl, *ctrl;
701 unsigned int style = 0, exstyle = 0, english_style = 0, english_exstyle = 0;
702 char *name;
703 char *title = english_dlg->title ? convert_msgid_ascii( english_dlg->title, 0 ) : xstrdup("??");
705 if (english_dlg->width != dlg->width || english_dlg->height != dlg->height)
706 warning( "%s: dialog %s doesn't have the same size (%d,%d vs %d,%d)\n",
707 get_language_name( dlg->lvc.language ), title, dlg->width, dlg->height,
708 english_dlg->width, english_dlg->height );
710 if (dlg->gotstyle) style = dlg->style->or_mask;
711 if (dlg->gotexstyle) exstyle = dlg->exstyle->or_mask;
712 if (english_dlg->gotstyle) english_style = english_dlg->style->or_mask;
713 if (english_dlg->gotexstyle) english_exstyle = english_dlg->exstyle->or_mask;
714 if (is_rtl_language( dlg->lvc.language )) english_exstyle |= WS_EX_LAYOUTRTL;
716 if (english_style != style)
717 warning( "%s: dialog %s doesn't have the same style (%08x vs %08x)\n",
718 get_language_name( dlg->lvc.language ), title, style, english_style );
719 if (english_exstyle != exstyle)
720 warning( "%s: dialog %s doesn't have the same exstyle (%08x vs %08x)\n",
721 get_language_name( dlg->lvc.language ), title, exstyle, english_exstyle );
723 if (english_dlg->font || dlg->font)
725 int size = 0, english_size = 0;
726 char *font = NULL, *english_font = NULL;
728 if (english_dlg->font)
730 english_font = convert_msgid_ascii( english_dlg->font->name, 0 );
731 english_size = english_dlg->font->size;
733 if (dlg->font)
735 font = convert_msgid_ascii( dlg->font->name, 0 );
736 size = dlg->font->size;
738 if (uses_larger_font( dlg->lvc.language )) english_size++;
740 if (!english_font || !font || strcasecmp( english_font, font ) || english_size != size)
741 warning( "%s: dialog %s doesn't have the same font (%s %u vs %s %u)\n",
742 get_language_name(dlg->lvc.language), title,
743 english_font ? english_font : "default", english_size,
744 font ? font : "default", size );
745 free( font );
746 free( english_font );
748 english_ctrl = english_dlg->controls;
749 ctrl = dlg->controls;
750 for ( ; english_ctrl && ctrl; ctrl = ctrl->next, english_ctrl = english_ctrl->next )
752 if (control_has_title( english_ctrl ))
753 name = convert_msgid_ascii( english_ctrl->title->name.s_name, 0 );
754 else
755 name = strmake( "%d", ctrl->id );
757 if (english_ctrl->width != ctrl->width || english_ctrl->height != ctrl->height)
758 warning( "%s: dialog %s control %s doesn't have the same size (%d,%d vs %d,%d)\n",
759 get_language_name( dlg->lvc.language ), title, name,
760 ctrl->width, ctrl->height, english_ctrl->width, english_ctrl->height );
761 if (english_ctrl->x != ctrl->x || english_ctrl->y != ctrl->y)
762 warning( "%s: dialog %s control %s doesn't have the same position (%d,%d vs %d,%d)\n",
763 get_language_name( dlg->lvc.language ), title, name,
764 ctrl->x, ctrl->y, english_ctrl->x, english_ctrl->y );
765 free( name );
767 free( title );
770 static void add_po_dialog_controls( po_file_t po, const control_t *english_ctrl,
771 const control_t *ctrl, const language_t *lang )
773 while (english_ctrl && ctrl)
775 if (control_has_title( english_ctrl ) && control_has_title( ctrl ))
776 add_po_string( po, english_ctrl->title->name.s_name, ctrl->title->name.s_name, lang );
778 ctrl = ctrl->next;
779 english_ctrl = english_ctrl->next;
783 static void add_po_dialog( const resource_t *english, const resource_t *res )
785 const dialog_t *english_dlg = english->res.dlg;
786 const dialog_t *dlg = res->res.dlg;
787 po_file_t po = get_po_file( dlg->lvc.language );
789 compare_dialogs( english_dlg, dlg );
791 if (english_dlg->title && dlg->title)
792 add_po_string( po, english_dlg->title, dlg->title, dlg->lvc.language );
793 add_po_dialog_controls( po, english_dlg->controls, dlg->controls, dlg->lvc.language );
796 static void add_pot_menu_items( po_file_t po, const menu_item_t *item )
798 while (item)
800 if (item->name) add_po_string( po, item->name, NULL, NULL );
801 if (item->popup) add_pot_menu_items( po, item->popup );
802 item = item->next;
806 static void add_pot_menu( po_file_t po, const resource_t *res )
808 add_pot_menu_items( po, res->res.men->items );
811 static void add_po_menu_items( po_file_t po, const menu_item_t *english_item,
812 const menu_item_t *item, const language_t *lang )
814 while (english_item && item)
816 if (english_item->name && item->name)
817 add_po_string( po, english_item->name, item->name, lang );
818 if (english_item->popup && item->popup)
819 add_po_menu_items( po, english_item->popup, item->popup, lang );
820 item = item->next;
821 english_item = english_item->next;
825 static void add_po_menu( const resource_t *english, const resource_t *res )
827 const menu_item_t *english_items = english->res.men->items;
828 const menu_item_t *items = res->res.men->items;
829 po_file_t po = get_po_file( res->res.men->lvc.language );
831 add_po_menu_items( po, english_items, items, res->res.men->lvc.language );
834 static BOOL string_has_context( const string_t *str )
836 char *id, *id_buffer, *context;
838 id_buffer = id = convert_msgid_ascii( str, 1 );
839 context = get_message_context( &id );
840 free( id_buffer );
841 return context != NULL;
844 static void add_pot_accel( po_file_t po, const resource_t *res )
846 event_t *event = res->res.acc->events;
848 while (event)
850 /* accelerators without a context don't make sense in po files */
851 if (event->str && string_has_context( event->str ))
852 add_po_string( po, event->str, NULL, NULL );
853 event = event->next;
857 static void add_po_accel( const resource_t *english, const resource_t *res )
859 event_t *english_event = english->res.acc->events;
860 event_t *event = res->res.acc->events;
861 po_file_t po = get_po_file( res->res.acc->lvc.language );
863 while (english_event && event)
865 if (english_event->str && event->str && string_has_context( english_event->str ))
866 add_po_string( po, english_event->str, event->str, res->res.acc->lvc.language );
867 event = event->next;
868 english_event = english_event->next;
872 static ver_block_t *get_version_langcharset_block( ver_block_t *block )
874 ver_block_t *stringfileinfo = NULL;
875 char *translation = NULL;
876 ver_value_t *val;
878 for (; block; block = block->next)
880 char *name;
881 name = convert_msgid_ascii( block->name, 0 );
882 if (!strcasecmp( name, "stringfileinfo" ))
883 stringfileinfo = block;
884 else if (!strcasecmp( name, "varfileinfo" ))
886 for (val = block->values; val; val = val->next)
888 char *key = convert_msgid_ascii( val->key, 0 );
889 if (val->type == val_words &&
890 !strcasecmp( key, "Translation" ) &&
891 val->value.words->nwords >= 2)
892 translation = strmake( "%04x%04x",
893 val->value.words->words[0],
894 val->value.words->words[1] );
895 free( key );
898 free( name );
901 if (!stringfileinfo || !translation) return NULL;
903 for (val = stringfileinfo->values; val; val = val->next)
905 char *block_name;
906 if (val->type != val_block) continue;
907 block_name = convert_msgid_ascii( val->value.block->name, 0 );
908 if (!strcasecmp( block_name, translation ))
910 free( block_name );
911 free( translation );
912 return val->value.block;
914 free( block_name );
916 free( translation );
917 return NULL;
920 static int version_value_needs_translation( const ver_value_t *val )
922 int ret;
923 char *key;
925 if (val->type != val_str) return 0;
926 if (!(key = convert_msgid_ascii( val->key, 0 ))) return 0;
928 /* most values contain version numbers or file names, only translate a few specific ones */
929 ret = (!strcasecmp( key, "FileDescription" ) || !strcasecmp( key, "ProductName" ));
931 free( key );
932 return ret;
935 static void add_pot_versioninfo( po_file_t po, const resource_t *res )
937 ver_value_t *val;
938 ver_block_t *langcharset = get_version_langcharset_block( res->res.ver->blocks );
940 if (!langcharset) return;
941 for (val = langcharset->values; val; val = val->next)
942 if (version_value_needs_translation( val ))
943 add_po_string( po, val->value.str, NULL, NULL );
946 static void add_po_versioninfo( const resource_t *english, const resource_t *res )
948 const ver_block_t *langcharset = get_version_langcharset_block( res->res.ver->blocks );
949 const ver_block_t *english_langcharset = get_version_langcharset_block( english->res.ver->blocks );
950 ver_value_t *val, *english_val;
951 po_file_t po = get_po_file( res->res.ver->lvc.language );
953 if (!langcharset && !english_langcharset) return;
954 val = langcharset->values;
955 english_val = english_langcharset->values;
956 while (english_val && val)
958 if (val->type == val_str)
959 add_po_string( po, english_val->value.str, val->value.str, res->res.ver->lvc.language );
960 val = val->next;
961 english_val = english_val->next;
965 static resource_t *find_english_resource( resource_t *res )
967 resource_t *ptr;
969 for (ptr = resource_top; ptr; ptr = ptr->next)
971 if (ptr->type != res->type) continue;
972 if (!ptr->lan) continue;
973 if (!is_english( ptr->lan )) continue;
974 if (compare_name_id( ptr->name, res->name )) continue;
975 return ptr;
977 return NULL;
980 void write_pot_file( const char *outname )
982 resource_t *res;
983 po_file_t po = create_po_file();
985 for (res = resource_top; res; res = res->next)
987 if (!is_english( res->lan )) continue;
989 switch (res->type)
991 case res_acc: add_pot_accel( po, res ); break;
992 case res_dlg: add_pot_dialog( po, res ); break;
993 case res_men: add_pot_menu( po, res ); break;
994 case res_stt: add_pot_stringtable( po, res ); break;
995 case res_ver: add_pot_versioninfo( po, res ); break;
996 case res_msg: break; /* FIXME */
997 default: break;
1000 po_file_write( po, outname, &po_xerror_handler );
1001 po_file_free( po );
1004 void write_po_files( const char *outname )
1006 resource_t *res, *english;
1008 for (res = resource_top; res; res = res->next)
1010 if (!(english = find_english_resource( res ))) continue;
1011 switch (res->type)
1013 case res_acc: add_po_accel( english, res ); break;
1014 case res_dlg: add_po_dialog( english, res ); break;
1015 case res_men: add_po_menu( english, res ); break;
1016 case res_stt: add_po_stringtable( english, res ); break;
1017 case res_ver: add_po_versioninfo( english, res ); break;
1018 case res_msg: break; /* FIXME */
1019 default: break;
1022 if (!flush_po_files( outname ))
1024 if (outname) error( "No translations found for %s\n", outname );
1025 else error( "No translations found\n" );
1029 #endif /* HAVE_LIBGETTEXTPO */
1031 static struct mo_file *mo_file;
1033 static void byteswap( unsigned int *data, unsigned int count )
1035 unsigned int i;
1037 for (i = 0; i < count; i++)
1038 data[i] = data[i] >> 24 | (data[i] >> 8 & 0xff00) | (data[i] << 8 & 0xff0000) | data[i] << 24;
1041 static void load_mo_file( const char *name )
1043 struct stat st;
1044 int res, fd;
1046 fd = open( name, O_RDONLY | O_BINARY );
1047 if (fd == -1) fatal_perror( "Failed to open %s", name );
1048 fstat( fd, &st );
1049 mo_file = xmalloc( st.st_size );
1050 res = read( fd, mo_file, st.st_size );
1051 if (res == -1) fatal_perror( "Failed to read %s", name );
1052 else if (res != st.st_size) error( "Failed to read %s\n", name );
1053 close( fd );
1055 /* sanity checks */
1057 if (st.st_size < sizeof(*mo_file))
1058 error( "%s is not a valid .mo file\n", name );
1059 if (mo_file->magic == 0xde120495)
1060 byteswap( &mo_file->revision, 4 );
1061 else if (mo_file->magic != 0x950412de)
1062 error( "%s is not a valid .mo file\n", name );
1063 if ((mo_file->revision >> 16) > 1)
1064 error( "%s: unsupported file version %x\n", name, mo_file->revision );
1065 if (mo_file->msgid_off >= st.st_size ||
1066 mo_file->msgstr_off >= st.st_size ||
1067 st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count)
1068 error( "%s: corrupted file\n", name );
1070 if (mo_file->magic == 0xde120495)
1072 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgid_off), 2 * mo_file->count );
1073 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgstr_off), 2 * mo_file->count );
1077 static void free_mo_file(void)
1079 free( mo_file );
1080 mo_file = NULL;
1083 static inline const char *get_mo_msgid( int index )
1085 const char *base = (const char *)mo_file;
1086 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgid_off);
1087 return base + offsets[2 * index + 1];
1090 static inline const char *get_mo_msgstr( int index )
1092 const char *base = (const char *)mo_file;
1093 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgstr_off);
1094 return base + offsets[2 * index + 1];
1097 static const char *get_msgstr( const char *msgid, const char *context, int *found )
1099 int pos, res, min, max;
1100 const char *ret = msgid;
1101 char *id = NULL;
1103 if (!mo_file) /* strings containing a context still need to be transformed */
1105 if (context) (*found)++;
1106 return ret;
1109 if (context) id = strmake( "%s%c%s", context, 4, msgid );
1110 min = 0;
1111 max = mo_file->count - 1;
1112 while (min <= max)
1114 pos = (min + max) / 2;
1115 res = strcmp( get_mo_msgid(pos), id ? id : msgid );
1116 if (!res)
1118 const char *str = get_mo_msgstr( pos );
1119 if (str[0]) /* ignore empty strings */
1121 ret = str;
1122 (*found)++;
1124 break;
1126 if (res > 0) max = pos - 1;
1127 else min = pos + 1;
1129 free( id );
1130 return ret;
1133 static string_t *translate_string( string_t *str, int *found )
1135 string_t *new;
1136 const char *transl;
1137 int res;
1138 char *buffer, *msgid, *context;
1140 if (!str->size || !(buffer = convert_msgid_ascii( str, 0 )))
1141 return convert_string( str, str_unicode, 1252 );
1143 msgid = buffer;
1144 context = get_message_context( &msgid );
1145 transl = get_msgstr( msgid, context, found );
1147 new = xmalloc( sizeof(*new) );
1148 new->type = str_unicode;
1149 new->size = wine_utf8_mbstowcs( 0, transl, strlen(transl), NULL, 0 );
1150 new->str.wstr = xmalloc( (new->size+1) * sizeof(WCHAR) );
1151 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, transl, strlen(transl), new->str.wstr, new->size );
1152 if (res == -2)
1153 error( "Invalid utf-8 character in string '%s'\n", transl );
1154 new->str.wstr[new->size] = 0;
1155 free( buffer );
1156 return new;
1159 static control_t *translate_controls( control_t *ctrl, int *found )
1161 control_t *new, *head = NULL, *tail = NULL;
1163 while (ctrl)
1165 new = xmalloc( sizeof(*new) );
1166 *new = *ctrl;
1167 if (control_has_title( ctrl ))
1169 new->title = new_name_id();
1170 *new->title = *ctrl->title;
1171 new->title->name.s_name = translate_string( ctrl->title->name.s_name, found );
1173 else new->title = dup_name_id( ctrl->title );
1174 new->ctlclass = dup_name_id( ctrl->ctlclass );
1175 if (tail) tail->next = new;
1176 else head = new;
1177 new->next = NULL;
1178 new->prev = tail;
1179 tail = new;
1180 ctrl = ctrl->next;
1182 return head;
1185 static menu_item_t *translate_items( menu_item_t *item, int *found )
1187 menu_item_t *new, *head = NULL, *tail = NULL;
1189 while (item)
1191 new = xmalloc( sizeof(*new) );
1192 *new = *item;
1193 if (item->name) new->name = translate_string( item->name, found );
1194 if (item->popup) new->popup = translate_items( item->popup, found );
1195 if (tail) tail->next = new;
1196 else head = new;
1197 new->next = NULL;
1198 new->prev = tail;
1199 tail = new;
1200 item = item->next;
1202 return head;
1205 static stringtable_t *translate_stringtable( stringtable_t *stt, language_t *lang, int *found )
1207 stringtable_t *new, *head = NULL, *tail = NULL;
1208 int i;
1210 while (stt)
1212 new = xmalloc( sizeof(*new) );
1213 *new = *stt;
1214 new->lvc.language = lang;
1215 new->lvc.version = get_dup_version( lang );
1216 new->entries = xmalloc( new->nentries * sizeof(*new->entries) );
1217 memcpy( new->entries, stt->entries, new->nentries * sizeof(*new->entries) );
1218 for (i = 0; i < stt->nentries; i++)
1219 if (stt->entries[i].str)
1220 new->entries[i].str = translate_string( stt->entries[i].str, found );
1222 if (tail) tail->next = new;
1223 else head = new;
1224 new->next = NULL;
1225 new->prev = tail;
1226 tail = new;
1227 stt = stt->next;
1229 return head;
1232 static void translate_dialog( dialog_t *dlg, dialog_t *new, int *found )
1234 if (dlg->title) new->title = translate_string( dlg->title, found );
1235 if (is_rtl_language( new->lvc.language ))
1237 new->gotexstyle = TRUE;
1238 if (dlg->gotexstyle)
1239 new->exstyle = new_style( dlg->exstyle->or_mask | WS_EX_LAYOUTRTL, dlg->exstyle->and_mask );
1240 else
1241 new->exstyle = new_style( WS_EX_LAYOUTRTL, 0 );
1243 if (dlg->font)
1245 new->font = xmalloc( sizeof(*dlg->font) );
1246 *new->font = *dlg->font;
1247 if (uses_larger_font( new->lvc.language )) new->font->size++;
1248 new->font->name = convert_string( dlg->font->name, str_unicode, 1252 );
1250 new->controls = translate_controls( dlg->controls, found );
1253 static event_t *translate_accel( accelerator_t *acc, accelerator_t *new, int *found )
1255 event_t *event, *new_ev, *head = NULL, *tail = NULL;
1257 event = acc->events;
1258 while (event)
1260 new_ev = new_event();
1261 *new_ev = *event;
1262 if (event->str) new_ev->str = translate_string( event->str, found );
1263 if (tail) tail->next = new_ev;
1264 else head = new_ev;
1265 new_ev->next = NULL;
1266 new_ev->prev = tail;
1267 tail = new_ev;
1268 event = event->next;
1270 return head;
1273 static ver_value_t *translate_langcharset_values( ver_value_t *val, language_t *lang, int *found )
1275 ver_value_t *new_val, *head = NULL, *tail = NULL;
1276 while (val)
1278 new_val = new_ver_value();
1279 *new_val = *val;
1280 if (val->type == val_str)
1281 new_val->value.str = translate_string( val->value.str, found );
1282 if (tail) tail->next = new_val;
1283 else head = new_val;
1284 new_val->next = NULL;
1285 new_val->prev = tail;
1286 tail = new_val;
1287 val = val->next;
1289 return head;
1292 static ver_value_t *translate_stringfileinfo( ver_value_t *val, language_t *lang, int *found )
1294 int i;
1295 ver_value_t *new_val, *head = NULL, *tail = NULL;
1296 const char *english_block_name[2] = { "040904b0", "040904e4" };
1297 char *block_name[2];
1298 LANGID langid = MAKELANGID( lang->id, get_default_sublang( lang ) );
1300 block_name[0] = strmake( "%04x%04x", langid, 1200 );
1301 block_name[1] = strmake( "%04x%04x", langid, get_language_codepage( lang->id, lang->sub ) );
1303 while (val)
1305 new_val = new_ver_value();
1306 *new_val = *val;
1307 if (val->type == val_block)
1309 ver_block_t *blk, *blk_head = NULL, *blk_tail = NULL;
1310 for (blk = val->value.block; blk; blk = blk->next)
1312 ver_block_t *new_blk;
1313 char *name;
1314 new_blk = new_ver_block();
1315 *new_blk = *blk;
1316 name = convert_msgid_ascii( blk->name, 0 );
1317 for (i = 0; i < sizeof(block_name)/sizeof(block_name[0]); i++)
1319 if (!strcasecmp( name, english_block_name[i] ))
1321 string_t *str;
1322 str = new_string();
1323 str->type = str_char;
1324 str->size = strlen( block_name[i] ) + 1;
1325 str->str.cstr = xstrdup( block_name[i] );
1326 new_blk->name = str;
1327 new_blk->values = translate_langcharset_values( blk->values, lang, found );
1330 free( name );
1331 if (blk_tail) blk_tail->next = new_blk;
1332 else blk_head = new_blk;
1333 new_blk->next = NULL;
1334 new_blk->prev = blk_tail;
1335 blk_tail = new_blk;
1337 new_val->value.block = blk_head;
1339 if (tail) tail->next = new_val;
1340 else head = new_val;
1341 new_val->next = NULL;
1342 new_val->prev = tail;
1343 tail = new_val;
1344 val = val->next;
1347 for (i = 0; i < sizeof(block_name)/sizeof(block_name[0]); i++)
1348 free( block_name[i] );
1349 return head;
1352 static ver_value_t *translate_varfileinfo( ver_value_t *val, language_t *lang )
1354 ver_value_t *new_val, *head = NULL, *tail = NULL;
1356 while (val)
1358 new_val = new_ver_value();
1359 *new_val = *val;
1360 if (val->type == val_words)
1362 char *key = convert_msgid_ascii( val->key, 0 );
1363 if (!strcasecmp( key, "Translation" ) &&
1364 val->value.words->nwords == 2 &&
1365 val->value.words->words[0] == MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ))
1367 ver_words_t *new_words;
1368 LANGID langid;
1369 WORD codepage;
1370 langid = MAKELANGID( lang->id, get_default_sublang( lang ) );
1371 new_words = new_ver_words( langid );
1372 if (val->value.words->words[1] == 1200)
1373 codepage = 1200;
1374 else
1375 codepage = get_language_codepage( lang->id, lang->sub );
1376 new_val->value.words = add_ver_words( new_words, codepage );
1378 free( key );
1380 if (tail) tail->next = new_val;
1381 else head = new_val;
1382 new_val->next = NULL;
1383 new_val->prev = tail;
1384 tail = new_val;
1385 val = val->next;
1387 return head;
1390 static ver_block_t *translate_versioninfo( ver_block_t *blk, language_t *lang, int *found )
1392 ver_block_t *new_blk, *head = NULL, *tail = NULL;
1393 char *name;
1395 while (blk)
1397 new_blk = new_ver_block();
1398 *new_blk = *blk;
1399 name = convert_msgid_ascii( blk->name, 0 );
1400 if (!strcasecmp( name, "stringfileinfo" ))
1401 new_blk->values = translate_stringfileinfo( blk->values, lang, found );
1402 else if (!strcasecmp( name, "varfileinfo" ))
1403 new_blk->values = translate_varfileinfo( blk->values, lang );
1404 free(name);
1405 if (tail) tail->next = new_blk;
1406 else head = new_blk;
1407 new_blk->next = NULL;
1408 new_blk->prev = tail;
1409 tail = new_blk;
1410 blk = blk->next;
1412 return head;
1415 static void translate_resources( language_t *lang )
1417 resource_t *res;
1419 for (res = resource_top; res; res = res->next)
1421 resource_t *new = NULL;
1422 int found = 0;
1424 if (!is_english( res->lan )) continue;
1426 switch (res->type)
1428 case res_acc:
1429 new = dup_resource( res, lang );
1430 new->res.acc->events = translate_accel( res->res.acc, new->res.acc, &found );
1431 break;
1432 case res_dlg:
1433 new = dup_resource( res, lang );
1434 translate_dialog( res->res.dlg, new->res.dlg, &found );
1435 break;
1436 case res_men:
1437 new = dup_resource( res, lang );
1438 new->res.men->items = translate_items( res->res.men->items, &found );
1439 break;
1440 case res_stt:
1441 new = dup_resource( res, lang );
1442 new->res.stt = translate_stringtable( res->res.stt, lang, &found );
1443 break;
1444 case res_ver:
1445 new = dup_resource( res, lang );
1446 new->res.ver->blocks = translate_versioninfo( res->res.ver->blocks, lang, &found );
1447 break;
1448 case res_msg:
1449 /* FIXME */
1450 break;
1451 default:
1452 break;
1455 if (new && found)
1457 if (new_tail) new_tail->next = new;
1458 else new_top = new;
1459 new->prev = new_tail;
1460 new_tail = new;
1465 void add_translations( const char *po_dir )
1467 resource_t *res;
1468 char buffer[256];
1469 char *p, *tok, *name;
1470 unsigned int i;
1471 FILE *f;
1473 /* first check if we have English resources to translate */
1474 for (res = resource_top; res; res = res->next) if (is_english( res->lan )) break;
1475 if (!res) return;
1477 if (!po_dir) /* run through the translation process to remove msg contexts */
1479 translate_resources( new_language( LANG_ENGLISH, SUBLANG_DEFAULT ));
1480 goto done;
1483 new_top = new_tail = NULL;
1485 name = strmake( "%s/LINGUAS", po_dir );
1486 if (!(f = fopen( name, "r" )))
1488 free( name );
1489 return;
1491 free( name );
1492 while (fgets( buffer, sizeof(buffer), f ))
1494 if ((p = strchr( buffer, '#' ))) *p = 0;
1495 for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
1497 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
1498 if (!strcmp( tok, languages[i].name )) break;
1500 if (i == sizeof(languages)/sizeof(languages[0]))
1501 error( "unknown language '%s'\n", tok );
1503 name = strmake( "%s/%s.mo", po_dir, tok );
1504 load_mo_file( name );
1505 translate_resources( new_language(languages[i].id, languages[i].sub) );
1506 free_mo_file();
1507 free( name );
1510 fclose( f );
1512 done:
1513 /* prepend the translated resources to the global list */
1514 if (new_tail)
1516 new_tail->next = resource_top;
1517 resource_top->prev = new_tail;
1518 resource_top = new_top;