Manage specific languages ​​in C ++

Is there any standard way to configure locale between platforms in a stream? I see that xlocale provides uselocale, but it is not supported on Windows. There is "_configthreadlocale (_ENABLE_PER_THREAD_LOCALE);" in windows, after which setlocale works based on the stream. My question is, is there a library that provides these locale-specific platform manipulations independently? Or any other way to do this?

Thanks, Gokul.

+1
source share
2 answers

, -. , .

newlocale , . .

// Locale Cross-Compatibility
#ifdef _WIN32
#define locale_t         _locale_t
#define freelocale       _free_locale

#define LC_GLOBAL_LOCALE ((locale_t)-1)
#define LC_ALL_MASK      LC_ALL
#define LC_COLLATE_MASK  LC_COLLATE
#define LC_CTYPE_MASK    LC_CTYPE
#define LC_MONETARY_MASK LC_MONETARY
#define LC_NUMERIC_MASK  LC_NUMERIC
#define LC_TIME_MASK     LC_TIME

// Base locale is ignored and mixing of masks is not supported
#define newlocale(mask, locale, base) _create_locale(mask, locale)

locale_t uselocale(locale_t new_locale)
{
    // Retrieve the current per thread locale setting
    bool bIsPerThread = (_configthreadlocale(0) == _ENABLE_PER_THREAD_LOCALE);

    // Retrieve the current thread-specific locale
    locale_t old_locale = bIsPerThread ? _get_current_locale() : LC_GLOBAL_LOCALE;

    if(new_locale == LC_GLOBAL_LOCALE)
    {
        // Restore the global locale
        _configthreadlocale(_DISABLE_PER_THREAD_LOCALE);
    }
    else if(new_locale != NULL)
    {
        // Configure the thread to set the locale only for this thread
        _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);

        // Set all locale categories
        for(int i = LC_MIN; i <= LC_MAX; i++)
            setlocale(i, new_locale->locinfo->lc_category[i].locale);
    }

    return old_locale;
}
#endif

, , :

#include <locale.h>
#ifdef __APPLE__
#include <xlocale.h>
#endif

// Apply a new locale to this thread
locale_t locale = newlocale(LC_NUMERIC_MASK, "C", NULL);
locale_t old_locale = uselocale(locale);

// Print out some text
printf("Always use dot-decimal notation: %f", 1.5);

// Restore the global locale
uselocale(old_locale);
freelocale(locale);
+1

Boost:: Locale , -, iconv. , , , C , . . .

, , , , , . , , , / .

0

Source: https://habr.com/ru/post/1667498/


All Articles