| GNU gettext for Delphi, C++ Builder and Kylix 1.2 beta | ||
|---|---|---|
| <<< Previous | API reference | Next >>> |
This function is the most important function of them all, and is the function that names GNU gettext. It takes a string as parameter and returns the translation of that string, if a translation can be found. It is used like this:
MessageDlg (gettext('Hello, World'),mtInformation,[mbOK],0); |
Because the _() function is the same as gettext(), the line above is normally written like this:
MessageDlg (_('Hello, World'),mtInformation,[mbOK],0); |
The string parameter should only contain ascii characters. If it contains non-ascii characters, the conversion from ansistring to widestring may be sensitive to current Windows locale.
The result value is a widestring. If you use this in context with the Delphi string type (ansistring), Delphi 6 and later will automatically convert the string types. The MessageDlg example above show this, the first parameter in MessageDlg() is a string, not a widestring. Delphi converts between string and widestring using Windows API calls, and therefore converts perfectly to and from multibyte character sets. On Linux, the behaviour is identical.
The gettext() functions looks up translations in the default.mo file (the default text domain) unless the textdomain() procedure has been called.
![]() | Special case: The empty string |
|---|---|
Please note that you should never try to translate an empty string (_('')). The translation of the empty string is the message header, which contains information about the translator, the character set, modification date etc. |
![]() | Performance |
|---|---|
The gettext() function is very fast at looking up translations. It does a binary search on the translation data, which are sorted in binary, and the search is case sensitive and only a perfect match will give a translation. The translation files are memory mapped on Windows and read into memory on Linux, and therefore it doesn't take much time to find a translation. However, you should consider to store a translated value if you need it a lot of times, like when populating a big array. |
| <<< Previous | Home | Next >>> |
| function GetCurrentLanguage:string; | Up | function dgettext(Domain: string; MsgId: widestring): widestring; |