2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-02-02 22:31:36 -08:00
|
|
|
#ifdef MOZ_WIDGET_QT
|
|
|
|
#include <QString>
|
|
|
|
#include <QtCore/QLocale>
|
2010-09-17 11:56:48 -07:00
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
2011-01-05 14:17:09 -08:00
|
|
|
#include "nsAutoPtr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsILocale.h"
|
|
|
|
#include "nsILocaleService.h"
|
|
|
|
#include "nsLocale.h"
|
|
|
|
#include "nsLocaleCID.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "prprf.h"
|
2007-09-18 16:12:06 -07:00
|
|
|
#include "nsTArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#if defined(XP_WIN)
|
2011-05-02 01:49:11 -07:00
|
|
|
# include "nsWin32Locale.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#elif defined(XP_OS2)
|
|
|
|
# include "unidef.h"
|
|
|
|
# include "nsIOS2Locale.h"
|
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
# include <Carbon/Carbon.h>
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 11:10:24 -08:00
|
|
|
#elif defined(XP_UNIX)
|
2007-03-22 10:30:00 -07:00
|
|
|
# include <locale.h>
|
|
|
|
# include <stdlib.h>
|
2011-06-22 00:34:27 -07:00
|
|
|
# include "nsPosixLocale.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// implementation constants
|
|
|
|
const int LocaleListLength = 6;
|
|
|
|
const char* LocaleList[LocaleListLength] =
|
|
|
|
{
|
|
|
|
NSILOCALE_COLLATE,
|
|
|
|
NSILOCALE_CTYPE,
|
|
|
|
NSILOCALE_MONETARY,
|
|
|
|
NSILOCALE_NUMERIC,
|
|
|
|
NSILOCALE_TIME,
|
|
|
|
NSILOCALE_MESSAGE
|
|
|
|
};
|
|
|
|
|
|
|
|
#define NSILOCALE_MAX_ACCEPT_LANGUAGE 16
|
|
|
|
#define NSILOCALE_MAX_ACCEPT_LENGTH 18
|
|
|
|
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 11:10:24 -08:00
|
|
|
#if (defined(XP_UNIX) && !defined(XP_MACOSX)) || defined(XP_OS2)
|
2007-03-22 10:30:00 -07:00
|
|
|
static int posix_locale_category[LocaleListLength] =
|
|
|
|
{
|
|
|
|
LC_COLLATE,
|
|
|
|
LC_CTYPE,
|
|
|
|
LC_MONETARY,
|
|
|
|
LC_NUMERIC,
|
|
|
|
LC_TIME,
|
|
|
|
#ifdef HAVE_I18N_LC_MESSAGES
|
|
|
|
LC_MESSAGES
|
|
|
|
#else
|
|
|
|
LC_CTYPE
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsILocaleService implementation
|
|
|
|
//
|
|
|
|
class nsLocaleService: public nsILocaleService {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsISupports
|
|
|
|
//
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsILocaleService
|
|
|
|
//
|
|
|
|
NS_DECL_NSILOCALESERVICE
|
|
|
|
|
|
|
|
|
|
|
|
nsLocaleService(void);
|
|
|
|
virtual ~nsLocaleService(void);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
nsresult SetSystemLocale(void);
|
|
|
|
nsresult SetApplicationLocale(void);
|
|
|
|
|
|
|
|
nsCOMPtr<nsILocale> mSystemLocale;
|
|
|
|
nsCOMPtr<nsILocale> mApplicationLocale;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsLocaleService methods
|
|
|
|
//
|
|
|
|
nsLocaleService::nsLocaleService(void)
|
|
|
|
: mSystemLocale(0), mApplicationLocale(0)
|
|
|
|
{
|
|
|
|
#ifdef XP_WIN
|
|
|
|
nsAutoString xpLocale;
|
2011-05-02 01:49:11 -07:00
|
|
|
//
|
|
|
|
// get the system LCID
|
|
|
|
//
|
|
|
|
LCID win_lcid = GetSystemDefaultLCID();
|
|
|
|
NS_ENSURE_TRUE(win_lcid, );
|
|
|
|
nsWin32Locale::GetXPLocale(win_lcid, xpLocale);
|
|
|
|
nsresult rv = NewLocale(xpLocale, getter_AddRefs(mSystemLocale));
|
|
|
|
NS_ENSURE_SUCCESS(rv, );
|
|
|
|
|
|
|
|
//
|
|
|
|
// get the application LCID
|
|
|
|
//
|
|
|
|
win_lcid = GetUserDefaultLCID();
|
|
|
|
NS_ENSURE_TRUE(win_lcid, );
|
|
|
|
nsWin32Locale::GetXPLocale(win_lcid, xpLocale);
|
|
|
|
rv = NewLocale(xpLocale, getter_AddRefs(mApplicationLocale));
|
|
|
|
NS_ENSURE_SUCCESS(rv, );
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 11:10:24 -08:00
|
|
|
#if defined(XP_UNIX) && !defined(XP_MACOSX)
|
2011-06-22 00:34:27 -07:00
|
|
|
nsRefPtr<nsLocale> resultLocale(new nsLocale());
|
|
|
|
NS_ENSURE_TRUE(resultLocale, );
|
2011-02-02 22:31:36 -08:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_QT
|
2011-06-22 00:34:27 -07:00
|
|
|
const char* lang = QLocale::system().name().toAscii();
|
2011-02-02 22:31:36 -08:00
|
|
|
#else
|
2011-06-22 00:34:27 -07:00
|
|
|
// Get system configuration
|
|
|
|
const char* lang = getenv("LANG");
|
2011-02-02 22:31:36 -08:00
|
|
|
#endif
|
|
|
|
|
2011-06-22 00:34:27 -07:00
|
|
|
nsAutoString xpLocale, platformLocale;
|
|
|
|
nsAutoString category, category_platform;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i = 0; i < LocaleListLength; i++ ) {
|
|
|
|
nsresult result;
|
|
|
|
// setlocale( , "") evaluates LC_* and LANG
|
|
|
|
char* lc_temp = setlocale(posix_locale_category[i], "");
|
|
|
|
CopyASCIItoUTF16(LocaleList[i], category);
|
|
|
|
category_platform = category;
|
|
|
|
category_platform.AppendLiteral("##PLATFORM");
|
2012-07-30 07:20:58 -07:00
|
|
|
if (lc_temp != nullptr) {
|
2011-06-22 00:34:27 -07:00
|
|
|
result = nsPosixLocale::GetXPLocale(lc_temp, xpLocale);
|
|
|
|
CopyASCIItoUTF16(lc_temp, platformLocale);
|
|
|
|
} else {
|
2012-07-30 07:20:58 -07:00
|
|
|
if ( lang == nullptr ) {
|
2011-06-22 00:34:27 -07:00
|
|
|
platformLocale.AssignLiteral("en_US");
|
|
|
|
result = nsPosixLocale::GetXPLocale("en-US", xpLocale);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2011-06-22 00:34:27 -07:00
|
|
|
CopyASCIItoUTF16(lang, platformLocale);
|
|
|
|
result = nsPosixLocale::GetXPLocale(lang, xpLocale);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2011-06-22 00:34:27 -07:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resultLocale->AddCategory(category, xpLocale);
|
|
|
|
resultLocale->AddCategory(category_platform, platformLocale);
|
|
|
|
}
|
|
|
|
mSystemLocale = do_QueryInterface(resultLocale);
|
|
|
|
mApplicationLocale = do_QueryInterface(resultLocale);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 11:10:24 -08:00
|
|
|
#endif // XP_UNIX
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef XP_OS2
|
|
|
|
nsCOMPtr<nsIOS2Locale> os2Converter = do_GetService(NS_OS2LOCALE_CONTRACTID);
|
|
|
|
nsAutoString xpLocale;
|
|
|
|
if (os2Converter) {
|
|
|
|
nsAutoString category;
|
|
|
|
int i;
|
|
|
|
|
2011-01-05 14:17:09 -08:00
|
|
|
nsRefPtr<nsLocale> resultLocale(new nsLocale());
|
2007-03-22 10:30:00 -07:00
|
|
|
if ( resultLocale == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LocaleObject locale_object = NULL;
|
|
|
|
int result = UniCreateLocaleObject(UNI_UCS_STRING_POINTER,
|
|
|
|
(UniChar *)L"", &locale_object);
|
|
|
|
if (result != ULS_SUCCESS) {
|
|
|
|
int result = UniCreateLocaleObject(UNI_UCS_STRING_POINTER,
|
|
|
|
(UniChar *)L"en_US", &locale_object);
|
|
|
|
}
|
|
|
|
char* lc_temp;
|
|
|
|
for( i = 0; i < LocaleListLength; i++ ) {
|
2012-07-30 07:20:58 -07:00
|
|
|
lc_temp = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
UniQueryLocaleObject(locale_object,
|
|
|
|
posix_locale_category[i],
|
|
|
|
UNI_MBS_STRING_POINTER,
|
|
|
|
(void **)&lc_temp);
|
2011-11-01 00:16:48 -07:00
|
|
|
category.AssignASCII(LocaleList[i]);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult result;
|
2012-07-30 07:20:58 -07:00
|
|
|
if (lc_temp != nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
result = os2Converter->GetXPLocale(lc_temp, xpLocale);
|
|
|
|
else {
|
|
|
|
char* lang = getenv("LANG");
|
2012-07-30 07:20:58 -07:00
|
|
|
if ( lang == nullptr ) {
|
2007-03-22 10:30:00 -07:00
|
|
|
result = os2Converter->GetXPLocale("en-US", xpLocale);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = os2Converter->GetXPLocale(lang, xpLocale);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
UniFreeMem(lc_temp);
|
|
|
|
UniFreeLocaleObject(locale_object);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resultLocale->AddCategory(category, xpLocale);
|
|
|
|
UniFreeMem(lc_temp);
|
|
|
|
}
|
|
|
|
UniFreeLocaleObject(locale_object);
|
|
|
|
mSystemLocale = do_QueryInterface(resultLocale);
|
|
|
|
mApplicationLocale = do_QueryInterface(resultLocale);
|
|
|
|
} // if ( NS_SUCCEEDED )...
|
|
|
|
#endif // XP_OS2
|
|
|
|
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// Get string representation of user's current locale
|
|
|
|
CFLocaleRef userLocaleRef = ::CFLocaleCopyCurrent();
|
|
|
|
CFStringRef userLocaleStr = ::CFLocaleGetIdentifier(userLocaleRef);
|
|
|
|
::CFRetain(userLocaleStr);
|
|
|
|
|
2007-09-18 16:12:06 -07:00
|
|
|
nsAutoTArray<UniChar, 32> buffer;
|
2007-03-22 10:30:00 -07:00
|
|
|
int size = ::CFStringGetLength(userLocaleStr);
|
2007-09-21 02:12:29 -07:00
|
|
|
if (buffer.SetLength(size + 1))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
CFRange range = ::CFRangeMake(0, size);
|
2007-09-18 16:12:06 -07:00
|
|
|
::CFStringGetCharacters(userLocaleStr, range, buffer.Elements());
|
|
|
|
buffer[size] = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Convert the locale string to the format that Mozilla expects
|
2007-09-18 16:12:06 -07:00
|
|
|
nsAutoString xpLocale(buffer.Elements());
|
2007-03-22 10:30:00 -07:00
|
|
|
xpLocale.ReplaceChar('_', '-');
|
|
|
|
|
|
|
|
nsresult rv = NewLocale(xpLocale, getter_AddRefs(mSystemLocale));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mApplicationLocale = mSystemLocale;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
::CFRelease(userLocaleStr);
|
|
|
|
::CFRelease(userLocaleRef);
|
|
|
|
|
|
|
|
NS_ASSERTION(mApplicationLocale, "Failed to create locale objects");
|
|
|
|
#endif // XP_MACOSX
|
|
|
|
}
|
|
|
|
|
|
|
|
nsLocaleService::~nsLocaleService(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsLocaleService, nsILocaleService)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocaleService::NewLocale(const nsAString &aLocale, nsILocale **_retval)
|
|
|
|
{
|
2007-08-14 20:54:16 -07:00
|
|
|
nsresult result;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*_retval = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-01-05 14:17:09 -08:00
|
|
|
nsRefPtr<nsLocale> resultLocale(new nsLocale());
|
2007-08-14 20:54:16 -07:00
|
|
|
if (!resultLocale) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
for (PRInt32 i = 0; i < LocaleListLength; i++) {
|
2011-11-01 00:16:48 -07:00
|
|
|
NS_ConvertASCIItoUTF16 category(LocaleList[i]);
|
2007-08-14 20:54:16 -07:00
|
|
|
result = resultLocale->AddCategory(category, aLocale);
|
2011-01-05 14:17:09 -08:00
|
|
|
if (NS_FAILED(result)) return result;
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 11:10:24 -08:00
|
|
|
#if defined(XP_UNIX) && !defined(XP_MACOSX)
|
2007-08-14 20:54:16 -07:00
|
|
|
category.AppendLiteral("##PLATFORM");
|
|
|
|
result = resultLocale->AddCategory(category, aLocale);
|
2011-01-05 14:17:09 -08:00
|
|
|
if (NS_FAILED(result)) return result;
|
2007-08-14 20:54:16 -07:00
|
|
|
#endif
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-14 20:54:16 -07:00
|
|
|
NS_ADDREF(*_retval = resultLocale);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocaleService::GetSystemLocale(nsILocale **_retval)
|
|
|
|
{
|
|
|
|
if (mSystemLocale) {
|
|
|
|
NS_ADDREF(*_retval = mSystemLocale);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*_retval = (nsILocale*)nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocaleService::GetApplicationLocale(nsILocale **_retval)
|
|
|
|
{
|
|
|
|
if (mApplicationLocale) {
|
|
|
|
NS_ADDREF(*_retval = mApplicationLocale);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*_retval=(nsILocale*)nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocaleService::GetLocaleFromAcceptLanguage(const char *acceptLanguage, nsILocale **_retval)
|
|
|
|
{
|
|
|
|
char* input;
|
|
|
|
char* cPtr;
|
|
|
|
char* cPtr1;
|
|
|
|
char* cPtr2;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int countLang = 0;
|
|
|
|
char acceptLanguageList[NSILOCALE_MAX_ACCEPT_LANGUAGE][NSILOCALE_MAX_ACCEPT_LENGTH];
|
|
|
|
nsresult result;
|
|
|
|
|
|
|
|
input = new char[strlen(acceptLanguage)+1];
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(input!=nullptr,"nsLocaleFactory::GetLocaleFromAcceptLanguage: memory allocation failed.");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (input == (char*)NULL){ return NS_ERROR_OUT_OF_MEMORY; }
|
|
|
|
|
|
|
|
strcpy(input, acceptLanguage);
|
|
|
|
cPtr1 = input-1;
|
|
|
|
cPtr2 = input;
|
|
|
|
|
|
|
|
/* put in standard form */
|
|
|
|
while (*(++cPtr1)) {
|
|
|
|
if (isalpha(*cPtr1)) *cPtr2++ = tolower(*cPtr1); /* force lower case */
|
|
|
|
else if (isspace(*cPtr1)) ; /* ignore any space */
|
|
|
|
else if (*cPtr1=='-') *cPtr2++ = '_'; /* "-" -> "_" */
|
|
|
|
else if (*cPtr1=='*') ; /* ignore "*" */
|
|
|
|
else *cPtr2++ = *cPtr1; /* else unchanged */
|
|
|
|
}
|
|
|
|
*cPtr2 = '\0';
|
|
|
|
|
|
|
|
countLang = 0;
|
|
|
|
|
|
|
|
if (strchr(input,';')) {
|
|
|
|
/* deal with the quality values */
|
|
|
|
|
|
|
|
float qvalue[NSILOCALE_MAX_ACCEPT_LANGUAGE];
|
|
|
|
float qSwap;
|
|
|
|
float bias = 0.0f;
|
|
|
|
char* ptrLanguage[NSILOCALE_MAX_ACCEPT_LANGUAGE];
|
|
|
|
char* ptrSwap;
|
|
|
|
|
|
|
|
cPtr = nsCRT::strtok(input,",",&cPtr2);
|
|
|
|
while (cPtr) {
|
|
|
|
qvalue[countLang] = 1.0f;
|
|
|
|
/* add extra parens to get rid of warning */
|
2012-07-30 07:20:58 -07:00
|
|
|
if ((cPtr1 = strchr(cPtr,';')) != nullptr) {
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_sscanf(cPtr1,";q=%f",&qvalue[countLang]);
|
|
|
|
*cPtr1 = '\0';
|
|
|
|
}
|
|
|
|
if (strlen(cPtr)<NSILOCALE_MAX_ACCEPT_LANGUAGE) { /* ignore if too long */
|
|
|
|
qvalue[countLang] -= (bias += 0.0001f); /* to insure original order */
|
|
|
|
ptrLanguage[countLang++] = cPtr;
|
|
|
|
if (countLang>=NSILOCALE_MAX_ACCEPT_LANGUAGE) break; /* quit if too many */
|
|
|
|
}
|
|
|
|
cPtr = nsCRT::strtok(cPtr2,",",&cPtr2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sort according to decending qvalue */
|
|
|
|
/* not a very good algorithm, but count is not likely large */
|
|
|
|
for ( i=0 ; i<countLang-1 ; i++ ) {
|
|
|
|
for ( j=i+1 ; j<countLang ; j++ ) {
|
|
|
|
if (qvalue[i]<qvalue[j]) {
|
|
|
|
qSwap = qvalue[i];
|
|
|
|
qvalue[i] = qvalue[j];
|
|
|
|
qvalue[j] = qSwap;
|
|
|
|
ptrSwap = ptrLanguage[i];
|
|
|
|
ptrLanguage[i] = ptrLanguage[j];
|
|
|
|
ptrLanguage[j] = ptrSwap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ( i=0 ; i<countLang ; i++ ) {
|
|
|
|
PL_strncpyz(acceptLanguageList[i],ptrLanguage[i],NSILOCALE_MAX_ACCEPT_LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* simple case: no quality values */
|
|
|
|
|
|
|
|
cPtr = nsCRT::strtok(input,",",&cPtr2);
|
|
|
|
while (cPtr) {
|
|
|
|
if (strlen(cPtr)<NSILOCALE_MAX_ACCEPT_LENGTH) { /* ignore if too long */
|
|
|
|
PL_strncpyz(acceptLanguageList[countLang++],cPtr,NSILOCALE_MAX_ACCEPT_LENGTH);
|
|
|
|
if (countLang>=NSILOCALE_MAX_ACCEPT_LENGTH) break; /* quit if too many */
|
|
|
|
}
|
|
|
|
cPtr = nsCRT::strtok(cPtr2,",",&cPtr2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// now create the locale
|
|
|
|
//
|
|
|
|
result = NS_ERROR_FAILURE;
|
|
|
|
if (countLang>0) {
|
|
|
|
result = NewLocale(NS_ConvertASCIItoUTF16(acceptLanguageList[0]), _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// clean up
|
|
|
|
//
|
|
|
|
delete[] input;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLocaleService::GetLocaleComponentForUserAgent(nsAString& retval)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILocale> system_locale;
|
|
|
|
nsresult result;
|
|
|
|
|
|
|
|
result = GetSystemLocale(getter_AddRefs(system_locale));
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
result = system_locale->
|
|
|
|
GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), retval);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewLocaleService(nsILocaleService** result)
|
|
|
|
{
|
|
|
|
if(!result)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
*result = new nsLocaleService();
|
|
|
|
if (! *result)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|