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:
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.
The IMLangLineBreakConsole interface is part of the MultiLanguage Object. 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.
For Unicode strings, use BreakLineW. For multibyte strings, use 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. // locale - locale identifier of the output string. // pMLLBC - pointer to an IMLangLineBreakConsole interface. long cchSize = wcslen(pwszStr); // 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. 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); wcsncpy(wStr, cchSkip + totalLine, cchLine); // Copy characters of pwszStr for output. wStr[cchLine] = L'\0'; totalLine = totalLine + cchLine + cchSkip; // Increase the total number of characters processed. wprintf(L"%s\n", wStr); // Output the string to the console-based application. }
The following link provides more information about the operations described in this article.