Friend Class translateCls
Private Const CONSTRING1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source="
Private Const CONSTRING2 As String = ";Mode=Share Deny None;Extended Properties="""";Jet OLEDB:System database="""";" & _
"Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=" & _
"5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLED" & _
"B:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create" & _
" System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Loc" & _
"ale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SF" & _
"P=False"
Protected Enum indexPosition
FirstWord = 0
MidString = 1
LastWord = 2
End Enum
Dim m_StringToTranslate As String = Nothing
Dim m_TranslatedString As String = Nothing
Friend Property StringToTranslate() As String
Get
Return m_StringToTranslate
End Get
Set(ByVal Value As String)
m_StringToTranslate = Value
End Set
End Property
Friend Property TranslatedString() As String
Get
Return m_TranslatedString
End Get
Set(ByVal Value As String)
m_TranslatedString = Value
End Set
End Property
Protected Friend Shared Function Translate(ByVal originalString As String) As String
Return Translator(originalString)
End Function
Protected Friend Sub Translate()
m_TranslatedString = Translator(m_StringToTranslate)
End Sub
Protected Shared Function Translator(Optional ByVal originalString As String = "") As String
Dim stringArray() As String = originalString.Split(" ")
Dim intLoop As Integer
Dim NewString As String = originalString
Dim indexPos As indexPosition = indexPosition.FirstWord
For intLoop = 0 To stringArray.Length - 1
If intLoop > 0 And intLoop < stringArray.Length - 1 Then
indexPos = indexPosition.MidString
ElseIf intLoop = stringArray.Length - 1 Then
indexPos = indexPosition.LastWord
End If
NewString = Replace(NewString, stringArray(intLoop), GetReplaceMentWord(stringArray(intLoop), indexPos))
Next intLoop
Return NewString
End Function
Protected Shared Function Replace(ByVal str As String, ByVal strSearchWord As String, ByVal strReplaceWord As String) As String
Dim regex As New System.Text.RegularExpressions.Regex("(?i:(" & strSearchWord & "\s)|(\s" & strSearchWord & ")|(" & strSearchWord & "\.)|(" & strSearchWord & ",))")
str = (regex.Replace(str, strReplaceWord))
Return str
End Function
Protected Shared Function GetReplaceMentWord(ByVal strWord As String, ByVal position As indexPosition) As String
Dim dbConn As New OleDb.OleDbConnection(CONSTRING1 & getExeLocation() & "dictionary.mdb" & CONSTRING2)
Dim strSql As String = "SELECT FrenchWord FROM dictionary WHERE EngWord = '" & strWord & "'"
Dim dbAdapter As New OleDb.OleDbDataAdapter(strSql, dbConn)
Dim ods As New DataSet()
dbConn.Open()
dbAdapter.Fill(ods)
dbConn.Close()
Dim strNewWord As String = ""
If (Not ods.Tables(0).Rows.Count = 0) Then
strNewWord = ods.Tables(0).Rows(0).Item("FrenchWord")
Else
strNewWord = "?"
End If
Select Case position
Case indexPosition.FirstWord
strNewWord = strNewWord & " "
Case indexPosition.MidString
strNewWord = " " & strNewWord
Case indexPosition.LastWord
strNewWord = " " & strNewWord
End Select
Return strNewWord
End Function
Protected Shared Function getExeLocation() As String
Dim strLoc As String = System.Reflection.Assembly.GetExecutingAssembly.Location
strLoc = strLoc.Remove((strLoc.LastIndexOf("\") + 1), (strLoc.Length - (strLoc.LastIndexOf("\") + 1)))
Return strLoc
End Function
End Class