Allow your VB program to support the switching of multi-national languages

Source: Internet
Author: User
Tags exit ini
Program has done a small VB project, the customer needs the software at the same time to support both Chinese and English, for this write an automatic language switching module to use, do not dare to enjoy, for everyone to refer to, if you have any improvement can also write to me. Mailto:tonyki@citiz.net

Module Mdllanmgr.bas:
Attribute vb_name = "Modulelanmgr"
Option Explicit

Private Declare Function getprivateprofilestring& Lib "kernel32" Alias "Getprivateprofilestringa" (ByVal Lpapplicationname As String, ByVal Lpkeyname as String, ByVal Lpdefault as String, ByVal lpreturnedstring as String, ByVal Nsize as Long, ByVal lpFileName as String)

Private Languagefilename as String

' Select language


Public Sub selectlanguage (lanname as String)
On Error Resume Next

Select Case Lanname ' reads different language files according to the name of the language selected by the user
Case "中文版":
Languagefilename = Addsplash (App.Path) & "English.lan"
Case "Chinese (simplify)":
Languagefilename = Addsplash (App.Path) & "Chineses.lan"
Case "Chinese (Traditional)":
Languagefilename = Addsplash (App.Path) & "Chineset.lan"
Case Else:
Languagefilename = ""
End Select
End Sub

' Translate text


Public Function Translatestr (ssection As String, SKey as String, Sdefval as String) as String
Dim svalue as String * 128
Dim N as Long

On Error Resume Next

Translatestr = Sdefval
If not FileExists (languagefilename) Then
Exit Function
End If
N = GetPrivateProfileString (ssection, SKey, Sdefval, svalue, 127, Languagefilename)
If N > 0 Then
Translatestr = Left (svalue, N)
End If
End Function

' Automatically converts some of the basic controls on the form (also extensible, currently supports Commandbutton,label,optionbutton,checkbutton)


Public Sub Translateform (Frm as Form)
Dim I as Long

On Error Resume Next

Frm.caption = Translatestr (Frm.name, "Caption", frm.caption)
For I = 0 to Frm.controls.count-1
If (TypeOf frm.controls (i) is CommandButton) Or (TypeOf frm.controls (i) is Label) _
or (TypeOf frm.controls (i) is OptionButton) or (TypeOf frm.controls (i) is CheckBox) Then
Frm.controls (I). Caption = Translatestr (Frm.name, Frm.controls (I). Name, Frm.controls (I). Caption)
End If
Next I
End Sub

The call is simple and is written in the form as the main window of the program:
Private Sub Form_Load ()
On Error GoTo Errfun
Language = "中文版"
If fileexists (Addsplash (App.Path) & "Sys.ini") Then ' reads the last set of language file names from the program's configuration file
Open Addsplash (App.Path) & "Sys.ini" for Input as #1
Input #1, Language
Close #1
End If
Selectlanguage (Language) ' Sets global variables, specifying the language used
Translateform Me ' auto-convert interface text

Errfun:
Resume Next
End Sub

In other form, a similar method is used to invoke Translateform (Me) in formload to toggle the language text on the form Button,label.

Finally, the language files used are formatted like this:
[FormMain]
CAPTION=PCTV Player
Labelcmd1=start Play
Labelcmd2=edit Play List
Labelcmd3=options ...
Labelcmd4=help
Labelcmd5=exit
Labelcmd6=exit && shut down computer

[Formedit]
Caption=edit PlayList ...
Cmd1=add Program
Cmd2=edit Program
Cmd3=del Program
Cmd4=move up
Cmd5=move down
Cmd6=quit Edit
Listview.title1=id
Listview.title2=time
Listview.title3=type
Listview.title4=title

Because the VB language (here refers to VB6.0 and previous versions, vb.net belong to another concept) or very weak, so can only do this to this extent, if you want to add some other control or other properties of automatic text conversion support, then you have to expand the Translateform () Function. (unlike Delphi, you can use Rtti to handle everything)
In addition, it is more troublesome to switch the interface language if there are several form displays then you need to manually call the Translateform () function on each window, if you want to improve, then you can write a process to traverse all the windows and call the function, this is left to you to do it.

I hope this will be helpful to everyone.




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.