Common Desktop Environment: Internationalization Programmer's Guide

Drawing Localized Text

There are several mechanisms provided to render a localized string, depending on the Motif or Xlib library being used. The following discusses the interfaces that are recommended for internationalized applications. Yet, it is recommended that all localized data be externalized from the program using the simple text.

Simple Text

The following Xlib multibyte (char*) drawing functions are available for internationalization:

The following Xlib wide character (wchar_t*) drawing functions are available for internationalization:

The following Xlib multibyte (char*) font metric functions are available for internationalization:

The following Xlib wide character (char_t*) font metric functions are available for internationalization:

XmString (Compound String)

For the Xm library, localized text should be inserted into XmString segments using XmStringCreateLocalized(). The tag associated with localized text is XmFONTLIST_DEFAULT_TAG, which is used to match an entry in a font list. Applications that mix several fonts within a compound string using XmStringCreate() should use XmFONTLIST_DEFAULT_TAG as the tag for any localized string.

More importantly, for interclient communications, the XmStringConvertToCT() function associates a segment tagged as XmFONTLIST_DEFAULT_TAG as being encoded in the code set of the locale. Otherwise, depending on the tag name used, the Xm library may not be able to properly identify the encoding on interclient communications for text data.

A localized string segment inside an XmString can be drawn with a font list having a font set with XmFONTLIST_DEFAULT_TAG. Use of a localized string is recommended for portability.

The following is an example of creating a font list for drawing a localized string:

XmFontList CreateFontList( Display* dpy, char* pattern)  
}   

SmFontListEntry font_entry; 
XmFontList fontlist; 
 font_entry = XmFontListEntryLoad( dpy, pattern,
                                XmFONT_IS_FONTSET,
            XmFONTLIST_DEFAULT_TAG);   

fontlist = XmFontListAppendEntry(NULL, font_entry);  
/* XmFontListEntryFree(font_entry); */   

if ( fontlist == NULL ) {
        XtWarning("fl2fs: can't create fontlist...");
  exit (0);  }   

return fontlist;  
}   

int main(argc,argv)  
int argc;  
char **argv;  
}
    Display *dpy;            /* Display                 */
    XtAppContext app_context;/*  Application Context    */

    XmFontList fontlist;
    XmFontSet fontset;
    XFontStruct** fontstructs;
    char** fontnames;
    int i,n;

char *progrname;     /* program name without the full pathname */
 
if (progname=strrchr(argv[0], `/')){
    progname++;
}
else  {
    progname = argv[0];
}
 
/*    Initialize toolkit and open display.
 */  
XtSetLanguageProc(NULL, NULL, NULL);  
XtToolkitInitialize(): 
app_context = XtCreateApplicationContext();  
dpy = XtOpenDisplay(app_context, NULL, progname, "XMdemos",
                      NULL, 0, &argc, argv);  
if (!dpy) {
      XtWarning("fl2fs: can't open display, exiting...");
      exit(0); 
}   

fontlist = CreateFontList(dpy, argv[1] ); 
 fontset = FontList2FontSet( fontlist );   

/*  
 * Print out BaseFontNames of Fontset  
 */  
n = XFontsOfFontSet( fontset, &fontstructs, &fontnames);

     printf("Fonts for %s is %d\n", argv[1], n);

     for (i = 0 ; i < n ; ++i ) printf("font[%d} - %s\n", i,\
                                        fontnames[i] );
     exit(1);
}

A localized string can be written in resource files because a compound string specified in resource files has a locale-encoded segment with Xm_FONTLIST_DEFAULT_TAG. For example, the fontList resource in the following example is automatically associated with XmFONTLIST_DEFAULT_TAG.

labelString:	Japanese string
	*fontList: -dt-interfacesystem-medium-r-normal-L*-*-*-*-*-*-*:

The following set of XmString functions is recommend for internationalization:

The following set of XmString functions is not recommend for internationalization because it takes a direction that may not work with languages not covered: