Advertisement
RandomClear

ITE Change Language

Jul 17th, 2019
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.39 KB | None | 0 0
  1. unit reinit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms;
  7.  
  8. procedure ReinitializeForms;
  9. function LoadNewResourceModule(Locale: LCID): Longint;
  10.  
  11. {
  12.  
  13. procedure TMainForm.SwitchLanguage(Sender: TObject);
  14. var
  15.   Name : String;
  16.   Size : Integer;
  17. begin
  18.   // TComponent(Sender).Tag could be like:
  19.   // ENGLISH = (SUBLANG_ENGLISH_US shl 10) or LANG_ENGLISH;
  20.   // FRENCH  = (SUBLANG_FRENCH shl 10) or LANG_FRENCH;
  21.   // GERMAN  = (SUBLANG_GERMAN shl 10) or LANG_GERMAN;
  22.   if LoadNewResourceModule(TComponent(Sender).Tag) <> 0 then
  23.   begin
  24.     Name := FontName.Text;
  25.     Size := StrToInt(FontSize.Text);
  26.     ReinitializeForms;
  27.     LanguageEnglish.Checked := (LanguageEnglish = Sender);
  28.     LanguageFrench.Checked  := (LanguageFrench  = Sender);
  29.     LanguageGerman.Checked  := (LanguageGerman  = Sender);
  30.  
  31.     CurrText.Name := Name;
  32.     CurrText.Size := Size;
  33.     SelectionChange(Self);
  34.     FontName.SelLength := 0;
  35.  
  36.     SetupRuler;
  37.     if Visible then Editor.SetFocus;
  38.   end;
  39. end;
  40.  
  41.  
  42. }
  43.  
  44. implementation
  45.  
  46. type
  47.   TAsInheritedReader = class(TReader)
  48.   public
  49.     procedure ReadPrefix(var Flags: TFilerFlags; var AChildPos: Integer); override;
  50.   end;
  51.  
  52. procedure TAsInheritedReader.ReadPrefix(var Flags: TFilerFlags; var AChildPos: Integer);
  53. begin
  54.   inherited ReadPrefix(Flags, AChildPos);
  55.   Include(Flags, ffInherited);
  56. end;
  57.  
  58. function SetResourceHInstance(NewInstance: Longint): Longint;
  59. var
  60.   CurModule: PLibModule;
  61. begin
  62.   CurModule := LibModuleList;
  63.   Result := 0;
  64.   while CurModule <> nil do
  65.   begin
  66.     if CurModule.Instance = HInstance then
  67.     begin
  68.       if CurModule.ResInstance <> CurModule.Instance then
  69.         FreeLibrary(CurModule.ResInstance);
  70.       CurModule.ResInstance := NewInstance;
  71.       Result := NewInstance;
  72.       Exit;
  73.     end;
  74.     CurModule := CurModule.Next;
  75.   end;
  76. end;
  77.  
  78. function LoadNewResourceModule(Locale: LCID): Longint;
  79. var
  80.   FileName: array [0..260] of char;
  81.   P: PChar;
  82.   LocaleName: array[0..4] of Char;
  83.   NewInst: Longint;
  84. begin
  85.   GetModuleFileName(HInstance, FileName, SizeOf(FileName));
  86.   GetLocaleInfo(Locale, LOCALE_SABBREVLANGNAME, LocaleName, SizeOf(LocaleName));
  87.   P := PChar(@FileName) + lstrlen(FileName);
  88.   while (P^ <> '.') and (P <> @FileName) do Dec(P);
  89.   NewInst := 0;
  90.   Result := 0;
  91.   if P <> @FileName then
  92.   begin
  93.     Inc(P);
  94.     if LocaleName[0] <> #0 then
  95.     begin
  96.       // Then look for a potential language/country translation
  97.       lstrcpy(P, LocaleName);
  98.       NewInst := LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE);
  99.       if NewInst = 0 then
  100.       begin
  101.         // Finally look for a language only translation
  102.         LocaleName[2] := #0;
  103.         lstrcpy(P, LocaleName);
  104.         NewInst := LoadLibraryEx(FileName, 0, LOAD_LIBRARY_AS_DATAFILE);
  105.       end;
  106.     end;
  107.   end;
  108.   if NewInst <> 0 then
  109.     Result := SetResourceHInstance(NewInst)
  110. end;
  111.  
  112. function InternalReloadComponentRes(const ResName: string; HInst: THandle; var Instance: TComponent): Boolean;
  113. var
  114.   HRsrc: THandle;
  115.   ResStream: TResourceStream;
  116.   AsInheritedReader: TAsInheritedReader;
  117. begin                   { avoid possible EResNotFound exception }
  118.   if HInst = 0 then HInst := HInstance;
  119.   HRsrc := FindResource(HInst, PChar(ResName), RT_RCDATA);
  120.   Result := HRsrc <> 0;
  121.   if not Result then Exit;
  122.   ResStream := TResourceStream.Create(HInst, ResName, RT_RCDATA);
  123.   try
  124.     AsInheritedReader := TAsInheritedReader.Create(ResStream, 4096);
  125.     try
  126.       Instance := AsInheritedReader.ReadRootComponent(Instance);
  127.     finally
  128.       AsInheritedReader.Free;
  129.     end;
  130.   finally
  131.     ResStream.Free;
  132.   end;
  133.   Result := True;
  134. end;
  135.  
  136. function ReloadInheritedComponent(Instance: TComponent; RootAncestor: TClass): Boolean;
  137.  
  138.   function InitComponent(ClassType: TClass): Boolean;
  139.   begin
  140.     Result := False;
  141.     if (ClassType = TComponent) or (ClassType = RootAncestor) then Exit;
  142.     Result := InitComponent(ClassType.ClassParent);
  143.     Result := InternalReloadComponentRes(ClassType.ClassName, FindResourceHInstance(
  144.       FindClassHInstance(ClassType)), Instance) or Result;
  145.   end;
  146.  
  147. begin
  148.   Result := InitComponent(Instance.ClassType);
  149. end;
  150.  
  151. procedure ReinitializeForms;
  152. var
  153.   Count: Integer;
  154.   I: Integer;
  155.   Form: TForm;
  156. begin
  157.   Count := Screen.FormCount;
  158.   for I := 0 to Count - 1 do
  159.   begin
  160.     Form := Screen.Forms[I];
  161.     ReloadInheritedComponent(Form, TForm);
  162.   end;
  163. end;
  164.  
  165. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement