ddraw/tests: Add a test for fog_start == fog_end.
[wine.git] / tools / wrc / po.c
blobb31594ff9ee294d40ba1f7349a6c3d3c1e09115e
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"
43 #include "wine/unicode.h"
45 static resource_t *new_top, *new_tail;
47 struct mo_file
49 unsigned int magic;
50 unsigned int revision;
51 unsigned int count;
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 )
80 name_id_t *new;
82 if (!id || id->type != name_str) return id;
83 new = new_name_id();
84 *new = *id;
85 new->name.s_name = convert_string( id->name.s_name, str_unicode, 1252 );
86 return new;
89 static char *convert_msgid_ascii( const string_t *str, int error_on_invalid_char )
91 int i;
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] );
105 free( buffer);
106 free_string( newstr );
107 return NULL;
109 buffer[i] = 0;
110 free_string( newstr );
111 return buffer;
114 static char *get_message_context( char **msgid )
116 static const char magic[] = "#msgctxt#";
117 char *id, *context;
119 if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
120 context = *msgid + sizeof(magic) - 1;
121 if (!(id = strchr( context, '#' ))) return NULL;
122 *id = 0;
123 *msgid = id + 1;
124 return context;
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)
136 case SS_LEFT:
137 case SS_CENTER:
138 case SS_RIGHT:
139 return 1;
140 default:
141 return 0;
144 return 1;
147 static resource_t *dup_resource( resource_t *res, language_t *lang )
149 resource_t *new = xmalloc( sizeof(*new) );
151 *new = *res;
152 new->lan = lang;
153 new->next = new->prev = NULL;
154 new->name = dup_name_id( res->name );
156 switch (res->type)
158 case res_acc:
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 );
163 break;
164 case res_dlg:
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 );
169 break;
170 case res_men:
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 );
175 break;
176 case res_stt:
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 );
181 break;
182 default:
183 assert(0);
185 return new;
188 static const struct
190 unsigned int id, sub;
191 const char *name;
192 } languages[] =
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_IRISH, SUBLANG_NEUTRAL, "ga" },
284 { LANG_IRISH, SUBLANG_IRISH_IRELAND, "ga_IE" },
285 { LANG_ITALIAN, SUBLANG_NEUTRAL, "it" },
286 { LANG_ITALIAN, SUBLANG_ITALIAN, "it_IT" },
287 { LANG_ITALIAN, SUBLANG_ITALIAN_SWISS, "it_CH" },
288 { LANG_JAPANESE, SUBLANG_NEUTRAL, "ja" },
289 { LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN, "ja_JP" },
290 { LANG_KOREAN, SUBLANG_NEUTRAL, "ko" },
291 { LANG_KOREAN, SUBLANG_KOREAN, "ko_KR" },
292 { LANG_DUTCH, SUBLANG_NEUTRAL, "nl" },
293 { LANG_DUTCH, SUBLANG_DUTCH, "nl_NL" },
294 { LANG_DUTCH, SUBLANG_DUTCH_BELGIAN, "nl_BE" },
295 { LANG_DUTCH, SUBLANG_DUTCH_SURINAM, "nl_SR" },
296 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL, "nb_NO" },
297 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK, "nn_NO" },
298 { LANG_POLISH, SUBLANG_NEUTRAL, "pl" },
299 { LANG_POLISH, SUBLANG_POLISH_POLAND, "pl_PL" },
300 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, "pt" },
301 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN, "pt_BR" },
302 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_PORTUGAL, "pt_PT" },
303 { LANG_ROMANSH, SUBLANG_NEUTRAL, "rm" },
304 { LANG_ROMANSH, SUBLANG_ROMANSH_SWITZERLAND, "rm_CH" },
305 { LANG_ROMANIAN, SUBLANG_NEUTRAL, "ro" },
306 { LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA, "ro_RO" },
307 { LANG_RUSSIAN, SUBLANG_NEUTRAL, "ru" },
308 { LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA, "ru_RU" },
309 { LANG_SCOTTISH_GAELIC,SUBLANG_NEUTRAL, "gd" },
310 { LANG_SCOTTISH_GAELIC,SUBLANG_SCOTTISH_GAELIC, "gd_GB" },
311 { LANG_SERBIAN, SUBLANG_NEUTRAL, "hr" },
312 { LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA, "hr_HR" },
313 { LANG_SERBIAN, SUBLANG_SERBIAN_LATIN, "sr_RS@latin" },
314 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, "sr_RS@cyrillic" },
315 { LANG_SLOVAK, SUBLANG_NEUTRAL, "sk" },
316 { LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA, "sk_SK" },
317 { LANG_ALBANIAN, SUBLANG_NEUTRAL, "sq" },
318 { LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA, "sq_AL" },
319 { LANG_SWEDISH, SUBLANG_NEUTRAL, "sv" },
320 { LANG_SWEDISH, SUBLANG_SWEDISH_SWEDEN, "sv_SE" },
321 { LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND, "sv_FI" },
322 { LANG_THAI, SUBLANG_NEUTRAL, "th" },
323 { LANG_THAI, SUBLANG_THAI_THAILAND, "th_TH" },
324 { LANG_TURKISH, SUBLANG_NEUTRAL, "tr" },
325 { LANG_TURKISH, SUBLANG_TURKISH_TURKEY, "tr_TR" },
326 { LANG_URDU, SUBLANG_NEUTRAL, "ur" },
327 { LANG_URDU, SUBLANG_URDU_PAKISTAN, "ur_PK" },
328 { LANG_INDONESIAN, SUBLANG_NEUTRAL, "id" },
329 { LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA, "id_ID" },
330 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, "uk" },
331 { LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE, "uk_UA" },
332 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, "be" },
333 { LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS, "be_BY" },
334 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, "sl" },
335 { LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA, "sl_SI" },
336 { LANG_ESTONIAN, SUBLANG_NEUTRAL, "et" },
337 { LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA, "et_EE" },
338 { LANG_LATVIAN, SUBLANG_NEUTRAL, "lv" },
339 { LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA, "lv_LV" },
340 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, "lt" },
341 { LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA, "lt_LT" },
342 { LANG_PERSIAN, SUBLANG_NEUTRAL, "fa" },
343 { LANG_PERSIAN, SUBLANG_PERSIAN_IRAN, "fa_IR" },
344 { LANG_ARMENIAN, SUBLANG_NEUTRAL, "hy" },
345 { LANG_ARMENIAN, SUBLANG_ARMENIAN_ARMENIA, "hy_AM" },
346 { LANG_AZERI, SUBLANG_NEUTRAL, "az" },
347 { LANG_AZERI, SUBLANG_AZERI_LATIN, "az_AZ@latin" },
348 { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, "az_AZ@cyrillic" },
349 { LANG_BASQUE, SUBLANG_NEUTRAL, "eu" },
350 { LANG_BASQUE, SUBLANG_BASQUE_BASQUE, "eu_ES" },
351 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, "mk" },
352 { LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA, "mk_MK" },
353 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, "af" },
354 { LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA, "af_ZA" },
355 { LANG_GEORGIAN, SUBLANG_NEUTRAL, "ka" },
356 { LANG_GEORGIAN, SUBLANG_GEORGIAN_GEORGIA, "ka_GE" },
357 { LANG_FAEROESE, SUBLANG_NEUTRAL, "fo" },
358 { LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS, "fo_FO" },
359 { LANG_HINDI, SUBLANG_NEUTRAL, "hi" },
360 { LANG_HINDI, SUBLANG_HINDI_INDIA, "hi_IN" },
361 { LANG_MALAY, SUBLANG_NEUTRAL, "ms" },
362 { LANG_MALAY, SUBLANG_MALAY_MALAYSIA, "ms_MY" },
363 { LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM, "ms_BN" },
364 { LANG_KAZAK, SUBLANG_NEUTRAL, "kk" },
365 { LANG_KAZAK, SUBLANG_KAZAK_KAZAKHSTAN, "kk_KZ" },
366 { LANG_KYRGYZ, SUBLANG_NEUTRAL, "ky" },
367 { LANG_KYRGYZ, SUBLANG_KYRGYZ_KYRGYZSTAN, "ky_KG" },
368 { LANG_SWAHILI, SUBLANG_NEUTRAL, "sw" },
369 { LANG_SWAHILI, SUBLANG_SWAHILI_KENYA, "sw_KE" },
370 { LANG_UZBEK, SUBLANG_NEUTRAL, "uz" },
371 { LANG_UZBEK, SUBLANG_UZBEK_LATIN, "uz_UZ@latin" },
372 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, "uz_UZ@cyrillic" },
373 { LANG_TATAR, SUBLANG_NEUTRAL, "tt" },
374 { LANG_TATAR, SUBLANG_TATAR_RUSSIA, "tt_TA" },
375 { LANG_PUNJABI, SUBLANG_NEUTRAL, "pa" },
376 { LANG_PUNJABI, SUBLANG_PUNJABI_INDIA, "pa_IN" },
377 { LANG_GUJARATI, SUBLANG_NEUTRAL, "gu" },
378 { LANG_GUJARATI, SUBLANG_GUJARATI_INDIA, "gu_IN" },
379 { LANG_ORIYA, SUBLANG_NEUTRAL, "or" },
380 { LANG_ORIYA, SUBLANG_ORIYA_INDIA, "or_IN" },
381 { LANG_TAMIL, SUBLANG_NEUTRAL, "ta" },
382 { LANG_TAMIL, SUBLANG_TAMIL_INDIA, "ta_IN" },
383 { LANG_TELUGU, SUBLANG_NEUTRAL, "te" },
384 { LANG_TELUGU, SUBLANG_TELUGU_INDIA, "te_IN" },
385 { LANG_KANNADA, SUBLANG_NEUTRAL, "kn" },
386 { LANG_KANNADA, SUBLANG_KANNADA_INDIA, "kn_IN" },
387 { LANG_MALAYALAM, SUBLANG_NEUTRAL, "ml" },
388 { LANG_MALAYALAM, SUBLANG_MALAYALAM_INDIA, "ml_IN" },
389 { LANG_MARATHI, SUBLANG_NEUTRAL, "mr" },
390 { LANG_MARATHI, SUBLANG_MARATHI_INDIA, "mr_IN" },
391 { LANG_SANSKRIT, SUBLANG_NEUTRAL, "sa" },
392 { LANG_SANSKRIT, SUBLANG_SANSKRIT_INDIA, "sa_IN" },
393 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, "mn" },
394 { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
395 { LANG_WELSH, SUBLANG_NEUTRAL, "cy" },
396 { LANG_WELSH, SUBLANG_WELSH_UNITED_KINGDOM, "cy_GB" },
397 { LANG_GALICIAN, SUBLANG_NEUTRAL, "gl" },
398 { LANG_GALICIAN, SUBLANG_GALICIAN_GALICIAN, "gl_ES" },
399 { LANG_KONKANI, SUBLANG_NEUTRAL, "kok" },
400 { LANG_KONKANI, SUBLANG_KONKANI_INDIA, "kok_IN" },
401 { LANG_DIVEHI, SUBLANG_NEUTRAL, "dv" },
402 { LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES, "dv_MV" },
403 { LANG_BRETON, SUBLANG_NEUTRAL, "br" },
404 { LANG_BRETON, SUBLANG_BRETON_FRANCE, "br_FR" },
406 #ifdef LANG_ESPERANTO
407 { LANG_ESPERANTO, SUBLANG_DEFAULT, "eo" },
408 #endif
409 #ifdef LANG_WALON
410 { LANG_WALON, SUBLANG_NEUTRAL, "wa" },
411 { LANG_WALON, SUBLANG_DEFAULT, "wa_BE" },
412 #endif
413 #ifdef LANG_CORNISH
414 { LANG_CORNISH, SUBLANG_NEUTRAL, "kw" },
415 { LANG_CORNISH, SUBLANG_DEFAULT, "kw_GB" },
416 #endif
417 #ifdef LANG_MANX_GAELIC
418 { LANG_MANX_GAELIC, SUBLANG_MANX_GAELIC, "gv_GB" },
419 #endif
422 #ifndef HAVE_LIBGETTEXTPO
424 void write_pot_file( const char *outname )
426 error( "PO files not supported in this wrc build\n" );
429 void write_po_files( const char *outname )
431 error( "PO files not supported in this wrc build\n" );
434 #else /* HAVE_LIBGETTEXTPO */
436 static void po_xerror( int severity, po_message_t message,
437 const char *filename, size_t lineno, size_t column,
438 int multiline_p, const char *message_text )
440 fprintf( stderr, "%s:%u:%u: %s\n",
441 filename, (unsigned int)lineno, (unsigned int)column, message_text );
442 if (severity) exit(1);
445 static void po_xerror2( int severity, po_message_t message1,
446 const char *filename1, size_t lineno1, size_t column1,
447 int multiline_p1, const char *message_text1,
448 po_message_t message2,
449 const char *filename2, size_t lineno2, size_t column2,
450 int multiline_p2, const char *message_text2 )
452 fprintf( stderr, "%s:%u:%u: %s\n",
453 filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
454 fprintf( stderr, "%s:%u:%u: %s\n",
455 filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
456 if (severity) exit(1);
459 static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
461 static char *convert_string_utf8( const string_t *str, int codepage )
463 string_t *newstr = convert_string( str, str_unicode, codepage );
464 char *buffer = xmalloc( newstr->size * 4 + 1 );
465 int len = wine_utf8_wcstombs( 0, newstr->str.wstr, newstr->size, buffer, newstr->size * 4 );
466 buffer[len] = 0;
467 free_string( newstr );
468 return buffer;
471 static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
472 po_message_iterator_t *iterator )
474 po_message_t msg;
475 const char *context;
477 *iterator = po_message_iterator( po, NULL );
478 while ((msg = po_next_message( *iterator )))
480 if (strcmp( po_message_msgid( msg ), msgid )) continue;
481 if (!msgctxt) break;
482 if (!(context = po_message_msgctxt( msg ))) continue;
483 if (!strcmp( context, msgctxt )) break;
485 return msg;
488 static void add_po_string( po_file_t po, const string_t *msgid, const string_t *msgstr,
489 const language_t *lang )
491 static const char dnt[] = "do not translate";
492 po_message_t msg;
493 po_message_iterator_t iterator;
494 int codepage;
495 char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
497 if (!msgid->size) return;
499 id_buffer = id = convert_msgid_ascii( msgid, 1 );
500 context = get_message_context( &id );
501 if (context && strcmp(context, dnt) == 0)
503 /* This string should not be translated */
504 free( id_buffer );
505 return;
508 if (msgstr)
510 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
511 else codepage = get_language_codepage( 0, 0 );
512 assert( codepage != -1 );
513 str_buffer = str = convert_string_utf8( msgstr, codepage );
514 if (is_english( lang )) get_message_context( &str );
516 if (!(msg = find_message( po, id, context, &iterator )))
518 msg = po_message_create();
519 po_message_set_msgid( msg, id );
520 po_message_set_msgstr( msg, str ? str : "" );
521 if (context) po_message_set_msgctxt( msg, context );
522 po_message_insert( iterator, msg );
524 if (msgid->loc.file) po_message_add_filepos( msg, msgid->loc.file, msgid->loc.line );
525 po_message_iterator_free( iterator );
526 free( id_buffer );
527 free( str_buffer );
530 struct po_file_lang
532 struct list entry;
533 language_t lang;
534 po_file_t po;
537 static struct list po_file_langs = LIST_INIT( po_file_langs );
539 static po_file_t create_po_file(void)
541 po_file_t po;
542 po_message_t msg;
543 po_message_iterator_t iterator;
545 po = po_file_create();
546 iterator = po_message_iterator( po, NULL );
547 msg = po_message_create();
548 po_message_set_msgid( msg, "" );
549 po_message_set_msgstr( msg,
550 "Project-Id-Version: Wine\n"
551 "Report-Msgid-Bugs-To: http://bugs.winehq.org\n"
552 "POT-Creation-Date: N/A\n"
553 "PO-Revision-Date: N/A\n"
554 "Last-Translator: Automatically generated\n"
555 "Language-Team: none\n"
556 "MIME-Version: 1.0\n"
557 "Content-Type: text/plain; charset=UTF-8\n"
558 "Content-Transfer-Encoding: 8bit\n" );
559 po_message_insert( iterator, msg );
560 po_message_iterator_free( iterator );
561 return po;
564 static po_file_t get_po_file( const language_t *lang )
566 struct po_file_lang *po_file;
568 LIST_FOR_EACH_ENTRY( po_file, &po_file_langs, struct po_file_lang, entry )
569 if (po_file->lang.id == lang->id && po_file->lang.sub == lang->sub) return po_file->po;
571 /* create a new one */
572 po_file = xmalloc( sizeof(*po_file) );
573 po_file->lang = *lang;
574 po_file->po = create_po_file();
575 list_add_tail( &po_file_langs, &po_file->entry );
576 return po_file->po;
579 static const char *get_language_name( const language_t *lang )
581 static char name[20];
582 unsigned int i;
584 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
585 if (languages[i].id == lang->id && languages[i].sub == lang->sub)
586 return languages[i].name;
588 sprintf( name, "%02x-%02x", lang->id, lang->sub );
589 return name;
592 static char *get_po_file_name( const language_t *lang )
594 return strmake( "%s.po", get_language_name( lang ) );
597 static unsigned int flush_po_files( const char *output_name )
599 struct po_file_lang *po_file, *next;
600 unsigned int count = 0;
602 LIST_FOR_EACH_ENTRY_SAFE( po_file, next, &po_file_langs, struct po_file_lang, entry )
604 char *name = get_po_file_name( &po_file->lang );
605 if (output_name)
607 const char *p = strrchr( output_name, '/' );
608 if (p) p++;
609 else p = output_name;
610 if (!strcmp( p, name ))
612 po_file_write( po_file->po, name, &po_xerror_handler );
613 count++;
616 else /* no specified output name, output a file for every language found */
618 po_file_write( po_file->po, name, &po_xerror_handler );
619 count++;
620 fprintf( stderr, "created %s\n", name );
622 po_file_free( po_file->po );
623 list_remove( &po_file->entry );
624 free( po_file );
625 free( name );
627 return count;
630 static void add_pot_stringtable( po_file_t po, const resource_t *res )
632 const stringtable_t *stt = res->res.stt;
633 int i;
635 while (stt)
637 for (i = 0; i < stt->nentries; i++)
638 if (stt->entries[i].str) add_po_string( po, stt->entries[i].str, NULL, NULL );
639 stt = stt->next;
643 static void add_po_stringtable( const resource_t *english, const resource_t *res )
645 const stringtable_t *english_stt = english->res.stt;
646 const stringtable_t *stt = res->res.stt;
647 po_file_t po = get_po_file( stt->lvc.language );
648 int i;
650 while (english_stt && stt)
652 for (i = 0; i < stt->nentries; i++)
653 if (english_stt->entries[i].str && stt->entries[i].str)
654 add_po_string( po, english_stt->entries[i].str, stt->entries[i].str, stt->lvc.language );
655 stt = stt->next;
656 english_stt = english_stt->next;
660 static void add_pot_dialog_controls( po_file_t po, const control_t *ctrl )
662 while (ctrl)
664 if (control_has_title( ctrl )) add_po_string( po, ctrl->title->name.s_name, NULL, NULL );
665 ctrl = ctrl->next;
669 static void add_pot_dialog( po_file_t po, const resource_t *res )
671 const dialog_t *dlg = res->res.dlg;
673 if (dlg->title) add_po_string( po, dlg->title, NULL, NULL );
674 add_pot_dialog_controls( po, dlg->controls );
677 static void compare_dialogs( const dialog_t *english_dlg, const dialog_t *dlg )
679 const control_t *english_ctrl, *ctrl;
680 unsigned int style = 0, exstyle = 0, english_style = 0, english_exstyle = 0;
681 char *name;
682 char *title = english_dlg->title ? convert_msgid_ascii( english_dlg->title, 0 ) : xstrdup("??");
684 if (english_dlg->width != dlg->width || english_dlg->height != dlg->height)
685 warning( "%s: dialog %s doesn't have the same size (%d,%d vs %d,%d)\n",
686 get_language_name( dlg->lvc.language ), title, dlg->width, dlg->height,
687 english_dlg->width, english_dlg->height );
689 if (dlg->gotstyle) style = dlg->style->or_mask;
690 if (dlg->gotexstyle) exstyle = dlg->exstyle->or_mask;
691 if (english_dlg->gotstyle) english_style = english_dlg->style->or_mask;
692 if (english_dlg->gotexstyle) english_exstyle = english_dlg->exstyle->or_mask;
693 if (is_rtl_language( dlg->lvc.language )) english_exstyle |= WS_EX_LAYOUTRTL;
695 if (english_style != style)
696 warning( "%s: dialog %s doesn't have the same style (%08x vs %08x)\n",
697 get_language_name( dlg->lvc.language ), title, style, english_style );
698 if (english_exstyle != exstyle)
699 warning( "%s: dialog %s doesn't have the same exstyle (%08x vs %08x)\n",
700 get_language_name( dlg->lvc.language ), title, exstyle, english_exstyle );
702 if (english_dlg->font || dlg->font)
704 int size = 0, english_size = 0;
705 char *font = NULL, *english_font = NULL;
707 if (english_dlg->font)
709 english_font = convert_msgid_ascii( english_dlg->font->name, 0 );
710 english_size = english_dlg->font->size;
712 if (dlg->font)
714 font = convert_msgid_ascii( dlg->font->name, 0 );
715 size = dlg->font->size;
717 if (uses_larger_font( dlg->lvc.language )) english_size++;
719 if (!english_font || !font || strcasecmp( english_font, font ) || english_size != size)
720 warning( "%s: dialog %s doesn't have the same font (%s %u vs %s %u)\n",
721 get_language_name(dlg->lvc.language), title,
722 english_font ? english_font : "default", english_size,
723 font ? font : "default", size );
724 free( font );
725 free( english_font );
727 english_ctrl = english_dlg->controls;
728 ctrl = dlg->controls;
729 for ( ; english_ctrl && ctrl; ctrl = ctrl->next, english_ctrl = english_ctrl->next )
731 if (control_has_title( english_ctrl ))
732 name = convert_msgid_ascii( english_ctrl->title->name.s_name, 0 );
733 else
734 name = strmake( "%d", ctrl->id );
736 if (english_ctrl->width != ctrl->width || english_ctrl->height != ctrl->height)
737 warning( "%s: dialog %s control %s doesn't have the same size (%d,%d vs %d,%d)\n",
738 get_language_name( dlg->lvc.language ), title, name,
739 ctrl->width, ctrl->height, english_ctrl->width, english_ctrl->height );
740 if (english_ctrl->x != ctrl->x || english_ctrl->y != ctrl->y)
741 warning( "%s: dialog %s control %s doesn't have the same position (%d,%d vs %d,%d)\n",
742 get_language_name( dlg->lvc.language ), title, name,
743 ctrl->x, ctrl->y, english_ctrl->x, english_ctrl->y );
744 free( name );
746 free( title );
749 static void add_po_dialog_controls( po_file_t po, const control_t *english_ctrl,
750 const control_t *ctrl, const language_t *lang )
752 while (english_ctrl && ctrl)
754 if (control_has_title( english_ctrl ) && control_has_title( ctrl ))
755 add_po_string( po, english_ctrl->title->name.s_name, ctrl->title->name.s_name, lang );
757 ctrl = ctrl->next;
758 english_ctrl = english_ctrl->next;
762 static void add_po_dialog( const resource_t *english, const resource_t *res )
764 const dialog_t *english_dlg = english->res.dlg;
765 const dialog_t *dlg = res->res.dlg;
766 po_file_t po = get_po_file( dlg->lvc.language );
768 compare_dialogs( english_dlg, dlg );
770 if (english_dlg->title && dlg->title)
771 add_po_string( po, english_dlg->title, dlg->title, dlg->lvc.language );
772 add_po_dialog_controls( po, english_dlg->controls, dlg->controls, dlg->lvc.language );
775 static void add_pot_menu_items( po_file_t po, const menu_item_t *item )
777 while (item)
779 if (item->name) add_po_string( po, item->name, NULL, NULL );
780 if (item->popup) add_pot_menu_items( po, item->popup );
781 item = item->next;
785 static void add_pot_menu( po_file_t po, const resource_t *res )
787 add_pot_menu_items( po, res->res.men->items );
790 static void add_po_menu_items( po_file_t po, const menu_item_t *english_item,
791 const menu_item_t *item, const language_t *lang )
793 while (english_item && item)
795 if (english_item->name && item->name)
796 add_po_string( po, english_item->name, item->name, lang );
797 if (english_item->popup && item->popup)
798 add_po_menu_items( po, english_item->popup, item->popup, lang );
799 item = item->next;
800 english_item = english_item->next;
804 static void add_po_menu( const resource_t *english, const resource_t *res )
806 const menu_item_t *english_items = english->res.men->items;
807 const menu_item_t *items = res->res.men->items;
808 po_file_t po = get_po_file( res->res.men->lvc.language );
810 add_po_menu_items( po, english_items, items, res->res.men->lvc.language );
813 static int string_has_context( const string_t *str )
815 char *id, *id_buffer, *context;
817 id_buffer = id = convert_msgid_ascii( str, 1 );
818 context = get_message_context( &id );
819 free( id_buffer );
820 return context != NULL;
823 static void add_pot_accel( po_file_t po, const resource_t *res )
825 event_t *event = res->res.acc->events;
827 while (event)
829 /* accelerators without a context don't make sense in po files */
830 if (event->str && string_has_context( event->str ))
831 add_po_string( po, event->str, NULL, NULL );
832 event = event->next;
836 static void add_po_accel( const resource_t *english, const resource_t *res )
838 event_t *english_event = english->res.acc->events;
839 event_t *event = res->res.acc->events;
840 po_file_t po = get_po_file( res->res.acc->lvc.language );
842 while (english_event && event)
844 if (english_event->str && event->str && string_has_context( english_event->str ))
845 add_po_string( po, english_event->str, event->str, res->res.acc->lvc.language );
846 event = event->next;
847 english_event = english_event->next;
851 static resource_t *find_english_resource( resource_t *res )
853 resource_t *ptr;
855 for (ptr = resource_top; ptr; ptr = ptr->next)
857 if (ptr->type != res->type) continue;
858 if (!ptr->lan) continue;
859 if (!is_english( ptr->lan )) continue;
860 if (compare_name_id( ptr->name, res->name )) continue;
861 return ptr;
863 return NULL;
866 void write_pot_file( const char *outname )
868 resource_t *res;
869 po_file_t po = create_po_file();
871 for (res = resource_top; res; res = res->next)
873 if (!is_english( res->lan )) continue;
875 switch (res->type)
877 case res_acc: add_pot_accel( po, res ); break;
878 case res_dlg: add_pot_dialog( po, res ); break;
879 case res_men: add_pot_menu( po, res ); break;
880 case res_stt: add_pot_stringtable( po, res ); break;
881 case res_msg: break; /* FIXME */
882 default: break;
885 po_file_write( po, outname, &po_xerror_handler );
886 po_file_free( po );
889 void write_po_files( const char *outname )
891 resource_t *res, *english;
893 for (res = resource_top; res; res = res->next)
895 if (!(english = find_english_resource( res ))) continue;
896 switch (res->type)
898 case res_acc: add_po_accel( english, res ); break;
899 case res_dlg: add_po_dialog( english, res ); break;
900 case res_men: add_po_menu( english, res ); break;
901 case res_stt: add_po_stringtable( english, res ); break;
902 case res_msg: break; /* FIXME */
903 default: break;
906 if (!flush_po_files( outname ))
908 if (outname) error( "No translations found for %s\n", outname );
909 else error( "No translations found\n" );
913 #endif /* HAVE_LIBGETTEXTPO */
915 static struct mo_file *mo_file;
917 static void byteswap( unsigned int *data, unsigned int count )
919 unsigned int i;
921 for (i = 0; i < count; i++)
922 data[i] = data[i] >> 24 | (data[i] >> 8 & 0xff00) | (data[i] << 8 & 0xff0000) | data[i] << 24;
925 static void load_mo_file( const char *name )
927 struct stat st;
928 int res, fd;
930 fd = open( name, O_RDONLY | O_BINARY );
931 if (fd == -1) fatal_perror( "Failed to open %s", name );
932 fstat( fd, &st );
933 mo_file = xmalloc( st.st_size );
934 res = read( fd, mo_file, st.st_size );
935 if (res == -1) fatal_perror( "Failed to read %s", name );
936 else if (res != st.st_size) error( "Failed to read %s\n", name );
937 close( fd );
939 /* sanity checks */
941 if (st.st_size < sizeof(*mo_file))
942 error( "%s is not a valid .mo file\n", name );
943 if (mo_file->magic == 0xde120495)
944 byteswap( &mo_file->revision, 4 );
945 else if (mo_file->magic != 0x950412de)
946 error( "%s is not a valid .mo file\n", name );
947 if ((mo_file->revision >> 16) > 1)
948 error( "%s: unsupported file version %x\n", name, mo_file->revision );
949 if (mo_file->msgid_off >= st.st_size ||
950 mo_file->msgstr_off >= st.st_size ||
951 st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count)
952 error( "%s: corrupted file\n", name );
954 if (mo_file->magic == 0xde120495)
956 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgid_off), 2 * mo_file->count );
957 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgstr_off), 2 * mo_file->count );
961 static void free_mo_file(void)
963 free( mo_file );
964 mo_file = NULL;
967 static inline const char *get_mo_msgid( int index )
969 const char *base = (const char *)mo_file;
970 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgid_off);
971 return base + offsets[2 * index + 1];
974 static inline const char *get_mo_msgstr( int index )
976 const char *base = (const char *)mo_file;
977 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgstr_off);
978 return base + offsets[2 * index + 1];
981 static const char *get_msgstr( const char *msgid, const char *context, int *found )
983 int pos, res, min, max;
984 const char *ret = msgid;
985 char *id = NULL;
987 if (!mo_file) /* strings containing a context still need to be transformed */
989 if (context) (*found)++;
990 return ret;
993 if (context) id = strmake( "%s%c%s", context, 4, msgid );
994 min = 0;
995 max = mo_file->count - 1;
996 while (min <= max)
998 pos = (min + max) / 2;
999 res = strcmp( get_mo_msgid(pos), id ? id : msgid );
1000 if (!res)
1002 const char *str = get_mo_msgstr( pos );
1003 if (str[0]) /* ignore empty strings */
1005 ret = str;
1006 (*found)++;
1008 break;
1010 if (res > 0) max = pos - 1;
1011 else min = pos + 1;
1013 free( id );
1014 return ret;
1017 static string_t *translate_string( string_t *str, int *found )
1019 string_t *new;
1020 const char *transl;
1021 int res;
1022 char *buffer, *msgid, *context;
1024 if (!str->size || !(buffer = convert_msgid_ascii( str, 0 )))
1025 return convert_string( str, str_unicode, 1252 );
1027 msgid = buffer;
1028 context = get_message_context( &msgid );
1029 transl = get_msgstr( msgid, context, found );
1031 new = xmalloc( sizeof(*new) );
1032 new->type = str_unicode;
1033 new->size = wine_utf8_mbstowcs( 0, transl, strlen(transl), NULL, 0 );
1034 new->str.wstr = xmalloc( (new->size+1) * sizeof(WCHAR) );
1035 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, transl, strlen(transl), new->str.wstr, new->size );
1036 if (res == -2)
1037 error( "Invalid utf-8 character in string '%s'\n", transl );
1038 new->str.wstr[new->size] = 0;
1039 free( buffer );
1040 return new;
1043 static control_t *translate_controls( control_t *ctrl, int *found )
1045 control_t *new, *head = NULL, *tail = NULL;
1047 while (ctrl)
1049 new = xmalloc( sizeof(*new) );
1050 *new = *ctrl;
1051 if (control_has_title( ctrl ))
1053 new->title = new_name_id();
1054 *new->title = *ctrl->title;
1055 new->title->name.s_name = translate_string( ctrl->title->name.s_name, found );
1057 else new->title = dup_name_id( ctrl->title );
1058 new->ctlclass = dup_name_id( ctrl->ctlclass );
1059 if (tail) tail->next = new;
1060 else head = new;
1061 new->next = NULL;
1062 new->prev = tail;
1063 tail = new;
1064 ctrl = ctrl->next;
1066 return head;
1069 static menu_item_t *translate_items( menu_item_t *item, int *found )
1071 menu_item_t *new, *head = NULL, *tail = NULL;
1073 while (item)
1075 new = xmalloc( sizeof(*new) );
1076 *new = *item;
1077 if (item->name) new->name = translate_string( item->name, found );
1078 if (item->popup) new->popup = translate_items( item->popup, found );
1079 if (tail) tail->next = new;
1080 else head = new;
1081 new->next = NULL;
1082 new->prev = tail;
1083 tail = new;
1084 item = item->next;
1086 return head;
1089 static stringtable_t *translate_stringtable( stringtable_t *stt, language_t *lang, int *found )
1091 stringtable_t *new, *head = NULL, *tail = NULL;
1092 int i;
1094 while (stt)
1096 new = xmalloc( sizeof(*new) );
1097 *new = *stt;
1098 new->lvc.language = lang;
1099 new->lvc.version = get_dup_version( lang );
1100 new->entries = xmalloc( new->nentries * sizeof(*new->entries) );
1101 memcpy( new->entries, stt->entries, new->nentries * sizeof(*new->entries) );
1102 for (i = 0; i < stt->nentries; i++)
1103 if (stt->entries[i].str)
1104 new->entries[i].str = translate_string( stt->entries[i].str, found );
1106 if (tail) tail->next = new;
1107 else head = new;
1108 new->next = NULL;
1109 new->prev = tail;
1110 tail = new;
1111 stt = stt->next;
1113 return head;
1116 static void translate_dialog( dialog_t *dlg, dialog_t *new, int *found )
1118 if (dlg->title) new->title = translate_string( dlg->title, found );
1119 if (is_rtl_language( new->lvc.language ))
1121 new->gotexstyle = TRUE;
1122 if (dlg->gotexstyle)
1123 new->exstyle = new_style( dlg->exstyle->or_mask | WS_EX_LAYOUTRTL, dlg->exstyle->and_mask );
1124 else
1125 new->exstyle = new_style( WS_EX_LAYOUTRTL, 0 );
1127 if (dlg->font)
1129 new->font = xmalloc( sizeof(*dlg->font) );
1130 *new->font = *dlg->font;
1131 if (uses_larger_font( new->lvc.language )) new->font->size++;
1132 new->font->name = convert_string( dlg->font->name, str_unicode, 1252 );
1134 new->controls = translate_controls( dlg->controls, found );
1137 static event_t *translate_accel( accelerator_t *acc, accelerator_t *new, int *found )
1139 event_t *event, *new_ev, *head = NULL, *tail = NULL;
1141 event = acc->events;
1142 while (event)
1144 new_ev = new_event();
1145 *new_ev = *event;
1146 if (event->str) new_ev->str = translate_string( event->str, found );
1147 if (tail) tail->next = new_ev;
1148 else head = new_ev;
1149 new_ev->next = NULL;
1150 new_ev->prev = tail;
1151 tail = new_ev;
1152 event = event->next;
1154 return head;
1157 static void translate_resources( language_t *lang )
1159 resource_t *res;
1161 for (res = resource_top; res; res = res->next)
1163 resource_t *new = NULL;
1164 int found = 0;
1166 if (!is_english( res->lan )) continue;
1168 switch (res->type)
1170 case res_acc:
1171 new = dup_resource( res, lang );
1172 new->res.acc->events = translate_accel( res->res.acc, new->res.acc, &found );
1173 break;
1174 case res_dlg:
1175 new = dup_resource( res, lang );
1176 translate_dialog( res->res.dlg, new->res.dlg, &found );
1177 break;
1178 case res_men:
1179 new = dup_resource( res, lang );
1180 new->res.men->items = translate_items( res->res.men->items, &found );
1181 break;
1182 case res_stt:
1183 new = dup_resource( res, lang );
1184 new->res.stt = translate_stringtable( res->res.stt, lang, &found );
1185 break;
1186 case res_msg:
1187 /* FIXME */
1188 break;
1189 default:
1190 break;
1193 if (new && found)
1195 if (new_tail) new_tail->next = new;
1196 else new_top = new;
1197 new->prev = new_tail;
1198 new_tail = new;
1203 void add_translations( const char *po_dir )
1205 resource_t *res;
1206 char buffer[256];
1207 char *p, *tok, *name;
1208 unsigned int i;
1209 FILE *f;
1211 /* first check if we have English resources to translate */
1212 for (res = resource_top; res; res = res->next) if (is_english( res->lan )) break;
1213 if (!res) return;
1215 if (!po_dir) /* run through the translation process to remove msg contexts */
1217 translate_resources( new_language( LANG_ENGLISH, SUBLANG_DEFAULT ));
1218 goto done;
1221 new_top = new_tail = NULL;
1223 name = strmake( "%s/LINGUAS", po_dir );
1224 if (!(f = fopen( name, "r" )))
1226 free( name );
1227 return;
1229 free( name );
1230 while (fgets( buffer, sizeof(buffer), f ))
1232 if ((p = strchr( buffer, '#' ))) *p = 0;
1233 for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
1235 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
1236 if (!strcmp( tok, languages[i].name )) break;
1238 if (i == sizeof(languages)/sizeof(languages[0]))
1239 error( "unknown language '%s'\n", tok );
1241 name = strmake( "%s/%s.mo", po_dir, tok );
1242 load_mo_file( name );
1243 translate_resources( new_language(languages[i].id, languages[i].sub) );
1244 free_mo_file();
1245 free( name );
1248 fclose( f );
1250 done:
1251 /* prepend the translated resources to the global list */
1252 if (new_tail)
1254 new_tail->next = resource_top;
1255 resource_top->prev = new_tail;
1256 resource_top = new_top;