--- cvs/wine/ole/ole2nls.c	Tue Aug 15 18:19:52 2000
+++ wine/ole/ole2nls.c	Tue Aug 15 18:43:47 2000
@@ -159,6 +159,143 @@
 	return GetUserDefaultLCID();
 }
 
+#define NLS_MAX_LANGUAGES 20
+typedef struct {
+    char lang[128];
+    char country[128];
+    LANGID lang_id[NLS_MAX_LANGUAGES];
+    char lang_id_country[NLS_MAX_LANGUAGES][3];
+    int n_lang_id;
+} LANG_FIND_DATA;
+
+static BOOL CALLBACK NLS_FindLanguageID_ProcA(HMODULE hModule, LPCSTR type,
+		LPCSTR name, WORD LangID, LONG lParam)
+{
+    LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
+    LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
+    char buf_SISO639LANGNAME[128];
+    char buf_SISO3166CTRYNAME[128];
+    char buf_SENGLANGUAGE[128];
+
+    TRACE("%04X\n", (UINT)LangID);
+    if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
+	return TRUE; /* continue search */
+
+    buf_SISO639LANGNAME[0] = 0;
+    buf_SISO3166CTRYNAME[0] = 0;
+    buf_SENGLANGUAGE[0] = 0;
+
+    GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE,
+	buf_SISO639LANGNAME, sizeof(buf_SISO639LANGNAME));
+    TRACE("LOCALE_SISO639LANGNAME: %s\n", buf_SISO639LANGNAME);
+
+    GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
+	buf_SISO3166CTRYNAME, sizeof(buf_SISO3166CTRYNAME));
+    TRACE("LOCALE_SISO3166CTRYNAME: %s\n", buf_SISO3166CTRYNAME);
+
+    if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_SISO639LANGNAME)) {
+	if(l_data->country && strlen(l_data->country) > 0) {
+	    if(!strcasecmp(l_data->country, buf_SISO3166CTRYNAME)) {
+		l_data->lang_id[0] = LangID;
+		l_data->n_lang_id = 1;
+		TRACE("Found lang_id %04X for %s_%s\n", LangID, l_data->lang, l_data->country);
+		return FALSE; /* stop enumeration */
+	    }
+	}
+	else { /* l_data->country not specified */
+	    if(l_data->n_lang_id < NLS_MAX_LANGUAGES) {
+		l_data->lang_id[l_data->n_lang_id] = LangID;
+		strncpy(l_data->lang_id_country[l_data->n_lang_id], buf_SISO3166CTRYNAME, 3);
+		l_data->n_lang_id++;
+	    }
+	}
+    }
+
+    /* Just in case, check LOCALE_SENGLANGUAGE too,
+     * in hope that possible alias name might have that value
+     */
+    GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
+	buf_SENGLANGUAGE, sizeof(buf_SENGLANGUAGE));
+    TRACE("LOCALE_SENGLANGUAGE: %s\n", buf_SENGLANGUAGE);
+
+    if(l_data->lang && strlen(l_data->lang) > 0 &&
+		!strcasecmp(l_data->lang, buf_SENGLANGUAGE)) {
+	l_data->lang_id[0] = LangID;
+	l_data->n_lang_id = 1;
+	TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
+	return FALSE; /* stop enumeration */
+    }
+
+    return TRUE; /* continue search */
+}
+
+/* FIXME: Charset and Dialect are not handled */
+static LANGID NLS_GetLanguageID(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
+{
+    LANG_FIND_DATA l_data;
+    char lang_string[256];
+
+    if(!Lang || strlen(Lang) < 2) {
+	l_data.lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
+	goto END;
+    }
+
+    memset(&l_data, 0, sizeof(LANG_FIND_DATA));
+
+    strncpy(l_data.lang, Lang, sizeof(l_data.lang));
+
+    if(Country && strlen(Country) >= 2)
+	strncpy(l_data.country, Country, sizeof(l_data.country));
+
+    EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
+	(LPCSTR)LOCALE_ILANGUAGE, NLS_FindLanguageID_ProcA, (LONG)&l_data);
+
+    strcpy(lang_string, l_data.lang);
+    if(l_data.country && strlen(l_data.country) > 0) {
+	strcat(lang_string, "_");
+	strcat(lang_string, l_data.country);
+    }
+
+    if(l_data.n_lang_id == 0) {
+	if(l_data.country && strlen(l_data.country) > 0) {
+	    MESSAGE("Warning: Language '%s' was not found, retrying without country name...\n", lang_string);
+	    l_data.country[0] = 0;
+	    EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
+		(LPCSTR)LOCALE_ILANGUAGE, NLS_FindLanguageID_ProcA, (LONG)&l_data);
+	}
+    }
+
+    /* Re-evaluate lang_string */
+    strcpy(lang_string, l_data.lang);
+    if(l_data.country && strlen(l_data.country) > 0) {
+	strcat(lang_string, "_");
+	strcat(lang_string, l_data.country);
+    }
+
+    if(l_data.n_lang_id == 0) {
+	MESSAGE("Warning: Language '%s' was not recognized, defaulting to English\n",
+		lang_string);
+	l_data.lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
+    }
+    else {
+	if(l_data.n_lang_id == 1)
+	    TRACE("For language '%s' lang_id %04X was found\n", lang_string, l_data.lang_id[0]);
+	else { /* l_data->n_lang_id > 1 */
+	    int i;
+	    MESSAGE("For language '%s' several lang_ids were found:\n", lang_string);
+	    for(i = 0; i < l_data.n_lang_id; i++)
+		MESSAGE("%s_%.2s - %04X; ", l_data.lang, l_data.lang_id_country[i], l_data.lang_id[i]);
+
+	    MESSAGE("\nInstead of using first in the list, suggest to define\n"
+		    "your LANG environment variable like this: LANG=%s_%.2s\n",
+		    l_data.lang, l_data.lang_id_country[0]);
+	}
+    }
+END:
+    TRACE("Returning %04X\n", l_data.lang_id[0]);
+    return l_data.lang_id[0];
+}
+
 /***********************************************************************
  *         GetUserDefaultLangID       [KERNEL32.426]
  */
@@ -192,7 +329,7 @@
 			charset=strchr(lang,'.'); if (charset) *charset++='\0';
 			country=strchr(lang,'_'); if (country) *country++='\0';
 			
-			userLCID = MAIN_GetLanguageID(lang, country, charset, dialect);
+			userLCID = NLS_GetLanguageID(lang, country, charset, dialect);
 			
 			lang=next;
 		} while (lang && !userLCID);
