How to Enumerate Code Pag...     How to Use Font Linking     MLang Tutorials    
Web Workshop (Miscellaneous)

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:

  1. 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.

  2. Obtain a pointer to an IMLangLineBreakConsole interface.

    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.

  3. Use the IMLangLineBreakConsole methods to break the string into the proper number of columns for output.

    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.
    }
    
  4. Remember to release the interfaces and uninitialize the Component Object Library before your program terminates.

The following link provides more information about the operations described in this article.



Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.