示例#1
0
std::string getSystemLocale()
{
	std::string localeStr;

#ifdef BBGE_BUILD_WINDOWS
	LCID lcid = GetThreadLocale();

	char buf[100];
	char ctry[100];

	if (GetLocaleInfo(lcid, LOCALE_SISO639LANGNAME, buf, sizeof buf) != 0)
	{
		localeStr = buf;

		if (GetLocaleInfo(lcid, LOCALE_SISO3166CTRYNAME, ctry, sizeof ctry) != 0)
		{
			localeStr += "_";
			localeStr += ctry;
		}
	}
#elif BBGE_BUILD_MACOSX
	CFLocaleRef locale = CFLocaleCopyCurrent();
	CFStringRef buf;

	if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleLanguageCode)) != NULL)
	{
		localeStr = _CFToStdString(buf);
		CFRelease(buf);

		if ((buf = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleCountryCode)) != NULL)
		{
			localeStr += "_";
			localeStr += _CFToStdString(buf);
			CFRelease(buf);
		}
	}

	CFRelease(locale);

#else
	const char *lang = (const char *)getenv("LANG");

	if (lang && *lang)
	{
		localeStr = lang;

		size_t found = localeStr.find('.');

		if (found != std::string::npos)
			localeStr.resize(found);
	}
#endif

	return localeStr;
}
示例#2
0
nglString nuiLocale::GetCurrentCountry()
{
#ifdef __APPLE__
  CFLocaleRef loc = CFLocaleCopyCurrent();
  CFStringRef str = (CFStringRef)CFLocaleGetValue(loc, kCFLocaleCountryCode);
  
  nglString s(str);
  
  CFRelease(str);
  CFRelease(loc);
  
  return s;
#else
  nglString str(setlocale(LC_ALL, NULL));
  int32 i = str.Find(_T('_'));
  if (i < 0)
    return nglString::Null;
  
  int32 j = str.Find(_T('.'));
  if (j < 0)
    j = str.GetLength();
  
  return str.Extract(i, j - i);
#endif
}
示例#3
0
static char* copyDefaultLocale()
{
#if !PLATFORM(IOS_FAMILY)
    return copyShortASCIIString(static_cast<CFStringRef>(CFLocaleGetValue(adoptCF(CFLocaleCopyCurrent()).get(), kCFLocaleCollatorIdentifier)));
#else
    // FIXME: Documentation claims the code above would work on iOS 4.0 and later. After test that works, we should remove this and use that instead.
    return copyShortASCIIString(adoptCF(static_cast<CFStringRef>(CFPreferencesCopyValue(CFSTR("AppleCollationOrder"), kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost))).get());
#endif
}
示例#4
0
CFCalendarRef CFCalendarCopyCurrent(void) {
    CFLocaleRef locale = CFLocaleCopyCurrent();
    CFCalendarRef calID = (CFCalendarRef)CFLocaleGetValue(locale, kCFLocaleCalendarIdentifier);
    if (calID) {
        CFCalendarRef calendar = CFCalendarCreateWithIdentifier(kCFAllocatorSystemDefault, (CFStringRef)calID);
        CFCalendarSetLocale(calendar, locale);
	CFRelease(locale);
        return calendar;
    }
    return NULL;
}
static void updateDateTemplate(CFDateIntervalFormatterRef dif, CFDateIntervalFormatterStyle dateStyle, CFDateIntervalFormatterStyle timeStyle) {
    CFDateFormatterRef formatter;
    {
        CFLocaleRef locale = dif->_locale ? CFRetain(dif->_locale) : CFLocaleCopyCurrent();
        CFCalendarRef unretainedCalendar = dif->_calendar ?: (CFCalendarRef)CFLocaleGetValue(locale, kCFLocaleCalendar);
        formatter = CFDateFormatterCreate(kCFAllocatorSystemDefault, locale, (CFDateFormatterStyle)dateStyle, (CFDateFormatterStyle)timeStyle);
        CFDateFormatterSetProperty(formatter, kCFDateFormatterCalendar, unretainedCalendar);
        CFRelease(locale);
    }
    
    CFStringRef template = CFDateFormatterGetFormat(formatter);
PassOwnPtr<Collator> Collator::userDefault()
{
#if OS(DARWIN) && USE(CF)
    // Mac OS X doesn't set UNIX locale to match user-selected one, so ICU default doesn't work.
    RetainPtr<CFLocaleRef> currentLocale(AdoptCF, CFLocaleCopyCurrent());
    CFStringRef collationOrder = (CFStringRef)CFLocaleGetValue(currentLocale.get(), kCFLocaleCollatorIdentifier);
    char buf[256];
    if (!collationOrder)
        return adoptPtr(new Collator(""));
    CFStringGetCString(collationOrder, buf, sizeof(buf), kCFStringEncodingASCII);
    return adoptPtr(new Collator(buf));
#else
    return adoptPtr(new Collator(0));
#endif
}
示例#7
0
nglString nuiLocale::Get()
{
#ifdef __APPLE__
  CFLocaleRef loc = CFLocaleCopyCurrent();
  CFStringRef str = (CFStringRef)CFLocaleGetValue(loc, kCFLocaleIdentifier);
  
  nglString s(str);
  
  CFRelease(str);
  CFRelease(loc);
  
  return s;
#else
  nglString str(setlocale(LC_ALL, NULL));
  return str;
#endif
}
PassOwnPtr<Collator> Collator::userDefault()
{
#if OS(DARWIN) && USE(CF)
    // Mac OS X doesn't set UNIX locale to match user-selected one, so ICU default doesn't work.
#if !OS(IOS)
    RetainPtr<CFLocaleRef> currentLocale = adoptCF(CFLocaleCopyCurrent());
    CFStringRef collationOrder = (CFStringRef)CFLocaleGetValue(currentLocale.get(), kCFLocaleCollatorIdentifier);
#else
    RetainPtr<CFStringRef> collationOrderRetainer = adoptCF((CFStringRef)CFPreferencesCopyValue(CFSTR("AppleCollationOrder"), kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost));
    CFStringRef collationOrder = collationOrderRetainer.get();
#endif
    char buf[256];
    if (!collationOrder)
        return adoptPtr(new Collator(""));
    CFStringGetCString(collationOrder, buf, sizeof(buf), kCFStringEncodingASCII);
    return adoptPtr(new Collator(buf));
#else
    return adoptPtr(new Collator(0));
#endif
}
OP_STATUS MacOpDesktopResources::GetCountryCode(OpString& country_code)
{
	country_code.Empty();

	CFLocaleRef userLocaleRef;
	CFStringRef stringRef;

	userLocaleRef = CFLocaleCopyCurrent();
	stringRef = (CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleCountryCode);
	int len = 0;
	if (stringRef) {
		len = CFStringGetLength(stringRef);
		RETURN_OOM_IF_NULL(country_code.Reserve(len+1));
		CFStringGetCharacters(stringRef, CFRangeMake(0,len), (UniChar *)country_code.CStr());
		country_code[len] = 0;
	}
	CFRelease(userLocaleRef);

	return OpStatus::OK;
}
示例#10
0
nglString nuiLocale::GetCurrentLanguage()
{
#ifdef __APPLE__
  CFLocaleRef loc = CFLocaleCopyCurrent();
  CFStringRef str = (CFStringRef)CFLocaleGetValue(loc, kCFLocaleLanguageCode);
  
  nglString s(str);
  
  CFRelease(str);
  CFRelease(loc);
  
  return s;
#else
  nglString str(setlocale(LC_ALL, NULL));
  int32 i = str.Find(_T('_'));
  if (i < 0)
    return str;
    
  return str.Extract(0, i);
#endif
}
std::auto_ptr<Collator> Collator::userDefault()
{
#if OS(DARWIN) && PLATFORM(CF)
    // Mac OS X doesn't set UNIX locale to match user-selected one, so ICU default doesn't work.
#if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) && !OS(IPHONE_OS)
    RetainPtr<CFLocaleRef> currentLocale(AdoptCF, CFLocaleCopyCurrent());
    CFStringRef collationOrder = (CFStringRef)CFLocaleGetValue(currentLocale.get(), kCFLocaleCollatorIdentifier);
#else
    RetainPtr<CFStringRef> collationOrderRetainer(AdoptCF, (CFStringRef)CFPreferencesCopyValue(CFSTR("AppleCollationOrder"), kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost));
    CFStringRef collationOrder = collationOrderRetainer.get();
#endif
    char buf[256];
    if (collationOrder) {
        CFStringGetCString(collationOrder, buf, sizeof(buf), kCFStringEncodingASCII);
        return std::auto_ptr<Collator>(new Collator(buf));
    } else
        return std::auto_ptr<Collator>(new Collator(""));
#else
    return std::auto_ptr<Collator>(new Collator(0));
#endif
}
示例#12
0
static gchar*
get_darwin_locale (void)
{
	static gchar *darwin_locale = NULL;
	CFLocaleRef locale = NULL;
	CFStringRef locale_language = NULL;
	CFStringRef locale_country = NULL;
	CFStringRef locale_script = NULL;
	CFStringRef locale_cfstr = NULL;
	CFIndex bytes_converted;
	CFIndex bytes_written;
	CFIndex len;
	int i;

	if (darwin_locale != NULL)
		return g_strdup (darwin_locale);

	locale = CFLocaleCopyCurrent ();

	if (locale) {
		locale_language = CFLocaleGetValue (locale, kCFLocaleLanguageCode);
		if (locale_language != NULL && CFStringGetBytes(locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
			len = bytes_converted + 1;

			locale_country = CFLocaleGetValue (locale, kCFLocaleCountryCode);
			if (locale_country != NULL && CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
				len += bytes_converted + 1;

				locale_script = CFLocaleGetValue (locale, kCFLocaleScriptCode);
				if (locale_script != NULL && CFStringGetBytes (locale_script, CFRangeMake (0, CFStringGetLength (locale_script)), kCFStringEncodingMacRoman, 0, FALSE, NULL, 0, &bytes_converted) > 0) {
					len += bytes_converted + 1;
				}

				darwin_locale = (char *) g_malloc (len + 1);
				CFStringGetBytes (locale_language, CFRangeMake (0, CFStringGetLength (locale_language)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) darwin_locale, len, &bytes_converted);

				darwin_locale[bytes_converted] = '-';
				bytes_written = bytes_converted + 1;
				if (locale_script != NULL && CFStringGetBytes (locale_script, CFRangeMake (0, CFStringGetLength (locale_script)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) &darwin_locale[bytes_written], len - bytes_written, &bytes_converted) > 0) {
					darwin_locale[bytes_written + bytes_converted] = '-';
					bytes_written += bytes_converted + 1;
				}

				CFStringGetBytes (locale_country, CFRangeMake (0, CFStringGetLength (locale_country)), kCFStringEncodingMacRoman, 0, FALSE, (UInt8 *) &darwin_locale[bytes_written], len - bytes_written, &bytes_converted);
				darwin_locale[bytes_written + bytes_converted] = '\0';
			}
		}

		if (darwin_locale == NULL) {
			locale_cfstr = CFLocaleGetIdentifier (locale);

			if (locale_cfstr) {
				len = CFStringGetMaximumSizeForEncoding (CFStringGetLength (locale_cfstr), kCFStringEncodingMacRoman) + 1;
				darwin_locale = (char *) g_malloc (len);
				if (!CFStringGetCString (locale_cfstr, darwin_locale, len, kCFStringEncodingMacRoman)) {
					g_free (darwin_locale);
					CFRelease (locale);
					darwin_locale = NULL;
					return NULL;
				}

				for (i = 0; i < strlen (darwin_locale); i++)
					if (darwin_locale [i] == '_')
						darwin_locale [i] = '-';
			}			
		}

		CFRelease (locale);
	}

	return g_strdup (darwin_locale);
}
示例#13
0
/*static*/ int wxLocale::GetSystemLanguage()
{
    CreateLanguagesDB();

    // init i to avoid compiler warning
    size_t i = 0,
        count = ms_languagesDB->GetCount();

#if defined(__UNIX__)
    // first get the string identifying the language from the environment
    wxString langFull;
#ifdef __WXMAC__
    wxCFRef<CFLocaleRef> userLocaleRef(CFLocaleCopyCurrent());

    // because the locale identifier (kCFLocaleIdentifier) is formatted a little bit differently, eg
    // az_Cyrl_AZ@calendar=buddhist;currency=JPY we just recreate the base info as expected by wx here

    wxCFStringRef str(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleLanguageCode)));
    langFull = str.AsString()+"_";
    str.reset(wxCFRetain((CFStringRef)CFLocaleGetValue(userLocaleRef, kCFLocaleCountryCode)));
    langFull += str.AsString();
#else
    if (!wxGetEnv(wxS("LC_ALL"), &langFull) &&
        !wxGetEnv(wxS("LC_MESSAGES"), &langFull) &&
        !wxGetEnv(wxS("LANG"), &langFull))
    {
        // no language specified, treat it as English
        return wxLANGUAGE_ENGLISH_US;
    }

    if ( langFull == wxS("C") || langFull == wxS("POSIX") )
    {
        // default C locale is English too
        return wxLANGUAGE_ENGLISH_US;
    }
#endif

    // the language string has the following form
    //
    //      lang[_LANG][.encoding][@modifier]
    //
    // (see environ(5) in the Open Unix specification)
    //
    // where lang is the primary language, LANG is a sublang/territory,
    // encoding is the charset to use and modifier "allows the user to select
    // a specific instance of localization data within a single category"
    //
    // for example, the following strings are valid:
    //      fr
    //      fr_FR
    //      de_DE.iso88591
    //      de_DE@euro
    //      de_DE.iso88591@euro

    // for now we don't use the encoding, although we probably should (doing
    // translations of the msg catalogs on the fly as required) (TODO)
    //
    // we need the modified for languages like Valencian: ca_ES@valencia
    // though, remember it
    wxString modifier;
    size_t posModifier = langFull.find_first_of(wxS("@"));
    if ( posModifier != wxString::npos )
        modifier = langFull.Mid(posModifier);

    size_t posEndLang = langFull.find_first_of(wxS("@."));
    if ( posEndLang != wxString::npos )
    {
        langFull.Truncate(posEndLang);
    }

    // do we have just the language (or sublang too)?
    const bool justLang = langFull.find('_') == wxString::npos;

    // 0. Make sure the lang is according to latest ISO 639
    //    (this is necessary because glibc uses iw and in instead
    //    of he and id respectively).

    // the language itself (second part is the dialect/sublang)
    wxString langOrig = ExtractLang(langFull);

    wxString lang;
    if ( langOrig == wxS("iw"))
        lang = wxS("he");
    else if (langOrig == wxS("in"))
        lang = wxS("id");
    else if (langOrig == wxS("ji"))
        lang = wxS("yi");
    else if (langOrig == wxS("no_NO"))
        lang = wxS("nb_NO");
    else if (langOrig == wxS("no_NY"))
        lang = wxS("nn_NO");
    else if (langOrig == wxS("no"))
        lang = wxS("nb_NO");
    else
        lang = langOrig;

    // did we change it?
    if ( lang != langOrig )
    {
        langFull = lang + ExtractNotLang(langFull);
    }

    // 1. Try to find the language either as is:
    // a) With modifier if set
    if ( !modifier.empty() )
    {
        wxString langFullWithModifier = langFull + modifier;
        for ( i = 0; i < count; i++ )
        {
            if ( ms_languagesDB->Item(i).CanonicalName == langFullWithModifier )
                break;
        }
    }

    // b) Without modifier
    if ( modifier.empty() || i == count )
    {
        for ( i = 0; i < count; i++ )
        {
            if ( ms_languagesDB->Item(i).CanonicalName == langFull )
                break;
        }
    }

    // 2. If langFull is of the form xx_YY, try to find xx:
    if ( i == count && !justLang )
    {
        for ( i = 0; i < count; i++ )
        {
            if ( ms_languagesDB->Item(i).CanonicalName == lang )
            {
                break;
            }
        }
    }

    // 3. If langFull is of the form xx, try to find any xx_YY record:
    if ( i == count && justLang )
    {
        for ( i = 0; i < count; i++ )
        {
            if ( ExtractLang(ms_languagesDB->Item(i).CanonicalName)
                    == langFull )
            {
                break;
            }
        }
    }


    if ( i == count )
    {
        // In addition to the format above, we also can have full language
        // names in LANG env var - for example, SuSE is known to use
        // LANG="german" - so check for use of non-standard format and try to
        // find the name in verbose description.
        for ( i = 0; i < count; i++ )
        {
            if (ms_languagesDB->Item(i).Description.CmpNoCase(langFull) == 0)
            {
                break;
            }
        }
    }
#elif defined(__WIN32__)
    LCID lcid = GetUserDefaultLCID();
    if ( lcid != 0 )
    {
        wxUint32 lang = PRIMARYLANGID(LANGIDFROMLCID(lcid));
        wxUint32 sublang = SUBLANGID(LANGIDFROMLCID(lcid));

        for ( i = 0; i < count; i++ )
        {
            if (ms_languagesDB->Item(i).WinLang == lang &&
                ms_languagesDB->Item(i).WinSublang == sublang)
            {
                break;
            }
        }
    }
    //else: leave wxlang == wxLANGUAGE_UNKNOWN
#endif // Unix/Win32

    if ( i < count )
    {
        // we did find a matching entry, use it
        return ms_languagesDB->Item(i).Language;
    }

    // no info about this language in the database
    return wxLANGUAGE_UNKNOWN;
}
示例#14
0
/* static */
wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory WXUNUSED(cat))
{
    CFLocaleRef userLocaleRefRaw;
    if ( wxGetLocale() )
    {
        userLocaleRefRaw = CFLocaleCreate
                        (
                                kCFAllocatorDefault,
                                wxCFStringRef(wxGetLocale()->GetCanonicalName())
                        );
    }
    else // no current locale, use the default one
    {
        userLocaleRefRaw = CFLocaleCopyCurrent();
    }

    wxCFRef<CFLocaleRef> userLocaleRef(userLocaleRefRaw);

    CFStringRef cfstr = 0;
    switch ( index )
    {
        case wxLOCALE_THOUSANDS_SEP:
            cfstr = (CFStringRef) CFLocaleGetValue(userLocaleRef, kCFLocaleGroupingSeparator);
            break;

        case wxLOCALE_DECIMAL_POINT:
            cfstr = (CFStringRef) CFLocaleGetValue(userLocaleRef, kCFLocaleDecimalSeparator);
            break;

        case wxLOCALE_SHORT_DATE_FMT:
        case wxLOCALE_LONG_DATE_FMT:
        case wxLOCALE_DATE_TIME_FMT:
        case wxLOCALE_TIME_FMT:
            {
                CFDateFormatterStyle dateStyle = kCFDateFormatterNoStyle;
                CFDateFormatterStyle timeStyle = kCFDateFormatterNoStyle;
                switch (index )
                {
                    case wxLOCALE_SHORT_DATE_FMT:
                        dateStyle = kCFDateFormatterShortStyle;
                        break;
                    case wxLOCALE_LONG_DATE_FMT:
                        dateStyle = kCFDateFormatterFullStyle;
                        break;
                    case wxLOCALE_DATE_TIME_FMT:
                        dateStyle = kCFDateFormatterFullStyle;
                        timeStyle = kCFDateFormatterMediumStyle;
                        break;
                    case wxLOCALE_TIME_FMT:
                        timeStyle = kCFDateFormatterMediumStyle;
                        break;
                    default:
                        wxFAIL_MSG( "unexpected time locale" );
                        return wxString();
                }
                wxCFRef<CFDateFormatterRef> dateFormatter( CFDateFormatterCreate
                    (NULL, userLocaleRef, dateStyle, timeStyle));
                wxCFStringRef cfs = wxCFRetain( CFDateFormatterGetFormat(dateFormatter ));
                wxString format = TranslateFromUnicodeFormat(cfs.AsString());
                // we always want full years
                format.Replace("%y","%Y");
                return format;
            }
            break;

        default:
            wxFAIL_MSG( "Unknown locale info" );
            return wxString();
    }

    wxCFStringRef str(wxCFRetain(cfstr));
    return str.AsString();
}