From c488556f349c82eee37f37c05144dde438351d35 Mon Sep 17 00:00:00 2001 From: "tague%netscape.com" Date: Wed, 25 Aug 1999 22:05:28 +0000 Subject: [PATCH] Implement bug #5561 (Locale support: map locale name to default character set) on windows. --- intl/uconv/public/nsIPlatformCharset.h | 2 + intl/uconv/src/nsMacCharset.cpp | 8 ++++ intl/uconv/src/nsUNIXCharset.cpp | 7 +++ intl/uconv/src/nsWinCharset.cpp | 42 +++++++++++++++++ intl/uconv/tests/makefile.win | 4 +- intl/uconv/tests/plattest.cpp | 63 ++++++++++++++++++++++++++ 6 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 intl/uconv/tests/plattest.cpp diff --git a/intl/uconv/public/nsIPlatformCharset.h b/intl/uconv/public/nsIPlatformCharset.h index c9db0d7e8b81..ac8c63620f7e 100644 --- a/intl/uconv/public/nsIPlatformCharset.h +++ b/intl/uconv/public/nsIPlatformCharset.h @@ -55,6 +55,8 @@ public: NS_IMETHOD GetCharset(nsPlatformCharsetSel selector, nsString& oResult) = 0; + NS_IMETHOD GetDefaultCharsetForLocale(const PRUnichar* localeName, PRUnichar** _retValue) = 0; + }; #endif /* nsIPlatformCharset_h__ */ diff --git a/intl/uconv/src/nsMacCharset.cpp b/intl/uconv/src/nsMacCharset.cpp index f6a407f54451..0a93af75296f 100644 --- a/intl/uconv/src/nsMacCharset.cpp +++ b/intl/uconv/src/nsMacCharset.cpp @@ -36,6 +36,8 @@ public: virtual ~nsMacCharset(); NS_IMETHOD GetCharset(nsPlatformCharsetSel selector, nsString& oResult); + NS_IMETHOD GetDefaultCharsetForLocale(const PRUnichar* localeName, PRUnichar** _retValue); + private: nsString mCharset; }; @@ -85,6 +87,12 @@ nsMacCharset::GetCharset(nsPlatformCharsetSel selector, nsString& oResult) return NS_OK; } +NS_IMETHODIMP +nsMacCharset::GetDefaultCharsetForLocale(const PRUnichar* localeName, PRUnichar** _retValue) +{ + return NS_OK; +} + class nsMacCharsetFactory : public nsIFactory { NS_DECL_ISUPPORTS diff --git a/intl/uconv/src/nsUNIXCharset.cpp b/intl/uconv/src/nsUNIXCharset.cpp index 6a0a94e95854..52657b8690d3 100644 --- a/intl/uconv/src/nsUNIXCharset.cpp +++ b/intl/uconv/src/nsUNIXCharset.cpp @@ -37,6 +37,7 @@ public: virtual ~nsUNIXCharset(); NS_IMETHOD GetCharset(nsPlatformCharsetSel selector, nsString& oResult); + NS_IMETHOD GetDefaultCharsetForLocale(const PRUnichar* localeName, PRUnichar** _retValue); private: nsString mCharset; @@ -89,6 +90,12 @@ nsUNIXCharset::GetCharset(nsPlatformCharsetSel selector, nsString& oResult) return NS_OK; } +NS_IMETHODIMP +nsUNIXCharset::GetDefaultCharsetForLocale(const PRUnichar* localeName, PRUnichar** _retValue) +{ + return NS_OK; +} + class nsUNIXCharsetFactory : public nsIFactory { NS_DECL_ISUPPORTS diff --git a/intl/uconv/src/nsWinCharset.cpp b/intl/uconv/src/nsWinCharset.cpp index 0394398b2234..411ae0463f38 100644 --- a/intl/uconv/src/nsWinCharset.cpp +++ b/intl/uconv/src/nsWinCharset.cpp @@ -23,7 +23,13 @@ #include #include "nsUConvDll.h" #include "nsPlatformCharsetFactory.h" +#include "nsIWin32Locale.h" +#include "nsCOMPtr.h" +#include "nsLocaleCID.h" +#include "nsIComponentManager.h" +NS_DEFINE_IID(kIWin32LocaleIID, NS_IWIN32LOCALE_IID); +NS_DEFINE_CID(kWin32LocaleFactoryCID, NS_WIN32LOCALEFACTORY_CID); class nsWinCharset : public nsIPlatformCharset { @@ -35,6 +41,7 @@ public: virtual ~nsWinCharset(); NS_IMETHOD GetCharset(nsPlatformCharsetSel selector, nsString& oResult); + NS_IMETHOD GetDefaultCharsetForLocale(const PRUnichar* localeName, PRUnichar** _retValue); private: nsString mCharset; }; @@ -78,6 +85,41 @@ nsWinCharset::GetCharset(nsPlatformCharsetSel selector, nsString& oResult) return NS_OK; } +NS_IMETHODIMP +nsWinCharset::GetDefaultCharsetForLocale(const PRUnichar* localeName, PRUnichar** _retValue) +{ + nsCOMPtr winLocale; + LCID localeAsLCID; + char acp_name[6]; + nsString charset("windows-1252"); + nsString localeAsNSString(localeName); + + // + // convert locale name to a code page (through the LCID) + // + nsresult result = nsComponentManager::CreateInstance(kWin32LocaleFactoryCID,nsnull,kIWin32LocaleIID, + getter_AddRefs(winLocale)); + result = winLocale->GetPlatformLocale(&localeAsNSString,&localeAsLCID); + if (NS_FAILED(result)) { *_retValue = charset.ToNewUnicode(); return result; } + + if (GetLocaleInfo(localeAsLCID,LOCALE_IDEFAULTANSICODEPAGE,acp_name,sizeof(acp_name))==0) { *_retValue = charset.ToNewUnicode(); return NS_ERROR_FAILURE; } + + // + // load property file and convert from LCID->charset + // + + nsAutoString property_url("resource:/res/wincharset.properties"); + nsURLProperties *charset_properties = new nsURLProperties(property_url); + if (!charset_properties) { *_retValue = charset.ToNewUnicode(); return NS_ERROR_OUT_OF_MEMORY; } + + nsAutoString acp_key("acp."); + acp_key.Append(acp_name); + result = charset_properties->Get(acp_key,charset); + + *_retValue = charset.ToNewUnicode(); + return result; +} + class nsWinCharsetFactory : public nsIFactory { NS_DECL_ISUPPORTS diff --git a/intl/uconv/tests/makefile.win b/intl/uconv/tests/makefile.win index 70425a205753..17c7f90a625e 100644 --- a/intl/uconv/tests/makefile.win +++ b/intl/uconv/tests/makefile.win @@ -23,7 +23,8 @@ MAKE_OBJ_TYPE = EXE PROG1 = .\$(OBJDIR)\convperf.exe PROG2 = .\$(OBJDIR)\nsTestUConv.exe PROG3 = .\$(OBJDIR)\nsconv.exe -PROGRAMS = $(PROG1) $(PROG2) $(PROG3) +PROG4 = .\$(OBJDIR)\plattest.exe +PROGRAMS = $(PROG1) $(PROG2) $(PROG3) $(PROG4) LCFLAGS=-DUSE_NSREG @@ -47,3 +48,4 @@ clobber:: $(PROG1): $(OBJDIR) convperf.cpp $(PROG2): $(OBJDIR) nsTestUConv.cpp $(PROG3): $(OBJDIR) nsconv.cpp +$(PROG4): $(OBJDIR) plattest.cpp diff --git a/intl/uconv/tests/plattest.cpp b/intl/uconv/tests/plattest.cpp new file mode 100644 index 000000000000..b88bfb7638aa --- /dev/null +++ b/intl/uconv/tests/plattest.cpp @@ -0,0 +1,63 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are Copyright (C) 1998 + * Netscape Communications Corporation. All Rights Reserved. + */ +#include "nsIPlatformCharset.h" +#include "nsILocaleService.h" +#include "nsCOMPtr.h" +#include "nsLocaleCID.h" +#include "nsIComponentManager.h" +#include + +NS_DEFINE_IID(kLocaleServiceIID,NS_ILOCALESERVICE_IID); +NS_DEFINE_CID(kLocaleServiceCID,NS_LOCALESERVICE_CID); +NS_DEFINE_IID(kPlatformCharsetIID,NS_IPLATFORMCHARSET_IID); + +int +main(int argc, const char** argv) +{ + nsCOMPtr locale_service; + nsCOMPtr locale; + nsCOMPtr platform_charset; + nsString locale_category("NSILOCALE_MESSAGES"); + PRUnichar* category_value, *charset; + nsString categoryAsNSString, charsetAsNSString; + + nsresult rv = nsComponentManager::CreateInstance(NS_PLATFORMCHARSET_PROGID,NULL,kPlatformCharsetIID,getter_AddRefs(platform_charset)); + rv = nsComponentManager::CreateInstance(kLocaleServiceCID,NULL,kLocaleServiceIID,getter_AddRefs(locale_service)); + if (NS_FAILED(rv)) return -1; + + rv = locale_service->GetSystemLocale(getter_AddRefs(locale)); + if (NS_FAILED(rv)) return -1; + + rv = locale->GetCategory(locale_category.GetUnicode(),&category_value); + if (NS_FAILED(rv)) return -1; + + rv = platform_charset->GetDefaultCharsetForLocale(category_value,&charset); + if (NS_FAILED(rv)) return -1; + + charsetAsNSString = charset; + categoryAsNSString = category_value; + printf("DefaultCharset for %s is %s\n",categoryAsNSString.ToNewCString(),charsetAsNSString.ToNewCString()); + + categoryAsNSString = "en-US"; + rv = platform_charset->GetDefaultCharsetForLocale(categoryAsNSString.GetUnicode(),&charset); + if (NS_FAILED(rv)) return -1; + + charsetAsNSString = charset; + printf("DefaultCharset for %s is %s\n",categoryAsNSString.ToNewCString(),charsetAsNSString.ToNewCString()); + + return 0; +} \ No newline at end of file