动态库的国际化要使用
- #define _(String) dgettext(GETTEXT_PACKAGE, String)
代替
- #define _(String) gettext(String)
Special requirement for libraries
If you are writing a library, almost everything written previously applies as with a standalone application. The only problem that arises is that a library will never really know what message domain is currently active when it is processing a string and you usually want to have the library's own domain active at that point. The simplest solution to this problem is to change one of the defintions in the i18n header file mentioned in the Section 2.1, “The i18n header file” section. Find the line that says
#define _(String) gettext(String) |
and change it to read
#define _(String) dgettext(GETTEXT_PACKAGE, String) |
In this way, all strings that pass through the _() function will be translated in the domain of the library all the time.
One other situation that arises very rarely may be worth knowing about. In libgnomeui, for example, there are functions that operate on arbitrary arrays of strings (menu items, in this case). Some of those strings are standard and have been translated in the library itself. Others will have been supplied by the client application and translated in that domain. So libgnomeui defines the following function and convenience macro.
Example 2. Code from libgnomeui/gnome-app-helper.c
#define L_(x) gnome_app_helper_gettext (x) |
Strings marked with L_() are thereby translated in the currently active domain if possible and the library's domain otherwise. You may wish to duplicate this code if you have similar circumstances.