Search code examples
windowsperllocalesetlocale

Unable to fetch system locale in perl on Windows


I am new to perl and I need to fetch the current system locale programmatically in perl on Windows. The way I am doing it is:

use locale;
use POSIX(locale_h);
$mylocale = setlocale(LC_ALL,"");

Here, even though, I have set my locale as 'en-gb', I am getting 'C' as the locale.

What is wrong/missing here?

Note: when I use setlocale method from a c++ program, it gives me locale as English_UnitedKingdom.


Solution

  • To get the Windows locale (as opposed to a non-existent POSIX locale) in a POSIX-style format, you can Win32::Locale's get_locale.

    >perl -e"use Win32::Locale qw( ); CORE::say Win32::Locale::get_locale()"
    en_ca
    

    That's mostly a guess. Windows locales are really numbers.

    >perl -e"use Win32::Locale qw( ); CORE::say sprintf '0x%X', Win32::Locale::get_ms_locale()"
    0x1009