diff --git a/mozilla/intl/strres/public/nsIStringBundle.h b/mozilla/intl/strres/public/nsIStringBundle.h index 364a82e8194..4ad3152aa8e 100644 --- a/mozilla/intl/strres/public/nsIStringBundle.h +++ b/mozilla/intl/strres/public/nsIStringBundle.h @@ -53,6 +53,8 @@ class nsIStringBundleService : public nsISupports { public: NS_IMETHOD CreateBundle(nsIURL* aURL, nsILocale* aLocale, + nsIStringBundle** aResult) = 0; /* deprecated */ + NS_IMETHOD CreateBundle(const char* aURLSpec, nsILocale* aLocale, nsIStringBundle** aResult) = 0; }; diff --git a/mozilla/intl/strres/src/nsStringBundle.cpp b/mozilla/intl/strres/src/nsStringBundle.cpp index ecf882684bf..25cb1015fb7 100644 --- a/mozilla/intl/strres/src/nsStringBundle.cpp +++ b/mozilla/intl/strres/src/nsStringBundle.cpp @@ -19,6 +19,7 @@ #define NS_IMPL_IDS #include "nsID.h" +#include "nsString2.h" #include "nsIProperties.h" #include "nsIStringBundle.h" #include "nscore.h" @@ -77,6 +78,7 @@ nsStringBundle::nsStringBundle(nsIURL* aURL, nsILocale* aLocale, return; } nsIInputStream *in = nsnull; + *aResult = pNetService->OpenBlockingStream(aURL, nsnull, &in); if (NS_FAILED(*aResult)) { #ifdef NS_DEBUG @@ -149,6 +151,8 @@ public: NS_IMETHOD CreateBundle(nsIURL* aURL, nsILocale* aLocale, nsIStringBundle** aResult); + NS_IMETHOD CreateBundle(const char* aURLSpec, nsILocale* aLocale, + nsIStringBundle** aResult); }; nsStringBundleService::nsStringBundleService() @@ -162,6 +166,7 @@ nsStringBundleService::~nsStringBundleService() NS_IMPL_ISUPPORTS(nsStringBundleService, kIStringBundleServiceIID) +/* deprecated */ NS_IMETHODIMP nsStringBundleService::CreateBundle(nsIURL* aURL, nsILocale* aLocale, nsIStringBundle** aResult) @@ -183,6 +188,102 @@ nsStringBundleService::CreateBundle(nsIURL* aURL, nsILocale* aLocale, return ret; } +NS_IMETHODIMP +nsStringBundleService::CreateBundle(const char* aURLSpec, nsILocale* aLocale, + nsIStringBundle** aResult) +{ + + /* locale binding */ + nsString2 strFile2; + if (aLocale) { + nsString lc_name; + nsString catagory("NSILOCALE_MESSAGES"); + nsresult result = aLocale->GetCategory(&catagory, &lc_name); + + NS_ASSERTION(result==NS_OK,"nsStringBundleService::CreateBundle: locale.GetCatagory failed"); + NS_ASSERTION(lc_name.Length()>0,"nsStringBundleService::CreateBundle: locale.GetCatagory failed"); + + /* find the place to concatenate locale name + */ + PRInt32 count = 0; + nsString2 strFile(aURLSpec); + PRInt32 mylen = strFile.Length(); + + /* assume the name always end with this + */ + PRInt32 dot = strFile.RFindCharInSet("."); + count = strFile.Left(strFile2, (dot>0)?dot:mylen); + + /* get lang-country code + */ + PRInt32 dash = lc_name.FindCharInSet("-"); + if (dash > 0) { + /* + */ + nsString lc_name2; + nsString right; + count = lc_name.Left(lc_name2, dash); + count = lc_name.Right(right, (lc_name.Length()-dash-1)); + lc_name2 += "_"; + lc_name2 += right; + strFile2 += "_"; + strFile2 += lc_name2; + } + else { + strFile2 += "_"; + strFile2 += lc_name; + } + + /* insert it + */ + if (dot > 0) { + nsString2 fileRight; + count = strFile.Right(fileRight, mylen-dot); + strFile2 += fileRight; + } +#ifdef NS_DEBUG + printf("\n--NEW URL--%s\n", strFile2.ToNewCString()); +#endif + } + /* locale binding */ + + nsresult ret = NS_OK; + + /* get the url + */ + nsINetService* pNetService = nsnull; + ret = nsServiceManager::GetService(kNetServiceCID, kINetServiceIID, + (nsISupports**) &pNetService); + if (NS_FAILED(ret)) { + printf("cannot get net service\n"); + return 1; + } + nsIURL *url = nsnull; + ret = pNetService->CreateURL(&url, strFile2, nsnull, nsnull, + nsnull); + if (NS_FAILED(ret)) { + printf("cannot create URL\n"); + return 1; + } + + /* do it + */ + nsStringBundle* bundle = new nsStringBundle(url, aLocale, &ret); + if (!bundle) { + return NS_ERROR_OUT_OF_MEMORY; + } + if (NS_FAILED(ret)) { + delete bundle; + return ret; + } + ret = bundle->QueryInterface(kIStringBundleIID, (void**) aResult); + if (NS_FAILED(ret)) { + delete bundle; + } + delete url; + return ret; +} + class nsStringBundleServiceFactory : public nsIFactory { public: diff --git a/mozilla/intl/strres/tests/StringBundleTest.cpp b/mozilla/intl/strres/tests/StringBundleTest.cpp index e62a2cedd8c..299c2e58dba 100644 --- a/mozilla/intl/strres/tests/StringBundleTest.cpp +++ b/mozilla/intl/strres/tests/StringBundleTest.cpp @@ -45,6 +45,68 @@ static NS_DEFINE_IID(kINetServiceIID, NS_INETSERVICE_IID); static NS_DEFINE_IID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID); static NS_DEFINE_IID(kIStringBundleServiceIID, NS_ISTRINGBUNDLESERVICE_IID); +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// let add some locale stuff +// +//////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +#include "nsILocale.h" +#include "nsILocaleFactory.h" +#include "nsLocaleCID.h" + +NS_DEFINE_CID(kLocaleFactoryCID, NS_LOCALEFACTORY_CID); +NS_DEFINE_IID(kILocaleFactoryIID, NS_ILOCALEFACTORY_IID); +// +// +// +nsILocale* +get_applocale(void) +{ + nsresult result; + nsILocaleFactory* localeFactory; + nsILocale* locale; + nsString* catagory; + nsString* value; + + result = nsComponentManager::FindFactory(kLocaleFactoryCID, + (nsIFactory**)&localeFactory); + NS_ASSERTION(localeFactory!=NULL,"nsLocaleTest: factory_create_interface failed."); + NS_ASSERTION(result==NS_OK,"nsLocaleTest: factory_create_interface failed"); + + // + // test GetApplicationLocale + // + result = localeFactory->GetApplicationLocale(&locale); + NS_ASSERTION(result==NS_OK,"nsLocaleTest: factory_get_locale failed"); + NS_ASSERTION(locale!=NULL,"nsLocaleTest: factory_get_locale failed"); + + // + // test and make sure the locale is a valid Interface + // + locale->AddRef(); + + catagory = new nsString("NSILOCALE_MESSAGES"); + value = new nsString(); + + result = locale->GetCategory(catagory,value); + NS_ASSERTION(result==NS_OK,"nsLocaleTest: factory_get_locale failed"); + NS_ASSERTION(value->Length()>0,"nsLocaleTest: factory_get_locale failed"); + + locale->Release(); + delete catagory; + delete value; + + localeFactory->Release(); + return locale; +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// end of locale stuff +// +//////////////////////////////////////////////////////////////////////////////////////////////////// int main(int argc, char *argv[]) { @@ -79,26 +141,19 @@ main(int argc, char *argv[]) printf("CreateThreadEventQueue failed\n"); return 1; } - - nsINetService* pNetService = nsnull; - ret = nsServiceManager::GetService(kNetServiceCID, kINetServiceIID, - (nsISupports**) &pNetService); - if (NS_FAILED(ret)) { - printf("cannot get net service\n"); - return 1; - } - nsIURL *url = nsnull; - ret = pNetService->CreateURL(&url, nsString(TEST_URL), nsnull, nsnull, - nsnull); - if (NS_FAILED(ret)) { - printf("cannot create URL\n"); - return 1; - } - - nsILocale* locale = nsnull; + nsILocale* locale = get_applocale(); nsIStringBundle* bundle = nsnull; + +#if 0 ret = service->CreateBundle(url, locale, &bundle); +#else + ret = service->CreateBundle(TEST_URL, locale, &bundle); +#endif + /* free it + */ + locale->Release(); + if (NS_FAILED(ret)) { printf("cannot create instance\n"); return 1;