RE: Special Encoding

"Carl W. Brown" <[email protected]>
Newsgroups gmane.comp.lib.icu.general
Message-ID <[email protected]>
Vineesh,

It looks like what you may need are context switching routines.  You will
use a locale using UTF-8 as a charset for all your work with Xerces and one
of several contexts with different charsets when dealing with data in
various different code pages.

As Steven says trying to change the default locale to pass locale
information is dangerous.

Thread safe context management is a easy way to keep nested functions and
object methods in sync with the data that it working with.  When you
identify the locale of an object then you set the context that all methods
will use when operating on the object.

You can also have different contexts for different classes of objects if
different objects share methods.  The method only has to set the current
context that represents that class of object before invoking the inner
method or function.

I have some code that is also open source that helps people develop
applications to use a context management system with ICU.  Unlike ICU the
code is designed to be adapted and integrated into your code and customized
to your specific needs.  The code is written in C so that it will work with
both C and C++ applications.

Check out: http://www.xnetinc.com/xiua/

Carl

> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of Sinha, Vineesh
> Sent: Friday, September 05, 2003 1:42 PM
> To: [email protected]
> Subject: Special Encoding
>
>
> 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
> }
> _______________________________________________
> icu mailing list
> [email protected]
> http://oss.software.ibm.com/developerworks/oss/mailman/listinfo/icu
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.