Mega Code Archive

 
Categories / Delphi / OOP
 

Play with the application interface for more languages support

Title: Play with the application interface for more languages support. Question: How do I reposition the components on the forms of my application to support more than one language? Answer: I wrote a business program that contains more than 200 forms and 250 reports. This program has an Arabic interface (right to left). After a while I received many requests from my customers to make the interface switch able between Arabic and English. At the beginning I thought this can done in two steps: 1-Changing the captions of the controls like Label to English. 2-Changing the BiDiMode of the forms will flip the position of the components form right to left and vies versa. The first step was simple and effective, but it was not the case with the second step. When I changed the BiDiMode property of the forms the controls did not flipped to the other side like I expected (it only works for String Grid and DBGrid), this can be achieved by designing two forms for each form in the application one with Arabic interface, and another one with English interface. This is a complete redundancy of the forms and an overhead for me to maintain two forms for each business screen. So I thought of writing a small routine to flip the controls of the form to the other side and return them back if needed. Enough talking lets watch the code: procedure RepositionFormComponents(var oForm : TComponent; iLanguage : Integer = 2); var i : Integer; begin if iLanguage = iArabic then Application.BiDiMode := bdRightToLeft else Application.BiDiMode := bdLeftToRight; TForm(oForm).BiDiMode := Application.BiDiMode; for i:=0 to oForm.ComponentCount-1 do begin if (oForm.Components[i] is TLabel) then begin TLabel(oForm.Components[i]).Font.Size := 8; end; if (oForm.Components[i] is TMaskEdit) then TMaskEdit(oForm.Components[i]).BiDiMode := bdRightToLeft; if (oForm.Components[i] is TEdit) or (oForm.Components[i] is TLabel) or (oForm.Components[i] is TCheckBox) or (oForm.Components[i] is TBitBtn) or (oForm.Components[i] is TMaskEdit) or (oForm.Components[i] is TGroupBox) or (oForm.Components[i] is TPanel) or (oForm.Components[i] is TComboBox) or (oForm.Components[i] is TDBEdit) or (oForm.Components[i] is TDBGrid) or (oForm.Components[i] is TDBlookUpComboBox) or (oForm.Components[i] is TDBRadioGroup) or (oForm.Components[i] is TDBImage) or (oForm.Components[i] is TDBCheckBox) then begin TLabel(oForm.Components[i]).Left := TControl(TControl(oForm.Components[i]).Parent).Width - (TLabel(TForm(oForm).Components[i]).Left+TLabel(TForm(oForm).Components[i]).Width); end; end; end; Example of use: procedure TForm1.FormCreate(Sender: TObject); begin //Change the captions to Arabic. Label1.Caption := ' '; Label2.Caption := ''; Label3.Caption := ''; .................................... .................................... //Switch the mode for Arabic. RepositionFormComponents(self,1); end; If the desired language is English: procedure TForm1.FormCreate(Sender: TObject); begin //Change the captions to English. Label1.Caption := 'Account No'; Label2.Caption := 'Name'; Label3.Caption := 'Group'; .................................... .................................... //Switch the mode for English. RepositionFormComponents(self,2); end; As you can see this routine receives two parameters. The first parameter is the form object and the second parameter is the required language interface. The second parameter could be 1 for Arabic and 2 for English. This routine not only works for Arabic and English but also for any right to left languages like Arabic, Farsi, and Urdu and any left to right languages like French, Dutch, or Spanish. Enjoy it