po.c 35 KB

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