Special Encoding
"Sinha, Vineesh" <[email protected]>
| Newsgroups | gmane.comp.lib.icu.general |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I'm struggling to get the right encoding for the localized text. The software I'm working on has a feature to switch the output language. A user can start the job that creates a XML with language set to English, and then half way through, choose to switch to Traditional Chinese, without changing the locale on the user machine. The XML produced (encoded in UTF-8) is expected to have appropriate strings of various language. During his production job, he may just switch the output language of the application, and might continue to be on his english locale.
The generated output xml is expected to be appropriately encoded file. For some reason, I'm not able to get the other language strings correctly. Can anyone please look at the following code and guide me as where am I going wrong? This sample code doesn't write to XML file, but attempts to use Xerces and ICU to get the job done. How can I avoid using Xerces altogether?
Thanks,
Vineesh
//-------------------------------------------------
//include other xerces headers
#include <xerces/util/TransService.hpp>
#include <unicode/uloc.h>
void DisplayText();
int main()
{
//Intantiate xerces
DisplayText();
return SUCCESS;
}
void DisplayText()
{
const char* currentLocale = uloc_getDefault();
//Expect en_US on my machine.
//I have not changed the locale.
//Is the above block of code equivalent to the following code chunck??
/*
UErrorCode status = U_ZERO_ERROR;
Locale::setDefault( Locale::getEnglish(), status );
assert(!U_FAILURE(status) );
*/
UErrorCode uerr = U_ZERO_ERROR;
uloc_setDefault(ULOC_TAIWAN, &uerr);
//Just assume that the output language is zh_TW
//Check that uerr is still U_ZERO_ERROR
///////// Locale is now set to zh_TW. /////////
const std::string& textInChinese = GetLocalisedString();
//Assume this function returns a good
//Traditional Chinese string.
XERCES_CPP_NAMESPACE_QUALIFIER
DOMString dString1(textInChinese.c_str());
DOMString dString2 =
XERCES_CPP_NAMESPACE_QUALIFIER
DOMString dString1(textInChinese.c_str());
const XMLCh* stringValue1 = dString1.rawBuffer();
const XMLCh* stringValue2 = dString2.rawBuffer();
if (stringValue1 && stringValue2)
{
char* ptr1 = XMLString::transcode( stringValue1 );
char* ptr2 = XMLString::transcode( stringValue2 );
std::cout << "First Chinese String " << ptr1 << "\n";
std::cout << "Second Chinese String " << ptr2 << "\n";
std::cout << std::endl;
//-------------------------------------------------------------------------------
//ptr1 and ptr2 are not good encoded Chinese strings!!
//-------------------------------------------------------------------------------
XMLString::release( &ptr1 );
XMLString::release( &ptr2 );
}
//I added this code to attempt force encoding.
XMLTransService::Codes failReason;
XMLCh* UTF8_ENCODING = XMLString::transcode("UTF-8");
XMLTranscoder* UTF8_TRANSCODER =
XMLPlatformUtils::fgTransService->makeNewTranscoderFor(
UTF8_ENCODING,
failReason,
16*1024,
XMLPlatformUtils::fgMemoryManager);
unsigned int charsEaten;
char resultXMLString_Encoded1[16*1024+4];
char resultXMLString_Encoded2[16*1024+4];
UTF8_TRANSCODER->transcodeTo(
textVal,
XMLString::stringLen(stringValue1),
(XMLByte*) resultXMLString_Encoded1,
16*1024,
charsEaten,
XMLTranscoder::UnRep_Throw );
UTF8_TRANSCODER->transcodeTo(
textVal,
XMLString::stringLen(stringValue2),
(XMLByte*) resultXMLString_Encoded2,
16*1024,
charsEaten,
XMLTranscoder::UnRep_Throw );
std::cout << "Second Attempt: First Chinese String " << resultXMLString_Encoded1 << "\n";
std::cout << "Second Attempt: Second Chinese String " << resultXMLString_Encoded2 << "\n";
std::cout << std::endl;
//-------------------------------------------------------------------------------------
//resultXMLString_Encoded1 and resultXMLString_Encoded2
//are not good encoded Chinese strings!!
//-------------------------------------------------------------------------------------
/////////////// Reset the Locale. ///////////////
uloc_setDefault(locale.c_str() , &uerr);
//Check that uerr is still U_ZERO_ERROR
}