How to upgrade from an existing program to a program that supports multiple languages!

Source: Internet
Author: User
Tags translate function

A friend on csdn asked how to upgrade from a completed project to a program in multiple languages, I believe that many of my friends have not used tlmpack since the beginning of the project (and it is not necessary ), next, I will briefly introduce how to upgrade an existing project to a project that supports multiple languages.

1. Install the tlmpack Standard Edition in Delphi (or BCB). The instructions on how to install the Help file are described in detail. (Of course, the prerequisite for installing the Standard Edition is that you use the standard ansivcl control to develop the program. If you use the tntunicode control to develop the program, you must use the Unicode tlmpack ).

2. Open the project that you need to upgrade to a multi-language, and place a ttlmcontroller control on each form (and qreport, module, and so on, the function of this control is to automatically change the display text of all the controls on the form in which the control is located. Generally, you only need to place the control on the form without any settings.

3. The controls on form can now be automatically updated based on the language settings. What should I do if the text displayed in functions such as MessageBox, the answer is to use the translate () function of ttlmcontroller. This function can automatically obtain the text resource from the current language file and return the result as the return value. Suppose there is a function like this:
Code:

Procedure tform1.button1click (Sender: tobject );
Begin
MessageBox (handle, pchar ('Say hello to the user! '), 'Message', mb_iconinformation );
End;

Then you need to use the translate function to wrap the text, as shown in the following figure:
Code:

Procedure tform1.button1click (Sender: tobject );
Begin
MessageBox (handle, pchar (tlmcontroller1.translate ('sayhel', 'Say hello to the user! '), 'Message', mb_iconinformation );
End;

The preceding 'sayhello' is the identifier of the text. Only through this identifier can the correct resource be found in the language file, the following text indicates the content returned by the function by default when the language file does not contain the resource to be searched (or the language file does not exist.
In addition, the translate function also supports displaying the same parameters as the format () function, as shown in the following figure:
Code:

Procedure tform1.button1click (Sender: tobject );
Begin
MessageBox (handle, pchar (tlmcontroller1.translate ('componentcount', 'componentcount = % d', [componentcount]), 'message', mb_iconinformation );
End;

4. The above MessageBox () function appears in form, and the form already has the ttlmcontroller control, so you can use the ttlmcontroller's translate () function to convert text, so if your project has a function class that is directly inherited from tobject, it also needs to display message, or need to record log text, what should I do at this time, the simplest way is to let this function class call the ttlmcontroller's translate () function in a form. However, the disadvantage is that the program may need to make major changes to this function, in addition, the text in the language files will become messy. In this case, the best solution is to use the ttlmobject class, which is the same as the ttlmcontroller class and can also be used by the translate function, but the difference is that it does not detect all the controls of the form, they are not automatically converted.
The following is a small function class.
Code:

Interface

Uses
....;

Type
Tfunctionclass = Class (tobject)
Public
Procedure showmessage ();
End;

Impenmentation

Procedure tfunctionclass. showmessage ();
Begin
Showmessage (nil, 'Say hello to the user! ', 'Message', mb_iconinformation );
End;

End.

After the ttlmobject class is used, the class is transformed:
Code:

Interface

Uses
..., Tlmobject;

Type
Tfunctionclass = Class (tobject)
Private
Ftlmobject: ttlmobject;
Public
Constructor create ();
Destructor destroy (); override;
Procedure showmessage ();
End;

Impenmentation

Constructor tfunctionclass. Create ();
Begin
Inherited create ();
Ftlmobject: = ttlmobject. Create (Self );
End;

Destructor tfunctionclass. Destroy ();
Begin
Ftlmobject. Free;
Inherited destroy ();
End;

Procedure tfunctionclass. showmessage ();
Begin
MessageBox (handle, pchar (tlmcontroller1.translate ('componentcount', 'componentcount = % d', [componentcount]), 'message', mb_iconinformation );
End;

End.

In this way, whenever you use a function that requires text display in this function class, the actually used text is recorded in the language file.

5. Tlmpack loads language and text from external language files. Since it is boring to manually write a language file, you can use the wizard control to get started with a program, if you want to manually write a language file, skip this section.
The so-called wizard control refers to the ttlminigenerator and ttlmxmlgenerator controls, which will output all the text originally required to be read by ttlmcontroller and ttlmobject to the language file in reverse when the program is running, all you need to do is place a ttlminigenerator on the main form of the Program (Here we assume you want to output a language file in ini format), and then run the program, when the program is running, a Setting dialog box is displayed, asking about the path of the generated language file and the character set format. Generally, you can use the default setting to directly OK. The prompt dialog box is as follows:

In the future, whenever code in the program that requires text conversion is called, ttlminigenerato will display a prompt dialog box prompting you to actually save the actual text content to the file, generally, you do not need to modify any text in this step, so you can directly output it. All you need to do is ensure that every code in the program that requires text display is passed over, otherwise the text will not be output, as shown in the dialog box:

6. Now, if you have successfully created a language file in step 1, or you have manually written such a file, then you need to use the language file as a template, copy it to multiple files with Different Extensions (for strict requirements on the extensions of each language, see the "Character Set" section in the Help file ), then, modify the text in the language file for each language and remember to modify the information in the general section of the language file. The content of each language is different, you can use the "Character Set" section of the Help file to learn how to write it.

7. In the last step, if ttlminigenerator is placed on the main form, delete it. Place a ttlminifilter on the main form (if you are using a language file in XML format, you need to place ttlmxmlfilter) and set its file path and other attributes, so that it can find the language file correctly, and write code control ttlminifilter. activelanguage attribute to achieve the effect of language switching during running. Well, run the program and try to modify the language. Does the program immediately switch the language, whether it is the interface or the displayed message, it's like a program is developed in this language... Is it great ?!

Tlmpack can be downloaded from here:
Http://www.tonixsoft.com/index.php? MM enu_id = 0003

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.