Unit implementing resource string translation using gettext files

The gettext unit can be used to hook into the resource string mechanism of Free Pascal to provide translations of the resource strings, based on the GNU gettext mechanism. The unit provides a class () to read the .mo files with localizations for various languages. It also provides a couple of calls to translate all resource strings in an application based on the translations in a .mo file.

Exception and string formatting support. Stream support file header bytes indicating a .mo file. This constant is found as the first integer in a .mo Structure found at the head of a .mo file. This structure describes the structure of a .mo file with string localizations. Magic constant, should equal MOFileHeaderMagic. File revision Number of string pairs in the file Offset of the original strings description table. Offset of the translated strings description table. Size of hashing table. Offset of hashing table. Structure describing string This record is one element in the string tables describing the original and translated strings. It describes the position and length of the string. The location of these tables is stored in the record at the start of the file. Length of the string Offset in the file Array of TMOStringInfo records. TMOStringTable is an array type containing records. It should never be used directly, as it would occupy too much memory. Pointer to a TMOStringTable array. Array of longword elements. TLongWordArray is an array used to define the pointer. A variable of type TLongWordArray should never be directly declared, as it would occupy too much memory. The PLongWordArray type can be used to allocate a dynamic number of elements. Pointer to a TLongWordArray array. Array of PChar elements TLongWordArray is an array used to define the pointer. A variable of type TPCharArray should never be directly declared, as it would occupy too much memory. The PPCharArray type can be used to allocate a dynamic number of elements. Pointer to a TPCharArray array. Class providing access to a .mo file. TMOFile is a class providing easy access to a .mo file. It can be used to translate any of the strings that reside in the .mo file. The internal structure of the .mo is completely hidden. Create a new instance of the TMOFile class.

Create creates a new instance of the MOFile class. It opens the file AFileName or the stream AStream. If a stream is provided, it should be seekable.

The whole contents of the file is read into memory during the Create call. This means that the stream is no longer needed after the Create call.

If the named file does not exist, then an exception may be raised. If the file does not contain a valid structure, then an exception is raised.
File to open. Contents of a .mo file. Removes the TMOFile instance from memory Destroy cleans the internal structures with the contents of the .mo. After this the TMOFile instance is removed from memory. Translate a string

Translate translates the string AOrig. The string should be in the .mo file as-is. The string can be given as a plain string, as a PChar (with length ALen). If the hash value (AHash) of the string is not given, it is calculated.

If the string is in the .mo file, the translated string is returned. If the string is not in the file, an empty string is returned.

None.
Translated string. String to translate. Length of the original string. Hash value of the original string. Exception raised in case of an error. EMOFileError is raised in case an instance is created with an invalid .mo. Return the current language IDs GetLanguageIDs returns the current language IDs (an ISO string) as returned by the operating system. On windows, the GetUserDefaultLCID and GetLocaleInfo calls are used. On other operating systems, the LC_ALL, LC_MESSAGES or LANG environment variables are examined. Primary language Secondary language Translate the resource strings of the application. TranslateResourceStrings translates all the resource strings in the application based on the values in the .mo file AFileName or AFile. The procedure creates an instance to read the .mo file if a filename is given. If the file does not exist or is an invalid .mo file. TMOfile instance with translated strings. Name of .mo file with translated strings Translate the resource strings of a unit. TranslateUnitResourceStrings is identical in function to , but translates the strings of a single unit (AUnitName) which was used to compile the application. This can be more convenient, since the resource string files are created on a unit basis. Name of the unit whose strings should be translated. TMOFile instance with translated strings. Name of .mo file with translated strings