home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / Bonus / Plasmatech / ptscp_examples.exe / %MAINDIR% / Examples / Translation / Delphi / FMain.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-08-31  |  7.8 KB  |  250 lines

  1. unit FMain;
  2. {
  3.   Shell Control Pack Examples
  4.   TrDemo - How to apply translations.
  5.  
  6.   Ensure the Plasmatools directory is in your project or library search path
  7.   and the Lang subdirectory is in place. This demo requires either the DCU
  8.   or Source versions of the Shell Control Pack. This demo won't do translations
  9.   with Delphi 2.
  10.  
  11.   Delphi 3
  12.   ========
  13.   You can choose to build translated strings directly into the EXE or include
  14.   them in a separate translation DLL.
  15.  
  16.   To build into the EXE, simply define one of the LANG_ constants in the
  17.   Project|Options|Directories/Conditionals|Conditional defines field.
  18.   See UPTShConsts.pas for a list of valid defines. eg. LANG_DE for German,
  19.   LANG_FR for French and LANG_ES for Spanish.
  20.  
  21.   To build seperate translation DLLs, just run the build.bat files in the
  22.   D3 directory. TrDemo.de and TrDemo.fr files will be placed with the
  23.   TrDemo.exe. You can then run TrDemo.exe to see the translations (if running
  24.   on the appropriate language version of Windows). Use the TrDemo.exe program
  25.   itself to override automatic translation DLL selection.  
  26.  
  27.   Delphi 2
  28.   ========
  29.   Ensure the .res files under the Plasmatools\Lang directory are built by
  30.   executing the makeres.bat file in the Lang directory.
  31.  
  32.   Build the translated .res files into your EXE by applying a LANG_ constant to 
  33.   the Project|Options|Directories/Conditionals|Conditional defines field.
  34.  
  35.   Delphi 2 does not natively support translation DLLs. If you are building your
  36.   own Translation DLL, simply include the appropriate .res file in each language
  37.   DLL and set the UPTShConsts.gptshResourceInstance global variable to the
  38.   HInstance of your translation DLL.
  39.  
  40.   Non-Ansi character sets: A bug in Delphi 2 means popup hints always use the MS
  41.   Sans Serif font - especially useless for double-byte languages like Japanese.
  42.   You can fix this with an Appliation.OnShowHint handler, or change the
  43.   HintWindowClass. This is only a problem in Delphi 2.
  44. }
  45.  
  46. interface
  47.  
  48. uses
  49.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  50.   Registry, StdCtrls, FPTOpenDlg, ComCtrls, UPTTreeList, UPTShellControls, UPTFrame,
  51.   Menus, FPTFolderBrowseDlg, UPTShellUtils;
  52.  
  53. type
  54.   TFrmMain = class(TForm)
  55.     PTOpenDlg1: TPTOpenDlg;
  56.     PTSaveDlg1: TPTSaveDlg;
  57.     Label1: TLabel;
  58.     MainMenu1: TMainMenu;
  59.     File1: TMenuItem;
  60.     Exit1: TMenuItem;
  61.     Test1: TMenuItem;
  62.     Opendialog1: TMenuItem;
  63.     Savedialog1: TMenuItem;
  64.     Folderbrowsedialog1: TMenuItem;
  65.     PTFrame1: TPTFrame;
  66.     LangEdt: TEdit;
  67.     Label2: TLabel;
  68.     PTFrame2: TPTFrame;
  69.     PTFolderBrowseDlg1: TPTFolderBrowseDlg;
  70.     N1: TMenuItem;
  71.     Changelanguage1: TMenuItem;
  72.     Clearoverride1: TMenuItem;
  73.     procedure FormCreate(Sender: TObject);
  74.     procedure Opendialog1Click(Sender: TObject);
  75.     procedure Savedialog1Click(Sender: TObject);
  76.     procedure Folderbrowsedialog1Click(Sender: TObject);
  77.     procedure Clearoverride1Click(Sender: TObject);
  78.     procedure Changelanguage1Click(Sender: TObject);
  79.     procedure Exit1Click(Sender: TObject);
  80.   private
  81.     { Private declarations }
  82.   public
  83.     { Public declarations }
  84.   end;
  85.  
  86. var
  87.   FrmMain: TFrmMain;
  88.  
  89. implementation
  90.  
  91. {$R *.DFM}
  92.  
  93. {$IFDEF VER100}
  94. function GetResourceModuleName(ModuleName: PChar): String; forward;
  95. {$ENDIF}
  96.  
  97. procedure TFrmMain.FormCreate(Sender: TObject);
  98. begin
  99. {$IFNDEF VER100}
  100.   LangEdt.Text := 'Language modules apply to Delphi 3 only.';
  101. {$ELSE}
  102.   LangEdt.Text := GetResourceModuleName(PChar(ParamStr(0)));
  103. {$ENDIF}
  104. end;
  105.  
  106. procedure TFrmMain.Opendialog1Click(Sender: TObject);
  107. begin
  108.   PTOpenDlg1.Execute;
  109. end;
  110.  
  111. procedure TFrmMain.Savedialog1Click(Sender: TObject);
  112. begin
  113.   PTSaveDlg1.Execute;
  114. end;
  115.  
  116. procedure TFrmMain.Folderbrowsedialog1Click(Sender: TObject);
  117. begin
  118.   PTFolderBrowseDlg1.Execute;
  119. end;
  120.  
  121. const LOCALE_OVERRIDE_KEY = 'Software\Borland\Delphi\Locales';
  122.  
  123. procedure TFrmMain.Clearoverride1Click(Sender: TObject);
  124. var r: TRegistry;
  125. begin
  126.   r := TRegistry.Create;
  127.   try
  128.     if r.OpenKey( LOCALE_OVERRIDE_KEY, FALSE ) then
  129.       r.DeleteValue( ParamStr(0) )
  130.     else
  131.       raise Exception.Create( 'Failed' );
  132.     r.CloseKey;
  133.     ShowMessage( 'Restart the application for the change to take effect. ' );
  134.   finally
  135.     r.Free;
  136.   end;
  137. end;
  138.  
  139. procedure TFrmMain.Changelanguage1Click(Sender: TObject);
  140. var o: TPTOpenDlg;
  141.     r: TRegistry;
  142.     h: THandle;
  143.     s: String;
  144. begin
  145.   o := TPTOpenDlg.Create( self );
  146.   o.Options := [ptofFileMustExist, ptofHideReadOnly, ptofAllowTree, ptofShowHints, ptofOleDrag, ptofOleDrop];
  147.   s := ExtractFileName(ParamStr(0));
  148.   s := Copy(s, 1, Length(s) - Length(ExtractFileExt(s)));
  149.   o.Filter := Format('Translation DLLs (%s.??)|%s.??|All files (*)|*|',[s,s]);
  150.   try
  151.     if o.Execute then
  152.     begin
  153.       r := TRegistry.Create;
  154.       try
  155.         h := LoadLibraryEx( PChar(o.FileName), 0, LOAD_LIBRARY_AS_DATAFILE );
  156.         if (h=0) then
  157.           raise Exception.Create( 'File is not a translation DLL.' )
  158.         else
  159.           FreeLibrary(h);
  160.  
  161.         if r.OpenKey( LOCALE_OVERRIDE_KEY, TRUE ) then
  162.           r.WriteString( ParamStr(0), Copy(ExtractFileExt(o.FileName), 2, $FF) )
  163.         else
  164.           raise Exception.Create( 'Failed' );
  165.         r.CloseKey;
  166.         ShowMessage( 'Restart the application for the change to take effect. ' );
  167.       finally
  168.         r.Free;
  169.       end;
  170.     end;
  171.   finally
  172.     o.Free;
  173.   end;
  174. end;
  175.  
  176. procedure TFrmMain.Exit1Click(Sender: TObject);
  177. begin
  178.   Close;
  179. end;
  180.  
  181.  
  182. {$IFDEF VER100}
  183.  
  184. { This method is not pertitent to the example. It detects which
  185.   language DLL is being used by Delphi 3. We cannot simply do a
  186.   GetModuleFileName(FindResourceHInstance.... since Delphi loads
  187.   resource dlls with the LOAD_LIBRARY_AS_DATAFILE flag. Windows
  188.   rejects calls to GetModuleName of DLLs loaded with this flag. }
  189.  
  190. function GetResourceModuleName(ModuleName: PChar): String;
  191. var
  192.   FileName: array[0..260] of Char;
  193.   Key: Integer;
  194.   LocaleName, LocaleOverride: array[0..4] of Char;
  195.   Size: Integer;
  196.   P: PChar;
  197.   hinst: THandle;
  198. begin
  199.   GetModuleFileName(0, FileName, SizeOf(FileName)); // Get host appliation name
  200.   LocaleOverride[0] := #0;
  201.   if RegOpenKeyEx(HKEY_CURRENT_USER, LOCALE_OVERRIDE_KEY, 0, KEY_ALL_ACCESS, Key) = 0 then
  202.   try
  203.     Size := SizeOf(LocaleOverride);
  204.     if RegQueryValueEx(Key, FileName, nil, nil, PByte(@LocaleOverride[0]), @Size) <> 0 then
  205.       RegQueryValueEx(Key, '', nil, nil, PByte(@LocaleOverride[0]), @Size);
  206.   finally
  207.     RegCloseKey(Key);
  208.   end;
  209.   lstrcpy(FileName, ModuleName);
  210.   GetLocaleInfo(GetThreadLocale, LOCALE_SABBREVLANGNAME, LocaleName, SizeOf(LocaleName));
  211.   hinst := 0;
  212.   if (FileName[0] <> #0) and ((LocaleName[0] <> #0) or (LocaleOverride[0] <> #0)) then
  213.   begin
  214.     P := PChar(@FileName) + lstrlen(FileName);
  215.     while (P^ <> '.') and (P <> @FileName) do Dec(P);
  216.     if P <> @FileName then
  217.     begin
  218.       Inc(P);
  219.       // First look for a locale registry override
  220.       if LocaleOverride[0] <> #0 then
  221.       begin
  222.         lstrcpy(P, LocaleOverride);
  223.         hinst := LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE);
  224.       end;
  225.       if (hinst = 0) and (LocaleName[0] <> #0) then
  226.       begin
  227.         // Then look for a potential language/country translation
  228.         lstrcpy(P, LocaleName);
  229.         hinst := LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE);
  230.         if hinst = 0 then
  231.         begin
  232.           // Finally look for a language only translation
  233.           LocaleName[2] := #0;
  234.           lstrcpy(P, LocaleName);
  235.           hinst := LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE);
  236.         end;
  237.       end;
  238.     end;
  239.   end;
  240.   if hinst=0 then
  241.     GetModuleFileName( HInstance, FileName, High(Filename) );
  242.   result := ExtractFileName(ShellGetDisplayPathName(Filename));
  243.   FreeLibrary( hinst );
  244. end;
  245.  
  246. {$ENDIF VER100}
  247.  
  248. end.
  249.  
  250.