Thread overview
Chinese characters printing issue under windows
Dec 09, 2011
Sam Hu
Dec 09, 2011
Bernard Helyer
Dec 09, 2011
Sam Hu
Dec 09, 2011
Sam Hu
December 09, 2011
For those who would like to print Chinese characters in windows console properly,below information for your easy reference.

After test,write works properly all the way,writeln only works properly when the args is no less than 2.writefln works properly all the way.

[code]
module cnsetlocale;

import core.stdc.wchar_;
import core.stdc.locale;

import std.stdio;
import std.string;
import std.conv;


extern(C) int setlocale(int, char*);   static this()   {
   fwide(core.stdc.stdio.stdout, 1);
   setlocale(0, cast(char*)"china");      }  int main(string[] args)
{
	printf("你好DMD2!!\n");
	writefln("%s","你好DMD2!!");
	string msg="你好DMD2!!";
	string msg1="很好很好的同学";
	string msg2="大大的了不起!";
	writefln("the string is %s",msg);
	writefln("the string is %s \n %s\n%s and\n%s",msg,msg1,msg2,"很牛很牛");
	
	readln;
	return 0;
}
[/code]
December 09, 2011
This is very useful information, so thank you for posting it, but this looks like something that Phobos should pick up automatically. I would note that the program works perfectly in Linux without the need for setting the locale, so I'd imagine its some encoding issue? Not really my area of expertise.
December 09, 2011
On Friday, 9 December 2011 at 00:51:35 UTC, Sam Hu wrote:
> For those who would like to print Chinese characters in windows console properly,below information for your easy reference.
>
> After test,write works properly all the way,writeln only works properly when the args is no less than 2.writefln works properly all the way.
>
> [code]
> module cnsetlocale;
>
> import core.stdc.wchar_;
> import core.stdc.locale;
>
> import std.stdio;
> import std.string;
> import std.conv;
>
>
> extern(C) int setlocale(int, char*);
 static this()   {
>   fwide(core.stdc.stdio.stdout, 1);
>   setlocale(0, cast(char*)"china");      }
 int main(string[]
> args)
> {
> 	printf("你好DMD2!!\n");
> 	writefln("%s","你好DMD2!!");
> 	string msg="你好DMD2!!";
> 	string msg1="很好很好的同学";
> 	string msg2="大大的了不起!";
> 	writefln("the string is %s",msg);
> 	writefln("the string is %s \n %s\n%s and\n%s",msg,msg1,msg2,"很牛很牛");
> 	
> 	readln;
> 	return 0;
> }
> [/code]

printf("%.s\n",cnStr) works as well while printf(cnStr) does not.
Re-format the sample code:


module cnsetlocale;

import core.stdc.wchar_;
import core.stdc.locale;

import std.stdio;
import std.string;
import std.conv;


extern(C) int setlocale(int, char*);   static this()   {
   fwide(core.stdc.stdio.stdout, 1);
   setlocale(0, cast(char*)"china");      }  int main(string[] args)
{
	printf("%*.s\n","你好DMD2!!\n");
	write("你好DMD2!!\n");
	writefln("%s","你好DMD2!!");
	string msg="你好DMD2!!";
	string msg1="很好很好的同学";
	string msg2="大大的了不起!";
	writefln("the string is %s",msg);
	writefln("the string is %s \n %s\n%s and\n%s",msg,msg1,msg2,"很牛很牛");
	
	readln;
	return 0;
}

December 09, 2011
Bernard Helyer Wrote:

> This is very useful information, so thank you for posting it, but this looks like something that Phobos should pick up automatically. I would note that the program works perfectly in Linux without the need for setting the locale, so I'd imagine its some encoding issue? Not really my area of expertise.
I seeked help here years ago and many of my friends who are D fans as well have been struggled of this issue so many years but still there is no pure D way to cope with it.Win32 API is the only choice.This time one of my friend posted this in local D forum so we would like to share with everyone.