Results 1 to 9 of 9

Thread: [RESOLVED] Multi-language applications: change language?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Location
    Portugal
    Posts
    24

    Resolved [RESOLVED] Multi-language applications: change language?

    I now know how to use resource files, and thus make multi-language applications. The thing is, no matter how hard I search, I just can't find any place that explains where to change the language. Let's say, the only languages available are English and Portuguese, and a Spanish user feels more comfortable with Portuguese, although, the application will use the English language without asking opinions. Now, I'm not asking how to change the default, but instead, what is the way to change the app's current language. I've seen some posts stating that you can't, but come on, people! It's got to be possible! Something so trivial, that it shouldn't even need a question.
    If it truly is impossible somehow, can someone tell me the most efficient (preferably fastest and shortest) way to change the system's language, so that the program uses the chosen language?

    Also, another semi-related question: Is it possible to make the caption on controls (and similar text properties) load the resource ("LoadResString (X)") without entering the code? In other words, having "LoadResString (X)" on the caption property and it'll load string X?

    Thanks.

  2. #2
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Multi-language applications: change language?

    Every string in the resource has a number. For example, the English strings have 1000, 1001, 1002, 1003, etc as number and the Portuguese strings have 2000, 2001, 2002, 2003, etc as number.

    English
    1000 = Hello
    1001 = Yes
    1002 = No
    1003 = Today

    Portuguese (not sure if the translation is correct, but this is only an example)
    2000 = Olá
    2001 = Sim
    2002 = Não
    2003 = Hoje

    vb Code:
    1. Public Sub SetLanguage(lang As Integer)
    2.     Select Case lang
    3.         Case 0: i = 1000 ' English
    4.         Case 1: i = 2000 ' Portuguese
    5.     End Select
    6.    
    7.     With frmMain
    8.         .mnuHello.Caption = LoadResString(i + 0)
    9.         .mnuYes.Caption = LoadResString(i + 1)
    10.         .mnuNo.Caption = LoadResString(i + 2)
    11.         .mnuToday.Caption = LoadResString(i + 3)
    12.     End With
    13. End Sub
    Last edited by Chris001; Jun 2nd, 2009 at 06:23 PM. Reason: typo

  3. #3
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Multi-language applications: change language?

    Of course it's possible

    There are many ways but nobody will write a complete solution for you.

    Our applications (from our company) use a Class that "knows" how to translate all the Controls (Label, TextBox, ListBox, ListView, MSFlexGrid,...). All you need is one call on each Form's "Form_Load()" Event. In addition, "MsgBox()", "InputBox()" and similar are replaced with our own versions that already use this translation functionality from the getgo. There's even a Function that simply translates and returns the String you pass to it. All translations are stored in DB and are made through a special interface that of course, only administrators can access.

    I can assure you this sounds more complex than it actually is. I was assigned to build this into one of our latests applications with about 200k lines of code. It took me only a day to do it. Of course, implementing this complete functionality would make it a little longer.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Multi-language applications: change language?

    An alternative which would take less code is to use the Tag property of each control to store the numbers, so Chris001's example would become this (no matter how many controls there are):
    Code:
          Public Sub SetLanguage(lang As Integer)
              Select Case lang
                  Case 0: i = 1000 ' English
                  Case 1: i = 2000 ' Portuguese
              End Select
             
          Dim objControl as Control
              For Each objControl In frmMain.Controls
                  objControl.Caption = LoadResString(objControl.Tag + 0)
              Next objControl
          End Sub
    ..you will of course still need to enter the numbers for each control, but only in the control properties.

    The code isn't perfect, as it only deals with controls that have a .Caption property - so you would need to add code in the loop to check the control type (perhaps using the TypeName function). It also doesn't deal with more complex controls, such as MsFlexGrid and the others that gavio mentioned.

  5. #5
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: Multi-language applications: change language?

    are you storing all strings in 1 resource file ?

    The way I do it is, the default English is the Build in Resource File and for each language I create a separate .DLL which holds the strings of that language...

    So if
    100 = Hello
    101 = Yes
    102 = No

    Then for the other langage .dll the same applies...

    100 = Olá
    101 = Sim
    102 = Não

    But I set a reference to an Object which links to the .DLL
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Location
    Portugal
    Posts
    24

    Re: Multi-language applications: change language?

    Chris, I was reading your solution and thinking "of course, it's so simple!". Although, I think that making it that way would be wasting the resource's functions... By that I mean that it supports several tables, but it'd be a waste to not use the others. Meh, it looks like that is the way it's going to be.

    Gavio, you did say something a bit confusing, but I understood. Yet, I'm not going to use something so complicated, but if I were, you helped!

    si_the_geek, that's a good way to do it, but it's not needed thanks.

    some1uk03, you say you use different dll files. Good idea, but how do you tell VB which DLL to use? (Also, I thought that they were RES files, not DLLS, unless it's like some "masked" DLL.)


    And I may have not explained the second question properly. I was asking if there is a way to add such string loading code to the property, on design time. On form view mode. You click the control, go to the property list on the right side, choose caption, and place the "code" there.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Multi-language applications: change language?

    You can't put code into any properties, the closest you can do is put the number in, and use a loop similar to what I showed.

  8. #8
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: Multi-language applications: change language?

    some1uk03, you say you use different dll files. Good idea, but how do you tell VB which DLL to use? (Also, I thought that they were RES files, not DLLS, unless it's like some "masked" DLL.)
    Yes they are in fact RES files, but wrapped inside a .DLL (masked)

    Heres a simple example:

    Code:
    Private Sub Form_Load()
    
    'Load the Name of the language from the Registry
    Language = RGGetKeyValue(HKEY_CURRENT_USER, "Software\CompanyName\" & AppExeName, "Language")
    
    If Dir(App.Path & "\Languages\" & Language & ".dll") <> "" Then
    'yourLanguage is the name of the DLL
    '.clsLanguage is the class inside the .dll which returns the strings to your
    Set myLanguage = CreateObject("yourLanguage_" & Language & ".clsLanguage")
    Else
    SkipVersion:
    Language = "English_UK"
    End If
    
    Me.Caption = LoadString(101)
    Label1.Caption = LoadString(102)
    'etc.... etc....
    End Sub
    Code:
    Public Function LoadString(ByVal whichResString As Long) As String
    
    If Language = "English_UK" Then
    GetEnglishText:
       LoadString = LoadResString(whichResString)
    Else
    On Error GoTo GetEnglishText
       LoadString = myLanguage.GetText(whichResString)
    End If
    End Function
    As for your 2nd question. No.

    You still need a code to reference to the RES file.
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Location
    Portugal
    Posts
    24

    Re: Multi-language applications: change language?

    Ok, I got it working the best way I could. I used Chris' strategy. Thanks! Maybe when I make some other multi-language apps, I'll consider the other methods.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width