The Microsoft documentation for setlocale() suggests that - since 2018 - they support UTF-8 locales and short (Unix-style) locale identifiers like "en_US". However, I cannot get that to work, not even with Microsoft's own example code. The linked page also contains the up-to-date documentation for setlocale() on Windows.

I'm running a recent Windows 10 system inside VirtualBox. I have tried to switch the locale with (Strawberry) Perl and two versions of MinGW gcc. The result is always the same. I can set the locale to something like "German_Germany.1252" but none of "de_DE", "de-DE", "German_Germany.UTF-8", ".UTF8", "German_Germany.65001", ... work. 65001 is the "code page" for utf-8.

Can anybody shed some light on this?

I'm using this C code for testing (run with "PROGRAMNAME LOCALE"):

#include <locale.h> #include <stdio.h> #include <time.h> #include <string.h> int main(int argc, char *argv[]) { char date[256]; time_t then = 1678658400; const char *locale = setlocale(LC_TIME, argv[1]); strftime(date, sizeof date, "%B", localtime(&then)); printf("%s: %s (%u bytes)\n", locale, date, strlen(date)); return 0; }

Or in Perl

use v5.10; use POSIX qw(LC_ALL setlocale strftime); my $locale = $ARGV[0]; say "$locale: ", POSIX::setlocale(LC_ALL, $locale); my $march = strftime("%B", 0, 0, 0, 1, 2, 123); say $march;

On non-Windows systems, both versions invoked as "./PROGRAM de_DE.UTF-8" spit out the German word for the month march "März" in utf-8.


In reply to Using setlocale() on Windows with utf-8 support by gflohr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.