ntdll: Move the keyed event functions to the Unix library.
[wine.git] / tools / wmc / po.c
blobd0a053cfb67ddbb1998e00712e43b0f767a22f05
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_AFRIKAANS, SUBLANG_NEUTRAL, "af" },
59 { LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA, "af_ZA" },
60 { LANG_ALBANIAN, SUBLANG_NEUTRAL, "sq" },
61 { LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA, "sq_AL" },
62 { LANG_AMHARIC, SUBLANG_NEUTRAL, "am" },
63 { LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA, "am_ET" },
64 { LANG_ARABIC, SUBLANG_NEUTRAL, "ar" },
65 { LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA, "ar_SA" },
66 { LANG_ARABIC, SUBLANG_ARABIC_IRAQ, "ar_IQ" },
67 { LANG_ARABIC, SUBLANG_ARABIC_EGYPT, "ar_EG" },
68 { LANG_ARABIC, SUBLANG_ARABIC_LIBYA, "ar_LY" },
69 { LANG_ARABIC, SUBLANG_ARABIC_ALGERIA, "ar_DZ" },
70 { LANG_ARABIC, SUBLANG_ARABIC_MOROCCO, "ar_MA" },
71 { LANG_ARABIC, SUBLANG_ARABIC_TUNISIA, "ar_TN" },
72 { LANG_ARABIC, SUBLANG_ARABIC_OMAN, "ar_OM" },
73 { LANG_ARABIC, SUBLANG_ARABIC_YEMEN, "ar_YE" },
74 { LANG_ARABIC, SUBLANG_ARABIC_SYRIA, "ar_SY" },
75 { LANG_ARABIC, SUBLANG_ARABIC_JORDAN, "ar_JO" },
76 { LANG_ARABIC, SUBLANG_ARABIC_LEBANON, "ar_LB" },
77 { LANG_ARABIC, SUBLANG_ARABIC_KUWAIT, "ar_KW" },
78 { LANG_ARABIC, SUBLANG_ARABIC_UAE, "ar_AE" },
79 { LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN, "ar_BH" },
80 { LANG_ARABIC, SUBLANG_ARABIC_QATAR, "ar_QA" },
81 { LANG_ARMENIAN, SUBLANG_NEUTRAL, "hy" },
82 { LANG_ARMENIAN, SUBLANG_ARMENIAN_ARMENIA, "hy_AM" },
83 { LANG_ASSAMESE, SUBLANG_NEUTRAL, "as" },
84 { LANG_ASSAMESE, SUBLANG_ASSAMESE_INDIA, "as_IN" },
85 { LANG_ASTURIAN, SUBLANG_NEUTRAL, "ast" },
86 { LANG_ASTURIAN, SUBLANG_DEFAULT, "ast_ES" },
87 { LANG_AZERBAIJANI, SUBLANG_NEUTRAL, "az" },
88 { LANG_AZERBAIJANI, SUBLANG_AZERBAIJANI_AZERBAIJAN_LATIN,"az_AZ@latin" },
89 { LANG_AZERBAIJANI, SUBLANG_AZERBAIJANI_AZERBAIJAN_CYRILLIC, "az_AZ@cyrillic" },
90 { LANG_BASQUE, SUBLANG_NEUTRAL, "eu" },
91 { LANG_BASQUE, SUBLANG_BASQUE_BASQUE, "eu_ES" },
92 { LANG_BELARUSIAN, SUBLANG_NEUTRAL, "be" },
93 { LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS, "be_BY" },
94 { LANG_BENGALI, SUBLANG_NEUTRAL, "bn" },
95 { LANG_BENGALI, SUBLANG_BENGALI_INDIA, "bn_IN" },
96 { LANG_BENGALI, SUBLANG_BENGALI_BANGLADESH, "bn_BD" },
97 { LANG_BRETON, SUBLANG_NEUTRAL, "br" },
98 { LANG_BRETON, SUBLANG_BRETON_FRANCE, "br_FR" },
99 { LANG_BULGARIAN, SUBLANG_NEUTRAL, "bg" },
100 { LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA, "bg_BG" },
101 { LANG_CATALAN, SUBLANG_NEUTRAL, "ca" },
102 { LANG_CATALAN, SUBLANG_CATALAN_CATALAN, "ca_ES" },
103 { LANG_CHINESE, SUBLANG_NEUTRAL, "zh" },
104 { LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL, "zh_TW" },
105 { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, "zh_CN" },
106 { LANG_CHINESE, SUBLANG_CHINESE_HONGKONG, "zh_HK" },
107 { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, "zh_SG" },
108 { LANG_CHINESE, SUBLANG_CHINESE_MACAU, "zh_MO" },
109 { LANG_CZECH, SUBLANG_NEUTRAL, "cs" },
110 { LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC, "cs_CZ" },
111 { LANG_DANISH, SUBLANG_NEUTRAL, "da" },
112 { LANG_DANISH, SUBLANG_DANISH_DENMARK, "da_DK" },
113 { LANG_DIVEHI, SUBLANG_NEUTRAL, "dv" },
114 { LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES, "dv_MV" },
115 { LANG_DUTCH, SUBLANG_NEUTRAL, "nl" },
116 { LANG_DUTCH, SUBLANG_DUTCH, "nl_NL" },
117 { LANG_DUTCH, SUBLANG_DUTCH_BELGIAN, "nl_BE" },
118 { LANG_DUTCH, SUBLANG_DUTCH_SURINAM, "nl_SR" },
119 { LANG_ENGLISH, SUBLANG_NEUTRAL, "en" },
120 { LANG_ENGLISH, SUBLANG_ENGLISH_US, "en_US" },
121 { LANG_ENGLISH, SUBLANG_ENGLISH_UK, "en_GB" },
122 { LANG_ENGLISH, SUBLANG_ENGLISH_AUS, "en_AU" },
123 { LANG_ENGLISH, SUBLANG_ENGLISH_CAN, "en_CA" },
124 { LANG_ENGLISH, SUBLANG_ENGLISH_NZ, "en_NZ" },
125 { LANG_ENGLISH, SUBLANG_ENGLISH_EIRE, "en_IE" },
126 { LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA, "en_ZA" },
127 { LANG_ENGLISH, SUBLANG_ENGLISH_JAMAICA, "en_JM" },
128 { LANG_ENGLISH, SUBLANG_ENGLISH_CARIBBEAN, "en_CB" },
129 { LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE, "en_BZ" },
130 { LANG_ENGLISH, SUBLANG_ENGLISH_TRINIDAD, "en_TT" },
131 { LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE, "en_ZW" },
132 { LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES, "en_PH" },
133 { LANG_ENGLISH, SUBLANG_ENGLISH_INDIA, "en_IN" },
134 { LANG_ENGLISH, SUBLANG_ENGLISH_MALAYSIA, "en_MY" },
135 { LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE, "en_SG" },
136 { LANG_ESTONIAN, SUBLANG_NEUTRAL, "et" },
137 { LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA, "et_EE" },
138 { LANG_FAEROESE, SUBLANG_NEUTRAL, "fo" },
139 { LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS, "fo_FO" },
140 { LANG_FILIPINO, SUBLANG_NEUTRAL, "fil" },
141 { LANG_FILIPINO, SUBLANG_FILIPINO_PHILIPPINES, "fil_PH" },
142 { LANG_FINNISH, SUBLANG_NEUTRAL, "fi" },
143 { LANG_FINNISH, SUBLANG_FINNISH_FINLAND, "fi_FI" },
144 { LANG_FRENCH, SUBLANG_NEUTRAL, "fr" },
145 { LANG_FRENCH, SUBLANG_FRENCH, "fr_FR" },
146 { LANG_FRENCH, SUBLANG_FRENCH_BELGIAN, "fr_BE" },
147 { LANG_FRENCH, SUBLANG_FRENCH_CANADIAN, "fr_CA" },
148 { LANG_FRENCH, SUBLANG_FRENCH_SWISS, "fr_CH" },
149 { LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG, "fr_LU" },
150 { LANG_FRENCH, SUBLANG_FRENCH_MONACO, "fr_MC" },
151 { LANG_GALICIAN, SUBLANG_NEUTRAL, "gl" },
152 { LANG_GALICIAN, SUBLANG_GALICIAN_GALICIAN, "gl_ES" },
153 { LANG_GEORGIAN, SUBLANG_NEUTRAL, "ka" },
154 { LANG_GEORGIAN, SUBLANG_GEORGIAN_GEORGIA, "ka_GE" },
155 { LANG_GERMAN, SUBLANG_NEUTRAL, "de" },
156 { LANG_GERMAN, SUBLANG_GERMAN, "de_DE" },
157 { LANG_GERMAN, SUBLANG_GERMAN_SWISS, "de_CH" },
158 { LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN, "de_AT" },
159 { LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG, "de_LU" },
160 { LANG_GERMAN, SUBLANG_GERMAN_LIECHTENSTEIN, "de_LI" },
161 { LANG_GREEK, SUBLANG_NEUTRAL, "el" },
162 { LANG_GREEK, SUBLANG_GREEK_GREECE, "el_GR" },
163 { LANG_GUJARATI, SUBLANG_NEUTRAL, "gu" },
164 { LANG_GUJARATI, SUBLANG_GUJARATI_INDIA, "gu_IN" },
165 { LANG_HAUSA, SUBLANG_NEUTRAL, "ha" },
166 { LANG_HAUSA, SUBLANG_HAUSA_NIGERIA, "ha_NG" },
167 { LANG_HAWAIIAN, SUBLANG_NEUTRAL, "haw" },
168 { LANG_HAWAIIAN, SUBLANG_HAWAIIAN_US, "haw_US" },
169 { LANG_HEBREW, SUBLANG_NEUTRAL, "he" },
170 { LANG_HEBREW, SUBLANG_HEBREW_ISRAEL, "he_IL" },
171 { LANG_HINDI, SUBLANG_NEUTRAL, "hi" },
172 { LANG_HINDI, SUBLANG_HINDI_INDIA, "hi_IN" },
173 { LANG_HUNGARIAN, SUBLANG_NEUTRAL, "hu" },
174 { LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY, "hu_HU" },
175 { LANG_ICELANDIC, SUBLANG_NEUTRAL, "is" },
176 { LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND, "is_IS" },
177 { LANG_IGBO, SUBLANG_NEUTRAL, "ig" },
178 { LANG_IGBO, SUBLANG_IGBO_NIGERIA, "ig_NG" },
179 { LANG_INDONESIAN, SUBLANG_NEUTRAL, "id" },
180 { LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA, "id_ID" },
181 { LANG_INUKTITUT, SUBLANG_NEUTRAL, "iu" },
182 { LANG_INUKTITUT, SUBLANG_INUKTITUT_CANADA, "iu_CA" },
183 { LANG_IRISH, SUBLANG_NEUTRAL, "ga" },
184 { LANG_IRISH, SUBLANG_IRISH_IRELAND, "ga_IE" },
185 { LANG_ITALIAN, SUBLANG_NEUTRAL, "it" },
186 { LANG_ITALIAN, SUBLANG_ITALIAN, "it_IT" },
187 { LANG_ITALIAN, SUBLANG_ITALIAN_SWISS, "it_CH" },
188 { LANG_JAPANESE, SUBLANG_NEUTRAL, "ja" },
189 { LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN, "ja_JP" },
190 { LANG_KANNADA, SUBLANG_NEUTRAL, "kn" },
191 { LANG_KANNADA, SUBLANG_KANNADA_INDIA, "kn_IN" },
192 { LANG_KAZAK, SUBLANG_NEUTRAL, "kk" },
193 { LANG_KAZAK, SUBLANG_KAZAK_KAZAKHSTAN, "kk_KZ" },
194 { LANG_KHMER, SUBLANG_NEUTRAL, "km" },
195 { LANG_KHMER, SUBLANG_KHMER_CAMBODIA, "km_KH" },
196 { LANG_KINYARWANDA, SUBLANG_NEUTRAL, "rw" },
197 { LANG_KINYARWANDA, SUBLANG_KINYARWANDA_RWANDA, "rw_RW" },
198 { LANG_KONKANI, SUBLANG_NEUTRAL, "kok" },
199 { LANG_KONKANI, SUBLANG_KONKANI_INDIA, "kok_IN" },
200 { LANG_KOREAN, SUBLANG_NEUTRAL, "ko" },
201 { LANG_KOREAN, SUBLANG_KOREAN, "ko_KR" },
202 { LANG_KYRGYZ, SUBLANG_NEUTRAL, "ky" },
203 { LANG_KYRGYZ, SUBLANG_KYRGYZ_KYRGYZSTAN, "ky_KG" },
204 { LANG_LAO, SUBLANG_NEUTRAL, "lo" },
205 { LANG_LAO, SUBLANG_LAO_LAO, "lo_LA" },
206 { LANG_LATVIAN, SUBLANG_NEUTRAL, "lv" },
207 { LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA, "lv_LV" },
208 { LANG_LITHUANIAN, SUBLANG_NEUTRAL, "lt" },
209 { LANG_LITHUANIAN, SUBLANG_LITHUANIAN, "lt_LT" },
210 { LANG_MACEDONIAN, SUBLANG_NEUTRAL, "mk" },
211 { LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA, "mk_MK" },
212 { LANG_MALAY, SUBLANG_NEUTRAL, "ms" },
213 { LANG_MALAY, SUBLANG_MALAY_MALAYSIA, "ms_MY" },
214 { LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM, "ms_BN" },
215 { LANG_MALAYALAM, SUBLANG_NEUTRAL, "ml" },
216 { LANG_MALAYALAM, SUBLANG_MALAYALAM_INDIA, "ml_IN" },
217 { LANG_MALTESE, SUBLANG_NEUTRAL, "mt" },
218 { LANG_MALTESE, SUBLANG_MALTESE_MALTA, "mt_MT" },
219 { LANG_MARATHI, SUBLANG_NEUTRAL, "mr" },
220 { LANG_MARATHI, SUBLANG_MARATHI_INDIA, "mr_IN" },
221 { LANG_MONGOLIAN, SUBLANG_NEUTRAL, "mn" },
222 { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
223 { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC, "mn_CN" },
224 { LANG_NEPALI, SUBLANG_NEUTRAL, "ne" },
225 { LANG_NEPALI, SUBLANG_NEPALI_NEPAL, "ne_NP" },
226 { LANG_NEPALI, SUBLANG_NEPALI_INDIA, "ne_IN" },
227 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL, "nb_NO" },
228 { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK, "nn_NO" },
229 { LANG_ODIA, SUBLANG_NEUTRAL, "or" },
230 { LANG_ODIA, SUBLANG_ODIA_INDIA, "or_IN" },
231 { LANG_PASHTO, SUBLANG_NEUTRAL, "ps" },
232 { LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN, "ps_AF" },
233 { LANG_PERSIAN, SUBLANG_NEUTRAL, "fa" },
234 { LANG_PERSIAN, SUBLANG_PERSIAN_IRAN, "fa_IR" },
235 { LANG_POLISH, SUBLANG_NEUTRAL, "pl" },
236 { LANG_POLISH, SUBLANG_POLISH_POLAND, "pl_PL" },
237 { LANG_PORTUGUESE, SUBLANG_NEUTRAL, "pt" },
238 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN, "pt_BR" },
239 { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_PORTUGAL, "pt_PT" },
240 { LANG_PUNJABI, SUBLANG_NEUTRAL, "pa" },
241 { LANG_PUNJABI, SUBLANG_PUNJABI_INDIA, "pa_IN" },
242 { LANG_PUNJABI, SUBLANG_PUNJABI_PAKISTAN, "pa_PK" },
243 { LANG_ROMANIAN, SUBLANG_NEUTRAL, "ro" },
244 { LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA, "ro_RO" },
245 { LANG_ROMANSH, SUBLANG_NEUTRAL, "rm" },
246 { LANG_ROMANSH, SUBLANG_ROMANSH_SWITZERLAND, "rm_CH" },
247 { LANG_RUSSIAN, SUBLANG_NEUTRAL, "ru" },
248 { LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA, "ru_RU" },
249 { LANG_SAMI, SUBLANG_NEUTRAL, "se" },
250 { LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY, "se_NO" },
251 { LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN, "se_SE" },
252 { LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND, "se_FI" },
253 { LANG_SANSKRIT, SUBLANG_NEUTRAL, "sa" },
254 { LANG_SANSKRIT, SUBLANG_SANSKRIT_INDIA, "sa_IN" },
255 { LANG_SCOTTISH_GAELIC,SUBLANG_NEUTRAL, "gd" },
256 { LANG_SCOTTISH_GAELIC,SUBLANG_SCOTTISH_GAELIC, "gd_GB" },
257 /* LANG_SERBIAN/LANG_CROATIAN/LANG_BOSNIAN are the same */
258 { LANG_SERBIAN, SUBLANG_NEUTRAL, "hr" },
259 { LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA, "hr_HR" },
260 { LANG_SERBIAN, SUBLANG_SERBIAN_LATIN, "sr_RS@latin" },
261 { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, "sr_RS@cyrillic" },
262 { LANG_SERBIAN, SUBLANG_CROATIAN_BOSNIA_HERZEGOVINA_LATIN, "hr_BA@latin" },
263 { LANG_SERBIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN, "bs_BA@latin" },
264 { LANG_SERBIAN, SUBLANG_SERBIAN_BOSNIA_HERZEGOVINA_LATIN, "sr_BA@latin" },
265 { LANG_SERBIAN, SUBLANG_SERBIAN_BOSNIA_HERZEGOVINA_CYRILLIC, "sr_BA@cyrillic" },
266 { LANG_SERBIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC, "bs_BA@cyrillic" },
267 { LANG_SERBIAN, SUBLANG_SERBIAN_SERBIA_LATIN, "sr_RS@latin" },
268 { LANG_SERBIAN, SUBLANG_SERBIAN_SERBIA_CYRILLIC, "sr_RS@cyrillic" },
269 { LANG_SERBIAN, SUBLANG_SERBIAN_MONTENEGRO_LATIN, "sr_ME@latin" },
270 { LANG_SERBIAN, SUBLANG_SERBIAN_MONTENEGRO_CYRILLIC, "sr_ME@cyrillic" },
271 { LANG_SINHALESE, SUBLANG_NEUTRAL, "si" },
272 { LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA, "si_LK" },
273 { LANG_SLOVAK, SUBLANG_NEUTRAL, "sk" },
274 { LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA, "sk_SK" },
275 { LANG_SLOVENIAN, SUBLANG_NEUTRAL, "sl" },
276 { LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA, "sl_SI" },
277 { LANG_SOTHO, SUBLANG_NEUTRAL, "nso" },
278 { LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA, "nso_ZA" },
279 { LANG_SPANISH, SUBLANG_NEUTRAL, "es" },
280 { LANG_SPANISH, SUBLANG_SPANISH, "es_ES" },
281 { LANG_SPANISH, SUBLANG_SPANISH_MEXICAN, "es_MX" },
282 { LANG_SPANISH, SUBLANG_SPANISH_MODERN, "es_ES_modern" },
283 { LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA, "es_GT" },
284 { LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA, "es_CR" },
285 { LANG_SPANISH, SUBLANG_SPANISH_PANAMA, "es_PA" },
286 { LANG_SPANISH, SUBLANG_SPANISH_DOMINICAN_REPUBLIC, "es_DO" },
287 { LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA, "es_VE" },
288 { LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA, "es_CO" },
289 { LANG_SPANISH, SUBLANG_SPANISH_PERU, "es_PE" },
290 { LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA, "es_AR" },
291 { LANG_SPANISH, SUBLANG_SPANISH_ECUADOR, "es_EC" },
292 { LANG_SPANISH, SUBLANG_SPANISH_CHILE, "es_CL" },
293 { LANG_SPANISH, SUBLANG_SPANISH_URUGUAY, "es_UY" },
294 { LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY, "es_PY" },
295 { LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA, "es_BO" },
296 { LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR, "es_SV" },
297 { LANG_SPANISH, SUBLANG_SPANISH_HONDURAS, "es_HN" },
298 { LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA, "es_NI" },
299 { LANG_SPANISH, SUBLANG_SPANISH_PUERTO_RICO, "es_PR" },
300 { LANG_SPANISH, SUBLANG_SPANISH_US, "es_US" },
301 { LANG_SWAHILI, SUBLANG_NEUTRAL, "sw" },
302 { LANG_SWAHILI, SUBLANG_SWAHILI_KENYA, "sw_KE" },
303 { LANG_SWEDISH, SUBLANG_NEUTRAL, "sv" },
304 { LANG_SWEDISH, SUBLANG_SWEDISH_SWEDEN, "sv_SE" },
305 { LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND, "sv_FI" },
306 { LANG_SYRIAC, SUBLANG_NEUTRAL, "syr" },
307 { LANG_SYRIAC, SUBLANG_SYRIAC_SYRIA, "syr_SY" },
308 { LANG_TAJIK, SUBLANG_NEUTRAL, "tg" },
309 { LANG_TAJIK, SUBLANG_TAJIK_TAJIKISTAN, "tg_TJ" },
310 { LANG_TAMIL, SUBLANG_NEUTRAL, "ta" },
311 { LANG_TAMIL, SUBLANG_TAMIL_INDIA, "ta_IN" },
312 { LANG_TATAR, SUBLANG_NEUTRAL, "tt" },
313 { LANG_TATAR, SUBLANG_TATAR_RUSSIA, "tt_TA" },
314 { LANG_TELUGU, SUBLANG_NEUTRAL, "te" },
315 { LANG_TELUGU, SUBLANG_TELUGU_INDIA, "te_IN" },
316 { LANG_THAI, SUBLANG_NEUTRAL, "th" },
317 { LANG_THAI, SUBLANG_THAI_THAILAND, "th_TH" },
318 { LANG_TIGRINYA, SUBLANG_NEUTRAL, "ti" },
319 { LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA, "ti_ET" },
320 { LANG_TIGRINYA, SUBLANG_TIGRINYA_ERITREA, "ti_ER" },
321 { LANG_TSWANA, SUBLANG_NEUTRAL, "tn" },
322 { LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA, "tn_ZA" },
323 { LANG_TURKISH, SUBLANG_NEUTRAL, "tr" },
324 { LANG_TURKISH, SUBLANG_TURKISH_TURKEY, "tr_TR" },
325 { LANG_UIGHUR, SUBLANG_NEUTRAL, "ug" },
326 { LANG_UIGHUR, SUBLANG_UIGHUR_PRC, "ug_CN" },
327 { LANG_UKRAINIAN, SUBLANG_NEUTRAL, "uk" },
328 { LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE, "uk_UA" },
329 { LANG_URDU, SUBLANG_NEUTRAL, "ur" },
330 { LANG_URDU, SUBLANG_URDU_PAKISTAN, "ur_PK" },
331 { LANG_URDU, SUBLANG_URDU_INDIA, "ur_IN" },
332 { LANG_UZBEK, SUBLANG_NEUTRAL, "uz" },
333 { LANG_UZBEK, SUBLANG_UZBEK_LATIN, "uz_UZ@latin" },
334 { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, "uz_UZ@cyrillic" },
335 { LANG_VIETNAMESE, SUBLANG_NEUTRAL, "vi" },
336 { LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM, "vi_VN" },
337 { LANG_WELSH, SUBLANG_NEUTRAL, "cy" },
338 { LANG_WELSH, SUBLANG_WELSH_UNITED_KINGDOM, "cy_GB" },
339 { LANG_WOLOF, SUBLANG_NEUTRAL, "wo" },
340 { LANG_WOLOF, SUBLANG_WOLOF_SENEGAL, "wo_SN" },
341 { LANG_XHOSA, SUBLANG_NEUTRAL, "xh" },
342 { LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA, "xh_ZA" },
343 { LANG_YORUBA, SUBLANG_NEUTRAL, "yo" },
344 { LANG_YORUBA, SUBLANG_YORUBA_NIGERIA, "yo_NG" },
345 { LANG_ZULU, SUBLANG_NEUTRAL, "zu" },
346 { LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA, "zu_ZA" },
348 #ifdef LANG_ESPERANTO
349 { LANG_ESPERANTO, SUBLANG_DEFAULT, "eo" },
350 #endif
351 #ifdef LANG_WALON
352 { LANG_WALON, SUBLANG_NEUTRAL, "wa" },
353 { LANG_WALON, SUBLANG_DEFAULT, "wa_BE" },
354 #endif
355 #ifdef LANG_CORNISH
356 { LANG_CORNISH, SUBLANG_NEUTRAL, "kw" },
357 { LANG_CORNISH, SUBLANG_DEFAULT, "kw_GB" },
358 #endif
359 #ifdef LANG_MANX_GAELIC
360 { LANG_MANX_GAELIC, SUBLANG_MANX_GAELIC, "gv_GB" },
361 #endif
364 static BOOL is_english( int lan )
366 return lan == MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
369 static char *convert_msgid_ascii( const lanmsg_t *msg, int error_on_invalid_char )
371 int i;
372 char *buffer = xmalloc( msg->len * 4 + 1 );
374 for (i = 0; i < msg->len; i++)
376 buffer[i] = msg->msg[i];
377 if (!msg->msg[i]) break;
378 if (msg->msg[i] >= 32 && msg->msg[i] <= 127) continue;
379 if (msg->msg[i] == '\t' || msg->msg[i] == '\n') continue;
380 if (error_on_invalid_char)
382 fprintf( stderr, "%s:%d: ", msg->file, msg->line );
383 error( "Invalid character %04x in source string\n", msg->msg[i] );
385 free( buffer );
386 return NULL;
388 buffer[i] = 0;
389 return buffer;
392 static char *get_message_context( char **msgid )
394 static const char magic[] = "#msgctxt#";
395 char *id, *context;
397 if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
398 context = *msgid + sizeof(magic) - 1;
399 if (!(id = strchr( context, '#' ))) return NULL;
400 *id = 0;
401 *msgid = id + 1;
402 return context;
405 #ifdef HAVE_LIBGETTEXTPO
407 static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
408 po_message_iterator_t *iterator )
410 po_message_t msg;
411 const char *context;
413 *iterator = po_message_iterator( po, NULL );
414 while ((msg = po_next_message( *iterator )))
416 if (strcmp( po_message_msgid( msg ), msgid )) continue;
417 if (!msgctxt) break;
418 if (!(context = po_message_msgctxt( msg ))) continue;
419 if (!strcmp( context, msgctxt )) break;
421 return msg;
424 static void po_xerror( int severity, po_message_t message,
425 const char *filename, size_t lineno, size_t column,
426 int multiline_p, const char *message_text )
428 fprintf( stderr, "%s:%u:%u: %s\n",
429 filename, (unsigned int)lineno, (unsigned int)column, message_text );
430 if (severity) exit(1);
433 static void po_xerror2( int severity, po_message_t message1,
434 const char *filename1, size_t lineno1, size_t column1,
435 int multiline_p1, const char *message_text1,
436 po_message_t message2,
437 const char *filename2, size_t lineno2, size_t column2,
438 int multiline_p2, const char *message_text2 )
440 fprintf( stderr, "%s:%u:%u: %s\n",
441 filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
442 fprintf( stderr, "%s:%u:%u: %s\n",
443 filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
444 if (severity) exit(1);
447 static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
449 static void add_po_string( po_file_t po, const lanmsg_t *msgid, const lanmsg_t *msgstr )
451 po_message_t msg;
452 po_message_iterator_t iterator;
453 char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
455 if (msgid->len <= 1) return;
457 id_buffer = id = convert_msgid_ascii( msgid, 1 );
458 context = get_message_context( &id );
460 if (msgstr)
462 int len;
463 str_buffer = str = unicode_to_utf8( msgstr->msg, msgstr->len, &len );
464 if (is_english( msgstr->lan )) get_message_context( &str );
466 if (!(msg = find_message( po, id, context, &iterator )))
468 msg = po_message_create();
469 po_message_set_msgid( msg, id );
470 po_message_set_msgstr( msg, str ? str : "" );
471 if (context) po_message_set_msgctxt( msg, context );
472 po_message_insert( iterator, msg );
474 if (msgid->file) po_message_add_filepos( msg, msgid->file, msgid->line );
475 po_message_iterator_free( iterator );
476 free( id_buffer );
477 free( str_buffer );
480 static po_file_t create_po_file(void)
482 po_file_t po;
483 po_message_t msg;
484 po_message_iterator_t iterator;
486 po = po_file_create();
487 iterator = po_message_iterator( po, NULL );
488 msg = po_message_create();
489 po_message_set_msgid( msg, "" );
490 po_message_set_msgstr( msg,
491 "Project-Id-Version: Wine\n"
492 "Report-Msgid-Bugs-To: https://bugs.winehq.org\n"
493 "POT-Creation-Date: N/A\n"
494 "PO-Revision-Date: N/A\n"
495 "Last-Translator: Automatically generated\n"
496 "Language-Team: none\n"
497 "MIME-Version: 1.0\n"
498 "Content-Type: text/plain; charset=UTF-8\n"
499 "Content-Transfer-Encoding: 8bit\n" );
500 po_message_insert( iterator, msg );
501 po_message_iterator_free( iterator );
502 return po;
505 void write_pot_file( const char *outname )
507 int i, j;
508 lan_blk_t *lbp;
509 po_file_t po = create_po_file();
511 for (lbp = lanblockhead; lbp; lbp = lbp->next)
513 if (!is_english( lbp->lan )) continue;
514 for (i = 0; i < lbp->nblk; i++)
516 block_t *blk = &lbp->blks[i];
517 for (j = 0; j < blk->nmsg; j++) add_po_string( po, blk->msgs[j], NULL );
520 po_file_write( po, outname, &po_xerror_handler );
521 po_file_free( po );
525 #else /* HAVE_LIBGETTEXTPO */
527 void write_pot_file( const char *outname )
529 error( "PO files not supported in this wmc build\n" );
532 #endif
534 static struct mo_file *mo_file;
536 static void byteswap( unsigned int *data, unsigned int count )
538 unsigned int i;
540 for (i = 0; i < count; i++)
541 data[i] = data[i] >> 24 | (data[i] >> 8 & 0xff00) | (data[i] << 8 & 0xff0000) | data[i] << 24;
544 static void load_mo_file( const char *name )
546 struct stat st;
547 int res, fd;
549 fd = open( name, O_RDONLY | O_BINARY );
550 if (fd == -1) fatal_perror( "Failed to open %s", name );
551 fstat( fd, &st );
552 mo_file = xmalloc( st.st_size );
553 res = read( fd, mo_file, st.st_size );
554 if (res == -1) fatal_perror( "Failed to read %s", name );
555 else if (res != st.st_size) error( "Failed to read %s\n", name );
556 close( fd );
558 /* sanity checks */
560 if (st.st_size < sizeof(*mo_file))
561 error( "%s is not a valid .mo file\n", name );
562 if (mo_file->magic == 0xde120495)
563 byteswap( &mo_file->revision, 4 );
564 else if (mo_file->magic != 0x950412de)
565 error( "%s is not a valid .mo file\n", name );
566 if ((mo_file->revision >> 16) > 1)
567 error( "%s: unsupported file version %x\n", name, mo_file->revision );
568 if (mo_file->msgid_off >= st.st_size ||
569 mo_file->msgstr_off >= st.st_size ||
570 st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count)
571 error( "%s: corrupted file\n", name );
573 if (mo_file->magic == 0xde120495)
575 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgid_off), 2 * mo_file->count );
576 byteswap( (unsigned int *)((char *)mo_file + mo_file->msgstr_off), 2 * mo_file->count );
580 static void free_mo_file(void)
582 free( mo_file );
583 mo_file = NULL;
586 static inline const char *get_mo_msgid( int index )
588 const char *base = (const char *)mo_file;
589 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgid_off);
590 return base + offsets[2 * index + 1];
593 static inline const char *get_mo_msgstr( int index )
595 const char *base = (const char *)mo_file;
596 const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgstr_off);
597 return base + offsets[2 * index + 1];
600 static const char *get_msgstr( const char *msgid, const char *context, int *found )
602 int pos, res, min, max;
603 const char *ret = msgid;
604 char *id = NULL;
606 if (!mo_file) /* strings containing a context still need to be transformed */
608 if (context) (*found)++;
609 return ret;
612 if (context) id = strmake( "%s%c%s", context, 4, msgid );
613 min = 0;
614 max = mo_file->count - 1;
615 while (min <= max)
617 pos = (min + max) / 2;
618 res = strcmp( get_mo_msgid(pos), id ? id : msgid );
619 if (!res)
621 const char *str = get_mo_msgstr( pos );
622 if (str[0]) /* ignore empty strings */
624 ret = str;
625 (*found)++;
627 break;
629 if (res > 0) max = pos - 1;
630 else min = pos + 1;
632 free( id );
633 return ret;
636 static lanmsg_t *translate_string( lanmsg_t *str, int lang, int *found )
638 lanmsg_t *new;
639 const char *transl;
640 char *buffer, *msgid, *context;
642 if (str->len <= 1 || !(buffer = convert_msgid_ascii( str, 0 ))) return str;
644 msgid = buffer;
645 context = get_message_context( &msgid );
646 transl = get_msgstr( msgid, context, found );
648 new = xmalloc( sizeof(*new) );
649 new->lan = lang;
650 new->cp = 0; /* FIXME */
651 new->file = str->file;
652 new->line = str->line;
653 new->msg = utf8_to_unicode( transl, strlen(transl) + 1, &new->len );
654 free( buffer );
655 return new;
658 static void translate_block( block_t *blk, block_t *new, int lang, int *found )
660 int i;
662 new->idlo = blk->idlo;
663 new->idhi = blk->idhi;
664 new->size = 0;
665 new->msgs = xmalloc( blk->nmsg * sizeof(*new->msgs) );
666 new->nmsg = blk->nmsg;
667 for (i = 0; i < blk->nmsg; i++)
669 new->msgs[i] = translate_string( blk->msgs[i], lang, found );
670 new->size += ((2 * new->msgs[i]->len + 3) & ~3) + 4;
674 static void translate_messages( int lang )
676 int i, found;
677 lan_blk_t *lbp, *new;
679 for (lbp = lanblockhead; lbp; lbp = lbp->next)
681 if (!is_english( lbp->lan )) continue;
682 found = 0;
683 new = xmalloc( sizeof(*new) );
684 /* English "translations" take precedence over the original contents */
685 new->version = is_english( lang ) ? 1 : -1;
686 new->lan = lang;
687 new->blks = xmalloc( lbp->nblk * sizeof(*new->blks) );
688 new->nblk = lbp->nblk;
690 for (i = 0; i < lbp->nblk; i++)
691 translate_block( &lbp->blks[i], &new->blks[i], lang, &found );
692 if (found)
694 if (new_tail) new_tail->next = new;
695 else new_top = new;
696 new->prev = new_tail;
697 new_tail = new;
699 else
701 free( new->blks );
702 free( new );
707 void add_translations( const char *po_dir )
709 lan_blk_t *lbp;
710 char buffer[256];
711 char *p, *tok, *name;
712 unsigned int i;
713 FILE *f;
715 /* first check if we have English resources to translate */
716 for (lbp = lanblockhead; lbp; lbp = lbp->next) if (is_english( lbp->lan )) break;
717 if (!lbp) return;
719 if (!po_dir) /* run through the translation process to remove msg contexts */
721 translate_messages( MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ));
722 goto done;
725 new_top = new_tail = NULL;
727 name = strmake( "%s/LINGUAS", po_dir );
728 if (!(f = fopen( name, "r" ))) return;
729 free( name );
730 while (fgets( buffer, sizeof(buffer), f ))
732 if ((p = strchr( buffer, '#' ))) *p = 0;
733 for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
735 for (i = 0; i < ARRAY_SIZE(languages); i++)
736 if (!strcmp( tok, languages[i].name )) break;
738 if (i == ARRAY_SIZE(languages))
739 error( "unknown language '%s'\n", tok );
741 name = strmake( "%s/%s.mo", po_dir, tok );
742 load_mo_file( name );
743 translate_messages( MAKELANGID(languages[i].id, languages[i].sub) );
744 free_mo_file();
745 free( name );
748 fclose( f );
750 done:
751 /* prepend the translated messages to the global list */
752 if (new_tail)
754 new_tail->next = lanblockhead;
755 lanblockhead->prev = new_tail;
756 lanblockhead = new_top;