/*static*/ nsresult DateTimeFormat::Initialize() { nsAutoString localeStr; nsAutoCString charset; nsresult rv = NS_OK; if (mDecoder) { return NS_OK; } charset.AssignLiteral("windows-1252"); nsCOMPtr<nsILocaleService> localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv); if (NS_SUCCEEDED(rv)) { nsCOMPtr<nsILocale> appLocale; rv = localeService->GetApplicationLocale(getter_AddRefs(appLocale)); if (NS_SUCCEEDED(rv)) { rv = appLocale->GetCategory(NS_LITERAL_STRING("NSILOCALE_TIME##PLATFORM"), localeStr); NS_ASSERTION(NS_SUCCEEDED(rv), "failed to get app locale info"); } } if (NS_SUCCEEDED(rv) && !localeStr.IsEmpty()) { nsCOMPtr<nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv); if (NS_SUCCEEDED(rv)) { nsAutoCString mappedCharset; rv = platformCharset->GetDefaultCharsetForLocale(localeStr, mappedCharset); if (NS_SUCCEEDED(rv)) { charset = mappedCharset; } } } mDecoder = EncodingUtils::DecoderForEncoding(charset); LocalePreferred24hour(); return rv; }
// init this interface to a specified locale nsresult nsDateTimeFormatUnix::Initialize(nsILocale* locale) { nsAutoString localeStr; NS_NAMED_LITERAL_STRING(aCategory, "NSILOCALE_TIME##PLATFORM"); nsresult res = NS_OK; // use cached info if match with stored locale if (!locale) { if (!mLocale.IsEmpty() && mLocale.Equals(mAppLocale, nsCaseInsensitiveStringComparator())) { return NS_OK; } } else { res = locale->GetCategory(aCategory, localeStr); if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { if (!mLocale.IsEmpty() && mLocale.Equals(localeStr, nsCaseInsensitiveStringComparator())) { return NS_OK; } } } mCharset.AssignLiteral("ISO-8859-1"); mPlatformLocale.Assign("en_US"); // get locale name string, use app default if no locale specified if (!locale) { nsCOMPtr<nsILocaleService> localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID, &res); if (NS_SUCCEEDED(res)) { nsCOMPtr<nsILocale> appLocale; res = localeService->GetApplicationLocale(getter_AddRefs(appLocale)); if (NS_SUCCEEDED(res)) { res = appLocale->GetCategory(aCategory, localeStr); if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { NS_ASSERTION(NS_SUCCEEDED(res), "failed to get app locale info"); mAppLocale = localeStr; // cache app locale name } } } } else { res = locale->GetCategory(aCategory, localeStr); NS_ASSERTION(NS_SUCCEEDED(res), "failed to get locale info"); } if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) { mLocale = localeStr; // cache locale name nsPosixLocale::GetPlatformLocale(mLocale, mPlatformLocale); nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &res); if (NS_SUCCEEDED(res)) { nsAutoCString mappedCharset; res = platformCharset->GetDefaultCharsetForLocale(mLocale, mappedCharset); if (NS_SUCCEEDED(res)) { mCharset = mappedCharset; } } } // Initialize unicode decoder nsCOMPtr <nsICharsetConverterManager> charsetConverterManager; charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res); if (NS_SUCCEEDED(res)) { res = charsetConverterManager->GetUnicodeDecoder(mCharset.get(), getter_AddRefs(mDecoder)); } LocalePreferred24hour(); return res; }