po.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. /*
  2. * Support for po files
  3. *
  4. * Copyright 2010 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_LIBGETTEXTPO
  29. #include <gettext-po.h>
  30. #endif
  31. #include "wrc.h"
  32. #include "genres.h"
  33. #include "newstruc.h"
  34. #include "utils.h"
  35. #include "windef.h"
  36. #include "winbase.h"
  37. #include "wingdi.h"
  38. #include "winuser.h"
  39. #include "wine/list.h"
  40. static resource_t *new_top, *new_tail;
  41. struct mo_file
  42. {
  43. unsigned int magic;
  44. unsigned int revision;
  45. unsigned int count;
  46. unsigned int msgid_off;
  47. unsigned int msgstr_off;
  48. /* ... rest of file data here */
  49. };
  50. static BOOL is_english( const language_t *lan )
  51. {
  52. return lan->id == LANG_ENGLISH && lan->sub == SUBLANG_DEFAULT;
  53. }
  54. static BOOL is_rtl_language( const language_t *lan )
  55. {
  56. return lan->id == LANG_ARABIC || lan->id == LANG_HEBREW || lan->id == LANG_PERSIAN;
  57. }
  58. static BOOL uses_larger_font( const language_t *lan )
  59. {
  60. return lan->id == LANG_CHINESE || lan->id == LANG_JAPANESE || lan->id == LANG_KOREAN;
  61. }
  62. static WORD get_default_sublang( const language_t *lan )
  63. {
  64. if (lan->sub != SUBLANG_NEUTRAL)
  65. return lan->sub;
  66. switch (lan->id)
  67. {
  68. case LANG_SPANISH:
  69. return SUBLANG_SPANISH_MODERN;
  70. case LANG_CHINESE:
  71. return SUBLANG_CHINESE_SIMPLIFIED;
  72. default:
  73. return SUBLANG_DEFAULT;
  74. }
  75. }
  76. static version_t *get_dup_version( language_t *lang )
  77. {
  78. /* English "translations" take precedence over the original rc contents */
  79. return new_version( is_english( lang ) ? 1 : -1 );
  80. }
  81. static name_id_t *dup_name_id( name_id_t *id )
  82. {
  83. name_id_t *new;
  84. if (!id || id->type != name_str) return id;
  85. new = new_name_id();
  86. *new = *id;
  87. new->name.s_name = convert_string_unicode( id->name.s_name, 1252 );
  88. return new;
  89. }
  90. static char *convert_msgid_ascii( const string_t *str, int error_on_invalid_char )
  91. {
  92. int i;
  93. char *buffer = xmalloc( str->size + 1 );
  94. for (i = 0; i < str->size; i++)
  95. {
  96. WCHAR ch = (str->type == str_unicode ? str->str.wstr[i] : (unsigned char)str->str.cstr[i]);
  97. buffer[i] = ch;
  98. if (ch >= 32 && ch <= 127) continue;
  99. if (ch == '\t' || ch == '\n') continue;
  100. if (error_on_invalid_char)
  101. {
  102. print_location( &str->loc );
  103. error( "Invalid character %04x in source string\n", ch );
  104. }
  105. free( buffer);
  106. return NULL;
  107. }
  108. buffer[i] = 0;
  109. return buffer;
  110. }
  111. static char *get_message_context( char **msgid )
  112. {
  113. static const char magic[] = "#msgctxt#";
  114. char *id, *context;
  115. if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
  116. context = *msgid + sizeof(magic) - 1;
  117. if (!(id = strchr( context, '#' ))) return NULL;
  118. *id = 0;
  119. *msgid = id + 1;
  120. return context;
  121. }
  122. static BOOL control_has_title( const control_t *ctrl )
  123. {
  124. if (!ctrl->title) return FALSE;
  125. if (ctrl->title->type != name_str) return FALSE;
  126. /* check for text static control */
  127. if (ctrl->ctlclass && ctrl->ctlclass->type == name_ord && ctrl->ctlclass->name.i_name == CT_STATIC)
  128. {
  129. switch (ctrl->style->or_mask & SS_TYPEMASK)
  130. {
  131. case SS_LEFT:
  132. case SS_CENTER:
  133. case SS_RIGHT:
  134. return TRUE;
  135. default:
  136. return FALSE;
  137. }
  138. }
  139. return TRUE;
  140. }
  141. static resource_t *dup_resource( resource_t *res, language_t *lang )
  142. {
  143. resource_t *new = xmalloc( sizeof(*new) );
  144. *new = *res;
  145. new->lan = lang;
  146. new->next = new->prev = NULL;
  147. new->name = dup_name_id( res->name );
  148. switch (res->type)
  149. {
  150. case res_acc:
  151. new->res.acc = xmalloc( sizeof(*(new)->res.acc) );
  152. *new->res.acc = *res->res.acc;
  153. new->res.acc->lvc.language = lang;
  154. new->res.acc->lvc.version = get_dup_version( lang );
  155. break;
  156. case res_dlg:
  157. new->res.dlg = xmalloc( sizeof(*(new)->res.dlg) );
  158. *new->res.dlg = *res->res.dlg;
  159. new->res.dlg->lvc.language = lang;
  160. new->res.dlg->lvc.version = get_dup_version( lang );
  161. break;
  162. case res_men:
  163. new->res.men = xmalloc( sizeof(*(new)->res.men) );
  164. *new->res.men = *res->res.men;
  165. new->res.men->lvc.language = lang;
  166. new->res.men->lvc.version = get_dup_version( lang );
  167. break;
  168. case res_stt:
  169. new->res.stt = xmalloc( sizeof(*(new)->res.stt) );
  170. *new->res.stt = *res->res.stt;
  171. new->res.stt->lvc.language = lang;
  172. new->res.stt->lvc.version = get_dup_version( lang );
  173. break;
  174. case res_ver:
  175. new->res.ver = xmalloc( sizeof(*(new)->res.ver) );
  176. *new->res.ver = *res->res.ver;
  177. new->res.ver->lvc.language = lang;
  178. new->res.ver->lvc.version = get_dup_version( lang );
  179. break;
  180. default:
  181. assert(0);
  182. }
  183. return new;
  184. }
  185. static const struct
  186. {
  187. unsigned int id, sub;
  188. const char *name;
  189. } languages[] =
  190. {
  191. { LANG_AFRIKAANS, SUBLANG_NEUTRAL, "af" },
  192. { LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA, "af_ZA" },
  193. { LANG_ALBANIAN, SUBLANG_NEUTRAL, "sq" },
  194. { LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA, "sq_AL" },
  195. { LANG_AMHARIC, SUBLANG_NEUTRAL, "am" },
  196. { LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA, "am_ET" },
  197. { LANG_ARABIC, SUBLANG_NEUTRAL, "ar" },
  198. { LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA, "ar_SA" },
  199. { LANG_ARABIC, SUBLANG_ARABIC_IRAQ, "ar_IQ" },
  200. { LANG_ARABIC, SUBLANG_ARABIC_EGYPT, "ar_EG" },
  201. { LANG_ARABIC, SUBLANG_ARABIC_LIBYA, "ar_LY" },
  202. { LANG_ARABIC, SUBLANG_ARABIC_ALGERIA, "ar_DZ" },
  203. { LANG_ARABIC, SUBLANG_ARABIC_MOROCCO, "ar_MA" },
  204. { LANG_ARABIC, SUBLANG_ARABIC_TUNISIA, "ar_TN" },
  205. { LANG_ARABIC, SUBLANG_ARABIC_OMAN, "ar_OM" },
  206. { LANG_ARABIC, SUBLANG_ARABIC_YEMEN, "ar_YE" },
  207. { LANG_ARABIC, SUBLANG_ARABIC_SYRIA, "ar_SY" },
  208. { LANG_ARABIC, SUBLANG_ARABIC_JORDAN, "ar_JO" },
  209. { LANG_ARABIC, SUBLANG_ARABIC_LEBANON, "ar_LB" },
  210. { LANG_ARABIC, SUBLANG_ARABIC_KUWAIT, "ar_KW" },
  211. { LANG_ARABIC, SUBLANG_ARABIC_UAE, "ar_AE" },
  212. { LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN, "ar_BH" },
  213. { LANG_ARABIC, SUBLANG_ARABIC_QATAR, "ar_QA" },
  214. { LANG_ARMENIAN, SUBLANG_NEUTRAL, "hy" },
  215. { LANG_ARMENIAN, SUBLANG_ARMENIAN_ARMENIA, "hy_AM" },
  216. { LANG_ASSAMESE, SUBLANG_NEUTRAL, "as" },
  217. { LANG_ASSAMESE, SUBLANG_ASSAMESE_INDIA, "as_IN" },
  218. { LANG_ASTURIAN, SUBLANG_NEUTRAL, "ast" },
  219. { LANG_ASTURIAN, SUBLANG_DEFAULT, "ast_ES" },
  220. { LANG_AZERBAIJANI, SUBLANG_NEUTRAL, "az" },
  221. { LANG_AZERBAIJANI, SUBLANG_AZERBAIJANI_AZERBAIJAN_LATIN,"az_AZ@latin" },
  222. { LANG_AZERBAIJANI, SUBLANG_AZERBAIJANI_AZERBAIJAN_CYRILLIC, "az_AZ@cyrillic" },
  223. { LANG_BASQUE, SUBLANG_NEUTRAL, "eu" },
  224. { LANG_BASQUE, SUBLANG_BASQUE_BASQUE, "eu_ES" },
  225. { LANG_BELARUSIAN, SUBLANG_NEUTRAL, "be" },
  226. { LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS, "be_BY" },
  227. { LANG_BENGALI, SUBLANG_NEUTRAL, "bn" },
  228. { LANG_BENGALI, SUBLANG_BENGALI_INDIA, "bn_IN" },
  229. { LANG_BENGALI, SUBLANG_BENGALI_BANGLADESH, "bn_BD" },
  230. { LANG_BRETON, SUBLANG_NEUTRAL, "br" },
  231. { LANG_BRETON, SUBLANG_BRETON_FRANCE, "br_FR" },
  232. { LANG_BULGARIAN, SUBLANG_NEUTRAL, "bg" },
  233. { LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA, "bg_BG" },
  234. { LANG_CATALAN, SUBLANG_NEUTRAL, "ca" },
  235. { LANG_CATALAN, SUBLANG_CATALAN_CATALAN, "ca_ES" },
  236. { LANG_CHINESE, SUBLANG_NEUTRAL, "zh" },
  237. { LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL, "zh_TW" },
  238. { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, "zh_CN" },
  239. { LANG_CHINESE, SUBLANG_CHINESE_HONGKONG, "zh_HK" },
  240. { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, "zh_SG" },
  241. { LANG_CHINESE, SUBLANG_CHINESE_MACAU, "zh_MO" },
  242. { LANG_CZECH, SUBLANG_NEUTRAL, "cs" },
  243. { LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC, "cs_CZ" },
  244. { LANG_DANISH, SUBLANG_NEUTRAL, "da" },
  245. { LANG_DANISH, SUBLANG_DANISH_DENMARK, "da_DK" },
  246. { LANG_DIVEHI, SUBLANG_NEUTRAL, "dv" },
  247. { LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES, "dv_MV" },
  248. { LANG_DUTCH, SUBLANG_NEUTRAL, "nl" },
  249. { LANG_DUTCH, SUBLANG_DUTCH, "nl_NL" },
  250. { LANG_DUTCH, SUBLANG_DUTCH_BELGIAN, "nl_BE" },
  251. { LANG_DUTCH, SUBLANG_DUTCH_SURINAM, "nl_SR" },
  252. { LANG_ENGLISH, SUBLANG_NEUTRAL, "en" },
  253. { LANG_ENGLISH, SUBLANG_ENGLISH_US, "en_US" },
  254. { LANG_ENGLISH, SUBLANG_ENGLISH_UK, "en_GB" },
  255. { LANG_ENGLISH, SUBLANG_ENGLISH_AUS, "en_AU" },
  256. { LANG_ENGLISH, SUBLANG_ENGLISH_CAN, "en_CA" },
  257. { LANG_ENGLISH, SUBLANG_ENGLISH_NZ, "en_NZ" },
  258. { LANG_ENGLISH, SUBLANG_ENGLISH_EIRE, "en_IE" },
  259. { LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA, "en_ZA" },
  260. { LANG_ENGLISH, SUBLANG_ENGLISH_JAMAICA, "en_JM" },
  261. { LANG_ENGLISH, SUBLANG_ENGLISH_CARIBBEAN, "en_CB" },
  262. { LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE, "en_BZ" },
  263. { LANG_ENGLISH, SUBLANG_ENGLISH_TRINIDAD, "en_TT" },
  264. { LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE, "en_ZW" },
  265. { LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES, "en_PH" },
  266. { LANG_ENGLISH, SUBLANG_ENGLISH_INDIA, "en_IN" },
  267. { LANG_ENGLISH, SUBLANG_ENGLISH_MALAYSIA, "en_MY" },
  268. { LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE, "en_SG" },
  269. { LANG_ESTONIAN, SUBLANG_NEUTRAL, "et" },
  270. { LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA, "et_EE" },
  271. { LANG_FAEROESE, SUBLANG_NEUTRAL, "fo" },
  272. { LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS, "fo_FO" },
  273. { LANG_FILIPINO, SUBLANG_NEUTRAL, "fil" },
  274. { LANG_FILIPINO, SUBLANG_FILIPINO_PHILIPPINES, "fil_PH" },
  275. { LANG_FINNISH, SUBLANG_NEUTRAL, "fi" },
  276. { LANG_FINNISH, SUBLANG_FINNISH_FINLAND, "fi_FI" },
  277. { LANG_FRENCH, SUBLANG_NEUTRAL, "fr" },
  278. { LANG_FRENCH, SUBLANG_FRENCH, "fr_FR" },
  279. { LANG_FRENCH, SUBLANG_FRENCH_BELGIAN, "fr_BE" },
  280. { LANG_FRENCH, SUBLANG_FRENCH_CANADIAN, "fr_CA" },
  281. { LANG_FRENCH, SUBLANG_FRENCH_SWISS, "fr_CH" },
  282. { LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG, "fr_LU" },
  283. { LANG_FRENCH, SUBLANG_FRENCH_MONACO, "fr_MC" },
  284. { LANG_GALICIAN, SUBLANG_NEUTRAL, "gl" },
  285. { LANG_GALICIAN, SUBLANG_GALICIAN_GALICIAN, "gl_ES" },
  286. { LANG_GEORGIAN, SUBLANG_NEUTRAL, "ka" },
  287. { LANG_GEORGIAN, SUBLANG_GEORGIAN_GEORGIA, "ka_GE" },
  288. { LANG_GERMAN, SUBLANG_NEUTRAL, "de" },
  289. { LANG_GERMAN, SUBLANG_GERMAN, "de_DE" },
  290. { LANG_GERMAN, SUBLANG_GERMAN_SWISS, "de_CH" },
  291. { LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN, "de_AT" },
  292. { LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG, "de_LU" },
  293. { LANG_GERMAN, SUBLANG_GERMAN_LIECHTENSTEIN, "de_LI" },
  294. { LANG_GREEK, SUBLANG_NEUTRAL, "el" },
  295. { LANG_GREEK, SUBLANG_GREEK_GREECE, "el_GR" },
  296. { LANG_GUJARATI, SUBLANG_NEUTRAL, "gu" },
  297. { LANG_GUJARATI, SUBLANG_GUJARATI_INDIA, "gu_IN" },
  298. { LANG_HAUSA, SUBLANG_NEUTRAL, "ha" },
  299. { LANG_HAUSA, SUBLANG_HAUSA_NIGERIA, "ha_NG" },
  300. { LANG_HAWAIIAN, SUBLANG_NEUTRAL, "haw" },
  301. { LANG_HAWAIIAN, SUBLANG_HAWAIIAN_US, "haw_US" },
  302. { LANG_HEBREW, SUBLANG_NEUTRAL, "he" },
  303. { LANG_HEBREW, SUBLANG_HEBREW_ISRAEL, "he_IL" },
  304. { LANG_HINDI, SUBLANG_NEUTRAL, "hi" },
  305. { LANG_HINDI, SUBLANG_HINDI_INDIA, "hi_IN" },
  306. { LANG_HUNGARIAN, SUBLANG_NEUTRAL, "hu" },
  307. { LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY, "hu_HU" },
  308. { LANG_ICELANDIC, SUBLANG_NEUTRAL, "is" },
  309. { LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND, "is_IS" },
  310. { LANG_IGBO, SUBLANG_NEUTRAL, "ig" },
  311. { LANG_IGBO, SUBLANG_IGBO_NIGERIA, "ig_NG" },
  312. { LANG_INDONESIAN, SUBLANG_NEUTRAL, "id" },
  313. { LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA, "id_ID" },
  314. { LANG_INUKTITUT, SUBLANG_NEUTRAL, "iu" },
  315. { LANG_INUKTITUT, SUBLANG_INUKTITUT_CANADA, "iu_CA" },
  316. { LANG_IRISH, SUBLANG_NEUTRAL, "ga" },
  317. { LANG_IRISH, SUBLANG_IRISH_IRELAND, "ga_IE" },
  318. { LANG_ITALIAN, SUBLANG_NEUTRAL, "it" },
  319. { LANG_ITALIAN, SUBLANG_ITALIAN, "it_IT" },
  320. { LANG_ITALIAN, SUBLANG_ITALIAN_SWISS, "it_CH" },
  321. { LANG_JAPANESE, SUBLANG_NEUTRAL, "ja" },
  322. { LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN, "ja_JP" },
  323. { LANG_KANNADA, SUBLANG_NEUTRAL, "kn" },
  324. { LANG_KANNADA, SUBLANG_KANNADA_INDIA, "kn_IN" },
  325. { LANG_KAZAK, SUBLANG_NEUTRAL, "kk" },
  326. { LANG_KAZAK, SUBLANG_KAZAK_KAZAKHSTAN, "kk_KZ" },
  327. { LANG_KHMER, SUBLANG_NEUTRAL, "km" },
  328. { LANG_KHMER, SUBLANG_KHMER_CAMBODIA, "km_KH" },
  329. { LANG_KINYARWANDA, SUBLANG_NEUTRAL, "rw" },
  330. { LANG_KINYARWANDA, SUBLANG_KINYARWANDA_RWANDA, "rw_RW" },
  331. { LANG_KONKANI, SUBLANG_NEUTRAL, "kok" },
  332. { LANG_KONKANI, SUBLANG_KONKANI_INDIA, "kok_IN" },
  333. { LANG_KOREAN, SUBLANG_NEUTRAL, "ko" },
  334. { LANG_KOREAN, SUBLANG_KOREAN, "ko_KR" },
  335. { LANG_KYRGYZ, SUBLANG_NEUTRAL, "ky" },
  336. { LANG_KYRGYZ, SUBLANG_KYRGYZ_KYRGYZSTAN, "ky_KG" },
  337. { LANG_LAO, SUBLANG_NEUTRAL, "lo" },
  338. { LANG_LAO, SUBLANG_LAO_LAO, "lo_LA" },
  339. { LANG_LATVIAN, SUBLANG_NEUTRAL, "lv" },
  340. { LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA, "lv_LV" },
  341. { LANG_LITHUANIAN, SUBLANG_NEUTRAL, "lt" },
  342. { LANG_LITHUANIAN, SUBLANG_LITHUANIAN, "lt_LT" },
  343. { LANG_MACEDONIAN, SUBLANG_NEUTRAL, "mk" },
  344. { LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA, "mk_MK" },
  345. { LANG_MALAY, SUBLANG_NEUTRAL, "ms" },
  346. { LANG_MALAY, SUBLANG_MALAY_MALAYSIA, "ms_MY" },
  347. { LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM, "ms_BN" },
  348. { LANG_MALAYALAM, SUBLANG_NEUTRAL, "ml" },
  349. { LANG_MALAYALAM, SUBLANG_MALAYALAM_INDIA, "ml_IN" },
  350. { LANG_MALTESE, SUBLANG_NEUTRAL, "mt" },
  351. { LANG_MALTESE, SUBLANG_MALTESE_MALTA, "mt_MT" },
  352. { LANG_MARATHI, SUBLANG_NEUTRAL, "mr" },
  353. { LANG_MARATHI, SUBLANG_MARATHI_INDIA, "mr_IN" },
  354. { LANG_MONGOLIAN, SUBLANG_NEUTRAL, "mn" },
  355. { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
  356. { LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC, "mn_CN" },
  357. { LANG_NEPALI, SUBLANG_NEUTRAL, "ne" },
  358. { LANG_NEPALI, SUBLANG_NEPALI_NEPAL, "ne_NP" },
  359. { LANG_NEPALI, SUBLANG_NEPALI_INDIA, "ne_IN" },
  360. { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL, "nb_NO" },
  361. { LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK, "nn_NO" },
  362. { LANG_ODIA, SUBLANG_NEUTRAL, "or" },
  363. { LANG_ODIA, SUBLANG_ODIA_INDIA, "or_IN" },
  364. { LANG_PASHTO, SUBLANG_NEUTRAL, "ps" },
  365. { LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN, "ps_AF" },
  366. { LANG_PERSIAN, SUBLANG_NEUTRAL, "fa" },
  367. { LANG_PERSIAN, SUBLANG_PERSIAN_IRAN, "fa_IR" },
  368. { LANG_POLISH, SUBLANG_NEUTRAL, "pl" },
  369. { LANG_POLISH, SUBLANG_POLISH_POLAND, "pl_PL" },
  370. { LANG_PORTUGUESE, SUBLANG_NEUTRAL, "pt" },
  371. { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN, "pt_BR" },
  372. { LANG_PORTUGUESE, SUBLANG_PORTUGUESE_PORTUGAL, "pt_PT" },
  373. { LANG_PUNJABI, SUBLANG_NEUTRAL, "pa" },
  374. { LANG_PUNJABI, SUBLANG_PUNJABI_INDIA, "pa_IN" },
  375. { LANG_PUNJABI, SUBLANG_PUNJABI_PAKISTAN, "pa_PK" },
  376. { LANG_ROMANIAN, SUBLANG_NEUTRAL, "ro" },
  377. { LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA, "ro_RO" },
  378. { LANG_ROMANSH, SUBLANG_NEUTRAL, "rm" },
  379. { LANG_ROMANSH, SUBLANG_ROMANSH_SWITZERLAND, "rm_CH" },
  380. { LANG_RUSSIAN, SUBLANG_NEUTRAL, "ru" },
  381. { LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA, "ru_RU" },
  382. { LANG_SAMI, SUBLANG_NEUTRAL, "se" },
  383. { LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY, "se_NO" },
  384. { LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN, "se_SE" },
  385. { LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND, "se_FI" },
  386. { LANG_SANSKRIT, SUBLANG_NEUTRAL, "sa" },
  387. { LANG_SANSKRIT, SUBLANG_SANSKRIT_INDIA, "sa_IN" },
  388. { LANG_SCOTTISH_GAELIC,SUBLANG_NEUTRAL, "gd" },
  389. { LANG_SCOTTISH_GAELIC,SUBLANG_SCOTTISH_GAELIC, "gd_GB" },
  390. /* LANG_SERBIAN/LANG_CROATIAN/LANG_BOSNIAN are the same */
  391. { LANG_SERBIAN, SUBLANG_NEUTRAL, "hr" },
  392. { LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA, "hr_HR" },
  393. { LANG_SERBIAN, SUBLANG_SERBIAN_LATIN, "sr_RS@latin" },
  394. { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, "sr_RS@cyrillic" },
  395. { LANG_SERBIAN, SUBLANG_CROATIAN_BOSNIA_HERZEGOVINA_LATIN, "hr_BA@latin" },
  396. { LANG_SERBIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN, "bs_BA@latin" },
  397. { LANG_SERBIAN, SUBLANG_SERBIAN_BOSNIA_HERZEGOVINA_LATIN, "sr_BA@latin" },
  398. { LANG_SERBIAN, SUBLANG_SERBIAN_BOSNIA_HERZEGOVINA_CYRILLIC, "sr_BA@cyrillic" },
  399. { LANG_SERBIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC, "bs_BA@cyrillic" },
  400. { LANG_SERBIAN, SUBLANG_SERBIAN_SERBIA_LATIN, "sr_RS@latin" },
  401. { LANG_SERBIAN, SUBLANG_SERBIAN_SERBIA_CYRILLIC, "sr_RS@cyrillic" },
  402. { LANG_SERBIAN, SUBLANG_SERBIAN_MONTENEGRO_LATIN, "sr_ME@latin" },
  403. { LANG_SERBIAN, SUBLANG_SERBIAN_MONTENEGRO_CYRILLIC, "sr_ME@cyrillic" },
  404. { LANG_SINHALESE, SUBLANG_NEUTRAL, "si" },
  405. { LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA, "si_LK" },
  406. { LANG_SLOVAK, SUBLANG_NEUTRAL, "sk" },
  407. { LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA, "sk_SK" },
  408. { LANG_SLOVENIAN, SUBLANG_NEUTRAL, "sl" },
  409. { LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA, "sl_SI" },
  410. { LANG_SOTHO, SUBLANG_NEUTRAL, "nso" },
  411. { LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA, "nso_ZA" },
  412. { LANG_SPANISH, SUBLANG_NEUTRAL, "es" },
  413. { LANG_SPANISH, SUBLANG_SPANISH, "es_ES" },
  414. { LANG_SPANISH, SUBLANG_SPANISH_MEXICAN, "es_MX" },
  415. { LANG_SPANISH, SUBLANG_SPANISH_MODERN, "es_ES_modern" },
  416. { LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA, "es_GT" },
  417. { LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA, "es_CR" },
  418. { LANG_SPANISH, SUBLANG_SPANISH_PANAMA, "es_PA" },
  419. { LANG_SPANISH, SUBLANG_SPANISH_DOMINICAN_REPUBLIC, "es_DO" },
  420. { LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA, "es_VE" },
  421. { LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA, "es_CO" },
  422. { LANG_SPANISH, SUBLANG_SPANISH_PERU, "es_PE" },
  423. { LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA, "es_AR" },
  424. { LANG_SPANISH, SUBLANG_SPANISH_ECUADOR, "es_EC" },
  425. { LANG_SPANISH, SUBLANG_SPANISH_CHILE, "es_CL" },
  426. { LANG_SPANISH, SUBLANG_SPANISH_URUGUAY, "es_UY" },
  427. { LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY, "es_PY" },
  428. { LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA, "es_BO" },
  429. { LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR, "es_SV" },
  430. { LANG_SPANISH, SUBLANG_SPANISH_HONDURAS, "es_HN" },
  431. { LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA, "es_NI" },
  432. { LANG_SPANISH, SUBLANG_SPANISH_PUERTO_RICO, "es_PR" },
  433. { LANG_SPANISH, SUBLANG_SPANISH_US, "es_US" },
  434. { LANG_SWAHILI, SUBLANG_NEUTRAL, "sw" },
  435. { LANG_SWAHILI, SUBLANG_SWAHILI_KENYA, "sw_KE" },
  436. { LANG_SWEDISH, SUBLANG_NEUTRAL, "sv" },
  437. { LANG_SWEDISH, SUBLANG_SWEDISH_SWEDEN, "sv_SE" },
  438. { LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND, "sv_FI" },
  439. { LANG_SYRIAC, SUBLANG_NEUTRAL, "syr" },
  440. { LANG_SYRIAC, SUBLANG_SYRIAC_SYRIA, "syr_SY" },
  441. { LANG_TAJIK, SUBLANG_NEUTRAL, "tg" },
  442. { LANG_TAJIK, SUBLANG_TAJIK_TAJIKISTAN, "tg_TJ" },
  443. { LANG_TAMIL, SUBLANG_NEUTRAL, "ta" },
  444. { LANG_TAMIL, SUBLANG_TAMIL_INDIA, "ta_IN" },
  445. { LANG_TATAR, SUBLANG_NEUTRAL, "tt" },
  446. { LANG_TATAR, SUBLANG_TATAR_RUSSIA, "tt_TA" },
  447. { LANG_TELUGU, SUBLANG_NEUTRAL, "te" },
  448. { LANG_TELUGU, SUBLANG_TELUGU_INDIA, "te_IN" },
  449. { LANG_THAI, SUBLANG_NEUTRAL, "th" },
  450. { LANG_THAI, SUBLANG_THAI_THAILAND, "th_TH" },
  451. { LANG_TIGRINYA, SUBLANG_NEUTRAL, "ti" },
  452. { LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA, "ti_ET" },
  453. { LANG_TIGRINYA, SUBLANG_TIGRINYA_ERITREA, "ti_ER" },
  454. { LANG_TSWANA, SUBLANG_NEUTRAL, "tn" },
  455. { LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA, "tn_ZA" },
  456. { LANG_TURKISH, SUBLANG_NEUTRAL, "tr" },
  457. { LANG_TURKISH, SUBLANG_TURKISH_TURKEY, "tr_TR" },
  458. { LANG_UIGHUR, SUBLANG_NEUTRAL, "ug" },
  459. { LANG_UIGHUR, SUBLANG_UIGHUR_PRC, "ug_CN" },
  460. { LANG_UKRAINIAN, SUBLANG_NEUTRAL, "uk" },
  461. { LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE, "uk_UA" },
  462. { LANG_URDU, SUBLANG_NEUTRAL, "ur" },
  463. { LANG_URDU, SUBLANG_URDU_PAKISTAN, "ur_PK" },
  464. { LANG_URDU, SUBLANG_URDU_INDIA, "ur_IN" },
  465. { LANG_UZBEK, SUBLANG_NEUTRAL, "uz" },
  466. { LANG_UZBEK, SUBLANG_UZBEK_LATIN, "uz_UZ@latin" },
  467. { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, "uz_UZ@cyrillic" },
  468. { LANG_VIETNAMESE, SUBLANG_NEUTRAL, "vi" },
  469. { LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM, "vi_VN" },
  470. { LANG_WELSH, SUBLANG_NEUTRAL, "cy" },
  471. { LANG_WELSH, SUBLANG_WELSH_UNITED_KINGDOM, "cy_GB" },
  472. { LANG_WOLOF, SUBLANG_NEUTRAL, "wo" },
  473. { LANG_WOLOF, SUBLANG_WOLOF_SENEGAL, "wo_SN" },
  474. { LANG_XHOSA, SUBLANG_NEUTRAL, "xh" },
  475. { LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA, "xh_ZA" },
  476. { LANG_YORUBA, SUBLANG_NEUTRAL, "yo" },
  477. { LANG_YORUBA, SUBLANG_YORUBA_NIGERIA, "yo_NG" },
  478. { LANG_ZULU, SUBLANG_NEUTRAL, "zu" },
  479. { LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA, "zu_ZA" },
  480. #ifdef LANG_ESPERANTO
  481. { LANG_ESPERANTO, SUBLANG_DEFAULT, "eo" },
  482. #endif
  483. #ifdef LANG_WALON
  484. { LANG_WALON, SUBLANG_NEUTRAL, "wa" },
  485. { LANG_WALON, SUBLANG_DEFAULT, "wa_BE" },
  486. #endif
  487. #ifdef LANG_CORNISH
  488. { LANG_CORNISH, SUBLANG_NEUTRAL, "kw" },
  489. { LANG_CORNISH, SUBLANG_DEFAULT, "kw_GB" },
  490. #endif
  491. #ifdef LANG_MANX_GAELIC
  492. { LANG_MANX_GAELIC, SUBLANG_MANX_GAELIC, "gv_GB" },
  493. #endif
  494. };
  495. #ifndef HAVE_LIBGETTEXTPO
  496. void write_pot_file( const char *outname )
  497. {
  498. error( "PO files not supported in this wrc build\n" );
  499. }
  500. void write_po_files( const char *outname )
  501. {
  502. error( "PO files not supported in this wrc build\n" );
  503. }
  504. #else /* HAVE_LIBGETTEXTPO */
  505. static void po_xerror( int severity, po_message_t message,
  506. const char *filename, size_t lineno, size_t column,
  507. int multiline_p, const char *message_text )
  508. {
  509. fprintf( stderr, "%s:%u:%u: %s\n",
  510. filename, (unsigned int)lineno, (unsigned int)column, message_text );
  511. if (severity) exit(1);
  512. }
  513. static void po_xerror2( int severity, po_message_t message1,
  514. const char *filename1, size_t lineno1, size_t column1,
  515. int multiline_p1, const char *message_text1,
  516. po_message_t message2,
  517. const char *filename2, size_t lineno2, size_t column2,
  518. int multiline_p2, const char *message_text2 )
  519. {
  520. fprintf( stderr, "%s:%u:%u: %s\n",
  521. filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
  522. fprintf( stderr, "%s:%u:%u: %s\n",
  523. filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
  524. if (severity) exit(1);
  525. }
  526. static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
  527. static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
  528. po_message_iterator_t *iterator )
  529. {
  530. po_message_t msg;
  531. const char *context;
  532. *iterator = po_message_iterator( po, NULL );
  533. while ((msg = po_next_message( *iterator )))
  534. {
  535. if (strcmp( po_message_msgid( msg ), msgid )) continue;
  536. if (!msgctxt) break;
  537. if (!(context = po_message_msgctxt( msg ))) continue;
  538. if (!strcmp( context, msgctxt )) break;
  539. }
  540. return msg;
  541. }
  542. static void add_po_string( po_file_t po, const string_t *msgid, const string_t *msgstr,
  543. const language_t *lang )
  544. {
  545. static const char dnt[] = "do not translate";
  546. po_message_t msg;
  547. po_message_iterator_t iterator;
  548. int codepage;
  549. char *id, *id_buffer, *context, *str = NULL, *str_buffer = NULL;
  550. if (!msgid->size) return;
  551. id_buffer = id = convert_msgid_ascii( msgid, 1 );
  552. context = get_message_context( &id );
  553. if (context && strcmp(context, dnt) == 0)
  554. {
  555. /* This string should not be translated */
  556. free( id_buffer );
  557. return;
  558. }
  559. if (msgstr)
  560. {
  561. if (lang) codepage = get_language_codepage( lang->id, lang->sub );
  562. else codepage = get_language_codepage( 0, 0 );
  563. assert( codepage != -1 );
  564. str = str_buffer = convert_string_utf8( msgstr, codepage );
  565. if (is_english( lang )) get_message_context( &str );
  566. }
  567. if (!(msg = find_message( po, id, context, &iterator )))
  568. {
  569. msg = po_message_create();
  570. po_message_set_msgid( msg, id );
  571. po_message_set_msgstr( msg, str ? str : "" );
  572. if (context) po_message_set_msgctxt( msg, context );
  573. po_message_insert( iterator, msg );
  574. }
  575. if (msgid->loc.file) po_message_add_filepos( msg, msgid->loc.file, msgid->loc.line );
  576. po_message_iterator_free( iterator );
  577. free( id_buffer );
  578. free( str_buffer );
  579. }
  580. struct po_file_lang
  581. {
  582. struct list entry;
  583. language_t lang;
  584. po_file_t po;
  585. };
  586. static struct list po_file_langs = LIST_INIT( po_file_langs );
  587. static po_file_t create_po_file(void)
  588. {
  589. po_file_t po;
  590. po_message_t msg;
  591. po_message_iterator_t iterator;
  592. po = po_file_create();
  593. iterator = po_message_iterator( po, NULL );
  594. msg = po_message_create();
  595. po_message_set_msgid( msg, "" );
  596. po_message_set_msgstr( msg,
  597. "Project-Id-Version: Wine\n"
  598. "Report-Msgid-Bugs-To: https://bugs.winehq.org\n"
  599. "POT-Creation-Date: N/A\n"
  600. "PO-Revision-Date: N/A\n"
  601. "Last-Translator: Automatically generated\n"
  602. "Language-Team: none\n"
  603. "MIME-Version: 1.0\n"
  604. "Content-Type: text/plain; charset=UTF-8\n"
  605. "Content-Transfer-Encoding: 8bit\n" );
  606. po_message_insert( iterator, msg );
  607. po_message_iterator_free( iterator );
  608. return po;
  609. }
  610. static po_file_t get_po_file( const language_t *lang )
  611. {
  612. struct po_file_lang *po_file;
  613. LIST_FOR_EACH_ENTRY( po_file, &po_file_langs, struct po_file_lang, entry )
  614. if (po_file->lang.id == lang->id && po_file->lang.sub == lang->sub) return po_file->po;
  615. /* create a new one */
  616. po_file = xmalloc( sizeof(*po_file) );
  617. po_file->lang = *lang;
  618. po_file->po = create_po_file();
  619. list_add_tail( &po_file_langs, &po_file->entry );
  620. return po_file->po;
  621. }
  622. static const char *get_language_name( const language_t *lang )
  623. {
  624. static char name[20];
  625. unsigned int i;
  626. for (i = 0; i < ARRAY_SIZE(languages); i++)
  627. if (languages[i].id == lang->id && languages[i].sub == lang->sub)
  628. return languages[i].name;
  629. sprintf( name, "%02x-%02x", lang->id, lang->sub );
  630. return name;
  631. }
  632. static char *get_po_file_name( const language_t *lang )
  633. {
  634. return strmake( "%s.po", get_language_name( lang ) );
  635. }
  636. static unsigned int flush_po_files( const char *output_name )
  637. {
  638. struct po_file_lang *po_file, *next;
  639. unsigned int count = 0;
  640. LIST_FOR_EACH_ENTRY_SAFE( po_file, next, &po_file_langs, struct po_file_lang, entry )
  641. {
  642. char *name = get_po_file_name( &po_file->lang );
  643. if (output_name)
  644. {
  645. const char *p = strrchr( output_name, '/' );
  646. if (p) p++;
  647. else p = output_name;
  648. if (!strcmp( p, name ))
  649. {
  650. po_file_write( po_file->po, name, &po_xerror_handler );
  651. count++;
  652. }
  653. }
  654. else /* no specified output name, output a file for every language found */
  655. {
  656. po_file_write( po_file->po, name, &po_xerror_handler );
  657. count++;
  658. fprintf( stderr, "created %s\n", name );
  659. }
  660. po_file_free( po_file->po );
  661. list_remove( &po_file->entry );
  662. free( po_file );
  663. free( name );
  664. }
  665. return count;
  666. }
  667. static void add_pot_stringtable( po_file_t po, const resource_t *res )
  668. {
  669. const stringtable_t *stt = res->res.stt;
  670. int i;
  671. while (stt)
  672. {
  673. for (i = 0; i < stt->nentries; i++)
  674. if (stt->entries[i].str) add_po_string( po, stt->entries[i].str, NULL, NULL );
  675. stt = stt->next;
  676. }
  677. }
  678. static void add_po_stringtable( const resource_t *english, const resource_t *res )
  679. {
  680. const stringtable_t *english_stt = english->res.stt;
  681. const stringtable_t *stt = res->res.stt;
  682. po_file_t po = get_po_file( stt->lvc.language );
  683. int i;
  684. while (english_stt && stt)
  685. {
  686. for (i = 0; i < stt->nentries; i++)
  687. if (english_stt->entries[i].str && stt->entries[i].str)
  688. add_po_string( po, english_stt->entries[i].str, stt->entries[i].str, stt->lvc.language );
  689. stt = stt->next;
  690. english_stt = english_stt->next;
  691. }
  692. }
  693. static void add_pot_dialog_controls( po_file_t po, const control_t *ctrl )
  694. {
  695. while (ctrl)
  696. {
  697. if (control_has_title( ctrl )) add_po_string( po, ctrl->title->name.s_name, NULL, NULL );
  698. ctrl = ctrl->next;
  699. }
  700. }
  701. static void add_pot_dialog( po_file_t po, const resource_t *res )
  702. {
  703. const dialog_t *dlg = res->res.dlg;
  704. if (dlg->title) add_po_string( po, dlg->title, NULL, NULL );
  705. add_pot_dialog_controls( po, dlg->controls );
  706. }
  707. static void compare_dialogs( const dialog_t *english_dlg, const dialog_t *dlg )
  708. {
  709. const control_t *english_ctrl, *ctrl;
  710. unsigned int style = 0, exstyle = 0, english_style = 0, english_exstyle = 0;
  711. char *name;
  712. char *title = english_dlg->title ? convert_msgid_ascii( english_dlg->title, 0 ) : xstrdup("??");
  713. if (english_dlg->width != dlg->width || english_dlg->height != dlg->height)
  714. warning( "%s: dialog %s doesn't have the same size (%d,%d vs %d,%d)\n",
  715. get_language_name( dlg->lvc.language ), title, dlg->width, dlg->height,
  716. english_dlg->width, english_dlg->height );
  717. if (dlg->gotstyle) style = dlg->style->or_mask;
  718. if (dlg->gotexstyle) exstyle = dlg->exstyle->or_mask;
  719. if (english_dlg->gotstyle) english_style = english_dlg->style->or_mask;
  720. if (english_dlg->gotexstyle) english_exstyle = english_dlg->exstyle->or_mask;
  721. if (is_rtl_language( dlg->lvc.language )) english_exstyle |= WS_EX_LAYOUTRTL;
  722. if (english_style != style)
  723. warning( "%s: dialog %s doesn't have the same style (%08x vs %08x)\n",
  724. get_language_name( dlg->lvc.language ), title, style, english_style );
  725. if (english_exstyle != exstyle)
  726. warning( "%s: dialog %s doesn't have the same exstyle (%08x vs %08x)\n",
  727. get_language_name( dlg->lvc.language ), title, exstyle, english_exstyle );
  728. if (english_dlg->font || dlg->font)
  729. {
  730. int size = 0, english_size = 0;
  731. char *font = NULL, *english_font = NULL;
  732. if (english_dlg->font)
  733. {
  734. english_font = convert_msgid_ascii( english_dlg->font->name, 0 );
  735. english_size = english_dlg->font->size;
  736. }
  737. if (dlg->font)
  738. {
  739. font = convert_msgid_ascii( dlg->font->name, 0 );
  740. size = dlg->font->size;
  741. }
  742. if (uses_larger_font( dlg->lvc.language )) english_size++;
  743. if (!english_font || !font || strcasecmp( english_font, font ) || english_size != size)
  744. warning( "%s: dialog %s doesn't have the same font (%s %u vs %s %u)\n",
  745. get_language_name(dlg->lvc.language), title,
  746. english_font ? english_font : "default", english_size,
  747. font ? font : "default", size );
  748. free( font );
  749. free( english_font );
  750. }
  751. english_ctrl = english_dlg->controls;
  752. ctrl = dlg->controls;
  753. for ( ; english_ctrl && ctrl; ctrl = ctrl->next, english_ctrl = english_ctrl->next )
  754. {
  755. if (control_has_title( english_ctrl ))
  756. name = convert_msgid_ascii( english_ctrl->title->name.s_name, 0 );
  757. else
  758. name = strmake( "%d", ctrl->id );
  759. if (english_ctrl->width != ctrl->width || english_ctrl->height != ctrl->height)
  760. warning( "%s: dialog %s control %s doesn't have the same size (%d,%d vs %d,%d)\n",
  761. get_language_name( dlg->lvc.language ), title, name,
  762. ctrl->width, ctrl->height, english_ctrl->width, english_ctrl->height );
  763. if (english_ctrl->x != ctrl->x || english_ctrl->y != ctrl->y)
  764. warning( "%s: dialog %s control %s doesn't have the same position (%d,%d vs %d,%d)\n",
  765. get_language_name( dlg->lvc.language ), title, name,
  766. ctrl->x, ctrl->y, english_ctrl->x, english_ctrl->y );
  767. free( name );
  768. }
  769. free( title );
  770. }
  771. static void add_po_dialog_controls( po_file_t po, const control_t *english_ctrl,
  772. const control_t *ctrl, const language_t *lang )
  773. {
  774. while (english_ctrl && ctrl)
  775. {
  776. if (control_has_title( english_ctrl ) && control_has_title( ctrl ))
  777. add_po_string( po, english_ctrl->title->name.s_name, ctrl->title->name.s_name, lang );
  778. ctrl = ctrl->next;
  779. english_ctrl = english_ctrl->next;
  780. }
  781. }
  782. static void add_po_dialog( const resource_t *english, const resource_t *res )
  783. {
  784. const dialog_t *english_dlg = english->res.dlg;
  785. const dialog_t *dlg = res->res.dlg;
  786. po_file_t po = get_po_file( dlg->lvc.language );
  787. compare_dialogs( english_dlg, dlg );
  788. if (english_dlg->title && dlg->title)
  789. add_po_string( po, english_dlg->title, dlg->title, dlg->lvc.language );
  790. add_po_dialog_controls( po, english_dlg->controls, dlg->controls, dlg->lvc.language );
  791. }
  792. static void add_pot_menu_items( po_file_t po, const menu_item_t *item )
  793. {
  794. while (item)
  795. {
  796. if (item->name) add_po_string( po, item->name, NULL, NULL );
  797. if (item->popup) add_pot_menu_items( po, item->popup );
  798. item = item->next;
  799. }
  800. }
  801. static void add_pot_menu( po_file_t po, const resource_t *res )
  802. {
  803. add_pot_menu_items( po, res->res.men->items );
  804. }
  805. static void add_po_menu_items( po_file_t po, const menu_item_t *english_item,
  806. const menu_item_t *item, const language_t *lang )
  807. {
  808. while (english_item && item)
  809. {
  810. if (english_item->name && item->name)
  811. add_po_string( po, english_item->name, item->name, lang );
  812. if (english_item->popup && item->popup)
  813. add_po_menu_items( po, english_item->popup, item->popup, lang );
  814. item = item->next;
  815. english_item = english_item->next;
  816. }
  817. }
  818. static void add_po_menu( const resource_t *english, const resource_t *res )
  819. {
  820. const menu_item_t *english_items = english->res.men->items;
  821. const menu_item_t *items = res->res.men->items;
  822. po_file_t po = get_po_file( res->res.men->lvc.language );
  823. add_po_menu_items( po, english_items, items, res->res.men->lvc.language );
  824. }
  825. static BOOL string_has_context( const string_t *str )
  826. {
  827. char *id, *id_buffer, *context;
  828. id_buffer = id = convert_msgid_ascii( str, 1 );
  829. context = get_message_context( &id );
  830. free( id_buffer );
  831. return context != NULL;
  832. }
  833. static void add_pot_accel( po_file_t po, const resource_t *res )
  834. {
  835. event_t *event = res->res.acc->events;
  836. while (event)
  837. {
  838. /* accelerators without a context don't make sense in po files */
  839. if (event->str && string_has_context( event->str ))
  840. add_po_string( po, event->str, NULL, NULL );
  841. event = event->next;
  842. }
  843. }
  844. static void add_po_accel( const resource_t *english, const resource_t *res )
  845. {
  846. event_t *english_event = english->res.acc->events;
  847. event_t *event = res->res.acc->events;
  848. po_file_t po = get_po_file( res->res.acc->lvc.language );
  849. while (english_event && event)
  850. {
  851. if (english_event->str && event->str && string_has_context( english_event->str ))
  852. add_po_string( po, english_event->str, event->str, res->res.acc->lvc.language );
  853. event = event->next;
  854. english_event = english_event->next;
  855. }
  856. }
  857. static ver_block_t *get_version_langcharset_block( ver_block_t *block )
  858. {
  859. ver_block_t *stringfileinfo = NULL;
  860. char *translation = NULL;
  861. ver_value_t *val;
  862. for (; block; block = block->next)
  863. {
  864. char *name;
  865. name = convert_msgid_ascii( block->name, 0 );
  866. if (!strcasecmp( name, "stringfileinfo" ))
  867. stringfileinfo = block;
  868. else if (!strcasecmp( name, "varfileinfo" ))
  869. {
  870. for (val = block->values; val; val = val->next)
  871. {
  872. char *key = convert_msgid_ascii( val->key, 0 );
  873. if (val->type == val_words &&
  874. !strcasecmp( key, "Translation" ) &&
  875. val->value.words->nwords >= 2)
  876. translation = strmake( "%04x%04x",
  877. val->value.words->words[0],
  878. val->value.words->words[1] );
  879. free( key );
  880. }
  881. }
  882. free( name );
  883. }
  884. if (!stringfileinfo || !translation) return NULL;
  885. for (val = stringfileinfo->values; val; val = val->next)
  886. {
  887. char *block_name;
  888. if (val->type != val_block) continue;
  889. block_name = convert_msgid_ascii( val->value.block->name, 0 );
  890. if (!strcasecmp( block_name, translation ))
  891. {
  892. free( block_name );
  893. free( translation );
  894. return val->value.block;
  895. }
  896. free( block_name );
  897. }
  898. free( translation );
  899. return NULL;
  900. }
  901. static int version_value_needs_translation( const ver_value_t *val )
  902. {
  903. int ret;
  904. char *key;
  905. if (val->type != val_str) return 0;
  906. if (!(key = convert_msgid_ascii( val->key, 0 ))) return 0;
  907. /* most values contain version numbers or file names, only translate a few specific ones */
  908. ret = (!strcasecmp( key, "FileDescription" ) || !strcasecmp( key, "ProductName" ));
  909. free( key );
  910. return ret;
  911. }
  912. static void add_pot_versioninfo( po_file_t po, const resource_t *res )
  913. {
  914. ver_value_t *val;
  915. ver_block_t *langcharset = get_version_langcharset_block( res->res.ver->blocks );
  916. if (!langcharset) return;
  917. for (val = langcharset->values; val; val = val->next)
  918. if (version_value_needs_translation( val ))
  919. add_po_string( po, val->value.str, NULL, NULL );
  920. }
  921. static void add_po_versioninfo( const resource_t *english, const resource_t *res )
  922. {
  923. const ver_block_t *langcharset = get_version_langcharset_block( res->res.ver->blocks );
  924. const ver_block_t *english_langcharset = get_version_langcharset_block( english->res.ver->blocks );
  925. ver_value_t *val, *english_val;
  926. po_file_t po = get_po_file( res->res.ver->lvc.language );
  927. if (!langcharset && !english_langcharset) return;
  928. val = langcharset->values;
  929. english_val = english_langcharset->values;
  930. while (english_val && val)
  931. {
  932. if (val->type == val_str)
  933. add_po_string( po, english_val->value.str, val->value.str, res->res.ver->lvc.language );
  934. val = val->next;
  935. english_val = english_val->next;
  936. }
  937. }
  938. static resource_t *find_english_resource( resource_t *res )
  939. {
  940. resource_t *ptr;
  941. for (ptr = resource_top; ptr; ptr = ptr->next)
  942. {
  943. if (ptr->type != res->type) continue;
  944. if (!ptr->lan) continue;
  945. if (!is_english( ptr->lan )) continue;
  946. if (compare_name_id( ptr->name, res->name )) continue;
  947. return ptr;
  948. }
  949. return NULL;
  950. }
  951. void write_pot_file( const char *outname )
  952. {
  953. resource_t *res;
  954. po_file_t po = create_po_file();
  955. for (res = resource_top; res; res = res->next)
  956. {
  957. if (!is_english( res->lan )) continue;
  958. switch (res->type)
  959. {
  960. case res_acc: add_pot_accel( po, res ); break;
  961. case res_dlg: add_pot_dialog( po, res ); break;
  962. case res_men: add_pot_menu( po, res ); break;
  963. case res_stt: add_pot_stringtable( po, res ); break;
  964. case res_ver: add_pot_versioninfo( po, res ); break;
  965. case res_msg: break; /* FIXME */
  966. default: break;
  967. }
  968. }
  969. po_file_write( po, outname, &po_xerror_handler );
  970. po_file_free( po );
  971. }
  972. void write_po_files( const char *outname )
  973. {
  974. resource_t *res, *english;
  975. for (res = resource_top; res; res = res->next)
  976. {
  977. if (!(english = find_english_resource( res ))) continue;
  978. switch (res->type)
  979. {
  980. case res_acc: add_po_accel( english, res ); break;
  981. case res_dlg: add_po_dialog( english, res ); break;
  982. case res_men: add_po_menu( english, res ); break;
  983. case res_stt: add_po_stringtable( english, res ); break;
  984. case res_ver: add_po_versioninfo( english, res ); break;
  985. case res_msg: break; /* FIXME */
  986. default: break;
  987. }
  988. }
  989. if (!flush_po_files( outname ))
  990. {
  991. if (outname) error( "No translations found for %s\n", outname );
  992. else error( "No translations found\n" );
  993. }
  994. }
  995. #endif /* HAVE_LIBGETTEXTPO */
  996. static struct mo_file *mo_file;
  997. static void byteswap( unsigned int *data, unsigned int count )
  998. {
  999. unsigned int i;
  1000. for (i = 0; i < count; i++)
  1001. data[i] = data[i] >> 24 | (data[i] >> 8 & 0xff00) | (data[i] << 8 & 0xff0000) | data[i] << 24;
  1002. }
  1003. static void load_mo_file( const char *name )
  1004. {
  1005. struct stat st;
  1006. int res, fd;
  1007. fd = open( name, O_RDONLY | O_BINARY );
  1008. if (fd == -1) fatal_perror( "Failed to open %s", name );
  1009. fstat( fd, &st );
  1010. mo_file = xmalloc( st.st_size );
  1011. res = read( fd, mo_file, st.st_size );
  1012. if (res == -1) fatal_perror( "Failed to read %s", name );
  1013. else if (res != st.st_size) error( "Failed to read %s\n", name );
  1014. close( fd );
  1015. /* sanity checks */
  1016. if (st.st_size < sizeof(*mo_file))
  1017. error( "%s is not a valid .mo file\n", name );
  1018. if (mo_file->magic == 0xde120495)
  1019. byteswap( &mo_file->revision, 4 );
  1020. else if (mo_file->magic != 0x950412de)
  1021. error( "%s is not a valid .mo file\n", name );
  1022. if ((mo_file->revision >> 16) > 1)
  1023. error( "%s: unsupported file version %x\n", name, mo_file->revision );
  1024. if (mo_file->msgid_off >= st.st_size ||
  1025. mo_file->msgstr_off >= st.st_size ||
  1026. st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count)
  1027. error( "%s: corrupted file\n", name );
  1028. if (mo_file->magic == 0xde120495)
  1029. {
  1030. byteswap( (unsigned int *)((char *)mo_file + mo_file->msgid_off), 2 * mo_file->count );
  1031. byteswap( (unsigned int *)((char *)mo_file + mo_file->msgstr_off), 2 * mo_file->count );
  1032. }
  1033. }
  1034. static void free_mo_file(void)
  1035. {
  1036. free( mo_file );
  1037. mo_file = NULL;
  1038. }
  1039. static inline const char *get_mo_msgid( int index )
  1040. {
  1041. const char *base = (const char *)mo_file;
  1042. const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgid_off);
  1043. return base + offsets[2 * index + 1];
  1044. }
  1045. static inline const char *get_mo_msgstr( int index )
  1046. {
  1047. const char *base = (const char *)mo_file;
  1048. const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgstr_off);
  1049. return base + offsets[2 * index + 1];
  1050. }
  1051. static const char *get_msgstr( const char *msgid, const char *context, int *found )
  1052. {
  1053. int pos, res, min, max;
  1054. const char *ret = msgid;
  1055. char *id = NULL;
  1056. if (!mo_file) /* strings containing a context still need to be transformed */
  1057. {
  1058. if (context) (*found)++;
  1059. return ret;
  1060. }
  1061. if (context) id = strmake( "%s%c%s", context, 4, msgid );
  1062. min = 0;
  1063. max = mo_file->count - 1;
  1064. while (min <= max)
  1065. {
  1066. pos = (min + max) / 2;
  1067. res = strcmp( get_mo_msgid(pos), id ? id : msgid );
  1068. if (!res)
  1069. {
  1070. const char *str = get_mo_msgstr( pos );
  1071. if (str[0]) /* ignore empty strings */
  1072. {
  1073. ret = str;
  1074. (*found)++;
  1075. }
  1076. break;
  1077. }
  1078. if (res > 0) max = pos - 1;
  1079. else min = pos + 1;
  1080. }
  1081. free( id );
  1082. return ret;
  1083. }
  1084. static string_t *translate_string( string_t *str, int *found )
  1085. {
  1086. string_t ustr, *new;
  1087. const char *transl;
  1088. char *buffer, *msgid, *context;
  1089. if (!str->size || !(buffer = convert_msgid_ascii( str, 0 )))
  1090. return convert_string_unicode( str, 1252 );
  1091. msgid = buffer;
  1092. context = get_message_context( &msgid );
  1093. transl = get_msgstr( msgid, context, found );
  1094. ustr.type = str_char;
  1095. ustr.size = strlen( transl );
  1096. ustr.str.cstr = (char *)transl;
  1097. ustr.loc = str->loc;
  1098. new = convert_string_unicode( &ustr, CP_UTF8 );
  1099. free( buffer );
  1100. return new;
  1101. }
  1102. static control_t *translate_controls( control_t *ctrl, int *found )
  1103. {
  1104. control_t *new, *head = NULL, *tail = NULL;
  1105. while (ctrl)
  1106. {
  1107. new = xmalloc( sizeof(*new) );
  1108. *new = *ctrl;
  1109. if (control_has_title( ctrl ))
  1110. {
  1111. new->title = new_name_id();
  1112. *new->title = *ctrl->title;
  1113. new->title->name.s_name = translate_string( ctrl->title->name.s_name, found );
  1114. }
  1115. else new->title = dup_name_id( ctrl->title );
  1116. new->ctlclass = dup_name_id( ctrl->ctlclass );
  1117. if (tail) tail->next = new;
  1118. else head = new;
  1119. new->next = NULL;
  1120. new->prev = tail;
  1121. tail = new;
  1122. ctrl = ctrl->next;
  1123. }
  1124. return head;
  1125. }
  1126. static menu_item_t *translate_items( menu_item_t *item, int *found )
  1127. {
  1128. menu_item_t *new, *head = NULL, *tail = NULL;
  1129. while (item)
  1130. {
  1131. new = xmalloc( sizeof(*new) );
  1132. *new = *item;
  1133. if (item->name) new->name = translate_string( item->name, found );
  1134. if (item->popup) new->popup = translate_items( item->popup, found );
  1135. if (tail) tail->next = new;
  1136. else head = new;
  1137. new->next = NULL;
  1138. new->prev = tail;
  1139. tail = new;
  1140. item = item->next;
  1141. }
  1142. return head;
  1143. }
  1144. static stringtable_t *translate_stringtable( stringtable_t *stt, language_t *lang, int *found )
  1145. {
  1146. stringtable_t *new, *head = NULL, *tail = NULL;
  1147. int i;
  1148. while (stt)
  1149. {
  1150. new = xmalloc( sizeof(*new) );
  1151. *new = *stt;
  1152. new->lvc.language = lang;
  1153. new->lvc.version = get_dup_version( lang );
  1154. new->entries = xmalloc( new->nentries * sizeof(*new->entries) );
  1155. memcpy( new->entries, stt->entries, new->nentries * sizeof(*new->entries) );
  1156. for (i = 0; i < stt->nentries; i++)
  1157. if (stt->entries[i].str)
  1158. new->entries[i].str = translate_string( stt->entries[i].str, found );
  1159. if (tail) tail->next = new;
  1160. else head = new;
  1161. new->next = NULL;
  1162. new->prev = tail;
  1163. tail = new;
  1164. stt = stt->next;
  1165. }
  1166. return head;
  1167. }
  1168. static void translate_dialog( dialog_t *dlg, dialog_t *new, int *found )
  1169. {
  1170. if (dlg->title) new->title = translate_string( dlg->title, found );
  1171. if (is_rtl_language( new->lvc.language ))
  1172. {
  1173. new->gotexstyle = TRUE;
  1174. if (dlg->gotexstyle)
  1175. new->exstyle = new_style( dlg->exstyle->or_mask | WS_EX_LAYOUTRTL, dlg->exstyle->and_mask );
  1176. else
  1177. new->exstyle = new_style( WS_EX_LAYOUTRTL, 0 );
  1178. }
  1179. if (dlg->font)
  1180. {
  1181. new->font = xmalloc( sizeof(*dlg->font) );
  1182. *new->font = *dlg->font;
  1183. if (uses_larger_font( new->lvc.language )) new->font->size++;
  1184. new->font->name = convert_string_unicode( dlg->font->name, 1252 );
  1185. }
  1186. new->controls = translate_controls( dlg->controls, found );
  1187. }
  1188. static event_t *translate_accel( accelerator_t *acc, accelerator_t *new, int *found )
  1189. {
  1190. event_t *event, *new_ev, *head = NULL, *tail = NULL;
  1191. event = acc->events;
  1192. while (event)
  1193. {
  1194. new_ev = new_event();
  1195. *new_ev = *event;
  1196. if (event->str) new_ev->str = translate_string( event->str, found );
  1197. if (tail) tail->next = new_ev;
  1198. else head = new_ev;
  1199. new_ev->next = NULL;
  1200. new_ev->prev = tail;
  1201. tail = new_ev;
  1202. event = event->next;
  1203. }
  1204. return head;
  1205. }
  1206. static ver_value_t *translate_langcharset_values( ver_value_t *val, language_t *lang, int *found )
  1207. {
  1208. ver_value_t *new_val, *head = NULL, *tail = NULL;
  1209. while (val)
  1210. {
  1211. new_val = new_ver_value();
  1212. *new_val = *val;
  1213. if (val->type == val_str)
  1214. new_val->value.str = translate_string( val->value.str, found );
  1215. if (tail) tail->next = new_val;
  1216. else head = new_val;
  1217. new_val->next = NULL;
  1218. new_val->prev = tail;
  1219. tail = new_val;
  1220. val = val->next;
  1221. }
  1222. return head;
  1223. }
  1224. static ver_value_t *translate_stringfileinfo( ver_value_t *val, language_t *lang, int *found )
  1225. {
  1226. int i;
  1227. ver_value_t *new_val, *head = NULL, *tail = NULL;
  1228. const char *english_block_name[2] = { "040904b0", "040904e4" };
  1229. char *block_name[2];
  1230. LANGID langid = MAKELANGID( lang->id, get_default_sublang( lang ) );
  1231. block_name[0] = strmake( "%04x%04x", langid, 1200 );
  1232. block_name[1] = strmake( "%04x%04x", langid, get_language_codepage( lang->id, lang->sub ) );
  1233. while (val)
  1234. {
  1235. new_val = new_ver_value();
  1236. *new_val = *val;
  1237. if (val->type == val_block)
  1238. {
  1239. ver_block_t *blk, *blk_head = NULL, *blk_tail = NULL;
  1240. for (blk = val->value.block; blk; blk = blk->next)
  1241. {
  1242. ver_block_t *new_blk;
  1243. char *name;
  1244. new_blk = new_ver_block();
  1245. *new_blk = *blk;
  1246. name = convert_msgid_ascii( blk->name, 0 );
  1247. for (i = 0; i < ARRAY_SIZE(block_name); i++)
  1248. {
  1249. if (!strcasecmp( name, english_block_name[i] ))
  1250. {
  1251. string_t str;
  1252. str.type = str_char;
  1253. str.size = strlen( block_name[i] ) + 1;
  1254. str.str.cstr = block_name[i];
  1255. str.loc = blk->name->loc;
  1256. new_blk->name = convert_string_unicode( &str, CP_UTF8 );
  1257. new_blk->values = translate_langcharset_values( blk->values, lang, found );
  1258. }
  1259. }
  1260. free( name );
  1261. if (blk_tail) blk_tail->next = new_blk;
  1262. else blk_head = new_blk;
  1263. new_blk->next = NULL;
  1264. new_blk->prev = blk_tail;
  1265. blk_tail = new_blk;
  1266. }
  1267. new_val->value.block = blk_head;
  1268. }
  1269. if (tail) tail->next = new_val;
  1270. else head = new_val;
  1271. new_val->next = NULL;
  1272. new_val->prev = tail;
  1273. tail = new_val;
  1274. val = val->next;
  1275. }
  1276. for (i = 0; i < ARRAY_SIZE(block_name); i++)
  1277. free( block_name[i] );
  1278. return head;
  1279. }
  1280. static ver_value_t *translate_varfileinfo( ver_value_t *val, language_t *lang )
  1281. {
  1282. ver_value_t *new_val, *head = NULL, *tail = NULL;
  1283. while (val)
  1284. {
  1285. new_val = new_ver_value();
  1286. *new_val = *val;
  1287. if (val->type == val_words)
  1288. {
  1289. char *key = convert_msgid_ascii( val->key, 0 );
  1290. if (!strcasecmp( key, "Translation" ) &&
  1291. val->value.words->nwords == 2 &&
  1292. val->value.words->words[0] == MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ))
  1293. {
  1294. ver_words_t *new_words;
  1295. LANGID langid;
  1296. WORD codepage;
  1297. langid = MAKELANGID( lang->id, get_default_sublang( lang ) );
  1298. new_words = new_ver_words( langid );
  1299. if (val->value.words->words[1] == 1200)
  1300. codepage = 1200;
  1301. else
  1302. codepage = get_language_codepage( lang->id, lang->sub );
  1303. new_val->value.words = add_ver_words( new_words, codepage );
  1304. }
  1305. free( key );
  1306. }
  1307. if (tail) tail->next = new_val;
  1308. else head = new_val;
  1309. new_val->next = NULL;
  1310. new_val->prev = tail;
  1311. tail = new_val;
  1312. val = val->next;
  1313. }
  1314. return head;
  1315. }
  1316. static ver_block_t *translate_versioninfo( ver_block_t *blk, language_t *lang, int *found )
  1317. {
  1318. ver_block_t *new_blk, *head = NULL, *tail = NULL;
  1319. char *name;
  1320. while (blk)
  1321. {
  1322. new_blk = new_ver_block();
  1323. *new_blk = *blk;
  1324. name = convert_msgid_ascii( blk->name, 0 );
  1325. if (!strcasecmp( name, "stringfileinfo" ))
  1326. new_blk->values = translate_stringfileinfo( blk->values, lang, found );
  1327. else if (!strcasecmp( name, "varfileinfo" ))
  1328. new_blk->values = translate_varfileinfo( blk->values, lang );
  1329. free(name);
  1330. if (tail) tail->next = new_blk;
  1331. else head = new_blk;
  1332. new_blk->next = NULL;
  1333. new_blk->prev = tail;
  1334. tail = new_blk;
  1335. blk = blk->next;
  1336. }
  1337. return head;
  1338. }
  1339. static void translate_resources( language_t *lang )
  1340. {
  1341. resource_t *res;
  1342. for (res = resource_top; res; res = res->next)
  1343. {
  1344. resource_t *new = NULL;
  1345. int found = 0;
  1346. if (!is_english( res->lan )) continue;
  1347. switch (res->type)
  1348. {
  1349. case res_acc:
  1350. new = dup_resource( res, lang );
  1351. new->res.acc->events = translate_accel( res->res.acc, new->res.acc, &found );
  1352. break;
  1353. case res_dlg:
  1354. new = dup_resource( res, lang );
  1355. translate_dialog( res->res.dlg, new->res.dlg, &found );
  1356. break;
  1357. case res_men:
  1358. new = dup_resource( res, lang );
  1359. new->res.men->items = translate_items( res->res.men->items, &found );
  1360. break;
  1361. case res_stt:
  1362. new = dup_resource( res, lang );
  1363. new->res.stt = translate_stringtable( res->res.stt, lang, &found );
  1364. break;
  1365. case res_ver:
  1366. new = dup_resource( res, lang );
  1367. new->res.ver->blocks = translate_versioninfo( res->res.ver->blocks, lang, &found );
  1368. break;
  1369. case res_msg:
  1370. /* FIXME */
  1371. break;
  1372. default:
  1373. break;
  1374. }
  1375. if (new && found)
  1376. {
  1377. if (new_tail) new_tail->next = new;
  1378. else new_top = new;
  1379. new->prev = new_tail;
  1380. new_tail = new;
  1381. }
  1382. }
  1383. }
  1384. void add_translations( const char *po_dir )
  1385. {
  1386. resource_t *res;
  1387. char buffer[256];
  1388. char *p, *tok, *name;
  1389. unsigned int i;
  1390. FILE *f;
  1391. /* first check if we have English resources to translate */
  1392. for (res = resource_top; res; res = res->next) if (is_english( res->lan )) break;
  1393. if (!res) return;
  1394. if (!po_dir) /* run through the translation process to remove msg contexts */
  1395. {
  1396. translate_resources( new_language( LANG_ENGLISH, SUBLANG_DEFAULT ));
  1397. goto done;
  1398. }
  1399. new_top = new_tail = NULL;
  1400. name = strmake( "%s/LINGUAS", po_dir );
  1401. if (!(f = fopen( name, "r" )))
  1402. {
  1403. free( name );
  1404. return;
  1405. }
  1406. free( name );
  1407. while (fgets( buffer, sizeof(buffer), f ))
  1408. {
  1409. if ((p = strchr( buffer, '#' ))) *p = 0;
  1410. for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
  1411. {
  1412. for (i = 0; i < ARRAY_SIZE(languages); i++)
  1413. if (!strcmp( tok, languages[i].name )) break;
  1414. if (i == ARRAY_SIZE(languages))
  1415. error( "unknown language '%s'\n", tok );
  1416. name = strmake( "%s/%s.mo", po_dir, tok );
  1417. load_mo_file( name );
  1418. translate_resources( new_language(languages[i].id, languages[i].sub) );
  1419. free_mo_file();
  1420. free( name );
  1421. }
  1422. }
  1423. fclose( f );
  1424. done:
  1425. /* prepend the translated resources to the global list */
  1426. if (new_tail)
  1427. {
  1428. new_tail->next = resource_top;
  1429. resource_top->prev = new_tail;
  1430. resource_top = new_top;
  1431. }
  1432. }