kernel32: Use binary search for key lookup in terminfo generated data.
[wine/multimedia.git] / tools / wmc / po.c
blob414e4a6604917a2df88fa2e21e6417301f2e7dae
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"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #ifdef HAVE_GETTEXT_PO_H
30 #include <gettext-po.h>
31 #endif
33 #include "wmc.h"
34 #include "utils.h"
35 #include "lang.h"
36 #include "write.h"
37 #include "windef.h"
39 #ifdef HAVE_LIBGETTEXTPO
41 static const struct
43 unsigned int id, sub;
44 const char *name;
45 } languages[] =
47 { LANG_ARABIC, SUBLANG_NEUTRAL, "ar" },
48 { LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA, "ar_SA" },
49 { LANG_ARABIC, SUBLANG_ARABIC_IRAQ, "ar_IQ" },
50 { LANG_ARABIC, SUBLANG_ARABIC_EGYPT, "ar_EG" },
51 { LANG_ARABIC, SUBLANG_ARABIC_LIBYA, "ar_LY" },
52 { LANG_ARABIC, SUBLANG_ARABIC_ALGERIA, "ar_DZ" },
53 { LANG_ARABIC, SUBLANG_ARABIC_MOROCCO, "ar_MA" },
54 { LANG_ARABIC, SUBLANG_ARABIC_TUNISIA, "ar_TN" },
55 { LANG_ARABIC, SUBLANG_ARABIC_OMAN, "ar_OM" },
56 { LANG_ARABIC, SUBLANG_ARABIC_YEMEN, "ar_YE" },
57 { LANG_ARABIC, SUBLANG_ARABIC_SYRIA, "ar_SY" },
58 { LANG_ARABIC, SUBLANG_ARABIC_JORDAN, "ar_JO" },
59 { LANG_ARABIC, SUBLANG_ARABIC_LEBANON, "ar_LB" },
60 { LANG_ARABIC, SUBLANG_ARABIC_KUWAIT, "ar_KW" },
61 { LANG_ARABIC, SUBLANG_ARABIC_UAE, "ar_AE" },
62 { LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN, "ar_BH" },
63 { LANG_ARABIC, SUBLANG_ARABIC_QATAR, "ar_QA" },
64 { LANG_BULGARIAN, SUBLANG_NEUTRAL, "bg" },
65 { LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA, "bg_BG" },
66 { LANG_CATALAN, SUBLANG_NEUTRAL, "ca" },
67 { LANG_CATALAN, SUBLANG_CATALAN_CATALAN, "ca_ES" },
68 { LANG_CHINESE, SUBLANG_NEUTRAL, "zh" },
69 { LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL, "zh_TW" },
70 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, "zh_CN" },
71 { LANG_CHINESE, SUBLANG_CHINESE_HONGKONG, "zh_HK" },
72 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, "zh_SG" },
73 { LANG_CHINESE, SUBLANG_CHINESE_MACAU, "zh_MO" },
74 { LANG_CZECH, SUBLANG_NEUTRAL, "cs" },
75 { LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC, "cs_CZ" },
76 { LANG_DANISH, SUBLANG_NEUTRAL, "da" },
77 { LANG_DANISH, SUBLANG_DANISH_DENMARK, "da_DK" },
78 { LANG_GERMAN, SUBLANG_NEUTRAL, "de" },
79 { LANG_GERMAN, SUBLANG_GERMAN, "de_DE" },
80 { LANG_GERMAN, SUBLANG_GERMAN_SWISS, "de_CH" },
81 { LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN, "de_AT" },
82 { LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG, "de_LU" },
83 { LANG_GERMAN, SUBLANG_GERMAN_LIECHTENSTEIN, "de_LI" },
84 { LANG_GREEK, SUBLANG_NEUTRAL, "el" },
85 { LANG_GREEK, SUBLANG_GREEK_GREECE, "el_GR" },
86 { LANG_ENGLISH, SUBLANG_NEUTRAL, "en" },
87 { LANG_ENGLISH, SUBLANG_ENGLISH_US, "en_US" },
88 { LANG_ENGLISH, SUBLANG_ENGLISH_UK, "en_GB" },
89 { LANG_ENGLISH, SUBLANG_ENGLISH_AUS, "en_AU" },
90 { LANG_ENGLISH, SUBLANG_ENGLISH_CAN, "en_CA" },
91 { LANG_ENGLISH, SUBLANG_ENGLISH_NZ, "en_NZ" },
92 { LANG_ENGLISH, SUBLANG_ENGLISH_EIRE, "en_IE" },
93 { LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA, "en_ZA" },
94 { LANG_ENGLISH, SUBLANG_ENGLISH_JAMAICA, "en_JM" },
95 { LANG_ENGLISH, SUBLANG_ENGLISH_CARIBBEAN, "en_CB" },
96 { LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE, "en_BZ" },
97 { LANG_ENGLISH, SUBLANG_ENGLISH_TRINIDAD, "en_TT" },
98 { LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE, "en_ZW" },
99 { LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES, "en_PH" },
100 { LANG_SPANISH, SUBLANG_NEUTRAL, "es" },
101 { LANG_SPANISH, SUBLANG_SPANISH, "es_ES" },
102 { LANG_SPANISH, SUBLANG_SPANISH_MEXICAN, "es_MX" },
103 { LANG_SPANISH, SUBLANG_SPANISH_MODERN, "es_ES_modern" },
104 { LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA, "es_GT" },
105 { LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA, "es_CR" },
106 { LANG_SPANISH, SUBLANG_SPANISH_PANAMA, "es_PA" },
107 { LANG_SPANISH, SUBLANG_SPANISH_DOMINICAN_REPUBLIC, "es_DO" },
108 { LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA, "es_VE" },
109 { LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA, "es_CO" },
110 { LANG_SPANISH, SUBLANG_SPANISH_PERU, "es_PE" },
111 { LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA, "es_AR" },
112 { LANG_SPANISH, SUBLANG_SPANISH_ECUADOR, "es_EC" },
113 { LANG_SPANISH, SUBLANG_SPANISH_CHILE, "es_CL" },
114 { LANG_SPANISH, SUBLANG_SPANISH_URUGUAY, "es_UY" },
115 { LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY, "es_PY" },
116 { LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA, "es_BO" },
117 { LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR, "es_SV" },
118 { LANG_SPANISH, SUBLANG_SPANISH_HONDURAS, "es_HN" },
119 { LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA, "es_NI" },
120 { LANG_SPANISH, SUBLANG_SPANISH_PUERTO_RICO, "es_PR" },
121 { LANG_FINNISH, SUBLANG_NEUTRAL, "fi" },
122 { LANG_FINNISH, SUBLANG_FINNISH_FINLAND, "fi_FI" },
123 { LANG_FRENCH, SUBLANG_NEUTRAL, "fr" },
124 { LANG_FRENCH, SUBLANG_FRENCH, "fr_FR" },
125 { LANG_FRENCH, SUBLANG_FRENCH_BELGIAN, "fr_BE" },
126 { LANG_FRENCH, SUBLANG_FRENCH_CANADIAN, "fr_CA" },
127 { LANG_FRENCH, SUBLANG_FRENCH_SWISS, "fr_CH" },
128 { LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG, "fr_LU" },
129 { LANG_FRENCH, SUBLANG_FRENCH_MONACO, "fr_MC" },
130 { LANG_HEBREW, SUBLANG_NEUTRAL, "he" },
131 { LANG_HEBREW, SUBLANG_HEBREW_ISRAEL, "he_IL" },
132 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, "hu" },
133 { LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY, "hu_HU" },
134 { LANG_ICELANDIC, SUBLANG_NEUTRAL, "is" },
135 { LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND, "is_IS" },
136 { LANG_ITALIAN, SUBLANG_NEUTRAL, "it" },
137 { LANG_ITALIAN, SUBLANG_ITALIAN, "it_IT" },
138 { LANG_ITALIAN, SUBLANG_ITALIAN_SWISS, "it_CH" },
139 { LANG_JAPANESE, SUBLANG_NEUTRAL, "ja" },
140 { LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN, "ja_JP" },
141 { LANG_KOREAN, SUBLANG_NEUTRAL, "ko" },
142 { LANG_KOREAN, SUBLANG_KOREAN, "ko_KR" },
143 { LANG_DUTCH, SUBLANG_NEUTRAL, "nl" },
144 { LANG_DUTCH, SUBLANG_DUTCH, "nl_NL" },
145 { LANG_DUTCH, SUBLANG_DUTCH_BELGIAN, "nl_BE" },
146 { LANG_DUTCH, SUBLANG_DUTCH_SURINAM, "nl_SR" },
147 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL, "nb_NO" },
148 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK, "nn_NO" },
149 { LANG_POLISH, SUBLANG_NEUTRAL, "pl" },
150 { LANG_POLISH, SUBLANG_POLISH_POLAND, "pl_PL" },
151 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, "pt" },
152 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN, "pt_BR" },
153 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_PORTUGAL, "pt_PT" },
154 { LANG_ROMANSH, SUBLANG_NEUTRAL, "rm" },
155 { LANG_ROMANSH, SUBLANG_ROMANSH_SWITZERLAND, "rm_CH" },
156 { LANG_ROMANIAN, SUBLANG_NEUTRAL, "ro" },
157 { LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA, "ro_RO" },
158 { LANG_RUSSIAN, SUBLANG_NEUTRAL, "ru" },
159 { LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA, "ru_RU" },
160 { LANG_SERBIAN, SUBLANG_NEUTRAL, "hr" },
161 { LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA, "hr_HR" },
162 { LANG_SERBIAN, SUBLANG_SERBIAN_LATIN, "sr_RS@latin" },
163 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, "sr_RS@cyrillic" },
164 { LANG_SLOVAK, SUBLANG_NEUTRAL, "sk" },
165 { LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA, "sk_SK" },
166 { LANG_ALBANIAN, SUBLANG_NEUTRAL, "sq" },
167 { LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA, "sq_AL" },
168 { LANG_SWEDISH, SUBLANG_NEUTRAL, "sv" },
169 { LANG_SWEDISH, SUBLANG_SWEDISH_SWEDEN, "sv_SE" },
170 { LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND, "sv_FI" },
171 { LANG_THAI, SUBLANG_NEUTRAL, "th" },
172 { LANG_THAI, SUBLANG_THAI_THAILAND, "th_TH" },
173 { LANG_TURKISH, SUBLANG_NEUTRAL, "tr" },
174 { LANG_TURKISH, SUBLANG_TURKISH_TURKEY, "tr_TR" },
175 { LANG_URDU, SUBLANG_NEUTRAL, "ur" },
176 { LANG_URDU, SUBLANG_URDU_PAKISTAN, "ur_PK" },
177 { LANG_INDONESIAN, SUBLANG_NEUTRAL, "id" },
178 { LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA, "id_ID" },
179 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, "uk" },
180 { LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE, "uk_UA" },
181 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, "be" },
182 { LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS, "be_BY" },
183 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, "sl" },
184 { LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA, "sl_SI" },
185 { LANG_ESTONIAN, SUBLANG_NEUTRAL, "et" },
186 { LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA, "et_EE" },
187 { LANG_LATVIAN, SUBLANG_NEUTRAL, "lv" },
188 { LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA, "lv_LV" },
189 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, "lt" },
190 { LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA, "lt_LT" },
191 { LANG_PERSIAN, SUBLANG_NEUTRAL, "fa" },
192 { LANG_PERSIAN, SUBLANG_PERSIAN_IRAN, "fa_IR" },
193 { LANG_ARMENIAN, SUBLANG_NEUTRAL, "hy" },
194 { LANG_ARMENIAN, SUBLANG_ARMENIAN_ARMENIA, "hy_AM" },
195 { LANG_AZERI, SUBLANG_NEUTRAL, "az" },
196 { LANG_AZERI, SUBLANG_AZERI_LATIN, "az_AZ@latin" },
197 { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, "az_AZ@cyrillic" },
198 { LANG_BASQUE, SUBLANG_NEUTRAL, "eu" },
199 { LANG_BASQUE, SUBLANG_BASQUE_BASQUE, "eu_ES" },
200 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, "mk" },
201 { LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA, "mk_MK" },
202 { LANG_AFRIKAANS, SUBLANG_NEUTRAL, "af" },
203 { LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA, "af_ZA" },
204 { LANG_GEORGIAN, SUBLANG_NEUTRAL, "ka" },
205 { LANG_GEORGIAN, SUBLANG_GEORGIAN_GEORGIA, "ka_GE" },
206 { LANG_FAEROESE, SUBLANG_NEUTRAL, "fo" },
207 { LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS, "fo_FO" },
208 { LANG_HINDI, SUBLANG_NEUTRAL, "hi" },
209 { LANG_HINDI, SUBLANG_HINDI_INDIA, "hi_IN" },
210 { LANG_MALAY, SUBLANG_NEUTRAL, "ms" },
211 { LANG_MALAY, SUBLANG_MALAY_MALAYSIA, "ms_MY" },
212 { LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM, "ms_BN" },
213 { LANG_KAZAK, SUBLANG_NEUTRAL, "kk" },
214 { LANG_KAZAK, SUBLANG_KAZAK_KAZAKHSTAN, "kk_KZ" },
215 { LANG_KYRGYZ, SUBLANG_NEUTRAL, "ky" },
216 { LANG_KYRGYZ, SUBLANG_KYRGYZ_KYRGYZSTAN, "ky_KG" },
217 { LANG_SWAHILI, SUBLANG_NEUTRAL, "sw" },
218 { LANG_SWAHILI, SUBLANG_SWAHILI_KENYA, "sw_KE" },
219 { LANG_UZBEK, SUBLANG_NEUTRAL, "uz" },
220 { LANG_UZBEK, SUBLANG_UZBEK_LATIN, "uz_UZ@latin" },
221 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, "uz_UZ@cyrillic" },
222 { LANG_TATAR, SUBLANG_NEUTRAL, "tt" },
223 { LANG_TATAR, SUBLANG_TATAR_RUSSIA, "tt_TA" },
224 { LANG_PUNJABI, SUBLANG_NEUTRAL, "pa" },
225 { LANG_PUNJABI, SUBLANG_PUNJABI_INDIA, "pa_IN" },
226 { LANG_GUJARATI, SUBLANG_NEUTRAL, "gu" },
227 { LANG_GUJARATI, SUBLANG_GUJARATI_INDIA, "gu_IN" },
228 { LANG_ORIYA, SUBLANG_NEUTRAL, "or" },
229 { LANG_ORIYA, SUBLANG_ORIYA_INDIA, "or_IN" },
230 { LANG_TAMIL, SUBLANG_NEUTRAL, "ta" },
231 { LANG_TAMIL, SUBLANG_TAMIL_INDIA, "ta_IN" },
232 { LANG_TELUGU, SUBLANG_NEUTRAL, "te" },
233 { LANG_TELUGU, SUBLANG_TELUGU_INDIA, "te_IN" },
234 { LANG_KANNADA, SUBLANG_NEUTRAL, "kn" },
235 { LANG_KANNADA, SUBLANG_KANNADA_INDIA, "kn_IN" },
236 { LANG_MALAYALAM, SUBLANG_NEUTRAL, "ml" },
237 { LANG_MALAYALAM, SUBLANG_MALAYALAM_INDIA, "ml_IN" },
238 { LANG_MARATHI, SUBLANG_NEUTRAL, "mr" },
239 { LANG_MARATHI, SUBLANG_MARATHI_INDIA, "mr_IN" },
240 { LANG_SANSKRIT, SUBLANG_NEUTRAL, "sa" },
241 { LANG_SANSKRIT, SUBLANG_SANSKRIT_INDIA, "sa_IN" },
242 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, "mn" },
243 { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
244 { LANG_WELSH, SUBLANG_NEUTRAL, "cy" },
245 { LANG_WELSH, SUBLANG_WELSH_UNITED_KINGDOM, "cy_GB" },
246 { LANG_GALICIAN, SUBLANG_NEUTRAL, "gl" },
247 { LANG_GALICIAN, SUBLANG_GALICIAN_GALICIAN, "gl_ES" },
248 { LANG_KONKANI, SUBLANG_NEUTRAL, "kok" },
249 { LANG_KONKANI, SUBLANG_KONKANI_INDIA, "kok_IN" },
250 { LANG_DIVEHI, SUBLANG_NEUTRAL, "dv" },
251 { LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES, "dv_MV" },
252 { LANG_BRETON, SUBLANG_NEUTRAL, "br" },
253 { LANG_BRETON, SUBLANG_BRETON_FRANCE, "br_FR" },
255 #ifdef LANG_ESPERANTO
256 { LANG_ESPERANTO, SUBLANG_DEFAULT, "eo" },
257 #endif
258 #ifdef LANG_WALON
259 { LANG_WALON, SUBLANG_NEUTRAL, "wa" },
260 { LANG_WALON, SUBLANG_DEFAULT, "wa_BE" },
261 #endif
262 #ifdef LANG_CORNISH
263 { LANG_CORNISH, SUBLANG_NEUTRAL, "kw" },
264 { LANG_CORNISH, SUBLANG_DEFAULT, "kw_GB" },
265 #endif
266 #ifdef LANG_GAELIC
267 { LANG_GAELIC, SUBLANG_NEUTRAL, "ga" },
268 { LANG_GAELIC, SUBLANG_GAELIC, "ga_IE" },
269 { LANG_GAELIC, SUBLANG_GAELIC_SCOTTISH, "gd_GB" },
270 { LANG_GAELIC, SUBLANG_GAELIC_MANX, "gv_GB" },
271 #endif
274 static void po_xerror( int severity, po_message_t message,
275 const char *filename, size_t lineno, size_t column,
276 int multiline_p, const char *message_text )
278 fprintf( stderr, "%s:%u:%u: %s\n",
279 filename, (unsigned int)lineno, (unsigned int)column, message_text );
280 if (severity) exit(1);
283 static void po_xerror2( int severity, po_message_t message1,
284 const char *filename1, size_t lineno1, size_t column1,
285 int multiline_p1, const char *message_text1,
286 po_message_t message2,
287 const char *filename2, size_t lineno2, size_t column2,
288 int multiline_p2, const char *message_text2 )
290 fprintf( stderr, "%s:%u:%u: %s\n",
291 filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
292 fprintf( stderr, "%s:%u:%u: %s\n",
293 filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
294 if (severity) exit(1);
297 static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
299 static int is_english( int lan )
301 return lan == MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
304 static char *convert_string_utf8( const lanmsg_t *msg )
306 char *buffer = xmalloc( msg->len * 4 + 1 );
307 int len = wine_utf8_wcstombs( 0, msg->msg, msg->len, buffer, msg->len * 4 );
308 buffer[len] = 0;
309 return buffer;
312 static char *convert_msgid_ascii( const lanmsg_t *msg, int error_on_invalid_char )
314 int i;
315 char *buffer = xmalloc( msg->len * 4 + 1 );
317 for (i = 0; i < msg->len; i++)
319 buffer[i] = msg->msg[i];
320 if (!msg->msg[i]) break;
321 if (msg->msg[i] >= 32 && msg->msg[i] <= 127) continue;
322 if (msg->msg[i] == '\t' || msg->msg[i] == '\n') continue;
323 if (error_on_invalid_char)
325 fprintf( stderr, "%s:%d: ", msg->file, msg->line );
326 error( "Invalid character %04x in source string\n", msg->msg[i] );
328 return NULL;
330 buffer[i] = 0;
331 return buffer;
334 static char *get_message_context( char **msgid )
336 static const char magic[] = "#msgctxt#";
337 char *id, *context;
339 if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
340 context = *msgid + sizeof(magic) - 1;
341 if (!(id = strchr( context, '#' ))) return NULL;
342 *id = 0;
343 *msgid = id + 1;
344 return context;
347 static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
348 po_message_iterator_t *iterator )
350 po_message_t msg;
351 const char *context;
353 *iterator = po_message_iterator( po, NULL );
354 while ((msg = po_next_message( *iterator )))
356 if (strcmp( po_message_msgid( msg ), msgid )) continue;
357 if (!msgctxt) break;
358 if (!(context = po_message_msgctxt( msg ))) break;
359 if (!strcmp( context, msgctxt )) break;
361 return msg;
364 static void add_po_string( po_file_t po, const lanmsg_t *msgid, const lanmsg_t *msgstr )
366 po_message_t msg;
367 po_message_iterator_t iterator;
368 char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
370 if (msgid->len <= 1) return;
372 id_buffer = id = convert_msgid_ascii( msgid, 1 );
373 context = get_message_context( &id );
375 if (msgstr)
377 str_buffer = str = convert_string_utf8( msgstr );
378 if (is_english( msgstr->lan )) get_message_context( &str );
380 if (!(msg = find_message( po, id, context, &iterator )))
382 msg = po_message_create();
383 po_message_set_msgid( msg, id );
384 po_message_set_msgstr( msg, str ? str : "" );
385 if (context) po_message_set_msgctxt( msg, context );
386 po_message_insert( iterator, msg );
388 if (msgid->file) po_message_add_filepos( msg, msgid->file, msgid->line );
389 po_message_iterator_free( iterator );
390 free( id_buffer );
391 free( str_buffer );
394 static po_file_t create_po_file(void)
396 po_file_t po;
397 po_message_t msg;
398 po_message_iterator_t iterator;
400 po = po_file_create();
401 iterator = po_message_iterator( po, NULL );
402 msg = po_message_create();
403 po_message_set_msgid( msg, "" );
404 po_message_set_msgstr( msg,
405 "Project-Id-Version: Wine\n"
406 "Report-Msgid-Bugs-To: http://bugs.winehq.org\n"
407 "POT-Creation-Date: N/A\n"
408 "PO-Revision-Date: N/A\n"
409 "Last-Translator: Automatically generated\n"
410 "Language-Team: none\n"
411 "MIME-Version: 1.0\n"
412 "Content-Type: text/plain; charset=UTF-8\n"
413 "Content-Transfer-Encoding: 8bit\n" );
414 po_message_insert( iterator, msg );
415 po_message_iterator_free( iterator );
416 return po;
419 void write_pot_file( const char *outname )
421 int i, j;
422 lan_blk_t *lbp;
423 po_file_t po = create_po_file();
425 for (lbp = lanblockhead; lbp; lbp = lbp->next)
427 if (!is_english( lbp->lan )) continue;
428 for (i = 0; i < lbp->nblk; i++)
430 block_t *blk = &lbp->blks[i];
431 for (j = 0; j < blk->nmsg; j++) add_po_string( po, blk->msgs[j], NULL );
434 po_file_write( po, outname, &po_xerror_handler );
435 po_file_free( po );
438 static lan_blk_t *new_top, *new_tail;
440 static lanmsg_t *translate_string( po_file_t po, lanmsg_t *str, int lang, int *found )
442 po_message_t msg;
443 po_message_iterator_t iterator;
444 lanmsg_t *new;
445 const char *transl;
446 int res;
447 char *buffer, *msgid, *context;
449 if (str->len <= 1 || !(buffer = convert_msgid_ascii( str, 0 ))) return str;
451 msgid = buffer;
452 context = get_message_context( &msgid );
453 msg = find_message( po, msgid, context, &iterator );
454 po_message_iterator_free( iterator );
456 if (msg && !po_message_is_fuzzy( msg ))
458 transl = po_message_msgstr( msg );
459 if (!transl[0]) transl = msgid; /* ignore empty strings */
460 else (*found)++;
462 else transl = msgid;
464 new = xmalloc( sizeof(*new) );
465 new->lan = lang;
466 new->cp = 0; /* FIXME */
467 new->file = str->file;
468 new->line = str->line;
469 new->len = wine_utf8_mbstowcs( 0, transl, strlen(transl) + 1, NULL, 0 );
470 new->msg = xmalloc( new->len * sizeof(WCHAR) );
471 res = wine_utf8_mbstowcs( MB_ERR_INVALID_CHARS, transl, strlen(transl) + 1, new->msg, new->len );
472 if (res == -2)
473 error( "Invalid utf-8 character in string '%s'\n", transl );
474 free( buffer );
475 return new;
478 static void translate_block( po_file_t po, block_t *blk, block_t *new, int lang, int *found )
480 int i;
482 new->idlo = blk->idlo;
483 new->idhi = blk->idhi;
484 new->size = 0;
485 new->msgs = xmalloc( blk->nmsg * sizeof(*new->msgs) );
486 new->nmsg = blk->nmsg;
487 for (i = 0; i < blk->nmsg; i++)
489 new->msgs[i] = translate_string( po, blk->msgs[i], lang, found );
490 new->size += ((2 * new->msgs[i]->len + 3) & ~3) + 4;
494 static void translate_messages( po_file_t po, int lang )
496 int i, found;
497 lan_blk_t *lbp, *new;
499 for (lbp = lanblockhead; lbp; lbp = lbp->next)
501 if (!is_english( lbp->lan )) continue;
502 found = 0;
503 new = xmalloc( sizeof(*new) );
504 /* English "translations" take precedence over the original contents */
505 new->version = is_english( lang ) ? 1 : -1;
506 new->lan = lang;
507 new->blks = xmalloc( lbp->nblk * sizeof(*new->blks) );
508 new->nblk = lbp->nblk;
510 for (i = 0; i < lbp->nblk; i++)
511 translate_block( po, &lbp->blks[i], &new->blks[i], lang, &found );
512 if (found)
514 if (new_tail) new_tail->next = new;
515 else new_top = new;
516 new->prev = new_tail;
517 new_tail = new;
519 else
521 free( new->blks );
522 free( new );
527 void add_translations( const char *po_dir )
529 lan_blk_t *lbp;
530 po_file_t po;
531 char buffer[256];
532 char *p, *tok, *name;
533 unsigned int i;
534 FILE *f;
536 /* first check if we have English resources to translate */
537 for (lbp = lanblockhead; lbp; lbp = lbp->next) if (is_english( lbp->lan )) break;
538 if (!lbp) return;
540 new_top = new_tail = NULL;
542 name = strmake( "%s/LINGUAS", po_dir );
543 if (!(f = fopen( name, "r" ))) return;
544 free( name );
545 while (fgets( buffer, sizeof(buffer), f ))
547 if ((p = strchr( buffer, '#' ))) *p = 0;
548 for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
550 for (i = 0; i < sizeof(languages)/sizeof(languages[0]); i++)
551 if (!strcmp( tok, languages[i].name )) break;
553 if (i == sizeof(languages)/sizeof(languages[0]))
554 error( "unknown language '%s'\n", tok );
556 name = strmake( "%s/%s.po", po_dir, tok );
557 if (!(po = po_file_read( name, &po_xerror_handler )))
558 error( "cannot load po file for language '%s'\n", tok );
559 translate_messages( po, MAKELANGID(languages[i].id, languages[i].sub) );
560 po_file_free( po );
561 free( name );
564 fclose( f );
566 /* prepend the translated messages to the global list */
567 if (new_tail)
569 new_tail->next = lanblockhead;
570 lanblockhead->prev = new_tail;
571 lanblockhead = new_top;
575 #else /* HAVE_LIBGETTEXTPO */
577 void write_pot_file( const char *outname )
579 error( "PO files not supported in this wmc build\n" );
582 void add_translations( const char *po_dir )
586 #endif