Programming Rules

Tip 1: Call the setlocale() function to set language (locale) at the beginning of programs.

Call setlocale() for non-Xt/Motif based programs. Because it affects locale sensitive functions, setlocale() should usually be called at the beginning of programs. But it can be called whenever necessary.

Call XtSetLanguageProc() for Xt/Motif based programs. XtSetLanguageProc() should be called before Xt initialization. It does not actually set a locale, but rather registers a default language procedure which contains a setlocale() function call. Because Xt only calls the procedure registered in XtSetLanguageProc() when Xt is initialized, any XtSetLanguageProc() function calls after Xt initialization have no effect.

Tip 2: Do not hard-code messages, labels, font names or widget layout values.

  • Get messages from the message catalogs for non-X based programs. Use the catgets() function.
  • Get messages, labels, font names and layout values from resource files (or message catalog files) for X based programs. You can also define your own resources and use them.
Tip 3: Use a font set or a font list instead of a font.
  • Some Asian languages need several fonts. The fonts should be handled as font sets or a font lists.
  • If you use Xlib functions and need to create a font set in your program, use XCreateFontSet(), instead of XLoadQueryFont().
  • If you use Motif functions and need to create a font list in your program, use functions such as XmFontListEntryLoad(), XmFontListEntryCreate() and XmFontListAppendEntry().
Tip 4: Do not use special characters for widget names with Xt/Motif programs.
  • All widgets should have names because the use of NULL as a widget name makes it impossible to identify widgets for localization. Widget names should contain only ASCII alphanumeric characters and underscore; it is difficult or impossible to localize strings if a widget name contains whitespace, punctuation or special characters such as '*', '.', ':' and '?'.
  • One common practice causes particular problems: naming a widget using its label string often leads to widget names that are difficult to use, because labels sometimes contain the aforementioned special characters.
Tip 5: Do not fix widget arrangements and sizes.
  • The length of strings or labels may change depending on languages. Do not fix a layout or sizes of widgets just for English.
  • A widget layout should be able to change automatically based on a language (locale). If this practice is difficult to implement, you should define widget sizes and spacings/locations in the resource files. 
  • Because font width varies among languages, the number of columns and the size of a text (field) widget vary. Therefore, the layout with text (field) widgets should be adjusted automatically based on the current language, or changed by values in a resource file. They should not be hard-coded.
  • Using a form widget is one relatively easy (and recommended) method to automatically adjust the layout of primitive widgets. Define values for ATTACH_* resources explicitly and correctly.
Tip 6: Use multi-byte functions for X based programs.
  • Use XmbDrawString() instead of XDrawString() to draw texts.
  • Use XmbLookupString() instead of XLookupString() in cases when the program handles X events directly.
Tip 7: Use multi-byte or wide character functions for text processing.
  • Do not assume characters are the ASCII code set. Some code sets use 8 bits for a character, and others may use multi-byte characters which have several bytes per character.
  • Use multi-byte or wide character functions for text processing, such as mblen(),mbtowc(), mbstowcs(), iswupper(), and isideogram().
  • In some languages, there are no spaces between words. This affects algorithms for word wrapping.
Tip 8: Use Selection for Cut & Paste with X based programs.
  • Use Selection instead of Cut Buffer for Cut & Paste.
  • Use compound texts for inter-client communications.
Tip 9: Use LC_MESSAGES to get messages.
  • Be sure to use the LC_MESSAGES sub-category of a locale when deciding where to get message catalogs, HTML files, etc. Do not use the LANG environment variable directly and do not use LC_ALL for querying.
  • When calling catopen(), specify the OFLAG of NL_CAT_LOCALE.
Tip 10: Use the XPG4 message catalog functions instead of gettxt()
  • Use the XPG4 message catalog functions such as catopen() and catgets() instead of the outdated interface: gettxt(). You may still use the gettxt command in shell scripts.