how to localize console and GUI apps in Windows
Andrei
aalub at mail.ru
Fri Dec 29 10:35:53 UTC 2017
On Thursday, 28 December 2017 at 18:45:39 UTC, H. S. Teoh wrote:
> On Thu, Dec 28, 2017 at 05:56:32PM +0000, Andrei via
> Digitalmars-d-learn wrote:
> ...
> The string / wstring / dstring types in D are intended to be
> Unicode strings. If you need to use other encodings, you
> really should be using ubyte[] or const(ubyte)[] or
> immutable(ubyte)[], instead of string.
Thank you Teoh for advise and good example! I was looking towards
writing something like that if no decision exists. Still this way
of deliberate translations seems to be not the best. It supposes
explicit workaround for every ahchoo in Russian and steady
converting ubyte[] to string and back around. No formatting gems,
no simple and elegant I/O statements or string/char comparisons.
This may be endurable if you write an application where Russian
is only one of rare options, and what if your whole environment
is totally Russian?
Or some other nonASCII locale... Many other cultures have same
mix of DOS/Window/Unix code pages. The decision to use only
Unicode for strings in D language seems excellent just because of
this, but the realization turns out to be delusive. Folks in such
countries won’t appreciate a language which is elegant only for
English-spoken intercommunications.
This problem is common for most programming languages and
runtimes I know of. The only system which has decided the whole
case is .NET I think.
The way proposed by zabruk70 below seems more appropriate though
more particular too - I feel it suits only console type of
applications. Alas, this type of application proved to be buggy
too.
On Thursday, 28 December 2017 at 22:49:30 UTC, zabruk70 wrote:
> you can just set console CP to UTF-8:
>
> https://github.com/CyberShadow/ae/blob/master/sys/console.d
Yes! This seems to be the required, thank you very much! Though
it is not suitable for GUI type of a Windows application.
Still some testing showed that this way conforms only console
output. Simple read/write/compare script listed below works very
well until the user enters something Russian. It then prints
**empty** response and falls into indefinite loop printing the
prompt and then immediately empty response without actually
reading it.
But I think this is subject for ”Issues” part of this forum.
p.s. I’ve found that I may set “Consolas” font for a console
window and then you can output properly localized UTF8 strings
without any special code in D script managing code pages. Still
this does not decide localized input problem: any localized input
throws an exception “std.utf.UTFException... Invalid UTF-8
sequence”.
The script:
import core.sys.windows.windows;
import std.stdio;
import std.string;
int main(string[] args)
{
const UTF8CP = 65001;
UINT oldCP, oldOutputCP;
oldCP = GetConsoleCP();
oldOutputCP = GetConsoleOutputCP();
SetConsoleCP(UTF8CP);
SetConsoleOutputCP(UTF8CP);
writeln("hello world, привет всем!");
bool quit = false;
string response;
while (!quit)
{
write("responde something: ");
response=readln().strip();
writefln("your response is \"%s\"", response);
if (response == "quit")
{
writeln("good buy then!");
quit = true;
}
}
SetConsoleCP(oldCP);
SetConsoleOutputCP(oldOutputCP);
return 0;
}
More information about the Digitalmars-d-learn
mailing list