Vb 6 0 Multi Language Ui With Unicode

A application requires switch language between English, Chinese, Japanese, Germen, Franch and Spanish, in a word, it needs MUI.

Official Unicode supporting of VB6

VB6 supports unicode internal but the controls convert unicode to ansi.
Reference:Display Unicode Strings in Visual Basic 6.0.

Methods

  • Replace standard controls with unicode supported control

*Forms 2.0 (fm20.dll);

  • Convert strings to local supported ansi with charset and font

Our method

;其实现思路如下:

  • Store strings in unicode in file (not utf8 or utf7)
  • Load data from file and covert strings with ADODB.Stream(msado25.tlb)
  • Set the Font.Charset and Font.Name dynamically
If g_Str.NumJapanese > 0 Then
         charset = "Shift_JIS"
         Text1.Font.Name = "MS UI Gothic"
         Text1.Font.charset = 128

     ElseIf g_Str.NumKorean > 0 Then
         charset = "ks_c_5601-1987"
         Text1.Font.Name = "GulimChe"
         Text1.Font.charset = 129

     ElseIf g_Str.NumCentralEuro > 0 Then
         charset = "windows-1250"
         Text1.Font.Name = "Arial"
         Text1.Font.charset = 238

     ElseIf g_Str.NumArabic > 0 Then
         charset = "windows-1256"
         Text1.Font.Name = "Traditional Arabic"
         Text1.Font.charset = 178

     ElseIf g_Str.NumHebrew > 0 Then
         charset = "windows-1255"
         Text1.Font.Name = "David"
         Text1.Font.charset = 177

     ElseIf g_Str.NumCyrillic > 0 Then
         charset = "windows-1251"
         Text1.Font.Name = "Arial"
         Text1.Font.charset = 204

     ElseIf g_Str.NumGreek > 0 Then
         charset = "windows-1253"
         Text1.Font.Name = "Arial"
         Text1.Font.charset = 161

     ElseIf g_Str.NumThai > 0 Then
         charset = "windows-874"
         Text1.Font.Name = "Angsana New"
         Text1.Font.charset = 222

     ElseIf g_Str.NumChinese > 0 Then
         charset = "gb2312"
         Text1.Font.Name = "SimSun"
         Text1.Font.charset = 134
         ' An alternative is to use Big5:
         ' Text1.Font.Name = "MingLiu"
         'charset = "big5"
         'fontCh = 136

     Else
         charset = "windows-1252"
         Text1.Font.charset = 0
         Text1.Font.Name = "Arial"
     End If
  • ADODB.Stream converting
 ' get LocaleID
 Private Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long

 ' covert unicode string to charset byte arrary
 ' read string from file
 Public Function ConvertStringToBytes(ByRef strText As String, charset As String) As Byte()

     Dim objStream As ADODB.Stream
     Dim data() As Byte

     ' init stream
     Set objStream = New ADODB.Stream
     objStream.charset = charset
     objStream.Mode = adModeReadWrite
     objStream.Type = adTypeText
     objStream.Open

     ' write bytes into stream
     objStream.WriteText strText
     objStream.Flush

     ' rewind stream and read text
     objStream.Position = 0
     objStream.Type = adTypeBinary
 '    objStream.Read 3 ' skip first 3 bytes as this is the utf-8 marker
     data = objStream.Read()

     ' close up and return
     objStream.Close
     ConvertStringToUtf8Bytes = data

 End Function
 ' convert bytes arrary to localid ansi string
 ' covert bytes array from ConvertStringToBytes() to GetCharset() string
 Public Function ConvertBytesToString(ByRef data() As Byte, charset As String) As String

     Dim objStream As ADODB.Stream
     Dim strTmp As String

     ' init stream
     Set objStream = New ADODB.Stream
     objStream.charset = charset
     objStream.Mode = adModeReadWrite
     objStream.Type = adTypeBinary
     objStream.Open

     ' write bytes into stream
     objStream.Write data
     objStream.Flush

     ' rewind stream and read text
     objStream.Position = 0
     objStream.Type = adTypeText
     strTmp = objStream.ReadText

     ' close up and return
     objStream.Close
     ConvertUtf8BytesToString = strTmp

 End Function

 ' LocaleId
 Public Function GetCharset() As String
     Dim localeId As Long
     Dim charset As String

     ' LocaleId
     localeId = GetSystemDefaultLCID()

     Select Case localeId

         Case 1033
             charset = "windows-1252"
         Case 2052
             charset = "gb2312"
         Case 1041
             charset = "Shift_JIS"
         Case Else
             charset = "windows-1252"
     End Select

     GetCharset = charset
 End Function

 ' display text of file
 Private Sub DisplayText(filename As String)

     Dim textBytes As Variant
     Dim f As New FileSystemObject
     Dim fs As TextStream

     ' read unicode text to var
     Set fs = f.OpenTextFile(filename, ForReading, False, TristateTrue)

     Do While Not fs.AtEndOfStream
         textBytes = fs.ReadAll
     Loop

     fs.Close

     ' Convert to a Unicode string:
     Dim s As String
     s = textBytes
     Dim t As Long
     ' CkString is a third party component to determine the charset of string
     ' http://www.chilkatsoft.com/download/CkString.zip
     Dim g_Str As New CkString
      g_Str.Str = s

     'set charset to textbox
     Dim charset As String
     If g_Str.NumJapanese > 0 Then  
         charset = "Shift_JIS"
         Text1.Font.Name = "MS UI Gothic"
         Text1.Font.charset = 128

     ElseIf g_Str.NumKorean > 0 Then
         charset = "ks_c_5601-1987"
         Text1.Font.Name = "GulimChe"
         Text1.Font.charset = 129

     ElseIf g_Str.NumCentralEuro > 0 Then 
         charset = "windows-1250"
         Text1.Font.Name = "Arial"
         Text1.Font.charset = 238

     ElseIf g_Str.NumArabic > 0 Then
         charset = "windows-1256"
         Text1.Font.Name = "Traditional Arabic"
         Text1.Font.charset = 178
     ElseIf g_Str.NumGreek > 0 Then 
         charset = "windows-1253"
         Text1.Font.Name = "Arial"
         Text1.Font.charset = 161
     ElseIf g_Str.NumThai > 0 Then  
         charset = "windows-874"
         Text1.Font.Name = "Angsana New"
         Text1.Font.charset = 222
     ElseIf g_Str.NumChinese > 0 Then
         charset = "gb2312"
         Text1.Font.Name = "SimSun"
         Text1.Font.charset = 134
         ' Tranditional Big5:
         ' Text1.Font.Name = "MingLiu"
         'charset = "big5"
         'Text1.Font.charset = 136
     Else 'Default 
         charset = "windows-1252"
         Text1.Font.charset = 0
         Text1.Font.Name = "Arial"
     End If

     Dim bytes() As Byte
     Dim g_OSCharset As String

     ' get os default charset
     g_OSCharset = GetCharset()
     ' convert unicode bytes to charset
     bytes = ConvertStringToBytes(s, charset)
     ' convert bytes to ansi
     vbstr2 = ConvertBytesToString(bytes, g_OSCharset)
     ' set text
     Me.Text1.Text = vbstr2
 End Sub

Reference

1) charset and localeId(http://www.livio.net/main/charset.asp)

http://www.cnblogs.com/beiguren/archive/2010/03/08/1680713.html

VB 6.0 Multi-Language UI with Unicode cn