comctl32/tests: Make run_test_() static.
[wine.git] / tools / wmc / po.c
blob6db02c8a729a005493728c272edbe466cf1265b7
1 /*
2 * Support for po files
4 * Copyright 2010, 2011 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_GETTEXT_PO_H
31 #include <gettext-po.h>
32 #endif
34 #include "wmc.h"
35 #include "utils.h"
36 #include "lang.h"
37 #include "write.h"
38 #include "windef.h"
40 struct mo_file
42 unsigned int magic;
43 unsigned int revision;
44 unsigned int count;
45 unsigned int msgid_off;
46 unsigned int msgstr_off;
47 /* ... rest of file data here */
50 static lan_blk_t *new_top, *new_tail;
52 static const struct
54 unsigned int id, sub;
55 const char *name;
56 } languages[] =
58 { LANG_ARABIC, SUBLANG_NEUTRAL, "ar" },
59 { LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA, "ar_SA" },
60 { LANG_ARABIC, SUBLANG_ARABIC_IRAQ, "ar_IQ" },
61 { LANG_ARABIC, SUBLANG_ARABIC_EGYPT, "ar_EG" },
62 { LANG_ARABIC, SUBLANG_ARABIC_LIBYA, "ar_LY" },
63 { LANG_ARABIC, SUBLANG_ARABIC_ALGERIA, "ar_DZ" },
64 { LANG_ARABIC, SUBLANG_ARABIC_MOROCCO, "ar_MA" },
65 { LANG_ARABIC, SUBLANG_ARABIC_TUNISIA, "ar_TN" },
66 { LANG_ARABIC, SUBLANG_ARABIC_OMAN, "ar_OM" },
67 { LANG_ARABIC, SUBLANG_ARABIC_YEMEN, "ar_YE" },
68 { LANG_ARABIC, SUBLANG_ARABIC_SYRIA, "ar_SY" },
69 { LANG_ARABIC, SUBLANG_ARABIC_JORDAN, "ar_JO" },
70 { LANG_ARABIC, SUBLANG_ARABIC_LEBANON, "ar_LB" },
71 { LANG_ARABIC, SUBLANG_ARABIC_KUWAIT, "ar_KW" },
72 { LANG_ARABIC, SUBLANG_ARABIC_UAE, "ar_AE" },
73 { LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN, "ar_BH" },
74 { LANG_ARABIC, SUBLANG_ARABIC_QATAR, "ar_QA" },
75 { LANG_BULGARIAN, SUBLANG_NEUTRAL, "bg" },
76 { LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA, "bg_BG" },
77 { LANG_CATALAN, SUBLANG_NEUTRAL, "ca" },
78 { LANG_CATALAN, SUBLANG_CATALAN_CATALAN, "ca_ES" },
79 { LANG_CHINESE, SUBLANG_NEUTRAL, "zh" },
80 { LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL, "zh_TW" },
81 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, "zh_CN" },
82 { LANG_CHINESE, SUBLANG_CHINESE_HONGKONG, "zh_HK" },
83 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, "zh_SG" },
84 { LANG_CHINESE, SUBLANG_CHINESE_MACAU, "zh_MO" },
85 { LANG_CZECH, SUBLANG_NEUTRAL, "cs" },
86 { LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC, "cs_CZ" },
87 { LANG_DANISH, SUBLANG_NEUTRAL, "da" },
88 { LANG_DANISH, SUBLANG_DANISH_DENMARK, "da_DK" },
89 { LANG_GERMAN, SUBLANG_NEUTRAL, "de" },
90 { LANG_GERMAN, SUBLANG_GERMAN, "de_DE" },
91 { LANG_GERMAN, SUBLANG_GERMAN_SWISS, "de_CH" },
92 { LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN, "de_AT" },
93 { LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG, "de_LU" },
94 { LANG_GERMAN, SUBLANG_GERMAN_LIECHTENSTEIN, "de_LI" },
95 { LANG_GREEK, SUBLANG_NEUTRAL, "el" },
96 { LANG_GREEK, SUBLANG_GREEK_GREECE, "el_GR" },
97 { LANG_ENGLISH, SUBLANG_NEUTRAL, "en" },
98 { LANG_ENGLISH, SUBLANG_ENGLISH_US, "en_US" },
99 { LANG_ENGLISH, SUBLANG_ENGLISH_UK, "en_GB" },
100 { LANG_ENGLISH, SUBLANG_ENGLISH_AUS, "en_AU" },
101 { LANG_ENGLISH, SUBLANG_ENGLISH_CAN, "en_CA" },
102 { LANG_ENGLISH, SUBLANG_ENGLISH_NZ, "en_NZ" },
103 { LANG_ENGLISH, SUBLANG_ENGLISH_EIRE, "en_IE" },
104 { LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA, "en_ZA" },
105 { LANG_ENGLISH, SUBLANG_ENGLISH_JAMAICA, "en_JM" },
106 { LANG_ENGLISH, SUBLANG_ENGLISH_CARIBBEAN, "en_CB" },
107 { LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE, "en_BZ" },
108 { LANG_ENGLISH, SUBLANG_ENGLISH_TRINIDAD, "en_TT" },
109 { LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE, "en_ZW" },
110 { LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES, "en_PH" },
111 { LANG_SPANISH, SUBLANG_NEUTRAL, "es" },
112 { LANG_SPANISH, SUBLANG_SPANISH, "es_ES" },
113 { LANG_SPANISH, SUBLANG_SPANISH_MEXICAN, "es_MX" },
114 { LANG_SPANISH, SUBLANG_SPANISH_MODERN, "es_ES_modern" },
115 { LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA, "es_GT" },
116 { LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA, "es_CR" },
117 { LANG_SPANISH, SUBLANG_SPANISH_PANAMA, "es_PA" },
118 { LANG_SPANISH, SUBLANG_SPANISH_DOMINICAN_REPUBLIC, "es_DO" },
119 { LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA, "es_VE" },
120 { LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA, "es_CO" },
121 { LANG_SPANISH, SUBLANG_SPANISH_PERU, "es_PE" },
122 { LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA, "es_AR" },
123 { LANG_SPANISH, SUBLANG_SPANISH_ECUADOR, "es_EC" },
124 { LANG_SPANISH, SUBLANG_SPANISH_CHILE, "es_CL" },
125 { LANG_SPANISH, SUBLANG_SPANISH_URUGUAY, "es_UY" },
126 { LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY, "es_PY" },
127 { LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA, "es_BO" },
128 { LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR, "es_SV" },
129 { LANG_SPANISH, SUBLANG_SPANISH_HONDURAS, "es_HN" },
130 { LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA, "es_NI" },
131 { LANG_SPANISH, SUBLANG_SPANISH_PUERTO_RICO, "es_PR" },
132 { LANG_FINNISH, SUBLANG_NEUTRAL, "fi" },
133 { LANG_FINNISH, SUBLANG_FINNISH_FINLAND, "fi_FI" },
134 { LANG_FRENCH, SUBLANG_NEUTRAL, "fr" },
135 { LANG_FRENCH, SUBLANG_FRENCH, "fr_FR" },
136 { LANG_FRENCH, SUBLANG_FRENCH_BELGIAN, "fr_BE" },
137 { LANG_FRENCH, SUBLANG_FRENCH_CANADIAN, "fr_CA" },
138 { LANG_FRENCH, SUBLANG_FRENCH_SWISS, "fr_CH" },
139 { LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG, "fr_LU" },
140 { LANG_FRENCH, SUBLANG_FRENCH_MONACO, "fr_MC" },
141 { LANG_HEBREW, SUBLANG_NEUTRAL, "he" },
142 { LANG_HEBREW, SUBLANG_HEBREW_ISRAEL, "he_IL" },
143 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, "hu" },
144 { LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY, "hu_HU" },
145 { LANG_ICELANDIC, SUBLANG_NEUTRAL, "is" },
146 { LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND, "is_IS" },
147 { LANG_IRISH, SUBLANG_NEUTRAL, "ga" },
148 { LANG_IRISH, SUBLANG_IRISH_IRELAND, "ga_IE" },
149 { LANG_ITALIAN, SUBLANG_NEUTRAL, "it" },
150 { LANG_ITALIAN, SUBLANG_ITALIAN, "it_IT" },
151 { LANG_ITALIAN, SUBLANG_ITALIAN_SWISS, "it_CH" },
152 { LANG_JAPANESE, SUBLANG_NEUTRAL, "ja" },
153 { LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN, "ja_JP" },
154 { LANG_KOREAN, SUBLANG_NEUTRAL, "ko" },
155 { LANG_KOREAN, SUBLANG_KOREAN, "ko_KR" },
156 { LANG_DUTCH, SUBLANG_NEUTRAL, "nl" },
157 { LANG_DUTCH, SUBLANG_DUTCH, "nl_NL" },
158 { LANG_DUTCH, SUBLANG_DUTCH_BELGIAN, "nl_BE" },
159 { LANG_DUTCH, SUBLANG_DUTCH_SURINAM, "nl_SR" },
160 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL, "nb_NO" },
161 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK, "nn_NO" },
162 { LANG_POLISH, SUBLANG_NEUTRAL, "pl" },
163 { LANG_POLISH, SUBLANG_POLISH_POLAND, "pl_PL" },
164 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, "pt" },
165 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN, "pt_BR" },
166 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_PORTUGAL, "pt_PT" },
167 { LANG_ROMANSH, SUBLANG_NEUTRAL, "rm" },
168 { LANG_ROMANSH, SUBLANG_ROMANSH_SWITZERLAND, "rm_CH" },
169 { LANG_ROMANIAN, SUBLANG_NEUTRAL, "ro" },
170 { LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA, "ro_RO" },
171 { LANG_RUSSIAN, SUBLANG_NEUTRAL, "ru" },
172 { LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA, "ru_RU" },
173 { LANG_SCOTTISH_GAELIC,SUBLANG_NEUTRAL, "gd" },
174 { LANG_SCOTTISH_GAELIC,SUBLANG_SCOTTISH_GAELIC, "gd_GB" },
175 { LANG_SERBIAN, SUBLANG_NEUTRAL, "hr" },
176 { LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA, "hr_HR" },
177 { LANG_SERBIAN, SUBLANG_SERBIAN_LATIN, "sr_RS@latin" },
178 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, "sr_RS@cyrillic" },
179 { LANG_SLOVAK, SUBLANG_NEUTRAL, "sk" },
180 { LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA, "sk_SK" },
181 { LANG_ALBANIAN, SUBLANG_NEUTRAL, "sq" },
182 { LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA, "sq_AL" },
183 { LANG_SWEDISH, SUBLANG_NEUTRAL, "sv" },
184 { LANG_SWEDISH, SUBLANG_SWEDISH_SWEDEN, "sv_SE" },
185 { LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND, "sv_FI" },
186 { LANG_THAI, SUBLANG_NEUTRAL, "th" },
187 { LANG_THAI, SUBLANG_THAI_THAILAND, "th_TH" },
188 { LANG_TURKISH, SUBLANG_NEUTRAL, "tr" },
189 { LANG_TURKISH, SUBLANG_TURKISH_TURKEY, "tr_TR" },
190 { LANG_URDU, SUBLANG_NEUTRAL, "ur" },
191 { LANG_URDU, SUBLANG_URDU_PAKISTAN, "ur_PK" },
192 { LANG_INDONESIAN, SUBLANG_NEUTRAL, "id" },
193 { LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA, "id_ID" },
194 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, "uk" },
195 { LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE, "uk_UA" },
196 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, "be" },
197 { LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS, "be_BY" },
198 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, "sl" },
199 { LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA, "sl_SI" },
200 { LANG_ESTONIAN, SUBLANG_NEUTRAL, "et" },
201 { LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA, "et_EE" },
202 { LANG_LATVIAN, SUBLANG_NEUTRAL, "lv" },
203 { LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA, "lv_LV" },
204 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, "lt" },
205 { LANG_LITHUANIAN, SUBLANG_LITHUANIAN, "lt_LT" },
206 { LANG_PERSIAN, SUBLANG_NEUTRAL, "fa" },
207 { LANG_PERSIAN, SUBLANG_PERSIAN_IRAN, "fa_IR" },
208 { LANG_ARMENIAN, SUBLANG_NEUTRAL, "hy" },
209 { LANG_ARMENIAN, SUBLANG_ARMENIAN_ARMENIA, "hy_AM" },
210 { LANG_AZERI, SUBLANG_NEUTRAL, "az" },
211 { LANG_AZERI, SUBLANG_AZERI_LATIN, "az_AZ@latin" },
212 { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, "az_AZ@cyrillic" },
213 { LANG_BASQUE, SUBLANG_NEUTRAL, "eu" },
214 { LANG_BASQUE, SUBLANG_BASQUE_BASQUE, "eu_ES" },
215 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, "mk" },
216 { LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA, "mk_MK" },
217 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, "af" },
218 { LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA, "af_ZA" },
219 { LANG_GEORGIAN, SUBLANG_NEUTRAL, "ka" },
220 { LANG_GEORGIAN, SUBLANG_GEORGIAN_GEORGIA, "ka_GE" },
221 { LANG_FAEROESE, SUBLANG_NEUTRAL, "fo" },
222 { LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS, "fo_FO" },
223 { LANG_HINDI, SUBLANG_NEUTRAL, "hi" },
224 { LANG_HINDI, SUBLANG_HINDI_INDIA, "hi_IN" },
225 { LANG_MALAY, SUBLANG_NEUTRAL, "ms" },
226 { LANG_MALAY, SUBLANG_MALAY_MALAYSIA, "ms_MY" },
227 { LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM, "ms_BN" },
228 { LANG_KAZAK, SUBLANG_NEUTRAL, "kk" },
229 { LANG_KAZAK, SUBLANG_KAZAK_KAZAKHSTAN, "kk_KZ" },
230 { LANG_KYRGYZ, SUBLANG_NEUTRAL, "ky" },
231 { LANG_KYRGYZ, SUBLANG_KYRGYZ_KYRGYZSTAN, "ky_KG" },
232 { LANG_SWAHILI, SUBLANG_NEUTRAL, "sw" },
233 { LANG_SWAHILI, SUBLANG_SWAHILI_KENYA, "sw_KE" },
234 { LANG_UZBEK, SUBLANG_NEUTRAL, "uz" },
235 { LANG_UZBEK, SUBLANG_UZBEK_LATIN, "uz_UZ@latin" },
236 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, "uz_UZ@cyrillic" },
237 { LANG_TATAR, SUBLANG_NEUTRAL, "tt" },
238 { LANG_TATAR, SUBLANG_TATAR_RUSSIA, "tt_TA" },
239 { LANG_PUNJABI, SUBLANG_NEUTRAL, "pa" },
240 { LANG_PUNJABI, SUBLANG_PUNJABI_INDIA, "pa_IN" },
241 { LANG_GUJARATI, SUBLANG_NEUTRAL, "gu" },
242 { LANG_GUJARATI, SUBLANG_GUJARATI_INDIA, "gu_IN" },
243 { LANG_ORIYA, SUBLANG_NEUTRAL, "or" },
244 { LANG_ORIYA, SUBLANG_ORIYA_INDIA, "or_IN" },
245 { LANG_TAMIL, SUBLANG_NEUTRAL, "ta" },
246 { LANG_TAMIL, SUBLANG_TAMIL_INDIA, "ta_IN" },
247 { LANG_TELUGU, SUBLANG_NEUTRAL, "te" },
248 { LANG_TELUGU, SUBLANG_TELUGU_INDIA, "te_IN" },
249 { LANG_KANNADA, SUBLANG_NEUTRAL, "kn" },
250 { LANG_KANNADA, SUBLANG_KANNADA_INDIA, "kn_IN" },
251 { LANG_MALAYALAM, SUBLANG_NEUTRAL, "ml" },
252 { LANG_MALAYALAM, SUBLANG_MALAYALAM_INDIA, "ml_IN" },
253 { LANG_MARATHI, SUBLANG_NEUTRAL, "mr" },
254 { LANG_MARATHI, SUBLANG_MARATHI_INDIA, "mr_IN" },
255 { LANG_SANSKRIT, SUBLANG_NEUTRAL, "sa" },
256 { LANG_SANSKRIT, SUBLANG_SANSKRIT_INDIA, "sa_IN" },
257 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, "mn" },
258 { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
259 { LANG_WELSH, SUBLANG_NEUTRAL, "cy" },
260 { LANG_WELSH, SUBLANG_WELSH_UNITED_KINGDOM, "cy_GB" },
261 { LANG_GALICIAN, SUBLANG_NEUTRAL, "gl" },
262 { LANG_GALICIAN, SUBLANG_GALICIAN_GALICIAN, "gl_ES" },
263 { LANG_KONKANI, SUBLANG_NEUTRAL, "kok" },
264 { LANG_KONKANI, SUBLANG_KONKANI_INDIA, "kok_IN" },
265 { LANG_DIVEHI, SUBLANG_NEUTRAL, "dv" },
266 { LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES, "dv_MV" },
267 { LANG_BRETON, SUBLANG_NEUTRAL, "br" },
268 { LANG_BRETON, SUBLANG_BRETON_FRANCE, "br_FR" },
270 #ifdef LANG_ESPERANTO
271 { LANG_ESPERANTO, SUBLANG_DEFAULT, "eo" },
272 #endif
273 #ifdef LANG_WALON
274 { LANG_WALON, SUBLANG_NEUTRAL, "wa" },
275 { LANG_WALON, SUBLANG_DEFAULT, "wa_BE" },
276 #endif
277 #ifdef LANG_CORNISH
278 { LANG_CORNISH, SUBLANG_NEUTRAL, "kw" },
279 { LANG_CORNISH, SUBLANG_DEFAULT, "kw_GB" },
280 #endif
281 #ifdef LANG_MANX_GAELIC
282 { LANG_MANX_GAELIC, SUBLANG_MANX_GAELIC, "gv_GB" },
283 #endif
286 static BOOL is_english( int lan )
288 return lan == MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
291 static char *convert_msgid_ascii( const lanmsg_t *msg, int error_on_invalid_char )
293 int i;
294 char *buffer = xmalloc( msg->len * 4 + 1 );
296 for (i = 0; i < msg->len; i++)
298 buffer[i] = msg->msg[i];
299 if (!msg->msg[i]) break;
300 if (msg->msg[i] >= 32 && msg->msg[i] <= 127) continue;
301 if (msg->msg[i] == '\t' || msg->msg[i] == '\n') continue;
302 if (error_on_invalid_char)
304 fprintf( stderr, "%s:%d: ", msg->file, msg->line );
305 error( "Invalid character %04x in source string\n", msg->msg[i] );
307 free( buffer );
308 return NULL;
310 buffer[i] = 0;
311 return buffer;
314 static char *get_message_context( char **msgid )
316 static const char magic[] = "#msgctxt#";
317 char *id, *context;
319 if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
320 context = *msgid + sizeof(magic) - 1;
321 if (!(id = strchr( context, '#' ))) return NULL;
322 *id = 0;
323 *msgid = id + 1;
324 return context;
327 #ifdef HAVE_LIBGETTEXTPO
329 static char *convert_string_utf8( const lanmsg_t *msg )
331 char *buffer = xmalloc( msg->len * 4 + 1 );
332 int len = wine_utf8_wcstombs( 0, msg->msg, msg->len, buffer, msg->len * 4 );
333 buffer[len] = 0;
334 return buffer;
337 static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
338 po_message_iterator_t *iterator )
340 po_message_t msg;
341 const char *context;
343 *iterator = po_message_iterator( po, NULL );
344 while ((msg = po_next_message( *iterator )))
346 if (strcmp( po_message_msgid( msg ), msgid )) continue;
347 if (!msgctxt) break;
348 if (!(context = po_message_msgctxt( msg ))) continue;
349 if (!strcmp( context, msgctxt )) break;
351 return msg;
354 static void po_xerror( int severity, po_message_t message,
355 const char *filename, size_t lineno, size_t column,
356 int multiline_p, const char *message_text )
358 fprintf( stderr, "%s:%u:%u: %s\n",
359 filename, (unsigned int)lineno, (unsigned int)column, message_text );
360 if (severity) exit(1);
363 static void po_xerror2( int severity, po_message_t message1,
364 const char *filename1, size_t lineno1, size_t column1,
365 int multiline_p1, const char *message_text1,
366 po_message_t message2,
367 const char *filename2, size_t lineno2, size_t column2,
368 int multiline_p2, const char *message_text2 )
370 fprintf( stderr, "%s:%u:%u: %s\n",
371 filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
372 fprintf( stderr, "%s:%u:%u: %s\n",
373 filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
374 if (severity) exit(1);
377 static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
379 static void add_po_string( po_file_t po, const lanmsg_t *msgid, const lanmsg_t *msgstr )
381 po_message_t msg;
382 po_message_iterator_t iterator;
383 char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
385 if (msgid->len <= 1) return;
387 id_buffer = id = convert_msgid_ascii( msgid, 1 );
388 context = get_message_context( &id );
390 if (msgstr)
392 str_buffer = str = convert_string_utf8( msgstr );
393 if (is_english( msgstr->lan )) get_message_context( &str );
395 if (!(msg = find_message( po, id, context, &iterator )))
397 msg = po_message_create();
398 po_message_set_msgid( msg, id );
399 po_message_set_msgstr( msg, str ? str : "" );
400 if (context) po_message_set_msgctxt( msg, context );
401 po_message_insert( iterator, msg );
403 if (msgid->file) po_message_add_filepos( msg, msgid->file, msgid->line );
404 po_message_iterator_free( iterator );
405 free( id_buffer );
406 free( str_buffer );
409 static po_file_t create_po_file(void)
411 po_file_t po;
412 po_message_t msg;
413 po_message_iterator_t iterator;
415 po = po_file_create();
416 iterator = po_message_iterator( po, NULL );
417 msg = po_message_create();
418 po_message_set_msgid( msg, "" );
419 po_message_set_msgstr( msg,
420 "Project-Id-Version: Wine\n"
421 "Report-Msgid-Bugs-To: https://bugs.winehq.org\n"
422 "POT-Creation-Date: N/A\n"
423 "PO-Revision-Date: N/A\n"
424 "Last-Translator: Automatically generated\n"
425 "Language-Team: none\n"
426 "MIME-Version: 1.0\n"
427 "Content-Type: text/plain; charset=UTF-8\n"
428 "Content-Transfer-Encoding: 8bit\n" );
429 po_message_insert( iterator, msg );
430 po_message_iterator_free( iterator );
431 return po;
434 void write_pot_file( const char *outname )
436 int i, j;
437 lan_blk_t *lbp;
438 po_file_t po = create_po_file();
440 for (lbp = lanblockhead; lbp; lbp = lbp->next)
442 if (!is_english( lbp->lan )) continue;
443 for (i = 0; i < lbp->nblk; i++)
445 block_t *blk = &lbp->blks[i];
446 for (j = 0; j < blk->nmsg; j++) add_po_string( po, blk->msgs[j], NULL );
449 po_file_write( po, outname, &po_xerror_handler );
450 po_file_free( po );
454 #else /* HAVE_LIBGETTEXTPO */
456 void write_pot_file( const char *outname )
458 error( "PO files not supported in this wmc build\n" );
461 #endif
463 static struct mo_file *mo_file;
465 static void byteswap( unsigned int *data, unsigned int count )
467 unsigned int i;
469 for (i = 0; i < count; i++)
470 data[i] = data[i] >> 24 | (data[i] >> 8 & 0xff00) | (data[i] << 8 & 0xff0000) | data[i] << 24;
473 static void load_mo_file( const char *name )
475 struct stat st;
476 int res, fd;
478 fd = open( name, O_RDONLY | O_BINARY );
479 if (fd == -1) fatal_perror( "Failed to open %s", name );
480 fstat( fd, &st );
481 mo_file = xmalloc( st.st_size );
482 res = read( fd, mo_file, st.st_size );
483 if (res == -1) fatal_perror( "Failed to read %s", name );
484 else if (res != st.st_size) error( "Failed to read %s\n", name );
485 close( fd );
487 /* sanity checks */
489 if (st.st_size < sizeof(*mo_file))
490 error( "%s is not a valid .mo file\n", name );
491 if (mo_file->magic == 0xde120495)
492 byteswap( &mo_file->revision, 4 );
493 else if (mo_file->magic != 0x950412de)
494 error( "%s is not a valid .mo file\n", name );
495 if ((mo_file->revision >> 16) > 1)
496 error( "%s: unsupported file version %x\n", name, mo_file->revision );
497 if (mo_file->msgid_off >= st.st_size ||
498 mo_file->msgstr_off >= st.st_size ||
499 st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count)
500 error( "%s: corrupted file\n", name );
502 if (mo_file->magic == 0xde120495)
504 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgid_off), 2 * mo_file->count );
505 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgstr_off), 2 * mo_file->count );
509 static void free_mo_file(void)
511 free( mo_file );
512 mo_file = NULL;
515 static inline const char *get_mo_msgid( int index )
517 const char *base = (const char *)mo_file;
518 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgid_off);
519 return base + offsets[2 * index + 1];
522 static inline const char *get_mo_msgstr( int index )
524 const char *base = (const char *)mo_file;
525 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgstr_off);
526 return base + offsets[2 * index + 1];
529 static const char *get_msgstr( const char *msgid, const char *context, int *found )
531 int pos, res, min, max;
532 const char *ret = msgid;
533 char *id = NULL;
535 if (!mo_file) /* strings containing a context still need to be transformed */
537 if (context) (*found)++;
538 return ret;
541 if (context) id = strmake( "%s%c%s", context, 4, msgid );
542 min = 0;
543 max = mo_file->count - 1;
544 while (min <= max)
546 pos = (min + max) / 2;
547 res = strcmp( get_mo_msgid(pos), id ? id : msgid );
548 if (!res)
550 const char *str = get_mo_msgstr( pos );
551 if (str[0]) /* ignore empty strings */
553 ret = str;
554 (*found)++;
556 break;
558 if (res > 0) max = pos - 1;
559 else min = pos + 1;
561 free( id );
562 return ret;
565 static lanmsg_t *translate_string( lanmsg_t *str, int lang, int *found )
567 lanmsg_t *new;
568 const char *transl;
569 int res;
570 char *buffer, *msgid, *context;
572 if (str->len <= 1 || !(buffer = convert_msgid_ascii( str, 0 ))) return str;
574 msgid = buffer;
575 context = get_message_context( &msgid );
576 transl = get_msgstr( msgid, context, found );
578 new = xmalloc( sizeof(*new) );
579 new->lan = lang;
580 new->cp = 0; /* FIXME */
581 new->file = str->file;
582 new->line = str->line;
583 new->len = wine_utf8_mbstowcs( 0, transl, strlen(transl) + 1, NULL, 0 );
584 new->msg = xmalloc( new->len * sizeof(WCHAR) );
585 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, transl, strlen(transl) + 1, new->msg, new->len );
586 if (res == -2)
587 error( "Invalid utf-8 character in string '%s'\n", transl );
588 free( buffer );
589 return new;
592 static void translate_block( block_t *blk, block_t *new, int lang, int *found )
594 int i;
596 new->idlo = blk->idlo;
597 new->idhi = blk->idhi;
598 new->size = 0;
599 new->msgs = xmalloc( blk->nmsg * sizeof(*new->msgs) );
600 new->nmsg = blk->nmsg;
601 for (i = 0; i < blk->nmsg; i++)
603 new->msgs[i] = translate_string( blk->msgs[i], lang, found );
604 new->size += ((2 * new->msgs[i]->len + 3) & ~3) + 4;
608 static void translate_messages( int lang )
610 int i, found;
611 lan_blk_t *lbp, *new;
613 for (lbp = lanblockhead; lbp; lbp = lbp->next)
615 if (!is_english( lbp->lan )) continue;
616 found = 0;
617 new = xmalloc( sizeof(*new) );
618 /* English "translations" take precedence over the original contents */
619 new->version = is_english( lang ) ? 1 : -1;
620 new->lan = lang;
621 new->blks = xmalloc( lbp->nblk * sizeof(*new->blks) );
622 new->nblk = lbp->nblk;
624 for (i = 0; i < lbp->nblk; i++)
625 translate_block( &lbp->blks[i], &new->blks[i], lang, &found );
626 if (found)
628 if (new_tail) new_tail->next = new;
629 else new_top = new;
630 new->prev = new_tail;
631 new_tail = new;
633 else
635 free( new->blks );
636 free( new );
641 void add_translations( const char *po_dir )
643 lan_blk_t *lbp;
644 char buffer[256];
645 char *p, *tok, *name;
646 unsigned int i;
647 FILE *f;
649 /* first check if we have English resources to translate */
650 for (lbp = lanblockhead; lbp; lbp = lbp->next) if (is_english( lbp->lan )) break;
651 if (!lbp) return;
653 if (!po_dir) /* run through the translation process to remove msg contexts */
655 translate_messages( MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ));
656 goto done;
659 new_top = new_tail = NULL;
661 name = strmake( "%s/LINGUAS", po_dir );
662 if (!(f = fopen( name, "r" ))) return;
663 free( name );
664 while (fgets( buffer, sizeof(buffer), f ))
666 if ((p = strchr( buffer, '#' ))) *p = 0;
667 for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
669 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
670 if (!strcmp( tok, languages[i].name )) break;
672 if (i == sizeof(languages)/sizeof(languages[0]))
673 error( "unknown language '%s'\n", tok );
675 name = strmake( "%s/%s.mo", po_dir, tok );
676 load_mo_file( name );
677 translate_messages( MAKELANGID(languages[i].id, languages[i].sub) );
678 free_mo_file();
679 free( name );
682 fclose( f );
684 done:
685 /* prepend the translated messages to the global list */
686 if (new_tail)
688 new_tail->next = lanblockhead;
689 lanblockhead->prev = new_tail;
690 lanblockhead = new_top;