9.3 Updating the string tables

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 language 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 one of these mode is required to use resource strings, the unit is always loaded when needed anyway.

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 its name, hash value, default value, and the current value, all as AnsiStrings.

The objpas unit offers 2 methods to set the current value of the strings:

type  
  TResourceIterator =  
    Function (Name,Value : AnsiString;  
              Hash : Longint;  
              arg:pointer) : AnsiString;  
 
Procedure SetResourceStrings (SetFunction: TResourceIterator;  
                              arg: pointer);  
Procedure SetUnitResourceStrings (UnitName: string;  
                               SetFunction: TResourceIterator;  
                               arg: pointer);

Two other function exist, for convenience only:

Function Hash(S: AnsiString): longint;  
Procedure ResetResourceTables;  
Procedure FinalizeResourceTables;

Here is a short explanation of what each function does. A more detailed explanation of the functions can be found in the Reference Guide.

SetResourceStrings
giving this function a callback will cause the calback to be called for all resource strings, one by one. On return of the callback the value of the resource string will be set to the return value of the callback. The arg pointer is passed to the callback (this can be used to pass e.g. an object instance).
SetUnitResourceStrings
Is identical in behaviour to SetResourceStrings, but only calls the callback for the resource strings of unit UnitName.
Hash
can be used to calculate the hash value of a string. The hash value stored in the tables is the result of this function, applied on the default value. That value is calculated at compile time by the compiler: having the value available can speed up translation operations.
ResetResourceTables
will reset all the resource strings to their default values. It is called by the initialization code of the objpas unit.
FinalizeResourceTables
will remove all the resource string values (i.e. make them empty)