These two functions are required for i18n implementation in Linux.
Setlocale is used to set locale, such as lc_all and lc_ctype. The general usage is setlocale (lc_all ,"")
This is used to set lc_all. The second parameter is an empty string that indicates the lc_all value defined in the environment variable.
Then bindtextdomain is used, for example: bindtextdomain ("libgammu", locale_path );
In Linux i18n, each resource file is a. Mo file, which is binary and is generated by a tool for a text file (the performance should be considered for Binary Conversion ). Therefore, in the code above, locale_path specifies a path for the mo file. Generally, if the above Code is called, The gettext library will find the mo file in this place:
/Usr/share/locale-langpack/<locale>/lc_messages/libgammu.mo
In addition, there are textdomain functions, such as textdomain ("gammu "); this function is used to set the text domain to be used currently (bindtextdomain must be used before these text domains so that gettext library can find the mo file ). If our program uses multiple Mo files, bindtextdomain needs to be used multiple times, and then textdomain is used to specify which one needs to be used before. For example, in gammu, The gammu command line program uses the text domain gammu and the libgammu library uses libgammu.mo.
The code for using these two functions in gammu is attached:
-
Code: select all
-
#ifdef GETTEXTLIBS_FOUND
void GSM_InitLocales(const char *path) {
/* setlocale, locale is "" means all locale settings are
depend on the environment setting. */
setlocale(LC_ALL, "");
if (path == NULL || strlen(path) == 0) {
#if defined(LOCALE_PATH)
bindtextdomain("libgammu", LOCALE_PATH);
#else
bindtextdomain("libgammu", ".");
#endif
} else {
/* bindtextdomain, libgammu is the catalog, path is the
message file root path. E.g: if path is /usr/share/locale-langpack,
then all messages represented by gettext will by found in:
/usr/share/locale-langpack/<locale>/LC_MESSAGES/libgammu.mo
Refer to manual of bindtextdomain for more details. */
bindtextdomain("libgammu", path);
}
}
#else
void GSM_InitLocales(const char UNUSED *path) {
setlocale(LC_ALL, "");
}
#endif