Explicitly bind gettext to the correct encoding on Windows.

Original patch from Hiroshi Inoue.
This commit is contained in:
Magnus Hagander 2009-01-22 10:09:48 +00:00
parent 95aaf25f39
commit 5841aa86eb
1 changed files with 57 additions and 16 deletions

View File

@ -4,7 +4,7 @@
* (currently mule internal code (mic) is used)
* Tatsuo Ishii
*
* $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.77 2009/01/19 15:34:23 mha Exp $
* $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.78 2009/01/22 10:09:48 mha Exp $
*/
#include "postgres.h"
@ -849,6 +849,46 @@ cliplen(const char *str, int len, int limit)
return l;
}
#if defined(ENABLE_NLS) && defined(WIN32)
static const struct codeset_map {
int encoding;
const char *codeset;
} codeset_map_array[] = {
{PG_UTF8, "UTF-8"},
{PG_LATIN1, "LATIN1"},
{PG_LATIN2, "LATIN2"},
{PG_LATIN3, "LATIN3"},
{PG_LATIN4, "LATIN4"},
{PG_ISO_8859_5, "ISO-8859-5"},
{PG_ISO_8859_6, "ISO_8859-6"},
{PG_ISO_8859_7, "ISO-8859-7"},
{PG_ISO_8859_8, "ISO-8859-8"},
{PG_LATIN5, "LATIN5"},
{PG_LATIN6, "LATIN6"},
{PG_LATIN7, "LATIN7"},
{PG_LATIN8, "LATIN8"},
{PG_LATIN9, "LATIN-9"},
{PG_LATIN10, "LATIN10"},
{PG_KOI8R, "KOI8-R"},
{PG_WIN1250, "CP1250"},
{PG_WIN1251, "CP1251"},
{PG_WIN1252, "CP1252"},
{PG_WIN1253, "CP1253"},
{PG_WIN1254, "CP1254"},
{PG_WIN1255, "CP1255"},
{PG_WIN1256, "CP1256"},
{PG_WIN1257, "CP1257"},
{PG_WIN1258, "CP1258"},
{PG_WIN866, "CP866"},
{PG_WIN874, "CP874"},
{PG_EUC_CN, "EUC-CN"},
{PG_EUC_JP, "EUC-JP"},
{PG_EUC_KR, "EUC-KR"},
{PG_EUC_TW, "EUC-TW"},
{PG_EUC_JIS_2004, "EUC-JP"}
};
#endif /* WIN32 */
void
SetDatabaseEncoding(int encoding)
{
@ -859,22 +899,23 @@ SetDatabaseEncoding(int encoding)
Assert(DatabaseEncoding->encoding == encoding);
/*
* On Windows, we allow UTF-8 database encoding to be used with any
* locale setting, because UTF-8 requires special handling anyway.
* But this means that gettext() might be misled about what output
* encoding it should use, so we have to tell it explicitly.
*
* In future we might want to call bind_textdomain_codeset
* unconditionally, but that requires knowing how to spell the codeset
* name properly for all encodings on all platforms, which might be
* problematic.
*
* This is presently unnecessary, but harmless, on non-Windows platforms.
* On Windows, we need to explicitly bind gettext to the correct
* encoding, because gettext() tends to get confused.
*/
#ifdef ENABLE_NLS
if (encoding == PG_UTF8)
if (bind_textdomain_codeset(textdomain(NULL), "UTF-8") == NULL)
elog(LOG, "bind_textdomain_codeset failed");
#if defined(ENABLE_NLS) && defined(WIN32)
{
int i;
for (i = 0; i < sizeof(codeset_map_array) / sizeof(codeset_map_array[0]); i++)
{
if (codeset_map_array[i].encoding == encoding)
{
if (bind_textdomain_codeset(textdomain(NULL), codeset_map_array[i].codeset) == NULL)
elog(LOG, "bind_textdomain_codeset failed");
break;
}
}
}
#endif
}