One of the most important feature for localization components is the ability to create multi-language applications that allow users to manage the translations. This includes updating localization and even add new localization language without recompiling the application. But this is easier than it might seem at first glance. Of course, if you use TsiLang localization components to create multilingual software.
Let’s describe the solution for this problem using sample project MastApp from the Demos Delphi sub-folder.
Translating the project.
We don’t describe the translation process in details. Because we assume, that you are already familiar with this part of TsiLang Components Suite. As a result, we will just mention the components used for the translation.
We use one TsiLangDispatcher; one TsiLang and TsiLangLinked on all other project forms.
Picture 1. Translation Wizard.
MastData unit is auto-created and used by all the project forms. So we place TsiLang and TsiLangDispatcher onto it.
Creating a translations file.
We can create an external translations file, either after providing the translations, or even without translations, but having just English terms. So we use File|Save/Load Translations|Save Project command from the TsiLang Expert menu (TsiLang Expert is available under the Tools IDE’s menu). We recommend to use SIB files to store translations as SIB files are much faster than SIL files. But you may also use SIL files as they store data in the simple ASCII format, which can be edited by any text editor.
Using an external translations file and dynamic update of available languages.
There are two ways to achieve this:
- Define the file name in FileName property of TsiLangDispatcher.
- Check the existence of the translation file in a specified location in code, and then load it manually.
By using the first way, the dispatcher will automatically check the availability of the file defined in FileName property and load it into all of the project forms. To implement the second way you can use the following sample code:
procedure TMastData.DataModuleCreate(Sender: TObject); var sOurSibFile: string; begin // determine the file name sOurSibFile := ExtractFilePath(Application.ExeName) + 'TheNameOfYourFile.sib'; // checking the existence of file if FileExists(sOurSibFile) then begin // set the property value // this will automatically load forms created later siLangDispatcher1.FileName := sOurSibFile; // load translations into already created forms siLangDispatcher1.LoadAllFromFile(sOurSibFile); end; end;
So you can select any of the ways above, depending on your own preferences.
Dynamically display the list of the available languages.
- Add the new top level menu item to the application’s main menu.
Picture 2. Creating the languages menu.
- Add the code that will dynamically adjust and create menu items according to the available languages. You can add this code to the main form OnShow event:
// changing active language upon menu item click procedure TMainForm.LanguageMenuItemClick(Sender: TObject); begin MastData.siLangDispatcher1.ActiveLanguage := TMenuItem(Sender).Tag; TMenuItem(Sender).Checked := True; end; procedure TMainForm.FormShow(Sender: TObject); var MenuItem: TMenuItem; I: Integer; begin for I := 1 to MastData.siLangDispatcher1.NumOfLanguages do begin MenuItem := TMenuItem.Create(Self); // set Caption MenuItem.Caption := MastData.siLangDispatcher1.LangNames[I - 1]; // set Tag property for easier language switching MenuItem.Tag := I; MenuItem.RadioItem := True; // display current language MenuItem.Checked := I = MastData.siLangDispatcher1.ActiveLanguage; // set event handler MenuItem.OnClick := LanguageMenuItemClick; // add menu item Language1.Add(MenuItem); end; end;
Please note: the code of language updating and displaying must be executed after loading of the translations file.
Run the project and “that’s all”! The task which looks complicated at first sight was solved in few minutes using TsiLang Components Suite. So your users will be able to translate your applications without recompiling and even without your assistance. Moreover, you can offer your users to use the SIL Editor for editing SIL/SIB files. SIL Editor could be downloaded from our download page.
TsiLang Components Suite home page: https://www.tsilang.com.
Latest version can be downloaded from: our download page.
You can download this article in PDF format here: creating_multilanguage_applications_translated_by_users.pdf.