Skip to content
bjmillerpa edited this page Dec 8, 2013 · 1 revision

Language Manager

The Language Manager is implemented through the static class TDKLanguageManager. It maintains a list of available translations, applies those translations via the language controllers on forms, and translates constants on demand.

class function  RegisterLangFile(const wsFileName: UnicodeString): Boolean;
class function  RegisterLangStream(stream: TStream): Boolean; 
class procedure RegisterLangResource(Instance: HINST; const wsResourceName: UnicodeString; wLangID: LANGID);
class procedure RegisterLangResource(Instance: HINST; iResID: Integer; wLangID: LANGID); overload;

class procedure UnregisterLangResource(wLangID: LANGID);

class function  ScanForLangFiles(const wsDir, wsMask: UnicodeString; bRecursive: Boolean): Integer;

class function  IndexOfLanguageID(wLangID: LANGID): Integer;

class procedure TranslateFromStream(Stream: TStream);

class property ConstantValue[const wsName: UnicodeString]: UnicodeString read ;
class property ConstantValueW[const wsName: UnicodeString]: UnicodeString read ;

class property DefaultLanguageID: LANGID read  write ;
class property LanguageID: LANGID read  write ;
class property LanguageIndex: Integer read  write ;
class property LanguageCount: Integer read ;
class property LanguageIDs[Index: Integer]: LANGID read ;
class property LanguageNames[Index: Integer]: UnicodeString read ;
class property LanguageNativeNames[Index: Integer]: UnicodeString read ;

class property LanguageResources[Index: Integer]: PDKLang_LangResource read ;

type LangManger = TDKLanguageManager;

The following three methods register a translation for a specified language. Each returns True if the translation was valid with a language specified. The translation replaces any language resource for that language registered before. However, you can never replace the DefaultLanguage.

  • RegisterLangFile - Registers a file-based translation.

  • RegisterLangStream - Registers a stream-based translation.

  • RegisterLangResource - Registers a translation stored in a resource.

  • UnregisterLangResource - Removes the language from the Language Manager. You cannot remove the DefaultLanguage.

  • ScanForLangFiles - Scans the specified directory for language files using given file mask. If bRecursive=True, also searches in the subdirectories of sDir. Returns the number of files successfully registered. TDKLanguageManager.ScanForLangFiles(ExtractFileDir(ParamStr(0)), '*.lng', False);

  • IndexOfLanguageID - Returns the index of specified LangID, or -1 if not found.

  • TranslateFromStream - Load language and translate from a stream. Assumes stream contains contents of a translations file. Could be used for reading in a language file from a database.

  • ConstantValue - Constant values keyed by name.

  • ConstantValueW - Constant values keyed by name. Same as ConstantValue (W version left for compatibility with old code).

  • DefaultLanguageID - Default language ID. The default value is US English ($409).

  • LanguageID - Current language ID. Initially equals to DefaultLanguageID. When being changed, affects all the registered language controllers as well as constants.

  • LanguageIndex - Current language index in list of registered languages.

  • LanguageCount - Number of languages (language resources) registered, including the default language

  • LanguageIDs - LangIDs of registered languages (language resources), index ranged 0 to LanguageCount-1

  • LanguageNames - Names (in English) of registered languages (language resources), index ranged 0 to LanguageCount-1

  • LanguageNativeNames - Native names of registered languages (language resources), index ranged 0 to LanguageCount-1

  • LanguageResources - Language resources of registered languages (language resources), index ranged 0 to LanguageCount-1. Always nil for Index=0, i.e. for default language.

  • DKLangConstW - Shortcut to LangManager.ConstantValueW. Comes in two versions: simple lookup and lookup and then call Format().

  • LangManager - is an alias to TDKLanguageManager to maintain compatibility with older code.

Language Controller

TDKLanguageController is a non-visual component used on forms (and frames). The Language Controller updates form component properties with new translations on command of the Language Manager. In general, most properties will not need to be changed from their defaults.

published properties

property IgnoreList: TStrings read  write ;
property Options: TDKLanguageControllerOptions read  write ;
property SectionName: UnicodeString read  write ;
property StoreList: TStrings read  write ;

property OnLanguageChanging: TNotifyEvent read  write ;
property OnLanguageChanged: TNotifyEvent read  write ;
  • IgnoreList - List of ignored properties

  • Options - Language controller options. Default is for all options to be selected.

    • dklcoAutoSaveLangSource - If on, the component will automatically save itself into the Project's language resource file (*.dklang)
    • dklcoIgnoreEmptyProps - Ignore all properties having no string assigned
    • dklcoIgnoreNonAlphaProps - Ignore all properties with no alpha characters (e.g. with numbers or symbols only); includes dklcoIgnoreEmptyProps behavior
    • dklcoIgnoreFontProps - Ignore all TFont properties
  • SectionName - Name of a section in the language file corresponding to the form or frame served by the controller. If empty (default), Owner's name is used as section name.

  • StoreList - List of forcibly stored properties

  • OnLanguageChanging - Event that fires when the language is changing through the Language Manager

  • OnLanguageChanged - Event that fires when the language has changed through the Language Manager

Language Code Support

Several utility functions were added at version 5 to support fetching language names and ISO-639 codes. These functions are located in DKL_LanguageCodes.pas

DKLang translations are stored with and referenced by a "LANGID" (actually a Windows locale ID). The following functions return their values based on a LANGID key.

Language Code

function GetLanguageCodeFromLangId(aLANGID: UInt32): string;

Returns the two (usually) character ISO-639-1 language code.

Returns "ru" for 1049. Returns "es" for 4106.

Culture Code

function GetCultureCodeFromLangId(aLANGID: UInt32): string;

Returns the two (usually) character ISO-639-1 language code along with the two character culture code.

Returns "ru-RU" for 1049. Returns "es-GT" for 4106.

Culture Name

function GetCultureNameFromLangId(aLANGID: UInt32): string;

Returns "Russian (Russia)" for 1049.

Returns "Spanish (Guatemala)" for 4106.

Language Native Name

function GetCultureNativeNameFromLangId(aLANGID: UInt32): string;

Returns "русский (Россия)" for 1049.

Returns "Español (Guatemala)" for 4106.

Language Name

function GetLanguageNameFromLangId(aLANGID: UInt32): string;

Returns the full name of the language in English.

Returns "Russian" for 1049.

Returns "Spanish" for 4106.

Language Native Name

function GetLanguageNativeNameFromLangId(aLANGID: UInt32): string;

Returns "русский" for 1049.

Returns "Español" for 4106.