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