Leftos
May 3rd, 2006, 02:37 PM
I'm an amateur Visual Basic coder, so this code goes to newbies like me, who don't know how to really handle text files, and want to have their string tables in text files rather than a RES file. It was made originally for my project as a more convenient method for other translators that didn't have VB.
I'm sure there are more efficient localization and translation methods out there, but I thought I'd throw in a bit of code.
So, here it is:
Public Language As String
Private Sub Form_Load()
'This can be replaced with whatever function you have that sets
' the language. Personally I prefer setting the language through
'an Options form into the registry with SaveSetting, and then load it
'back with GetSetting
Language = "English"
End Sub
Public Function GetString(ID As Integer) As String
Dim intID As Integer
Open App.Path & "\" & Language & ".txt" For Input As #1
Do Until intID = ID
Input #1, intID, GetString
Loop
Close #1
Exit Sub
End Function
What it does is really obvious. It loads the correct language file, and then searches for the line with the correct ID, loading it each time along with the corresponding string. If after loading the ID read was the ID we wanted, it returns the string. It works just like LoadResString(ID), only now you use something like...
MsgBox GetString(1952)
cmdButton.Caption = GetString(252)
The language file shall have a very simple structure:
001 First String
002 Second String
003 Third String
852 This is string No. 852!
No particular sorting needed, no limit on the number of strings (well, depends on what you set intID as), and no need to add 0's before each ID, it's just done so that the whole file can be read easier by anyone.
I know it's nothing special, but as I said I'm a newbie and this goes out to newbies and to people with just the same problem that I had.
I'm sure there are more efficient localization and translation methods out there, but I thought I'd throw in a bit of code.
So, here it is:
Public Language As String
Private Sub Form_Load()
'This can be replaced with whatever function you have that sets
' the language. Personally I prefer setting the language through
'an Options form into the registry with SaveSetting, and then load it
'back with GetSetting
Language = "English"
End Sub
Public Function GetString(ID As Integer) As String
Dim intID As Integer
Open App.Path & "\" & Language & ".txt" For Input As #1
Do Until intID = ID
Input #1, intID, GetString
Loop
Close #1
Exit Sub
End Function
What it does is really obvious. It loads the correct language file, and then searches for the line with the correct ID, loading it each time along with the corresponding string. If after loading the ID read was the ID we wanted, it returns the string. It works just like LoadResString(ID), only now you use something like...
MsgBox GetString(1952)
cmdButton.Caption = GetString(252)
The language file shall have a very simple structure:
001 First String
002 Second String
003 Third String
852 This is string No. 852!
No particular sorting needed, no limit on the number of strings (well, depends on what you set intID as), and no need to add 0's before each ID, it's just done so that the whole file can be read easier by anyone.
I know it's nothing special, but as I said I'm a newbie and this goes out to newbies and to people with just the same problem that I had.