po.c 61.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Support for po files
 *
 * Copyright 2010 Alexandre Julliard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"
22
#include "wine/port.h"
23 24 25 26 27 28 29

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <ctype.h>
30
#ifdef HAVE_LIBGETTEXTPO
31 32 33 34 35 36 37 38 39 40 41 42 43
#include <gettext-po.h>
#endif

#include "wrc.h"
#include "genres.h"
#include "newstruc.h"
#include "utils.h"
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "wine/list.h"

44 45
static resource_t *new_top, *new_tail;

46 47 48 49 50 51 52 53 54 55
struct mo_file
{
    unsigned int magic;
    unsigned int revision;
    unsigned int count;
    unsigned int msgid_off;
    unsigned int msgstr_off;
    /* ... rest of file data here */
};

56
static BOOL is_english( const language_t *lan )
57 58 59 60
{
    return lan->id == LANG_ENGLISH && lan->sub == SUBLANG_DEFAULT;
}

61
static BOOL is_rtl_language( const language_t *lan )
62 63 64 65
{
    return lan->id == LANG_ARABIC || lan->id == LANG_HEBREW || lan->id == LANG_PERSIAN;
}

66
static BOOL uses_larger_font( const language_t *lan )
67 68 69 70
{
    return lan->id == LANG_CHINESE || lan->id == LANG_JAPANESE || lan->id == LANG_KOREAN;
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static WORD get_default_sublang( const language_t *lan )
{
    if (lan->sub != SUBLANG_NEUTRAL)
        return lan->sub;

    switch (lan->id)
    {
    case LANG_SPANISH:
        return SUBLANG_SPANISH_MODERN;
    case LANG_CHINESE:
        return SUBLANG_CHINESE_SIMPLIFIED;
    default:
        return SUBLANG_DEFAULT;
    }
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
static version_t *get_dup_version( language_t *lang )
{
    /* English "translations" take precedence over the original rc contents */
    return new_version( is_english( lang ) ? 1 : -1 );
}

static name_id_t *dup_name_id( name_id_t *id )
{
    name_id_t *new;

    if (!id || id->type != name_str) return id;
    new = new_name_id();
    *new = *id;
    new->name.s_name = convert_string( id->name.s_name, str_unicode, 1252 );
    return new;
}

static char *convert_msgid_ascii( const string_t *str, int error_on_invalid_char )
{
    int i;
    string_t *newstr = convert_string( str, str_unicode, 1252 );
    char *buffer = xmalloc( newstr->size + 1 );

    for (i = 0; i < newstr->size; i++)
    {
        buffer[i] =  newstr->str.wstr[i];
        if (newstr->str.wstr[i] >= 32 && newstr->str.wstr[i] <= 127) continue;
        if (newstr->str.wstr[i] == '\t' || newstr->str.wstr[i] == '\n') continue;
        if (error_on_invalid_char)
        {
            print_location( &newstr->loc );
            error( "Invalid character %04x in source string\n", newstr->str.wstr[i] );
        }
        free( buffer);
        free_string( newstr );
        return NULL;
    }
    buffer[i] = 0;
    free_string( newstr );
    return buffer;
}

static char *get_message_context( char **msgid )
{
    static const char magic[] = "#msgctxt#";
    char *id, *context;

    if (strncmp( *msgid, magic, sizeof(magic) - 1 )) return NULL;
    context = *msgid + sizeof(magic) - 1;
    if (!(id = strchr( context, '#' ))) return NULL;
    *id = 0;
    *msgid = id + 1;
    return context;
}

142
static BOOL control_has_title( const control_t *ctrl )
143
{
144 145
    if (!ctrl->title) return FALSE;
    if (ctrl->title->type != name_str) return FALSE;
146 147 148 149 150 151 152 153
    /* check for text static control */
    if (ctrl->ctlclass && ctrl->ctlclass->type == name_ord && ctrl->ctlclass->name.i_name == CT_STATIC)
    {
        switch (ctrl->style->or_mask & SS_TYPEMASK)
        {
        case SS_LEFT:
        case SS_CENTER:
        case SS_RIGHT:
154
            return TRUE;
155
        default:
156
            return FALSE;
157 158
        }
    }
159
    return TRUE;
160 161 162 163 164 165 166 167 168 169 170 171 172
}

static resource_t *dup_resource( resource_t *res, language_t *lang )
{
    resource_t *new = xmalloc( sizeof(*new) );

    *new = *res;
    new->lan = lang;
    new->next = new->prev = NULL;
    new->name = dup_name_id( res->name );

    switch (res->type)
    {
173 174 175 176 177 178
    case res_acc:
        new->res.acc = xmalloc( sizeof(*(new)->res.acc) );
        *new->res.acc = *res->res.acc;
        new->res.acc->lvc.language = lang;
        new->res.acc->lvc.version = get_dup_version( lang );
        break;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    case res_dlg:
        new->res.dlg = xmalloc( sizeof(*(new)->res.dlg) );
        *new->res.dlg = *res->res.dlg;
        new->res.dlg->lvc.language = lang;
        new->res.dlg->lvc.version = get_dup_version( lang );
        break;
    case res_men:
        new->res.men = xmalloc( sizeof(*(new)->res.men) );
        *new->res.men = *res->res.men;
        new->res.men->lvc.language = lang;
        new->res.men->lvc.version = get_dup_version( lang );
        break;
    case res_stt:
        new->res.stt = xmalloc( sizeof(*(new)->res.stt) );
        *new->res.stt = *res->res.stt;
        new->res.stt->lvc.language = lang;
        new->res.stt->lvc.version = get_dup_version( lang );
        break;
197 198 199 200 201 202
    case res_ver:
        new->res.ver = xmalloc( sizeof(*(new)->res.ver) );
        *new->res.ver = *res->res.ver;
        new->res.ver->lvc.language = lang;
        new->res.ver->lvc.version = get_dup_version( lang );
        break;
203 204 205 206 207
    default:
        assert(0);
    }
    return new;
}
208 209 210 211 212 213 214

static const struct
{
    unsigned int id, sub;
    const char *name;
} languages[] =
{
215 216 217 218 219 220
    { LANG_AFRIKAANS,      SUBLANG_NEUTRAL,                     "af" },
    { LANG_AFRIKAANS,      SUBLANG_AFRIKAANS_SOUTH_AFRICA,      "af_ZA" },
    { LANG_ALBANIAN,       SUBLANG_NEUTRAL,                     "sq" },
    { LANG_ALBANIAN,       SUBLANG_ALBANIAN_ALBANIA,            "sq_AL" },
    { LANG_AMHARIC,        SUBLANG_NEUTRAL,                     "am" },
    { LANG_AMHARIC,        SUBLANG_AMHARIC_ETHIOPIA,            "am_ET" },
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    { LANG_ARABIC,         SUBLANG_NEUTRAL,                     "ar" },
    { LANG_ARABIC,         SUBLANG_ARABIC_SAUDI_ARABIA,         "ar_SA" },
    { LANG_ARABIC,         SUBLANG_ARABIC_IRAQ,                 "ar_IQ" },
    { LANG_ARABIC,         SUBLANG_ARABIC_EGYPT,                "ar_EG" },
    { LANG_ARABIC,         SUBLANG_ARABIC_LIBYA,                "ar_LY" },
    { LANG_ARABIC,         SUBLANG_ARABIC_ALGERIA,              "ar_DZ" },
    { LANG_ARABIC,         SUBLANG_ARABIC_MOROCCO,              "ar_MA" },
    { LANG_ARABIC,         SUBLANG_ARABIC_TUNISIA,              "ar_TN" },
    { LANG_ARABIC,         SUBLANG_ARABIC_OMAN,                 "ar_OM" },
    { LANG_ARABIC,         SUBLANG_ARABIC_YEMEN,                "ar_YE" },
    { LANG_ARABIC,         SUBLANG_ARABIC_SYRIA,                "ar_SY" },
    { LANG_ARABIC,         SUBLANG_ARABIC_JORDAN,               "ar_JO" },
    { LANG_ARABIC,         SUBLANG_ARABIC_LEBANON,              "ar_LB" },
    { LANG_ARABIC,         SUBLANG_ARABIC_KUWAIT,               "ar_KW" },
    { LANG_ARABIC,         SUBLANG_ARABIC_UAE,                  "ar_AE" },
    { LANG_ARABIC,         SUBLANG_ARABIC_BAHRAIN,              "ar_BH" },
    { LANG_ARABIC,         SUBLANG_ARABIC_QATAR,                "ar_QA" },
238 239 240 241
    { LANG_ARMENIAN,       SUBLANG_NEUTRAL,                     "hy" },
    { LANG_ARMENIAN,       SUBLANG_ARMENIAN_ARMENIA,            "hy_AM" },
    { LANG_ASSAMESE,       SUBLANG_NEUTRAL,                     "as" },
    { LANG_ASSAMESE,       SUBLANG_ASSAMESE_INDIA,              "as_IN" },
242 243
    { LANG_ASTURIAN,       SUBLANG_NEUTRAL,                     "ast" },
    { LANG_ASTURIAN,       SUBLANG_DEFAULT,                     "ast_ES" },
244 245 246 247 248 249 250 251 252 253 254 255
    { LANG_AZERBAIJANI,    SUBLANG_NEUTRAL,                     "az" },
    { LANG_AZERBAIJANI,    SUBLANG_AZERBAIJANI_AZERBAIJAN_LATIN,"az_AZ@latin" },
    { LANG_AZERBAIJANI,    SUBLANG_AZERBAIJANI_AZERBAIJAN_CYRILLIC, "az_AZ@cyrillic" },
    { LANG_BASQUE,         SUBLANG_NEUTRAL,                     "eu" },
    { LANG_BASQUE,         SUBLANG_BASQUE_BASQUE,               "eu_ES" },
    { LANG_BELARUSIAN,     SUBLANG_NEUTRAL,                     "be" },
    { LANG_BELARUSIAN,     SUBLANG_BELARUSIAN_BELARUS,          "be_BY" },
    { LANG_BENGALI,        SUBLANG_NEUTRAL,                     "bn" },
    { LANG_BENGALI,        SUBLANG_BENGALI_INDIA,               "bn_IN" },
    { LANG_BENGALI,        SUBLANG_BENGALI_BANGLADESH,          "bn_BD" },
    { LANG_BRETON,         SUBLANG_NEUTRAL,                     "br" },
    { LANG_BRETON,         SUBLANG_BRETON_FRANCE,               "br_FR" },
256 257 258 259 260 261 262 263 264 265 266 267 268 269
    { LANG_BULGARIAN,      SUBLANG_NEUTRAL,                     "bg" },
    { LANG_BULGARIAN,      SUBLANG_BULGARIAN_BULGARIA,          "bg_BG" },
    { LANG_CATALAN,        SUBLANG_NEUTRAL,                     "ca" },
    { LANG_CATALAN,        SUBLANG_CATALAN_CATALAN,             "ca_ES" },
    { LANG_CHINESE,        SUBLANG_NEUTRAL,                     "zh" },
    { LANG_CHINESE,        SUBLANG_CHINESE_TRADITIONAL,         "zh_TW" },
    { LANG_CHINESE,        SUBLANG_CHINESE_SIMPLIFIED,          "zh_CN" },
    { LANG_CHINESE,        SUBLANG_CHINESE_HONGKONG,            "zh_HK" },
    { LANG_CHINESE,        SUBLANG_CHINESE_SINGAPORE,           "zh_SG" },
    { LANG_CHINESE,        SUBLANG_CHINESE_MACAU,               "zh_MO" },
    { LANG_CZECH,          SUBLANG_NEUTRAL,                     "cs" },
    { LANG_CZECH,          SUBLANG_CZECH_CZECH_REPUBLIC,        "cs_CZ" },
    { LANG_DANISH,         SUBLANG_NEUTRAL,                     "da" },
    { LANG_DANISH,         SUBLANG_DANISH_DENMARK,              "da_DK" },
270 271 272 273 274 275
    { LANG_DIVEHI,         SUBLANG_NEUTRAL,                     "dv" },
    { LANG_DIVEHI,         SUBLANG_DIVEHI_MALDIVES,             "dv_MV" },
    { LANG_DUTCH,          SUBLANG_NEUTRAL,                     "nl" },
    { LANG_DUTCH,          SUBLANG_DUTCH,                       "nl_NL" },
    { LANG_DUTCH,          SUBLANG_DUTCH_BELGIAN,               "nl_BE" },
    { LANG_DUTCH,          SUBLANG_DUTCH_SURINAM,               "nl_SR" },
276 277 278 279 280 281 282 283 284 285 286 287 288 289
    { LANG_ENGLISH,        SUBLANG_NEUTRAL,                     "en" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_US,                  "en_US" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_UK,                  "en_GB" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_AUS,                 "en_AU" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_CAN,                 "en_CA" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_NZ,                  "en_NZ" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_EIRE,                "en_IE" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_SOUTH_AFRICA,        "en_ZA" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_JAMAICA,             "en_JM" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_CARIBBEAN,           "en_CB" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_BELIZE,              "en_BZ" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_TRINIDAD,            "en_TT" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_ZIMBABWE,            "en_ZW" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_PHILIPPINES,         "en_PH" },
290 291 292 293 294 295 296 297 298
    { LANG_ENGLISH,        SUBLANG_ENGLISH_INDIA,               "en_IN" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_MALAYSIA,            "en_MY" },
    { LANG_ENGLISH,        SUBLANG_ENGLISH_SINGAPORE,           "en_SG" },
    { LANG_ESTONIAN,       SUBLANG_NEUTRAL,                     "et" },
    { LANG_ESTONIAN,       SUBLANG_ESTONIAN_ESTONIA,            "et_EE" },
    { LANG_FAEROESE,       SUBLANG_NEUTRAL,                     "fo" },
    { LANG_FAEROESE,       SUBLANG_FAEROESE_FAROE_ISLANDS,      "fo_FO" },
    { LANG_FILIPINO,       SUBLANG_NEUTRAL,                     "fil" },
    { LANG_FILIPINO,       SUBLANG_FILIPINO_PHILIPPINES,        "fil_PH" },
299 300 301 302 303 304 305 306 307
    { LANG_FINNISH,        SUBLANG_NEUTRAL,                     "fi" },
    { LANG_FINNISH,        SUBLANG_FINNISH_FINLAND,             "fi_FI" },
    { LANG_FRENCH,         SUBLANG_NEUTRAL,                     "fr" },
    { LANG_FRENCH,         SUBLANG_FRENCH,                      "fr_FR" },
    { LANG_FRENCH,         SUBLANG_FRENCH_BELGIAN,              "fr_BE" },
    { LANG_FRENCH,         SUBLANG_FRENCH_CANADIAN,             "fr_CA" },
    { LANG_FRENCH,         SUBLANG_FRENCH_SWISS,                "fr_CH" },
    { LANG_FRENCH,         SUBLANG_FRENCH_LUXEMBOURG,           "fr_LU" },
    { LANG_FRENCH,         SUBLANG_FRENCH_MONACO,               "fr_MC" },
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    { LANG_GALICIAN,       SUBLANG_NEUTRAL,                     "gl" },
    { LANG_GALICIAN,       SUBLANG_GALICIAN_GALICIAN,           "gl_ES" },
    { LANG_GEORGIAN,       SUBLANG_NEUTRAL,                     "ka" },
    { LANG_GEORGIAN,       SUBLANG_GEORGIAN_GEORGIA,            "ka_GE" },
    { LANG_GERMAN,         SUBLANG_NEUTRAL,                     "de" },
    { LANG_GERMAN,         SUBLANG_GERMAN,                      "de_DE" },
    { LANG_GERMAN,         SUBLANG_GERMAN_SWISS,                "de_CH" },
    { LANG_GERMAN,         SUBLANG_GERMAN_AUSTRIAN,             "de_AT" },
    { LANG_GERMAN,         SUBLANG_GERMAN_LUXEMBOURG,           "de_LU" },
    { LANG_GERMAN,         SUBLANG_GERMAN_LIECHTENSTEIN,        "de_LI" },
    { LANG_GREEK,          SUBLANG_NEUTRAL,                     "el" },
    { LANG_GREEK,          SUBLANG_GREEK_GREECE,                "el_GR" },
    { LANG_GUJARATI,       SUBLANG_NEUTRAL,                     "gu" },
    { LANG_GUJARATI,       SUBLANG_GUJARATI_INDIA,              "gu_IN" },
    { LANG_HAUSA,          SUBLANG_NEUTRAL,                     "ha" },
    { LANG_HAUSA,          SUBLANG_HAUSA_NIGERIA,               "ha_NG" },
    { LANG_HAWAIIAN,       SUBLANG_NEUTRAL,                     "haw" },
    { LANG_HAWAIIAN,       SUBLANG_HAWAIIAN_US,                 "haw_US" },
326 327
    { LANG_HEBREW,         SUBLANG_NEUTRAL,                     "he" },
    { LANG_HEBREW,         SUBLANG_HEBREW_ISRAEL,               "he_IL" },
328 329
    { LANG_HINDI,          SUBLANG_NEUTRAL,                     "hi" },
    { LANG_HINDI,          SUBLANG_HINDI_INDIA,                 "hi_IN" },
330 331 332 333
    { LANG_HUNGARIAN,      SUBLANG_NEUTRAL,                     "hu" },
    { LANG_HUNGARIAN,      SUBLANG_HUNGARIAN_HUNGARY,           "hu_HU" },
    { LANG_ICELANDIC,      SUBLANG_NEUTRAL,                     "is" },
    { LANG_ICELANDIC,      SUBLANG_ICELANDIC_ICELAND,           "is_IS" },
334 335 336 337 338 339
    { LANG_IGBO,           SUBLANG_NEUTRAL,                     "ig" },
    { LANG_IGBO,           SUBLANG_IGBO_NIGERIA,                "ig_NG" },
    { LANG_INDONESIAN,     SUBLANG_NEUTRAL,                     "id" },
    { LANG_INDONESIAN,     SUBLANG_INDONESIAN_INDONESIA,        "id_ID" },
    { LANG_INUKTITUT,      SUBLANG_NEUTRAL,                     "iu" },
    { LANG_INUKTITUT,      SUBLANG_INUKTITUT_CANADA,            "iu_CA" },
340 341
    { LANG_IRISH,          SUBLANG_NEUTRAL,                     "ga" },
    { LANG_IRISH,          SUBLANG_IRISH_IRELAND,               "ga_IE" },
342 343 344 345 346
    { LANG_ITALIAN,        SUBLANG_NEUTRAL,                     "it" },
    { LANG_ITALIAN,        SUBLANG_ITALIAN,                     "it_IT" },
    { LANG_ITALIAN,        SUBLANG_ITALIAN_SWISS,               "it_CH" },
    { LANG_JAPANESE,       SUBLANG_NEUTRAL,                     "ja" },
    { LANG_JAPANESE,       SUBLANG_JAPANESE_JAPAN,              "ja_JP" },
347 348 349 350 351 352 353 354 355 356
    { LANG_KANNADA,        SUBLANG_NEUTRAL,                     "kn" },
    { LANG_KANNADA,        SUBLANG_KANNADA_INDIA,               "kn_IN" },
    { LANG_KAZAK,          SUBLANG_NEUTRAL,                     "kk" },
    { LANG_KAZAK,          SUBLANG_KAZAK_KAZAKHSTAN,            "kk_KZ" },
    { LANG_KHMER,          SUBLANG_NEUTRAL,                     "km" },
    { LANG_KHMER,          SUBLANG_KHMER_CAMBODIA,              "km_KH" },
    { LANG_KINYARWANDA,    SUBLANG_NEUTRAL,                     "rw" },
    { LANG_KINYARWANDA,    SUBLANG_KINYARWANDA_RWANDA,          "rw_RW" },
    { LANG_KONKANI,        SUBLANG_NEUTRAL,                     "kok" },
    { LANG_KONKANI,        SUBLANG_KONKANI_INDIA,               "kok_IN" },
357 358
    { LANG_KOREAN,         SUBLANG_NEUTRAL,                     "ko" },
    { LANG_KOREAN,         SUBLANG_KOREAN,                      "ko_KR" },
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    { LANG_KYRGYZ,         SUBLANG_NEUTRAL,                     "ky" },
    { LANG_KYRGYZ,         SUBLANG_KYRGYZ_KYRGYZSTAN,           "ky_KG" },
    { LANG_LAO,            SUBLANG_NEUTRAL,                     "lo" },
    { LANG_LAO,            SUBLANG_LAO_LAO,                     "lo_LA" },
    { LANG_LATVIAN,        SUBLANG_NEUTRAL,                     "lv" },
    { LANG_LATVIAN,        SUBLANG_LATVIAN_LATVIA,              "lv_LV" },
    { LANG_LITHUANIAN,     SUBLANG_NEUTRAL,                     "lt" },
    { LANG_LITHUANIAN,     SUBLANG_LITHUANIAN,                  "lt_LT" },
    { LANG_MACEDONIAN,     SUBLANG_NEUTRAL,                     "mk" },
    { LANG_MACEDONIAN,     SUBLANG_MACEDONIAN_MACEDONIA,        "mk_MK" },
    { LANG_MALAY,          SUBLANG_NEUTRAL,                     "ms" },
    { LANG_MALAY,          SUBLANG_MALAY_MALAYSIA,              "ms_MY" },
    { LANG_MALAY,          SUBLANG_MALAY_BRUNEI_DARUSSALAM,     "ms_BN" },
    { LANG_MALAYALAM,      SUBLANG_NEUTRAL,                     "ml" },
    { LANG_MALAYALAM,      SUBLANG_MALAYALAM_INDIA,             "ml_IN" },
    { LANG_MALTESE,        SUBLANG_NEUTRAL,                     "mt" },
    { LANG_MALTESE,        SUBLANG_MALTESE_MALTA,               "mt_MT" },
    { LANG_MARATHI,        SUBLANG_NEUTRAL,                     "mr" },
    { LANG_MARATHI,        SUBLANG_MARATHI_INDIA,               "mr_IN" },
    { LANG_MONGOLIAN,      SUBLANG_NEUTRAL,                     "mn" },
    { LANG_MONGOLIAN,      SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA, "mn_MN" },
    { LANG_MONGOLIAN,      SUBLANG_MONGOLIAN_PRC,               "mn_CN" },
    { LANG_NEPALI,         SUBLANG_NEUTRAL,                     "ne" },
    { LANG_NEPALI,         SUBLANG_NEPALI_NEPAL,                "ne_NP" },
    { LANG_NEPALI,         SUBLANG_NEPALI_INDIA,                "ne_IN" },
384 385
    { LANG_NORWEGIAN,      SUBLANG_NORWEGIAN_BOKMAL,            "nb_NO" },
    { LANG_NORWEGIAN,      SUBLANG_NORWEGIAN_NYNORSK,           "nn_NO" },
386 387 388 389 390 391
    { LANG_ODIA,           SUBLANG_NEUTRAL,                     "or" },
    { LANG_ODIA,           SUBLANG_ODIA_INDIA,                  "or_IN" },
    { LANG_PASHTO,         SUBLANG_NEUTRAL,                     "ps" },
    { LANG_PASHTO,         SUBLANG_PASHTO_AFGHANISTAN,          "ps_AF" },
    { LANG_PERSIAN,        SUBLANG_NEUTRAL,                     "fa" },
    { LANG_PERSIAN,        SUBLANG_PERSIAN_IRAN,                "fa_IR" },
392 393 394 395 396
    { LANG_POLISH,         SUBLANG_NEUTRAL,                     "pl" },
    { LANG_POLISH,         SUBLANG_POLISH_POLAND,               "pl_PL" },
    { LANG_PORTUGUESE,     SUBLANG_NEUTRAL,                     "pt" },
    { LANG_PORTUGUESE,     SUBLANG_PORTUGUESE_BRAZILIAN,        "pt_BR" },
    { LANG_PORTUGUESE,     SUBLANG_PORTUGUESE_PORTUGAL,         "pt_PT" },
397 398 399
    { LANG_PUNJABI,        SUBLANG_NEUTRAL,                     "pa" },
    { LANG_PUNJABI,        SUBLANG_PUNJABI_INDIA,               "pa_IN" },
    { LANG_PUNJABI,        SUBLANG_PUNJABI_PAKISTAN,            "pa_PK" },
400 401
    { LANG_ROMANIAN,       SUBLANG_NEUTRAL,                     "ro" },
    { LANG_ROMANIAN,       SUBLANG_ROMANIAN_ROMANIA,            "ro_RO" },
402 403
    { LANG_ROMANSH,        SUBLANG_NEUTRAL,                     "rm" },
    { LANG_ROMANSH,        SUBLANG_ROMANSH_SWITZERLAND,         "rm_CH" },
404 405
    { LANG_RUSSIAN,        SUBLANG_NEUTRAL,                     "ru" },
    { LANG_RUSSIAN,        SUBLANG_RUSSIAN_RUSSIA,              "ru_RU" },
406 407 408 409 410 411
    { LANG_SAMI,           SUBLANG_NEUTRAL,                     "se" },
    { LANG_SAMI,           SUBLANG_SAMI_NORTHERN_NORWAY,        "se_NO" },
    { LANG_SAMI,           SUBLANG_SAMI_NORTHERN_SWEDEN,        "se_SE" },
    { LANG_SAMI,           SUBLANG_SAMI_NORTHERN_FINLAND,       "se_FI" },
    { LANG_SANSKRIT,       SUBLANG_NEUTRAL,                     "sa" },
    { LANG_SANSKRIT,       SUBLANG_SANSKRIT_INDIA,              "sa_IN" },
412 413
    { LANG_SCOTTISH_GAELIC,SUBLANG_NEUTRAL,                     "gd" },
    { LANG_SCOTTISH_GAELIC,SUBLANG_SCOTTISH_GAELIC,             "gd_GB" },
414
    /* LANG_SERBIAN/LANG_CROATIAN/LANG_BOSNIAN are the same */
415 416
    { LANG_SERBIAN,        SUBLANG_NEUTRAL,                     "hr" },
    { LANG_SERBIAN,        SUBLANG_SERBIAN_CROATIA,             "hr_HR" },
417 418
    { LANG_SERBIAN,        SUBLANG_SERBIAN_LATIN,               "sr_RS@latin" },
    { LANG_SERBIAN,        SUBLANG_SERBIAN_CYRILLIC,            "sr_RS@cyrillic" },
419 420 421 422 423 424 425 426 427 428 429
    { LANG_SERBIAN,        SUBLANG_CROATIAN_BOSNIA_HERZEGOVINA_LATIN,   "hr_BA@latin" },
    { LANG_SERBIAN,        SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN,    "bs_BA@latin" },
    { LANG_SERBIAN,        SUBLANG_SERBIAN_BOSNIA_HERZEGOVINA_LATIN,    "sr_BA@latin" },
    { LANG_SERBIAN,        SUBLANG_SERBIAN_BOSNIA_HERZEGOVINA_CYRILLIC, "sr_BA@cyrillic" },
    { LANG_SERBIAN,        SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC, "bs_BA@cyrillic" },
    { LANG_SERBIAN,        SUBLANG_SERBIAN_SERBIA_LATIN,        "sr_RS@latin" },
    { LANG_SERBIAN,        SUBLANG_SERBIAN_SERBIA_CYRILLIC,     "sr_RS@cyrillic" },
    { LANG_SERBIAN,        SUBLANG_SERBIAN_MONTENEGRO_LATIN,    "sr_ME@latin" },
    { LANG_SERBIAN,        SUBLANG_SERBIAN_MONTENEGRO_CYRILLIC, "sr_ME@cyrillic" },
    { LANG_SINHALESE,      SUBLANG_NEUTRAL,                     "si" },
    { LANG_SINHALESE,      SUBLANG_SINHALESE_SRI_LANKA,         "si_LK" },
430 431
    { LANG_SLOVAK,         SUBLANG_NEUTRAL,                     "sk" },
    { LANG_SLOVAK,         SUBLANG_SLOVAK_SLOVAKIA,             "sk_SK" },
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    { LANG_SLOVENIAN,      SUBLANG_NEUTRAL,                     "sl" },
    { LANG_SLOVENIAN,      SUBLANG_SLOVENIAN_SLOVENIA,          "sl_SI" },
    { LANG_SOTHO,          SUBLANG_NEUTRAL,                     "nso" },
    { LANG_SOTHO,          SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA, "nso_ZA" },
    { LANG_SPANISH,        SUBLANG_NEUTRAL,                     "es" },
    { LANG_SPANISH,        SUBLANG_SPANISH,                     "es_ES" },
    { LANG_SPANISH,        SUBLANG_SPANISH_MEXICAN,             "es_MX" },
    { LANG_SPANISH,        SUBLANG_SPANISH_MODERN,              "es_ES_modern" },
    { LANG_SPANISH,        SUBLANG_SPANISH_GUATEMALA,           "es_GT" },
    { LANG_SPANISH,        SUBLANG_SPANISH_COSTA_RICA,          "es_CR" },
    { LANG_SPANISH,        SUBLANG_SPANISH_PANAMA,              "es_PA" },
    { LANG_SPANISH,        SUBLANG_SPANISH_DOMINICAN_REPUBLIC,  "es_DO" },
    { LANG_SPANISH,        SUBLANG_SPANISH_VENEZUELA,           "es_VE" },
    { LANG_SPANISH,        SUBLANG_SPANISH_COLOMBIA,            "es_CO" },
    { LANG_SPANISH,        SUBLANG_SPANISH_PERU,                "es_PE" },
    { LANG_SPANISH,        SUBLANG_SPANISH_ARGENTINA,           "es_AR" },
    { LANG_SPANISH,        SUBLANG_SPANISH_ECUADOR,             "es_EC" },
    { LANG_SPANISH,        SUBLANG_SPANISH_CHILE,               "es_CL" },
    { LANG_SPANISH,        SUBLANG_SPANISH_URUGUAY,             "es_UY" },
    { LANG_SPANISH,        SUBLANG_SPANISH_PARAGUAY,            "es_PY" },
    { LANG_SPANISH,        SUBLANG_SPANISH_BOLIVIA,             "es_BO" },
    { LANG_SPANISH,        SUBLANG_SPANISH_EL_SALVADOR,         "es_SV" },
    { LANG_SPANISH,        SUBLANG_SPANISH_HONDURAS,            "es_HN" },
    { LANG_SPANISH,        SUBLANG_SPANISH_NICARAGUA,           "es_NI" },
    { LANG_SPANISH,        SUBLANG_SPANISH_PUERTO_RICO,         "es_PR" },
    { LANG_SPANISH,        SUBLANG_SPANISH_US,                  "es_US" },
    { LANG_SWAHILI,        SUBLANG_NEUTRAL,                     "sw" },
    { LANG_SWAHILI,        SUBLANG_SWAHILI_KENYA,               "sw_KE" },
460 461 462
    { LANG_SWEDISH,        SUBLANG_NEUTRAL,                     "sv" },
    { LANG_SWEDISH,        SUBLANG_SWEDISH_SWEDEN,              "sv_SE" },
    { LANG_SWEDISH,        SUBLANG_SWEDISH_FINLAND,             "sv_FI" },
463 464 465 466 467 468 469 470 471 472
    { LANG_SYRIAC,         SUBLANG_NEUTRAL,                     "syr" },
    { LANG_SYRIAC,         SUBLANG_SYRIAC_SYRIA,                "syr_SY" },
    { LANG_TAJIK,          SUBLANG_NEUTRAL,                     "tg" },
    { LANG_TAJIK,          SUBLANG_TAJIK_TAJIKISTAN,            "tg_TJ" },
    { LANG_TAMIL,          SUBLANG_NEUTRAL,                     "ta" },
    { LANG_TAMIL,          SUBLANG_TAMIL_INDIA,                 "ta_IN" },
    { LANG_TATAR,          SUBLANG_NEUTRAL,                     "tt" },
    { LANG_TATAR,          SUBLANG_TATAR_RUSSIA,                "tt_TA" },
    { LANG_TELUGU,         SUBLANG_NEUTRAL,                     "te" },
    { LANG_TELUGU,         SUBLANG_TELUGU_INDIA,                "te_IN" },
473 474
    { LANG_THAI,           SUBLANG_NEUTRAL,                     "th" },
    { LANG_THAI,           SUBLANG_THAI_THAILAND,               "th_TH" },
475 476 477 478 479
    { LANG_TIGRINYA,       SUBLANG_NEUTRAL,                     "ti" },
    { LANG_TIGRINYA,       SUBLANG_TIGRINYA_ETHIOPIA,           "ti_ET" },
    { LANG_TIGRINYA,       SUBLANG_TIGRINYA_ERITREA,            "ti_ER" },
    { LANG_TSWANA,         SUBLANG_NEUTRAL,                     "tn" },
    { LANG_TSWANA,         SUBLANG_TSWANA_SOUTH_AFRICA,         "tn_ZA" },
480 481
    { LANG_TURKISH,        SUBLANG_NEUTRAL,                     "tr" },
    { LANG_TURKISH,        SUBLANG_TURKISH_TURKEY,              "tr_TR" },
482 483
    { LANG_UIGHUR,         SUBLANG_NEUTRAL,                     "ug" },
    { LANG_UIGHUR,         SUBLANG_UIGHUR_PRC,                  "ug_CN" },
484 485
    { LANG_UKRAINIAN,      SUBLANG_NEUTRAL,                     "uk" },
    { LANG_UKRAINIAN,      SUBLANG_UKRAINIAN_UKRAINE,           "uk_UA" },
486 487 488
    { LANG_URDU,           SUBLANG_NEUTRAL,                     "ur" },
    { LANG_URDU,           SUBLANG_URDU_PAKISTAN,               "ur_PK" },
    { LANG_URDU,           SUBLANG_URDU_INDIA,                  "ur_IN" },
489
    { LANG_UZBEK,          SUBLANG_NEUTRAL,                     "uz" },
490 491
    { LANG_UZBEK,          SUBLANG_UZBEK_LATIN,                 "uz_UZ@latin" },
    { LANG_UZBEK,          SUBLANG_UZBEK_CYRILLIC,              "uz_UZ@cyrillic" },
492 493
    { LANG_VIETNAMESE,     SUBLANG_NEUTRAL,                     "vi" },
    { LANG_VIETNAMESE,     SUBLANG_VIETNAMESE_VIETNAM,          "vi_VN" },
494 495
    { LANG_WELSH,          SUBLANG_NEUTRAL,                     "cy" },
    { LANG_WELSH,          SUBLANG_WELSH_UNITED_KINGDOM,        "cy_GB" },
496 497 498 499 500 501 502 503
    { LANG_WOLOF,          SUBLANG_NEUTRAL,                     "wo" },
    { LANG_WOLOF,          SUBLANG_WOLOF_SENEGAL,               "wo_SN" },
    { LANG_XHOSA,          SUBLANG_NEUTRAL,                     "xh" },
    { LANG_XHOSA,          SUBLANG_XHOSA_SOUTH_AFRICA,          "xh_ZA" },
    { LANG_YORUBA,         SUBLANG_NEUTRAL,                     "yo" },
    { LANG_YORUBA,         SUBLANG_YORUBA_NIGERIA,              "yo_NG" },
    { LANG_ZULU,           SUBLANG_NEUTRAL,                     "zu" },
    { LANG_ZULU,           SUBLANG_ZULU_SOUTH_AFRICA,           "zu_ZA" },
504 505 506 507 508 509 510 511 512 513 514 515

#ifdef LANG_ESPERANTO
    { LANG_ESPERANTO,      SUBLANG_DEFAULT,                     "eo" },
#endif
#ifdef LANG_WALON
    { LANG_WALON,          SUBLANG_NEUTRAL,                     "wa" },
    { LANG_WALON,          SUBLANG_DEFAULT,                     "wa_BE" },
#endif
#ifdef LANG_CORNISH
    { LANG_CORNISH,        SUBLANG_NEUTRAL,                     "kw" },
    { LANG_CORNISH,        SUBLANG_DEFAULT,                     "kw_GB" },
#endif
516 517
#ifdef LANG_MANX_GAELIC
    { LANG_MANX_GAELIC,    SUBLANG_MANX_GAELIC,                 "gv_GB" },
518 519 520
#endif
};

521 522 523 524 525 526 527 528 529 530 531 532 533 534
#ifndef HAVE_LIBGETTEXTPO

void write_pot_file( const char *outname )
{
    error( "PO files not supported in this wrc build\n" );
}

void write_po_files( const char *outname )
{
    error( "PO files not supported in this wrc build\n" );
}

#else  /* HAVE_LIBGETTEXTPO */

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
static void po_xerror( int severity, po_message_t message,
                       const char *filename, size_t lineno, size_t column,
                       int multiline_p, const char *message_text )
{
    fprintf( stderr, "%s:%u:%u: %s\n",
             filename, (unsigned int)lineno, (unsigned int)column, message_text );
    if (severity) exit(1);
}

static void po_xerror2( int severity, po_message_t message1,
                        const char *filename1, size_t lineno1, size_t column1,
                        int multiline_p1, const char *message_text1,
                        po_message_t message2,
                        const char *filename2, size_t lineno2, size_t column2,
                        int multiline_p2, const char *message_text2 )
{
    fprintf( stderr, "%s:%u:%u: %s\n",
             filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
    fprintf( stderr, "%s:%u:%u: %s\n",
             filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
    if (severity) exit(1);
}

static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };

560
static string_t *convert_string_utf8( const string_t *str, int codepage )
561
{
562 563 564 565
    string_t *wstr = convert_string( str, str_unicode, codepage );
    string_t *ustr = convert_string( wstr, str_char, CP_UTF8 );
    free_string( wstr );
    return ustr;
566 567 568 569 570 571 572 573 574 575 576 577 578
}

static po_message_t find_message( po_file_t po, const char *msgid, const char *msgctxt,
                                  po_message_iterator_t *iterator )
{
    po_message_t msg;
    const char *context;

    *iterator = po_message_iterator( po, NULL );
    while ((msg = po_next_message( *iterator )))
    {
        if (strcmp( po_message_msgid( msg ), msgid )) continue;
        if (!msgctxt) break;
579
        if (!(context = po_message_msgctxt( msg ))) continue;
580 581 582 583 584 585 586 587
        if (!strcmp( context, msgctxt )) break;
    }
    return msg;
}

static void add_po_string( po_file_t po, const string_t *msgid, const string_t *msgstr,
                           const language_t *lang )
{
588
    static const char dnt[] = "do not translate";
589 590 591
    po_message_t msg;
    po_message_iterator_t iterator;
    int codepage;
592 593
    string_t *str_buffer = NULL;
    char *id, *id_buffer, *context, *str = NULL;
594 595 596 597 598

    if (!msgid->size) return;

    id_buffer = id = convert_msgid_ascii( msgid, 1 );
    context = get_message_context( &id );
599 600 601 602 603 604
    if (context && strcmp(context, dnt) == 0)
    {
        /* This string should not be translated */
        free( id_buffer );
        return;
    }
605 606 607 608 609 610

    if (msgstr)
    {
        if (lang) codepage = get_language_codepage( lang->id, lang->sub );
        else codepage = get_language_codepage( 0, 0 );
        assert( codepage != -1 );
611 612
        str_buffer = convert_string_utf8( msgstr, codepage );
        str = str_buffer->str.cstr;
613
        if (is_english( lang )) get_message_context( &str );
614 615 616 617 618
    }
    if (!(msg = find_message( po, id, context, &iterator )))
    {
        msg = po_message_create();
        po_message_set_msgid( msg, id );
619
        po_message_set_msgstr( msg, str ? str : "" );
620 621 622 623 624 625
        if (context) po_message_set_msgctxt( msg, context );
        po_message_insert( iterator, msg );
    }
    if (msgid->loc.file) po_message_add_filepos( msg, msgid->loc.file, msgid->loc.line );
    po_message_iterator_free( iterator );
    free( id_buffer );
626
    if (str_buffer) free_string( str_buffer );
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
}

struct po_file_lang
{
    struct list entry;
    language_t  lang;
    po_file_t   po;
};

static struct list po_file_langs = LIST_INIT( po_file_langs );

static po_file_t create_po_file(void)
{
    po_file_t po;
    po_message_t msg;
    po_message_iterator_t iterator;

    po = po_file_create();
    iterator = po_message_iterator( po, NULL );
    msg = po_message_create();
    po_message_set_msgid( msg, "" );
    po_message_set_msgstr( msg,
                           "Project-Id-Version: Wine\n"
650
                           "Report-Msgid-Bugs-To: https://bugs.winehq.org\n"
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
                           "POT-Creation-Date: N/A\n"
                           "PO-Revision-Date: N/A\n"
                           "Last-Translator: Automatically generated\n"
                           "Language-Team: none\n"
                           "MIME-Version: 1.0\n"
                           "Content-Type: text/plain; charset=UTF-8\n"
                           "Content-Transfer-Encoding: 8bit\n" );
    po_message_insert( iterator, msg );
    po_message_iterator_free( iterator );
    return po;
}

static po_file_t get_po_file( const language_t *lang )
{
    struct po_file_lang *po_file;

    LIST_FOR_EACH_ENTRY( po_file, &po_file_langs, struct po_file_lang, entry )
        if (po_file->lang.id == lang->id && po_file->lang.sub == lang->sub) return po_file->po;

    /* create a new one */
    po_file = xmalloc( sizeof(*po_file) );
    po_file->lang = *lang;
    po_file->po = create_po_file();
    list_add_tail( &po_file_langs, &po_file->entry );
    return po_file->po;
}

678
static const char *get_language_name( const language_t *lang )
679
{
680
    static char name[20];
681 682
    unsigned int i;

683
    for (i = 0; i < ARRAY_SIZE(languages); i++)
684
        if (languages[i].id == lang->id && languages[i].sub == lang->sub)
685 686 687 688 689 690 691 692 693
            return languages[i].name;

    sprintf( name, "%02x-%02x", lang->id, lang->sub );
    return name;
}

static char *get_po_file_name( const language_t *lang )
{
    return strmake( "%s.po", get_language_name( lang ) );
694 695 696 697 698
}

static unsigned int flush_po_files( const char *output_name )
{
    struct po_file_lang *po_file, *next;
Huw Davies's avatar
Huw Davies committed
699
    unsigned int count = 0;
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775

    LIST_FOR_EACH_ENTRY_SAFE( po_file, next, &po_file_langs, struct po_file_lang, entry )
    {
        char *name = get_po_file_name( &po_file->lang );
        if (output_name)
        {
            const char *p = strrchr( output_name, '/' );
            if (p) p++;
            else p = output_name;
            if (!strcmp( p, name ))
            {
                po_file_write( po_file->po, name, &po_xerror_handler );
                count++;
            }
        }
        else  /* no specified output name, output a file for every language found */
        {
            po_file_write( po_file->po, name, &po_xerror_handler );
            count++;
            fprintf( stderr, "created %s\n", name );
        }
        po_file_free( po_file->po );
        list_remove( &po_file->entry );
        free( po_file );
        free( name );
    }
    return count;
}

static void add_pot_stringtable( po_file_t po, const resource_t *res )
{
    const stringtable_t *stt = res->res.stt;
    int i;

    while (stt)
    {
        for (i = 0; i < stt->nentries; i++)
            if (stt->entries[i].str) add_po_string( po, stt->entries[i].str, NULL, NULL );
        stt = stt->next;
    }
}

static void add_po_stringtable( const resource_t *english, const resource_t *res )
{
    const stringtable_t *english_stt = english->res.stt;
    const stringtable_t *stt = res->res.stt;
    po_file_t po = get_po_file( stt->lvc.language );
    int i;

    while (english_stt && stt)
    {
        for (i = 0; i < stt->nentries; i++)
            if (english_stt->entries[i].str && stt->entries[i].str)
                add_po_string( po, english_stt->entries[i].str, stt->entries[i].str, stt->lvc.language );
        stt = stt->next;
        english_stt = english_stt->next;
    }
}

static void add_pot_dialog_controls( po_file_t po, const control_t *ctrl )
{
    while (ctrl)
    {
        if (control_has_title( ctrl )) add_po_string( po, ctrl->title->name.s_name, NULL, NULL );
        ctrl = ctrl->next;
    }
}

static void add_pot_dialog( po_file_t po, const resource_t *res )
{
    const dialog_t *dlg = res->res.dlg;

    if (dlg->title) add_po_string( po, dlg->title, NULL, NULL );
    add_pot_dialog_controls( po, dlg->controls );
}

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
static void compare_dialogs( const dialog_t *english_dlg, const dialog_t *dlg )
{
    const control_t *english_ctrl, *ctrl;
    unsigned int style = 0, exstyle = 0, english_style = 0, english_exstyle = 0;
    char *name;
    char *title = english_dlg->title ? convert_msgid_ascii( english_dlg->title, 0 ) : xstrdup("??");

    if (english_dlg->width != dlg->width || english_dlg->height != dlg->height)
        warning( "%s: dialog %s doesn't have the same size (%d,%d vs %d,%d)\n",
                 get_language_name( dlg->lvc.language ), title, dlg->width, dlg->height,
                 english_dlg->width, english_dlg->height );

    if (dlg->gotstyle) style = dlg->style->or_mask;
    if (dlg->gotexstyle) exstyle = dlg->exstyle->or_mask;
    if (english_dlg->gotstyle) english_style = english_dlg->style->or_mask;
    if (english_dlg->gotexstyle) english_exstyle = english_dlg->exstyle->or_mask;
792 793
    if (is_rtl_language( dlg->lvc.language )) english_exstyle |= WS_EX_LAYOUTRTL;

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
    if (english_style != style)
        warning( "%s: dialog %s doesn't have the same style (%08x vs %08x)\n",
                 get_language_name( dlg->lvc.language ), title, style, english_style );
    if (english_exstyle != exstyle)
        warning( "%s: dialog %s doesn't have the same exstyle (%08x vs %08x)\n",
                 get_language_name( dlg->lvc.language ), title, exstyle, english_exstyle );

    if (english_dlg->font || dlg->font)
    {
        int size = 0, english_size = 0;
        char *font = NULL, *english_font = NULL;

        if (english_dlg->font)
        {
            english_font = convert_msgid_ascii( english_dlg->font->name, 0 );
            english_size = english_dlg->font->size;
        }
        if (dlg->font)
        {
            font = convert_msgid_ascii( dlg->font->name, 0 );
            size = dlg->font->size;
        }
816
        if (uses_larger_font( dlg->lvc.language )) english_size++;
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847

        if (!english_font || !font || strcasecmp( english_font, font ) || english_size != size)
            warning( "%s: dialog %s doesn't have the same font (%s %u vs %s %u)\n",
                     get_language_name(dlg->lvc.language), title,
                     english_font ? english_font : "default", english_size,
                     font ? font : "default", size );
        free( font );
        free( english_font );
    }
    english_ctrl = english_dlg->controls;
    ctrl = dlg->controls;
    for ( ; english_ctrl && ctrl; ctrl = ctrl->next, english_ctrl = english_ctrl->next )
    {
        if (control_has_title( english_ctrl ))
            name = convert_msgid_ascii( english_ctrl->title->name.s_name, 0 );
        else
            name = strmake( "%d", ctrl->id );

        if (english_ctrl->width != ctrl->width || english_ctrl->height != ctrl->height)
            warning( "%s: dialog %s control %s doesn't have the same size (%d,%d vs %d,%d)\n",
                     get_language_name( dlg->lvc.language ), title, name,
                     ctrl->width, ctrl->height, english_ctrl->width, english_ctrl->height );
        if (english_ctrl->x != ctrl->x || english_ctrl->y != ctrl->y)
            warning( "%s: dialog %s control %s doesn't have the same position (%d,%d vs %d,%d)\n",
                     get_language_name( dlg->lvc.language ), title, name,
                     ctrl->x, ctrl->y, english_ctrl->x, english_ctrl->y );
        free( name );
    }
    free( title );
}

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
static void add_po_dialog_controls( po_file_t po, const control_t *english_ctrl,
                                    const control_t *ctrl, const language_t *lang )
{
    while (english_ctrl && ctrl)
    {
        if (control_has_title( english_ctrl ) && control_has_title( ctrl ))
            add_po_string( po, english_ctrl->title->name.s_name, ctrl->title->name.s_name, lang );

        ctrl = ctrl->next;
        english_ctrl = english_ctrl->next;
    }
}

static void add_po_dialog( const resource_t *english, const resource_t *res )
{
    const dialog_t *english_dlg = english->res.dlg;
    const dialog_t *dlg = res->res.dlg;
    po_file_t po = get_po_file( dlg->lvc.language );

867 868
    compare_dialogs( english_dlg, dlg );

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
    if (english_dlg->title && dlg->title)
        add_po_string( po, english_dlg->title, dlg->title, dlg->lvc.language );
    add_po_dialog_controls( po, english_dlg->controls, dlg->controls, dlg->lvc.language );
}

static void add_pot_menu_items( po_file_t po, const menu_item_t *item )
{
    while (item)
    {
        if (item->name) add_po_string( po, item->name, NULL, NULL );
        if (item->popup) add_pot_menu_items( po, item->popup );
        item = item->next;
    }
}

static void add_pot_menu( po_file_t po, const resource_t *res )
{
    add_pot_menu_items( po, res->res.men->items );
}

static void add_po_menu_items( po_file_t po, const menu_item_t *english_item,
                               const menu_item_t *item, const language_t *lang )
{
    while (english_item && item)
    {
        if (english_item->name && item->name)
            add_po_string( po, english_item->name, item->name, lang );
        if (english_item->popup && item->popup)
            add_po_menu_items( po, english_item->popup, item->popup, lang );
        item = item->next;
        english_item = english_item->next;
    }
}

static void add_po_menu( const resource_t *english, const resource_t *res )
{
    const menu_item_t *english_items = english->res.men->items;
    const menu_item_t *items = res->res.men->items;
    po_file_t po = get_po_file( res->res.men->lvc.language );

    add_po_menu_items( po, english_items, items, res->res.men->lvc.language );
}

912
static BOOL string_has_context( const string_t *str )
913 914 915 916 917 918 919 920 921
{
    char *id, *id_buffer, *context;

    id_buffer = id = convert_msgid_ascii( str, 1 );
    context = get_message_context( &id );
    free( id_buffer );
    return context != NULL;
}

922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
static void add_pot_accel( po_file_t po, const resource_t *res )
{
    event_t *event = res->res.acc->events;

    while (event)
    {
        /* accelerators without a context don't make sense in po files */
        if (event->str && string_has_context( event->str ))
            add_po_string( po, event->str, NULL, NULL );
        event = event->next;
    }
}

static void add_po_accel( const resource_t *english, const resource_t *res )
{
    event_t *english_event = english->res.acc->events;
    event_t *event = res->res.acc->events;
    po_file_t po = get_po_file( res->res.acc->lvc.language );

    while (english_event && event)
    {
        if (english_event->str && event->str && string_has_context( english_event->str ))
            add_po_string( po, english_event->str, event->str, res->res.acc->lvc.language );
        event = event->next;
        english_event = english_event->next;
    }
}

950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
static ver_block_t *get_version_langcharset_block( ver_block_t *block )
{
    ver_block_t *stringfileinfo = NULL;
    char *translation = NULL;
    ver_value_t *val;

    for (; block; block = block->next)
    {
        char *name;
        name = convert_msgid_ascii( block->name, 0 );
        if (!strcasecmp( name, "stringfileinfo" ))
            stringfileinfo = block;
        else if (!strcasecmp( name, "varfileinfo" ))
        {
            for (val = block->values; val; val = val->next)
            {
                char *key = convert_msgid_ascii( val->key, 0 );
                if (val->type == val_words &&
                    !strcasecmp( key, "Translation" ) &&
                    val->value.words->nwords >= 2)
                    translation = strmake( "%04x%04x",
                                           val->value.words->words[0],
                                           val->value.words->words[1] );
                free( key );
            }
        }
        free( name );
    }

    if (!stringfileinfo || !translation) return NULL;

    for (val = stringfileinfo->values; val; val = val->next)
    {
        char *block_name;
        if (val->type != val_block) continue;
        block_name = convert_msgid_ascii( val->value.block->name, 0 );
        if (!strcasecmp( block_name, translation ))
        {
            free( block_name );
            free( translation );
            return val->value.block;
        }
        free( block_name );
    }
    free( translation );
    return NULL;
}

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
static int version_value_needs_translation( const ver_value_t *val )
{
    int ret;
    char *key;

    if (val->type != val_str) return 0;
    if (!(key = convert_msgid_ascii( val->key, 0 ))) return 0;

    /* most values contain version numbers or file names, only translate a few specific ones */
    ret = (!strcasecmp( key, "FileDescription" ) || !strcasecmp( key, "ProductName" ));

    free( key );
    return ret;
}

1013 1014 1015 1016 1017 1018 1019
static void add_pot_versioninfo( po_file_t po, const resource_t *res )
{
    ver_value_t *val;
    ver_block_t *langcharset = get_version_langcharset_block( res->res.ver->blocks );

    if (!langcharset) return;
    for (val = langcharset->values; val; val = val->next)
1020 1021
        if (version_value_needs_translation( val ))
            add_po_string( po, val->value.str, NULL, NULL );
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
}

static void add_po_versioninfo( const resource_t *english, const resource_t *res )
{
    const ver_block_t *langcharset = get_version_langcharset_block( res->res.ver->blocks );
    const ver_block_t *english_langcharset = get_version_langcharset_block( english->res.ver->blocks );
    ver_value_t *val, *english_val;
    po_file_t po = get_po_file( res->res.ver->lvc.language );

    if (!langcharset && !english_langcharset) return;
    val = langcharset->values;
    english_val = english_langcharset->values;
    while (english_val && val)
    {
        if (val->type == val_str)
            add_po_string( po, english_val->value.str, val->value.str, res->res.ver->lvc.language );
        val = val->next;
        english_val = english_val->next;
    }
}

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
static resource_t *find_english_resource( resource_t *res )
{
    resource_t *ptr;

    for (ptr = resource_top; ptr; ptr = ptr->next)
    {
        if (ptr->type != res->type) continue;
        if (!ptr->lan) continue;
        if (!is_english( ptr->lan )) continue;
        if (compare_name_id( ptr->name, res->name )) continue;
        return ptr;
    }
    return NULL;
}

void write_pot_file( const char *outname )
{
    resource_t *res;
    po_file_t po = create_po_file();

    for (res = resource_top; res; res = res->next)
    {
        if (!is_english( res->lan )) continue;

        switch (res->type)
        {
1069
        case res_acc: add_pot_accel( po, res ); break;
1070 1071 1072
        case res_dlg: add_pot_dialog( po, res ); break;
        case res_men: add_pot_menu( po, res ); break;
        case res_stt: add_pot_stringtable( po, res ); break;
1073
        case res_ver: add_pot_versioninfo( po, res ); break;
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
        case res_msg: break;  /* FIXME */
        default: break;
        }
    }
    po_file_write( po, outname, &po_xerror_handler );
    po_file_free( po );
}

void write_po_files( const char *outname )
{
    resource_t *res, *english;

    for (res = resource_top; res; res = res->next)
    {
        if (!(english = find_english_resource( res ))) continue;
        switch (res->type)
        {
1091
        case res_acc: add_po_accel( english, res ); break;
1092 1093 1094
        case res_dlg: add_po_dialog( english, res ); break;
        case res_men: add_po_menu( english, res ); break;
        case res_stt: add_po_stringtable( english, res ); break;
1095
        case res_ver: add_po_versioninfo( english, res ); break;
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
        case res_msg: break;  /* FIXME */
        default: break;
        }
    }
    if (!flush_po_files( outname ))
    {
        if (outname) error( "No translations found for %s\n", outname );
        else error( "No translations found\n" );
    }
}

1107
#endif  /* HAVE_LIBGETTEXTPO */
1108

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
static struct mo_file *mo_file;

static void byteswap( unsigned int *data, unsigned int count )
{
    unsigned int i;

    for (i = 0; i < count; i++)
        data[i] = data[i] >> 24 | (data[i] >> 8 & 0xff00) | (data[i] << 8 & 0xff0000) | data[i] << 24;
}

static void load_mo_file( const char *name )
{
    struct stat st;
    int res, fd;

    fd = open( name, O_RDONLY | O_BINARY );
    if (fd == -1) fatal_perror( "Failed to open %s", name );
    fstat( fd, &st );
    mo_file = xmalloc( st.st_size );
    res = read( fd, mo_file, st.st_size );
    if (res == -1) fatal_perror( "Failed to read %s", name );
    else if (res != st.st_size) error( "Failed to read %s\n", name );
    close( fd );

    /* sanity checks */

    if (st.st_size < sizeof(*mo_file))
        error( "%s is not a valid .mo file\n", name );
    if (mo_file->magic == 0xde120495)
        byteswap( &mo_file->revision, 4 );
    else if (mo_file->magic != 0x950412de)
        error( "%s is not a valid .mo file\n", name );
    if ((mo_file->revision >> 16) > 1)
        error( "%s: unsupported file version %x\n", name, mo_file->revision );
    if (mo_file->msgid_off >= st.st_size ||
        mo_file->msgstr_off >= st.st_size ||
        st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count)
        error( "%s: corrupted file\n", name );

    if (mo_file->magic == 0xde120495)
    {
        byteswap( (unsigned int *)((char *)mo_file + mo_file->msgid_off), 2 * mo_file->count );
        byteswap( (unsigned int *)((char *)mo_file + mo_file->msgstr_off), 2 * mo_file->count );
    }
}

static void free_mo_file(void)
{
    free( mo_file );
    mo_file = NULL;
}

static inline const char *get_mo_msgid( int index )
{
    const char *base = (const char *)mo_file;
    const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgid_off);
    return base + offsets[2 * index + 1];
}

static inline const char *get_mo_msgstr( int index )
{
    const char *base = (const char *)mo_file;
    const unsigned int *offsets = (const unsigned int *)(base + mo_file->msgstr_off);
    return base + offsets[2 * index + 1];
}

static const char *get_msgstr( const char *msgid, const char *context, int *found )
{
    int pos, res, min, max;
    const char *ret = msgid;
    char *id = NULL;

    if (!mo_file)  /* strings containing a context still need to be transformed */
    {
        if (context) (*found)++;
        return ret;
    }

    if (context) id = strmake( "%s%c%s", context, 4, msgid );
    min = 0;
    max = mo_file->count - 1;
    while (min <= max)
    {
        pos = (min + max) / 2;
1193
        res = strcmp( get_mo_msgid(pos), id ? id : msgid );
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
        if (!res)
        {
            const char *str = get_mo_msgstr( pos );
            if (str[0])  /* ignore empty strings */
            {
                ret = str;
                (*found)++;
            }
            break;
        }
        if (res > 0) max = pos - 1;
        else min = pos + 1;
    }
    free( id );
    return ret;
}

1211
static string_t *translate_string( string_t *str, int *found )
1212
{
1213
    string_t ustr, *new;
1214 1215 1216 1217 1218 1219 1220 1221
    const char *transl;
    char *buffer, *msgid, *context;

    if (!str->size || !(buffer = convert_msgid_ascii( str, 0 )))
        return convert_string( str, str_unicode, 1252 );

    msgid = buffer;
    context = get_message_context( &msgid );
1222
    transl = get_msgstr( msgid, context, found );
1223

1224 1225 1226 1227 1228 1229
    ustr.type = str_char;
    ustr.size = strlen( transl );
    ustr.str.cstr = (char *)transl;
    ustr.loc = str->loc;

    new = convert_string( &ustr, str_unicode, CP_UTF8 );
1230 1231 1232 1233
    free( buffer );
    return new;
}

1234
static control_t *translate_controls( control_t *ctrl, int *found )
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
{
    control_t *new, *head = NULL, *tail = NULL;

    while (ctrl)
    {
        new = xmalloc( sizeof(*new) );
        *new = *ctrl;
        if (control_has_title( ctrl ))
        {
            new->title = new_name_id();
            *new->title = *ctrl->title;
1246
            new->title->name.s_name = translate_string( ctrl->title->name.s_name, found );
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
        }
        else new->title = dup_name_id( ctrl->title );
        new->ctlclass = dup_name_id( ctrl->ctlclass );
        if (tail) tail->next = new;
        else head = new;
        new->next = NULL;
        new->prev = tail;
        tail = new;
        ctrl = ctrl->next;
    }
    return head;
}

1260
static menu_item_t *translate_items( menu_item_t *item, int *found )
1261 1262 1263 1264 1265 1266 1267
{
    menu_item_t *new, *head = NULL, *tail = NULL;

    while (item)
    {
        new = xmalloc( sizeof(*new) );
        *new = *item;
1268 1269
        if (item->name) new->name = translate_string( item->name, found );
        if (item->popup) new->popup = translate_items( item->popup, found );
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
        if (tail) tail->next = new;
        else head = new;
        new->next = NULL;
        new->prev = tail;
        tail = new;
        item = item->next;
    }
    return head;
}

1280
static stringtable_t *translate_stringtable( stringtable_t *stt, language_t *lang, int *found )
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
{
    stringtable_t *new, *head = NULL, *tail = NULL;
    int i;

    while (stt)
    {
        new = xmalloc( sizeof(*new) );
        *new = *stt;
        new->lvc.language = lang;
        new->lvc.version = get_dup_version( lang );
        new->entries = xmalloc( new->nentries * sizeof(*new->entries) );
        memcpy( new->entries, stt->entries, new->nentries * sizeof(*new->entries) );
        for (i = 0; i < stt->nentries; i++)
            if (stt->entries[i].str)
1295
                new->entries[i].str = translate_string( stt->entries[i].str, found );
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306

        if (tail) tail->next = new;
        else head = new;
        new->next = NULL;
        new->prev = tail;
        tail = new;
        stt = stt->next;
    }
    return head;
}

1307
static void translate_dialog( dialog_t *dlg, dialog_t *new, int *found )
1308
{
1309
    if (dlg->title) new->title = translate_string( dlg->title, found );
1310 1311 1312 1313 1314 1315 1316 1317
    if (is_rtl_language( new->lvc.language ))
    {
        new->gotexstyle = TRUE;
        if (dlg->gotexstyle)
            new->exstyle = new_style( dlg->exstyle->or_mask | WS_EX_LAYOUTRTL, dlg->exstyle->and_mask );
        else
            new->exstyle = new_style( WS_EX_LAYOUTRTL, 0 );
    }
1318 1319 1320
    if (dlg->font)
    {
        new->font = xmalloc( sizeof(*dlg->font) );
1321 1322 1323
        *new->font = *dlg->font;
        if (uses_larger_font( new->lvc.language )) new->font->size++;
        new->font->name = convert_string( dlg->font->name, str_unicode, 1252 );
1324
    }
1325
    new->controls = translate_controls( dlg->controls, found );
1326 1327
}

1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
static event_t *translate_accel( accelerator_t *acc, accelerator_t *new, int *found )
{
    event_t *event, *new_ev, *head = NULL, *tail = NULL;

    event = acc->events;
    while (event)
    {
        new_ev = new_event();
        *new_ev = *event;
        if (event->str) new_ev->str = translate_string( event->str, found );
        if (tail) tail->next = new_ev;
        else head = new_ev;
        new_ev->next = NULL;
        new_ev->prev = tail;
        tail = new_ev;
        event = event->next;
    }
    return head;
}

1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
static ver_value_t *translate_langcharset_values( ver_value_t *val, language_t *lang, int *found )
{
    ver_value_t *new_val, *head = NULL, *tail = NULL;
    while (val)
    {
        new_val = new_ver_value();
        *new_val = *val;
        if (val->type == val_str)
            new_val->value.str = translate_string( val->value.str, found );
        if (tail) tail->next = new_val;
        else head = new_val;
        new_val->next = NULL;
        new_val->prev = tail;
        tail = new_val;
        val = val->next;
    }
    return head;
}

static ver_value_t *translate_stringfileinfo( ver_value_t *val, language_t *lang, int *found )
{
    int i;
    ver_value_t *new_val, *head = NULL, *tail = NULL;
    const char *english_block_name[2] = { "040904b0", "040904e4" };
    char *block_name[2];
    LANGID langid = MAKELANGID( lang->id, get_default_sublang( lang ) );

    block_name[0] = strmake( "%04x%04x", langid, 1200 );
    block_name[1] = strmake( "%04x%04x", langid, get_language_codepage( lang->id, lang->sub ) );

    while (val)
    {
        new_val = new_ver_value();
        *new_val = *val;
        if (val->type == val_block)
        {
            ver_block_t *blk, *blk_head = NULL, *blk_tail = NULL;
            for (blk = val->value.block; blk; blk = blk->next)
            {
                ver_block_t *new_blk;
                char *name;
                new_blk = new_ver_block();
                *new_blk = *blk;
                name = convert_msgid_ascii( blk->name, 0 );
1392
                for (i = 0; i < ARRAY_SIZE(block_name); i++)
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
                {
                    if (!strcasecmp( name, english_block_name[i] ))
                    {
                        string_t *str;
                        str = new_string();
                        str->type     = str_char;
                        str->size     = strlen( block_name[i] ) + 1;
                        str->str.cstr = xstrdup( block_name[i] );
                        new_blk->name   = str;
                        new_blk->values = translate_langcharset_values( blk->values, lang, found );
                    }
                }
                free( name );
                if (blk_tail) blk_tail->next = new_blk;
                else blk_head = new_blk;
                new_blk->next = NULL;
                new_blk->prev = blk_tail;
                blk_tail = new_blk;
            }
            new_val->value.block = blk_head;
        }
        if (tail) tail->next = new_val;
        else head = new_val;
        new_val->next = NULL;
        new_val->prev = tail;
        tail = new_val;
        val = val->next;
    }

1422
    for (i = 0; i < ARRAY_SIZE(block_name); i++)
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
        free( block_name[i] );
    return head;
}

static ver_value_t *translate_varfileinfo( ver_value_t *val, language_t *lang )
{
    ver_value_t *new_val, *head = NULL, *tail = NULL;

    while (val)
    {
        new_val = new_ver_value();
        *new_val = *val;
        if (val->type == val_words)
        {
            char *key = convert_msgid_ascii( val->key, 0 );
            if (!strcasecmp( key, "Translation" ) &&
                val->value.words->nwords == 2 &&
                val->value.words->words[0] == MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ))
            {
                ver_words_t *new_words;
                LANGID langid;
                WORD codepage;
                langid = MAKELANGID( lang->id, get_default_sublang( lang ) );
                new_words = new_ver_words( langid );
                if (val->value.words->words[1] == 1200)
                    codepage = 1200;
                else
                    codepage = get_language_codepage( lang->id, lang->sub );
                new_val->value.words = add_ver_words( new_words, codepage );
            }
            free( key );
        }
        if (tail) tail->next = new_val;
        else head = new_val;
        new_val->next = NULL;
        new_val->prev = tail;
        tail = new_val;
        val = val->next;
    }
    return head;
}

static ver_block_t *translate_versioninfo( ver_block_t *blk, language_t *lang, int *found )
{
    ver_block_t *new_blk, *head = NULL, *tail = NULL;
    char *name;

    while (blk)
    {
        new_blk = new_ver_block();
        *new_blk = *blk;
        name = convert_msgid_ascii( blk->name, 0 );
        if (!strcasecmp( name, "stringfileinfo" ))
            new_blk->values = translate_stringfileinfo( blk->values, lang, found );
        else if (!strcasecmp( name, "varfileinfo" ))
            new_blk->values = translate_varfileinfo( blk->values, lang );
        free(name);
        if (tail) tail->next = new_blk;
        else head = new_blk;
        new_blk->next = NULL;
        new_blk->prev = tail;
        tail = new_blk;
        blk = blk->next;
    }
    return head;
}

1490
static void translate_resources( language_t *lang )
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
{
    resource_t *res;

    for (res = resource_top; res; res = res->next)
    {
        resource_t *new = NULL;
        int found = 0;

        if (!is_english( res->lan )) continue;

        switch (res->type)
        {
        case res_acc:
1504 1505
            new = dup_resource( res, lang );
            new->res.acc->events = translate_accel( res->res.acc, new->res.acc, &found );
1506 1507 1508
            break;
        case res_dlg:
            new = dup_resource( res, lang );
1509
            translate_dialog( res->res.dlg, new->res.dlg, &found );
1510 1511 1512
            break;
        case res_men:
            new = dup_resource( res, lang );
1513
            new->res.men->items = translate_items( res->res.men->items, &found );
1514 1515 1516
            break;
        case res_stt:
            new = dup_resource( res, lang );
1517
            new->res.stt = translate_stringtable( res->res.stt, lang, &found );
1518
            break;
1519 1520 1521 1522
        case res_ver:
            new = dup_resource( res, lang );
            new->res.ver->blocks = translate_versioninfo( res->res.ver->blocks, lang, &found );
            break;
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
        case res_msg:
            /* FIXME */
            break;
        default:
            break;
        }

        if (new && found)
        {
            if (new_tail) new_tail->next = new;
            else new_top = new;
            new->prev = new_tail;
            new_tail = new;
        }
    }
}

void add_translations( const char *po_dir )
{
    resource_t *res;
    char buffer[256];
    char *p, *tok, *name;
    unsigned int i;
    FILE *f;

    /* first check if we have English resources to translate */
    for (res = resource_top; res; res = res->next) if (is_english( res->lan )) break;
    if (!res) return;

1552 1553 1554 1555 1556 1557
    if (!po_dir)  /* run through the translation process to remove msg contexts */
    {
        translate_resources( new_language( LANG_ENGLISH, SUBLANG_DEFAULT ));
        goto done;
    }

1558 1559 1560
    new_top = new_tail = NULL;

    name = strmake( "%s/LINGUAS", po_dir );
1561 1562 1563 1564 1565
    if (!(f = fopen( name, "r" )))
    {
        free( name );
        return;
    }
1566 1567 1568 1569 1570 1571
    free( name );
    while (fgets( buffer, sizeof(buffer), f ))
    {
        if ((p = strchr( buffer, '#' ))) *p = 0;
        for (tok = strtok( buffer, " \t\r\n" ); tok; tok = strtok( NULL, " \t\r\n" ))
        {
1572
            for (i = 0; i < ARRAY_SIZE(languages); i++)
1573 1574
                if (!strcmp( tok, languages[i].name )) break;

1575
            if (i == ARRAY_SIZE(languages))
1576 1577
                error( "unknown language '%s'\n", tok );

1578 1579
            name = strmake( "%s/%s.mo", po_dir, tok );
            load_mo_file( name );
1580
            translate_resources( new_language(languages[i].id, languages[i].sub) );
1581
            free_mo_file();
1582 1583 1584 1585 1586
            free( name );
        }
    }
    fclose( f );

1587
done:
1588 1589 1590 1591 1592 1593 1594 1595
    /* prepend the translated resources to the global list */
    if (new_tail)
    {
        new_tail->next = resource_top;
        resource_top->prev = new_tail;
        resource_top = new_top;
    }
}