What is the alternative to the setlocale function of c in D? Thank you.

John Chapman johnch_atms at hotmail.com
Sun Jan 27 10:44:04 UTC 2019


On Sunday, 27 January 2019 at 06:14:15 UTC, FrankLike wrote:
> On Saturday, 26 January 2019 at 09:33:33 UTC, John Chapman 
> wrote:
>
>> What has that code got to do with setting the console's font? 
>> So you need to add more code to accomplish that.
>
> You don't need to set the font to achieve the goal, why not?

This should work:

const(char)[] toCodePage(const(char)[] s, uint codePage = 0) {
   import core.sys.windows.winnls, std.utf;

   foreach (char c; s) {
     if (c >= 0x80) {
       auto temp = s.toUTF16z();
       char[] result;
       if ((result.length = WideCharToMultiByte(codePage, 0, temp, 
-1, null, 0, null, null)) != 0)
         WideCharToMultiByte(codePage, 0, temp, -1, result.ptr, 
cast(int)result.length, null, null);
       return result;
     }
   }
   return s;
}

void main() {
   import core.sys.windows.wincon, std.stdio;

   SetConsoleOutputCP(936); // Simplified Chinese codepage
   writeln("字符".toCodePage(936));
}


More information about the Digitalmars-d-learn mailing list