How to Break Text Based on Locale
Different languages might have different rules for hyphenation, spacing, and where a line should be broken on a page. For this reason, the MLang IMLangLineBreakConsole interface provides a locale-aware line-breaking functionality for console-based applications.
To use this feature to break a Unicode string for output:
Make sure the Component Object Library has been initialized.
Before you can use any of the functionality provided by MLang, you must first initialize the Component Object Library through a call to CoInitialize. Every call to CoInitialize must be accompanied by a call to CoUninitialize when the application terminates. CoUninitialize ensures that the application does not quit until it has received all of its pending messages.
Obtain a pointer to an IMLangLineBreakConsole interface.
The IMLangLineBreakConsole interface is part of the MultiLanguage. If no such object exists, you must create one and obtain a pointer to the corresponding IMultiLanguage interface through a call to CoCreateInstance. Once this has been accomplished, call QueryInterface through IMultiLanguage for a pointer to an IMLangLineBreakConsole interface.
Use the IMLangLineBreakConsole methods to break the string into the proper number of columns for output.
For Unicode strings, use IMLangLineBreakConsole::BreakLineW. For multibyte strings, use IMLangLineBreakConsole::BreakLineA. The following code sample breaks the null-terminated Unicode string
pwszStr
, based on the constant NUMCOLUMNS and the given locale, and prints it to the screen.// pwszStr - null-terminated Unicode string of cchMax characters or less. // locale - locale identifier of the output string. // pMLLBC - pointer to an IMLangLineBreakConsole interface. size_t cchMax = STRSAFE_MAX_CCH; size_t cchLength; WCHAR pwszSrc[cchMax] = L("some source string"); HRESULT lengthHr = StringCchLengthW(pwszSrc, cchMax, &cchLength); long cchSize = cchLength; // Size of pwszStr in characters. int MaxColumns = NUMCOLUMNS; // Desired number of columns for output. long cchLine; // Number of characters to output in the current line. long cchSkip; // Number of characters to skip before starting the next line. long totalLine = 0; // Total number of characters processed. if(SUCCEEDED(lengthHr)) { WCHAR wStr[NUMCOLUMNS] = ""; HRESULT hr = S_OK; while((totalLine < cchSize) && SUCCEEDED(hr)) // Process the entire string unless an error occurs. { hr = pMLLBC->BreakLineW(locale, pwszStr + totalLine, cchSize - totalLine, MaxColumns, &cchLine, &cchSkip); HRESULT copyHr = StringCchCopyW(wStr, cchLine + 1, pwszStr + cchSkip + totalLine); // Copy characters of pwszStr for output. if(SUCCEEDED(copyHr)) { totalLine = totalLine + cchLine + cchSkip; // Increase the total number of characters processed. wprintf(L"%s\n", wStr); // Output the string to the console-based application. } } } else { // TODO: Add error handling code here. }
Remember to release the interfaces and uninitialize the Component Object Library before your program terminates.