Having compiled a program with resourcestrings is not enough to internationalize your program. At run-time, the program must initialize the string tables with the correct values for the anguage that the user selected. By default no such initialization is performed. All strings are initialized with their declared values.
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 offers methods to retrieve the number of resourcestring tables, the number of strings per table, and the above information for each string. It also offers 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); |
Function Hash(S : AnsiString) : longint; Procedure ResetResourceTables; |
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; |