The objpas unit provides the mechanism to correctly initialize the string tables. There is no need to include this unit in a uses clause, since it is automatically loaded when a program or unit is compiled in Delphi or objfpc mode. Since this is required to use resource strings, the unit is always loaded when needed.
The resource strings are stored in tables, one per unit, and one for the program, if it contains a resourcestring section as well. Each resourcestring is stored with it's name, hash value, default value, and the current value, all as AnsiStrings.
The objpas unit o ers methods to retrieve the number of resourcestring tables, the number of strings per table, and the above information for each string. It also o ers a method to set the current value of the strings.
Here are the declarations of all the functions:
Function ResourceStringTableCount : Longint;
Function ResourceStringCount(TableIndex : longint) : longint; Function GetResourceStringName(TableIndex, StringIndex : Longint) : Ansistring; Function GetResourceStringHash(TableIndex, StringIndex : Longint) : Longint; Function GetResourceStringDefaultValue(TableIndex, StringIndex : Longint) : AnsiString; Function GetResourceStringCurrentValue(TableIndex, StringIndex : Longint) : AnsiString; Function SetResourceStringValue(TableIndex, StringIndex : longint; Value : Ansistring) : Boolean; Procedure SetResourceStrings (SetFunction : TResourceIterator); |
Two other function exist, for convenience only:
Function Hash(S : AnsiString) : longint;
Procedure ResetResourceTables; |
Here is a short explanation of what each function does. A more detailed explanation of the functions can be found in the Reference guide.
Two other functions exist, for convenience only:
Given some Translate function, the following code would initialize all resource strings:
Var I,J : Longint;
S : AnsiString; begin For I:=0 to ResourceStringTableCount-1 do For J:=0 to ResourceStringCount(i)-1 do begin S:=Translate(GetResourceStringDefaultValue(I,J)); SetResourceStringValue(I,J,S); end; end; |
Other methods are of course possible, and the Translate function can be implemented in a variety of ways.