www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Chinese characters printing issue under windows

reply "Sam Hu" <samhudotsamhu gmail.com> writes:
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]
Dec 08 2011
next sibling parent reply "Bernard Helyer" <b.helyer gmail.com> writes:
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.
Dec 08 2011
parent Sam Hu <samhudotsamhu gmail.com> writes:
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.
Dec 08 2011
prev sibling parent "Sam Hu" <samhudotsamhu gmail.com> writes:
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; }
Dec 08 2011