Re: Multi language application
Hi,
I think that if you want to correctly display Unicode characters in Windows,
you must use UTF-16 (which is the Unicode encoding used internally by
Windows).
However, if for some reasons you do want to use UTF-8 inside your
application, I think that you can do that (e.g. storing your strings in
CStringA or std::string), and then you can just convert your UTF-8 strings
to UTF-16 just before passing them to Windows controls (like edit control)
for display.
You can use ::MultiByteToWideChar Win32 API to convert from UTF-8 to UTF-16,
and pass the UTF-16 string to Windows controls.
e.g.
<code>
// Your UTF-8 string
CStringA utf8;
... process your utf8 string ...
...
// Convert UTF-8 string to Unicode UTF-16
// to be used by Windows
CStringW utf16 = ConvertUtf8ToUtf16( utf8 );
// Pass the UTF-16 string to Windows
... e.g.
pSomeEditCtrl->SetWindowText( utf16 );
</code>
I can share a function I developed to convert strings from UTF-8 to UTF-16,
feel free to use that in your code, if you need:
<code>
// ======================================================================
//
// FUNCTION: ConvertUtf8ToUtf16
// AUTHOR: Giovanni Dicanio
//
// Converts from Unicode UTF-8 string to UTF-16.
//
// On error: ASSERTs in debug builds; in release builds throws using
// AtlThrow or AtlThrowLastWin32 (see documentations of these functions
// for more details).
//
// This function should work since VS2003 (VC++7.1), but not on VC6,
// because of lack of newer ATL-MFC shared classes and functions like
// CStringA/W in VC6.
//
// ======================================================================
CStringW ConvertUtf8ToUtf16( const CStringA & utf8 )
{
//
// Special case of empty string
//
if ( utf8.IsEmpty() )
{
return L"";
}
//
// Consider byte count corresponding to total string length,
// including end-of-string (\0) character
//
const int utf8ByteCount = utf8.GetLength() + 1;
//
// Get size of destination UTF-16 buffer, in wchar_t's
//
int utf16Size = ::MultiByteToWideChar(
CP_UTF8, // convert from UTF-8
MB_ERR_INVALID_CHARS, // error on invalid chars
static_cast<const char *>(utf8), // source UTF-8 string
utf8ByteCount, // total length of source UTF-8 string,
// in bytes, including end-of-string \0
NULL, // unused - no conversion done in this step
0 // request size of destination buffer, in wchar_t's
);
ATLASSERT( utf16Size != 0 );
if ( utf16Size == 0 )
{
AtlThrowLastWin32();
}
//
// Allocate destination buffer to store UTF-16 string
//
std::vector< wchar_t > utf16Buffer( utf16Size );
//
// Do the conversion from UTF-8 to UTF-16
//
int result = ::MultiByteToWideChar(
CP_UTF8, // convert from UTF-8
MB_ERR_INVALID_CHARS, // error on invalid chars
static_cast< const char *>(utf8), // source UTF-8 string
utf8ByteCount, // total length of source UTF-8 string,
// in bytes, including end-of-string \0
&utf16Buffer[0], // destination buffer
utf16Size // size of destination buffer, in wchar_t's
);
ATLASSERT( result != 0 );
if ( result == 0 )
{
AtlThrowLastWin32();
}
//
// Build UTF-16 string from conversion buffer
//
return CStringW( &utf16Buffer[0] );
}
</code>
HTH,
Giovanni
"Nord Pierre" <non> ha scritto nel messaggio
news:480cb5d3$0$8138$426a34cc@news.free.fr...
Hello,
I would like to know if there is any method to create an app (without
16- bits unicode) to be translated later in different language (including
language with non us-ascii) - I'm quiet alergic to unicode 16bits but i'm
ready to work a lot with UTF8 - I also have in an app a window that can
display many messages in english-french-german-japanese-chinese-korean ...
And i would like to know if there is a way to display each message (one
after the other) in a correct format (all source is in utf8) - It's a
classic CEdit control
Thanks
PS : sorry if i'm dreaming of an easy solution :)