I need to write VB application with option where user can select application language. When he select language all user GUI (labels, captions ,datagrid....)must be on selected language.
How to do this?
You will need to store all user localizable text and graphics in an external file or database. When a form is about to be displayed, you must replace each label, image, graphic, etc. with the appropriate one from the database.
Note that VB6 doesn't handle foreign characters being entered into textboxes, etc. all that well. While strings in VB6 are 2-byte Unicode (UTF-16), the forms engine is primarily still ANSI, so any Asian or other double-byte language will have problems.
If you really need Chinese, Japanese, and/or Korean, your best bet would be to switch to VB.NET, which handles all this stuff much more cleanly.
Chip H.
____________________________________________________________________ If you want to get the best response to a question, please read FAQ222-2244 first
You can use the controls from the Forms 2.0 Library and VB6 will display Japanese and other asian fonts just fine ( assuming you have the correct font drivers ).
And do like strongm suggests and research how to use resource files.
Yes, I "third" strongm's recommendation: look into resource (.res) files. They are there to handle exactly this issue, as well as the way to include graphic files with your application.
I have used resource files for exactly this purpose, instead of a database table. I have a system running in French, German, Russian, English and Spanish languages.
Each language has a resource file associated with it and then on the form load event, I teest for the language the user is set-up for and load up the appropriate resource file and then get the required ID's out of the resource file for each of the controls / lables / message boxes etc.
By default I use English language text, of course, if no resource file for the appropriate language is avalable.
Let me know if you need some sample code to get the appropriate text out of the resource file (if I remember corrrectly it is just one simple API call) and I will post it up.
' This module contains all the code necessary for the application
' to use a satellite DLL for localization purposes.
' Please refer to the MSDN for more information regarding the APIs
' used in this example.
Public Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
' Object reference to the DLL that contains the resources
' to be loaded.
Private clsSatellite As Object
' localid will be 3 chaaracters that tie in with the resource file ID i.e. FRN
Public Function LoadLocalizedResources(IlocalID As String) As Boolean
' Find the LocalID.
lLocalID = Hex(GetUserDefaultLCID)
' Load the Satellite DLL that contains the local
' resource object to be used. If CreateObject
' fails, there is no local version of the
' resources.
Set clsSatellite = Nothing
On Error GoTo NoLocalResource
' Create a local object containing resources.
Set clsSatellite = CreateObject("resourcefile" & RTrim(LTrim(IlocalID)) & _
".clsResources")
' Return true, then read local resources.
LoadLocalizedResources = True
Exit Function
NoLocalResource:
' There is no local satellite DLL. As a result, false is returned.
LoadLocalizedResources = False
End Function
' GetString will access the object and return the string
' resources specific to the region. For this example, only
' basic error handling is implemented.
Public Function GetString(StringIndex As Long) As String
On Error Resume Next
' Make sure there is a resource object.
If Not (clsSatellite Is Nothing) Then
' Get the resource from the resource object.
GetString = clsSatellite.GetResourceString(StringIndex)
Else
' if there is no resource return null
GetString = ""
End If
End Function
You then need to create a seperate DLL project which you can add to you project group if you like) called resourcefileXXX where XXX is the ID for the language eg. resourcefileFRN
You insert a resource file into this DLL project along with a class module which should be called clsresources and needs to contain the following code :-
' dll project for language localisation
Public Function getresourcestring(resourceindex As Long) As String
getresourcestring = LoadResData(resourceindex, 6)
End Function
In you main system code you then just need to test for the presence of the foreign language satellite DLL and then call the function getstring with the appropriate ID for the text that you want from the resource file.
i.e. in the form load event:-
' test for the presence of a foreign language satellite dll
loadlocallanguage = False
If (LoadLocalizedResources(language_id)) Then
loadlocallanguage = True
End If
Then anywhere on the form you can test the variable loadlocalizedresources and if it is true you know that a satellite DLL is registrede on the system and so you can call the getsrting method, passing it the ID of the item that you wish to obtain from the resource file.
Bob gets more specific than my "It can be somewhat easier than that" comment, but is talking about exactly the same solution that I was thinking about.
However the reason I said 'can' and didn't provide the solution or a link to it is because of the requirement that we have an "application language", which differs significantly from the PC having a local id set.
That being said, we can still provide (if the OP wants) a single resource file solution to the problem (although it will require a couple of API calls (LoadResource and FindResourceEx)
Isn't the point of having separate resource files is the ability to load (say from internet) only needed language packs or load Russian resource later then it'll became available (and use English one before that)?
Well, it may apply only to "If you know this language, send us a translation" sort of programs. But I've seen such programs, and idea seems good for me.
It's a lot simpler if the machine's regional setting matches the language the user wants in the application. Perhaps the OP can explain why this isn't a solution?
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.