Using Resource DLLs

From RAD Studio
Jump to: navigation, search

Go Up to Localizing Applications


The executable, DLLs, and packages (.bpl files) that make up your application contain all the necessary resources. However, to replace those resources by localized versions, you need only ship your application with localized resource DLLs that have the same name as your executable, DLL, or package files.

When your application starts up, it checks the locale of the local system. If it finds any resource DLLs with the same name as the EXE, DLL, or BPL files it is using, it checks the extension on those DLLs. If the extension of the resource module matches the language and country of the system locale, your application will use the resources in that resource module instead of the resources in the executable, DLL, or package. If there is not a resource module that matches both the language and the country, your application will try to locate a resource module that matches just the language. If there is no resource module that matches the language, your application will use the resources compiled with the executable, DLL, or package.

If you want your application to use a different resource module than the one that matches the locale of the local system, you can set a "locale override" entry in the Windows registry. Under the HKEY_CURRENT_USER\Software\Embarcadero\Locales key, select the New > String Value command to add a new key. In the Name column, insert your application's path and executable file name as a string value, and in the Data value, insert the desired extension of your resource DLLs. At startup, the application looks for resource DLLs with this extension before trying the system locale. Setting this registry entry allows you to test localized versions of your application without changing the locale on your system. (For more information about usage of "locale override" registry keys see Localizing Applications and Deploying Localized Applications.)

For example, the following procedure might be used in an install or setup program to set the registry key value that indicates the locale to use when loading applications:

procedure SetLocaleOverride(const FileName, LocaleOverride: string);
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    if Reg.OpenKey('Software\Embarcadero\Locales', True) then
      Reg.WriteString(FileName, LocaleOverride);
  finally
    Reg.Free;
  end;
end;
void SetLocalOverrides(char* FileName, char* LocaleOverride) {
	HKEY Key;
	const char* LocaleOverrideKey = "Software\\Embarcadero\\Locales";
	if (RegOpenKeyEx(HKEY_CURRENT_USER, LocaleOverrideKey, 0, KEY_ALL_ACCESS,
		&Key) == ERROR_SUCCESS) {
		if (lstrlen(LocaleOverride) == 3)
			RegSetValueEx(Key, FileName, 0, REG_SZ,
			(const BYTE*)LocaleOverride, 4);
		RegCloseKey(Key);
	}
}

Within your application, use the global System.FindResourceHInstance function to obtain the handle of the current resource module. For example:

LoadStr(FindResourceHInstance(HInstance), 
 IDS_AmountDueName, szQuery, SizeOf(szQuery));

 LoadString(FindResourceHInstance(HInstance),
  IDS_AmountDueName, szQuery, sizeof(szQuery));
LoadString(FindResourceHInstance(HInstance), IDS_AmountDueName, szQuery, sizeof(szQuery));

You can ship a single application that adapts itself automatically to the locale of the system it is running on, simply by providing the appropriate resource DLLs.

See Also