How to change interface language at runtime? - delphi

I'm using embedded Delphi 2010 localization tools and I can't find the way how to change interface language at runtime. So, how can I change interface language at runtime ?

VCL is not designed such way that it would make runtime language switch easy. The RichEdit demo is actually using form reallocation. It is too strong action for a language switch because you will lose everything you have done for the forms on runtime.
Some localization tools have implemented add-on to VCL to implement runtime language switch. Some use components together with proprietary translations data to perform this. Some use the standard resource DLL with some units linked to application to perform language switch. For example take a look at this document. It describes step by step who to perform runtime language switch without needing to reallocate forms and without losing any data.
http://www.sisulizer.com/translation/vcl/VCL.pdf
Be careful with arrays containing resourcestring!

See the RichEdit demo in Demos\DelphiWin32\VCLWin32\RichEdit directory.

Related

Delphi Application internationalization and localization

I had read documentation about localizing Embarcadero DocWiki.
But I do not know how to:
change language in Run-Time - for example I would to click in application menu change language to English or Polish
Show translated text in dialogs, e.g: ShowMessage('Zmieniłeś język');
1.
There was sample project (shipped with Delphi) called RichEdit which had run-time change of language. To achieve that, Reinit unit was used (you can take it from here) which actually reloads all the resources.
It's working normally on simple programs like that, but may be real pain to implement in something more complex, as all the controls on form are reverted to initial state in which they have been at start-up, so it's your responsibility to keep your data consistent.
Though in proper implementation where all the application logic is separated from GUI (so there are no vars on TForm, no storing user data in edit boxes etc), it should work normally.
See RichEdit sample's source code to understand how to use Reinit.pas
2.
The classical approach is to use ResourceString. In interface section of your unit or better in separate unit, write:
ResourceString
ChangeLanguageStr = 'Zmieniłeś język';
//another strings here
and for showing message:
ShowMessage(ChangeLanguageStr);
These strings will be shown in translation manager.

How do I access unsupported UI elements?

I want to test a Delphi application with Coded UI Tests, but I have some problems. I want access to some elements in the UI but cannot access all elements.
There is a list on the screen with some rows (according to the search parameters) and I want to access these rows to check the consistency of the values. But with the cross of Coded UI Test Builder I cannot access the rows on the application. How do I access the rows?
I just found one solution for now: Develop an extension for Coded UI Tests to interact properly with my Delphi application like the sample on the Microsoft Web site. It's an extension for Excel that allow you to access each cell. But it sounds difficult and the application I have to test is really complex as well.
If your interface is plain VCL, you can access most components by using their underneath HWND handle. It will work for TEdit TMemo TComboBox TCheckBox and so one. But some graphics components won't be able to be accessible from GDI messages, e.g. TLabel or TGrid.
So I guess you'll have to use a Delphi plug-in in your application, to let accessible the VCL components level. Use the ComponentCount + Components[] properties of a TForm (via recursive call) to access your component to be tested. Then publish its properties to the Coded UI Tests extension, which is to be written.
I speak about a "plug-in" here, because I do not think we may easily have access to all classes to be monitored / modified. Some "plug-in" architecture may help an application to work as usual, or in "Coded UI Tests mode", during testing phase. Perhaps better that a separated compilation for the purpose of tests: you should better test the final compiled executable. If you want only unit testing, you may recompile, stub and mock your application to only test a given form. But you'll need to code the form to be easily unit-tested (using dependency injection or such), which is far from easy in the default Delphi world (as with other RAD approaches).
Could be interesting to initiate an Open Source project (included with DUnit?) to develop such a platform. Or use an existing UI test framework for Delphi as base. A lot of companies we work in are mixing .Net and Delphi technologies, and would benefit for such a tool.

Creating resource only DLL with translated form resources

I have a large application which i want to migrate to Arabic. I have defined the strings that we show to the user under the resourcestring keyword.
I am using the External Translation manager provided with Delphi 6. However, I am not very much comfortable using the tool. I want to create a resource only dll with all the translated strings like how the Delphi ETM does, and then switch between languages at run-time at the click of a button.
I was able to link the resourcestrings to the Dll, but how about form's captions and hints and the component properties? I am loading the Dll at runtime depending on the language, but the form properties are not reflecting as they were not available in the Dll.
Any pointers in the right direction ???
Thanks
Rahul W
What the Delphi localization tool and runtime support do is to redirect resource loading - including forms - from the executable to the DLL. Forms, their components and controls (non default properties) are stored as resources into the executable (as long as you don't create them fully at runtime - but then you would have to set their properties one by one).
Thereby if you want to work alike the standard translation tool you have to work the same way. What the DLL Resource Wizard does is to extract all project .dfm (and those you add manually) and resourcestrings to a copy that can be localized. When an app is started the form loading code checks from where the .dfm should be loaded. You should override this code to load your resources.
Be aware that changes language at runtime may need a different approach, because loading a whole form from resources may "reset" it to the creation state. On the other hand, compared to approaches like gettext, it allows to localize far more than the form text, including images, colors, and to adapt control sizes to the new string easily. IMHO gettext is good for simple needs, but when localization becomes a complex task and you may need to localize for a very different "culture", more powerful tools are needed.

Delphi and i18n

Does Delphi support internationalization in any way?
I've seen that I can add different languages for a project, but that seems to create multiple instances of the dfm files. Am I right that the language therefore cannot be changed at runtime?
How do you handle internationalization (if you do)? Are there any best-practices?
Delphi ships with some tools to do this.
Here is a tutorial for Delphi 2005, and a video of Nick Hodges showing off the more recent version for Delphi 2009.
I have once experimented with runtime change of languages. It worked great, but I needed to write lots of code myself (and circumvent the dfm files). The problem is, it is a lot of hassle and you almost never need this.
The better option (in my opinion) is to create a base language version and use a translator tool for the other language. We use a third party translator that works on the executable and dll. It replaces the strings in the resources and creates new binary files.
One big plus of this tool is that it shows the translated dialogs (standard windows components only) so you have direct feedback if labels are too large.
Amd then there is dxgettext:
http://dxgettext.po.dk/
traditionally one of the tools used to change language runtime without restart.
Note that with Delphi/unicode the amount of code changes with this packages has decreased (one no longer has to change any resourcestring use)
I have been using the SiComponents before. They allow you to change all text independently from the executable. You can embed the diferent languages in your executable (not recomended) or load them from files. There is also an editor to support translation. They are very good, stable, and fast.
In Delphi 2006 under Tools, Options, Translation Tools Options you can configure the translation tools, especially theTranslation Repository. It can be found under View, Translation Manager. All translated strings will be ressource strings and can be stored in language specific dll. Searching the help for Translation Manager will point you in the right direction.
I second the recommendation for using the TsiLang components(www.sicomponents.com). I've just used them to upgrade from English-only to English + Chinese, selectable at run-time, and found the process to be quite a lot easier than I was expecting. The end results are not perfect, but certainly good enough to work with.

What technique would be the least effort to internationalise (at least multi-language) existing Delphi Applications?

I have developed about 300 Applications which I would like to provide with multi-language capabilities independent from the Operating System. I have written a just-in-time translator, but that is too slow in applications with many components. What would you suggest I do?
We are using TsiLang and are very happy with it.
One of the best points is that you can pretranslate the project with a dictionary (which you filled from existing translations).
I've heard that the TsiLang components are nice, but your looking at an inplace solution...
I've used GNU gettext for Delphi which does exactly the thing you want, it loads the translations from a text file and replaces the text in your components. It even has a pas/dfm scanner to automatically generate the English translation file.
It's also possible to automatically change your pascal source code to inject the gettext procedure inplace of your static strings. If I'm not mistaken it just adds a underscore as function to it, as below.
ShowMessage('Hello'); // before
ShowMessage(_('Hello')); // after
I must say it has been 2 years since I last used this method.
One thing will remain problematic, the Delphi components are not unicode enabled (D2009 fixes this), so when you don't change the components you'll still have limited support for other languages.
A good free solution would be GNU gettext for Delphi. It has some capabilities not present in TsiLang - for example, you can put the knowledge on how to count things (different endings for one, two, four, hundred and two, many ...) into the translation file so that you don't have to teach each program to know this stuff.
License for the Delphi part is very permissive but I'm not sure how much the included GNU stuff will affect your application.
Get Multilizer. It is made in Delphi and it can handle Delphi programs like no other with special support for VCL. You can even redo your screens easy for every language. With Multilizer you can use different techniques to translate and run your program.
Delphi 2009 has added an Integrated Translation Environment/External Translation Manager
ITE and ETM are now available for both Delphi and C++Builder.
In Codegear's article: What's New in Delphi and C++Builder 2009, they state:
The Integrated Translation Environment
(ITE) is a part of the IDE that
simplifies localizing your projects.
ITE can create a new localized project
from an existing project. ITE does not
automatically translate text, but
provides a dialog listing all text
that needs to be localized and fields
in which to enter the corresponding
translated text. Once you have entered
the translated text and built the
localized project, you can set another
language active and display a form in
the localized text; you don't have to
switch locales and reboot your system.
This allows you to perform
localization without requiring a
localized system.
The External Translation Manager (ETM)
is a standalone application that works
with DFM files and text strings in the
source code. Although ETM does not
allow you to create a new localized
project, it does provide a dialog
listing localized text and the
translated text, similarly to ITE.
This is what I plan to try first once I am at the point that I want to Internationalize my product.
However, to me the easy part is to translate the program. The hard part is to translate the help file.
I would say GNU gettext for Delphi in combination with TMS Unicode Component Pack (previously free under TntWare) to get unicode support in the components.
To work with, or have translators work with, the gettext files I recommend looking at the free cross-platform Poedit that may edit the .po files.
Just to mention cxLocalizer if you own DexExpress components.

Resources