'Multilanguage-Class by Mottzi
'Feel free to use this Class
'Version 2.1
'''
''' With this Class you can access the Muiltilanguage-Functions
''' This Class is by Mottzi for vb-paradise.de
'''
Public Class MultiLanguage
'Holds the Multilanguage File
Dim gLangFile As String = ""
'Holds the Language
Dim gLang As String = ""
'Is True when the Controls in the LangForm are written else False
Dim boolFormControlsWritten As Boolean = False
'Hold the selectLanguage Form
Dim form As New Form
'Holds the Combobox on the selectLangs Dialog
Dim combo As New ComboBox
'Holds the File of the selectLanguage Dialog
Dim tempLangFile As String = ""
'''
''' Returns the Saved Language (the Language which should be used in app) in the Multilangagesfile
'''
'''
''' Multilanguegesfilepath
'''
''' Defaultlanguage as String
Public Function getSavedLang(Optional ByVal sFile As String = "") As String
Dim content() As String
If sFile = "" Then
content = IO.File.ReadAllLines(gLangFile, System.Text.Encoding.UTF7)
Else
content = IO.File.ReadAllLines(sFile, System.Text.Encoding.UTF7)
End If
Return content(0)
End Function
'''
''' Returns a List of String with the Languages aviable in the sFile Multilanguagesfile
'''
'''
''' The Multilanguagefilepath
'''
'''
''' A List of String
'''
Public Function getLangs(ByVal sFile As String) As List(Of String)
Dim languages As New List(Of String)
If getLangCount(sFile) <= 0 Then
languages.Add("Error")
Return languages
End If
Dim sContent() As String = IO.File.ReadAllLines(sFile, System.Text.Encoding.UTF7)
For i As Integer = 0 To UBound(sContent)
If sContent(i).StartsWith(";[") Then
'Remove ";[" to get Langname
sContent(i) = sContent(i).Substring(2).ToString
'Remove Comments
Dim pos As Integer = sContent(i).IndexOf("//")
If Not pos = -1 Then
sContent(i) = sContent(i).Substring(0, pos)
End If
languages.Add(sContent(i))
End If
Next
Return languages
End Function
'''
''' Sets the Multilanguage Language
'''
'''
''' Language as String
'''
''' 1 When Succeed
'''
Public Function setLang(ByVal sLang As String)
gLang = sLang
Return 1
End Function
'''
''' Sets the Multilanguagefile
'''
'''
''' Multilanguage file as string
'''
'''
''' If its True, the Class will automaticly load the Saved/Default Language in the ML File (1. Line)
'''
''' 1 When Succeed
Public Function setFile(ByVal sFile As String, Optional ByVal bSetSavedLang As Boolean = False)
gLangFile = sFile
If bSetSavedLang Then
gLang = getSavedLang(sFile)
End If
Return 1
End Function
'''
''' Returns the Data of a Expression in the File. "%var" in the Data will be replaced by the sVariables
'''
'''
''' The Expression its Data will be returned. setLang() and setFile required.
'''
'''
''' Optional variable wich are going to be replaced with %var. Multiple variables must be seperated by ";"
'''
''' Data of Expression. On Failure it returns "Error"
Public Function getML(ByVal sExpression As String, ByVal ParamArray sVariables() As String) As String
Try
If gLang = "" Then Return "Error 1"
If gLangFile = "" Then Return "Error2"
'Holds the Content of the ML File
Dim szFileContent() As String = IO.File.ReadAllLines(gLangFile)
'Integer where the Languagepart (gLang) starts in Content
Dim iStartPosition As Integer = -1
'Fill the Startposition and ignore the 0. Index (Default lang)
For i As Integer = 1 To UBound(szFileContent)
If szFileContent(i).StartsWith(";[" & gLang) Then
iStartPosition = i
End If
Next
'If Language not found: Error
If iStartPosition = -1 Then
Return "Error3"
End If
'Search the Ausdruck and return
For i As Integer = iStartPosition + 1 To UBound(szFileContent)
If szFileContent(i).EndsWith("];") Then
Return "Error4"
Else
'Seperare Expression from Data
'Ignore Line if its a comment
If Not szFileContent(i).StartsWith("//") Then
Dim sLine() As String = Split(szFileContent(i), "=")
'Mögliche Leerzeichen entfernen
If Trim(sLine(0)) = Trim(sExpression) Then
'Leerzeichen am Anfang und am Ende entfernen
If UBound(sVariables) >= 0 Then
sLine(1) = replaceParameters(sVariables, sLine(1))
End If
'Remove Comments
Dim pos As Integer = sLine(1).IndexOf("//")
If Not pos = -1 Then
sLine(1) = sLine(1).Substring(0, pos)
End If
'Make Newlines
sLine(1) = sLine(1).Replace("%n", vbNewLine)
Return Trim(sLine(1))
End If
End If
End If
Next
Return "Error5"
Catch ex As Exception
Return "Error6"
End Try
End Function
'''
''' Returns the Count of the Languges in the ML File
'''
'''
''' If this Parameter is set, the Function will return the Count of this File. Else it will return the File which in setFile is set.
'''
'''
''' Integer count. Returns -1 when fails.
'''
Public Function getLangCount(Optional ByVal sFile As String = "") As Integer
'Holds the Content of the ML File
Dim szFileContent As String = ""
If sFile = "" Then
szFileContent = IO.File.ReadAllText(gLangFile)
Else
szFileContent = IO.File.ReadAllText(sFile)
End If
If szFileContent.ToString = "" Then
Return -1
End If
Dim count As Integer = Nothing
Dim pos As Integer = 0
For i As Integer = 0 To 1000
'Pos holds the Position of the last Language or -1 if there is no more
pos = szFileContent.IndexOf(";[", pos + 1)
If Not pos = -1 Then
'If Language found, + 1
count = count + 1
Else
'If theres no more Languages exit the for
Exit For
End If
Next
'Return the Variable
Return count + 1
End Function
'''
''' Returns the Value of the count how many times %var is in a String
'''
'''
''' The String which will be checked
'''
''' Integer count
Public Function getVarCount(ByVal data As String) As Integer
Dim counter As Integer = Nothing
Dim pos As Integer = Nothing
For i = 0 To Len(data)
pos = data.IndexOf("%var", pos + 1)
If Not pos = -1 Then
counter = counter + 1
Else
Exit For
End If
Next
Return counter
End Function
'''
''' The Class uses this Function in getML() to Replace the %var with Data
'''
'''
''' Integer
'''
Private Function replaceParameters(ByVal parameters As Array, ByVal data As String) As String
Dim pars() As String = parameters
Dim ml As New MultiLanguage
data = data.Insert(0, " ")
If UBound(pars) + 1 = ml.getVarCount(data) Then
Dim pos As Integer
For i As Integer = 0 To ml.getVarCount(data) - 1
pos = data.IndexOf("%var")
data = data.Remove(pos, 4)
data = data.Insert(pos, pars(i))
Next
'Leerzeichen entfernen
data = data.Remove(0, 1)
Return data
Else
Return "Error"
End If
End Function
'''
''' Opens the selectLanguage Dialog
'''
'''
''' Language File
'''
Public Sub formLangSelect(ByVal sFile As String)
If Not sFile = "" Then
If boolFormControlsWritten = False Then
'Make the Form with its controls
With form
.Text = "Select Your Language"
.FormBorderStyle = FormBorderStyle.FixedDialog
.Size = New Point(230, 115)
End With
Dim label As New Label
Dim button As New Button
With combo
.Location = New Point(70, 20)
.Width = .Width + 10
End With
With label
.Text = "Language"
.Location = New Point(8, 20)
End With
With button
.Text = "OK"
.Location = New Point(combo.Location.X + combo.Width - button.Width, 50)
End With
form.Controls.Add(combo)
form.Controls.Add(label)
form.Controls.Add(button)
'Dont write the Controls a second time
boolFormControlsWritten = True
'Add the Button Click Handler
AddHandler button.Click, AddressOf button_Click
End If
tempLangFile = sFile
' Clear combobox
combo.Items.Clear()
'Get the Languages
Dim languages As New List(Of String)
languages = getLangs(sFile)
'And fill the Combobox
combo.Text = "Select your Language"
For i As Integer = 0 To languages.Count - 1
combo.Items.Add(languages(i))
Next
form.ShowDialog()
End If
End Sub
'''
''' The Handler of the Button in the selectLanguage form here we Save the Language
'''
Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
If Not combo.SelectedIndex = -1 Then
'If language is Selected close the Form
form.Close()
If combo.Items.Item(combo.SelectedIndex) = getSavedLang(tempLangFile) Then
MessageBox.Show("You already have this Language saved!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Dim str As New IO.StreamReader(tempLangFile)
Dim lines As New List(Of String)()
While Not str.EndOfStream
lines.Add(str.ReadLine())
End While
str.Close()
lines(0) = combo.Items.Item(combo.SelectedIndex)
Dim strw As New IO.StreamWriter(tempLangFile)
For Each line As String In lines
strw.WriteLine(line)
Next
strw.Flush()
strw.Close()
If MessageBox.Show("Restart Application to change the Language to " & combo.Items.Item(combo.SelectedIndex) & "?", "Restart", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
Application.Restart()
End If
End If
Else
MessageBox.Show("You must select a Language", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Catch ex As Exception
MessageBox.Show("Unknown Error. Try it again, please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class