Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Member Functions | Protected Attributes
nsLocaleService Class Reference
Inheritance diagram for nsLocaleService:
Inheritance graph
[legend]
Collaboration diagram for nsLocaleService:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS
NS_DECL_NSILOCALESERVICE 
nsLocaleService (void)
virtual ~nsLocaleService (void)
nsILocale newLocale (in AString aLocale)
nsILocale newLocaleObject (in nsILocaleDefinition localeDefinition)
nsILocale getSystemLocale ()
nsILocale getApplicationLocale ()
nsILocale getLocaleFromAcceptLanguage (in string acceptLanguage)
AString getLocaleComponentForUserAgent ()

Protected Member Functions

nsresult SetSystemLocale (void)
nsresult SetApplicationLocale (void)

Protected Attributes

nsCOMPtr< nsILocalemSystemLocale
nsCOMPtr< nsILocalemApplicationLocale

Detailed Description

Definition at line 108 of file nsLocaleService.cpp.


Constructor & Destructor Documentation

Definition at line 168 of file nsLocaleService.cpp.

    : mSystemLocale(0), mApplicationLocale(0)
{
#if defined(XP_WIN)
    nsCOMPtr<nsIWin32Locale> win32Converter = do_GetService(NS_WIN32LOCALE_CONTRACTID);

    NS_ASSERTION(win32Converter, "nsLocaleService: can't get win32 converter\n");

    nsAutoString        xpLocale;
    if (win32Converter) {
        
        nsresult result;
        //
        // get the system LCID
        //
        LCID win_lcid = GetSystemDefaultLCID();
        if (win_lcid==0) { return;}
            result = win32Converter->GetXPLocale(win_lcid, xpLocale);
        if (NS_FAILED(result)) { return;}
            result = NewLocale(xpLocale, getter_AddRefs(mSystemLocale));
        if (NS_FAILED(result)) { return;}

        //
        // get the application LCID
        //
        win_lcid = GetUserDefaultLCID();
        if (win_lcid==0) { return;}
            result = win32Converter->GetXPLocale(win_lcid, xpLocale);
        if (NS_FAILED(result)) { return;}
            result = NewLocale(xpLocale, getter_AddRefs(mApplicationLocale));
        if (NS_FAILED(result)) { return;}
    }
#endif
#if (defined(XP_UNIX) && !defined(XP_MACOSX)) || defined(XP_BEOS)
    nsCOMPtr<nsIPosixLocale> posixConverter = do_GetService(NS_POSIXLOCALE_CONTRACTID);

    nsAutoString xpLocale, platformLocale;
    if (posixConverter) {
        nsAutoString category, category_platform;
        nsLocale* resultLocale;
        int i;

        resultLocale = new nsLocale();
        if ( resultLocale == NULL ) { 
            return; 
        }
        for( i = 0; i < LocaleListLength; i++ ) {
            nsresult result;
            char* lc_temp = setlocale(posix_locale_category[i], "");
            CopyASCIItoUTF16(LocaleList[i], category);
            category_platform = category; 
            category_platform.AppendLiteral("##PLATFORM");
            if (lc_temp != nsnull) {
                result = posixConverter->GetXPLocale(lc_temp, xpLocale);
                CopyASCIItoUTF16(lc_temp, platformLocale);
            } else {
                char* lang = getenv("LANG");
                if ( lang == nsnull ) {
                    platformLocale.AssignLiteral("en_US");
                    result = posixConverter->GetXPLocale("en-US", xpLocale);
                }
                else {
                    CopyASCIItoUTF16(lang, platformLocale);
                    result = posixConverter->GetXPLocale(lang, xpLocale); 
                }
            }
            if (NS_FAILED(result)) {
                return;
            }
            resultLocale->AddCategory(category, xpLocale);
            resultLocale->AddCategory(category_platform, platformLocale);
        }
        mSystemLocale = do_QueryInterface(resultLocale);
        mApplicationLocale = do_QueryInterface(resultLocale);
    }  // if ( NS_SUCCEEDED )...
       
#endif // XP_UNIX || XP_BEOS
#if defined(XP_OS2)
    nsCOMPtr<nsIOS2Locale> os2Converter = do_GetService(NS_OS2LOCALE_CONTRACTID);
    nsAutoString xpLocale;
    if (os2Converter) {
        nsAutoString category;
        nsLocale* resultLocale;
        int i;

        resultLocale = new nsLocale();
        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++ ) {
            lc_temp = nsnull;
            UniQueryLocaleObject(locale_object,
                                 posix_locale_category[i],
                                 UNI_MBS_STRING_POINTER,
                                 (void **)&lc_temp);
            category.AssignWithConversion(LocaleList[i]);
            nsresult result;
            if (lc_temp != nsnull)
                result = os2Converter->GetXPLocale(lc_temp, xpLocale);
            else {
                char* lang = getenv("LANG");
                if ( lang == nsnull ) {
                    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

#if defined(XP_MAC) || defined(XP_MACOSX)
    // On MacOSX, the recommended way to get the user's current locale is to use
    // the CFLocale APIs.  However, these are only available on 10.3 and later.
    // So for the older systems, we have to keep using the Script Manager APIs.

    static PRBool checked = PR_FALSE;
    static fpCFLocaleCopyCurrent_type fpCFLocaleCopyCurrent = NULL;
    static fpCFLocaleGetIdentifier_type fpCFLocaleGetIdentifier = NULL;

    if (!checked)
    {
        CFBundleRef bundle =
            ::CFBundleGetBundleWithIdentifier(CFSTR("com.apple.Carbon"));
        if (bundle)
        {
            // We dynamically load these two functions and only use them if
            // they are available (OS 10.3+).
            fpCFLocaleCopyCurrent = (fpCFLocaleCopyCurrent_type)
                ::CFBundleGetFunctionPointerForName(bundle,
                                                    CFSTR("CFLocaleCopyCurrent"));
            fpCFLocaleGetIdentifier = (fpCFLocaleGetIdentifier_type)
                ::CFBundleGetFunctionPointerForName(bundle,
                                                    CFSTR("CFLocaleGetIdentifier"));
        }
        checked = PR_TRUE;
    }

    if (fpCFLocaleCopyCurrent)
    {
        // Get string representation of user's current locale
        CFLocaleRef userLocaleRef = fpCFLocaleCopyCurrent();
        CFStringRef userLocaleStr = fpCFLocaleGetIdentifier(userLocaleRef);
        ::CFRetain(userLocaleStr);

        nsAutoBuffer<UniChar, 32> buffer;
        int size = ::CFStringGetLength(userLocaleStr);
        if (buffer.EnsureElemCapacity(size))
        {
            CFRange range = ::CFRangeMake(0, size);
            ::CFStringGetCharacters(userLocaleStr, range, buffer.get());
            buffer.get()[size] = 0;

            // Convert the locale string to the format that Mozilla expects
            nsAutoString xpLocale(buffer.get());
            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");
    }
    else
    {
        // Legacy MacOSX locale code
        long script = GetScriptManagerVariable(smSysScript);
        long lang = GetScriptVariable(smSystemScript,smScriptLang);
        long region = GetScriptManagerVariable(smRegionCode);
        nsCOMPtr<nsIMacLocale> macConverter = do_GetService(NS_MACLOCALE_CONTRACTID);
        if (macConverter) {
            nsresult result;
            nsAutoString xpLocale;
            result = macConverter->GetXPLocale((short)script,(short)lang,(short)region, xpLocale);
            if (NS_SUCCEEDED(result)) {
                result = NewLocale(xpLocale, getter_AddRefs(mSystemLocale));
                if (NS_SUCCEEDED(result)) {
                    mApplicationLocale = mSystemLocale;
                }
            }
        }
    }
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 378 of file nsLocaleService.cpp.

{
}

Member Function Documentation

nsILocale nsILocaleService::newLocale ( in AString  aLocale) [inherited]

Member Data Documentation

Definition at line 132 of file nsLocaleService.cpp.

Definition at line 131 of file nsLocaleService.cpp.


The documentation for this class was generated from the following file: