'mdLanguage - a language translation module
'Version 0.1b

Type MdLanguage
	key As String
	tex As String
End Type

Declare Sub stringSplitEx(text As String, sep As String, array() As String)
Declare Sub stringSplitExEx(text As String, separator As String, array() As String)
Declare Function loadLang(language() As MdLanguage, lang As String, path As String = "", prefix As String = "Language_", suffix As String = ".properties") As Integer
Declare Function getTrans Cdecl (language() As MdLanguage, key As String, ...) As String

#Macro loadLanguage(l)
	#Ifndef l
		ReDim Shared As MdLanguage l()
	#EndIf
	loadLang(l(), #l)
#EndMacro
#Macro loadCustomLanguage(l, args...)
	#Ifndef l
		ReDim Shared As MdLanguage l()
	#EndIf
	loadLang(l(), args)
#EndMacro
#Define getTranslation(l, args...) getTrans(l(), args, Cast(Any Ptr, 0))

Function loadLang(language() As MdLanguage, lang As String, path As String = "", prefix As String = "Language_", suffix As String = ".properties") As Integer
	Dim As String tempString, tempPath
	Dim As Integer lineCount
	
	If path <> "" Then
		Select Case Right(path, 1)
			Case "/", "\"
				tempPath = path
			Case Else
				tempPath = path & "/"
		End Select
	EndIf
	
	Dim As Integer FF
	
	FF = FreeFile
	If Open(tempPath & prefix & lang & suffix For Input As #FF) <> 0 Then
		Print "Error: Can't load language file " & tempPath & prefix & lang & suffix & " for " & lang
		Return 1
	Else
		While Not Eof(FF)
			Line Input #FF, tempString
			lineCount += 1
		Wend
		Close #FF
	EndIf
	
	ReDim As MdLanguage language(lineCount)
	lineCount = 0
	
	FF = FreeFile
	If Open(tempPath & prefix & lang & suffix For Input As #FF) <> 0 Then
		Print "Error: Can't load language file " & tempPath & prefix & lang & suffix & " for " & lang
		Return 1
	Else
		While Not Eof(FF)
			Dim As String tempArray(0 To 1)
			Line Input #FF, tempString
			stringSplitEx(tempString, "=", tempArray())
			If tempArray(1) = "" Then
				Continue While
			EndIf
			language(lineCount).key = tempArray(0)
			language(lineCount).tex = tempArray(1)
			lineCount += 1
		Wend
		Close #FF
	EndIf
	
	Return 0
End Function
Function getTrans Cdecl (language() As MdLanguage, key As String, ...) As String
	Dim As String ret
	Dim As Integer i, j, k, paramCount
	ReDim As String parameter(paramCount + 9)
	Dim As ZString Ptr temp
	
	Dim As Any Ptr param = Va_First()
	
	While param
		temp = Va_Arg(param, ZString Ptr)
		If temp = 0 Then
			Exit While
		EndIf
		
		parameter(paramCount) = *temp
		param = Va_Next(param, ZString Ptr)
		paramCount += 1
		If paramCount Mod 8 = 0 Then
			ReDim Preserve parameter(paramCount + 9)
		EndIf
	Wend
	
	For i = 0 To UBound(language)
		If language(i).key = key Then
			If paramCount > 0 Then
				ret = language(i).tex
				
				For j = 0 To paramCount - 1
					Dim As String tempArray()
					stringSplitExEx(ret, "{" & j & "}", tempArray())
					ret = ""
					
					For k = 0 To UBound(tempArray) - 1
						ret &= tempArray(k) & parameter(j)
					Next
					ret &= tempArray(UBound(tempArray))
				Next
			Else
				ret = language(i).tex
			EndIf
		EndIf
	Next
	
	If ret = "" Then
		ret = "<" & key & ">"
	EndIf
	
	Return ret
End Function

Sub stringSplitEx(text As String, sep As String, array() As String)
	Dim As Integer i, arrayIndex, lb = LBound( array )
	
	If @array( lb ) = 0 Then
		arrayIndex = 0
		ReDim array( 1 )
		For i = 0 To Len( text ) - 1
			If text[i] = sep[0] Then
				arrayIndex += 1
				ReDim Preserve array( arrayIndex )
			Else
				array( arrayIndex ) &= Chr( text[i] )
			EndIf
		Next
	Else
		Dim As Integer ub = UBound( array )
		arrayIndex = lb
		For i = 0 To Len( text ) - 1
			If text[i] = sep[0] Then
				If arrayIndex < ub Then
					arrayIndex += 1
				Else
					array( arrayIndex ) &= Chr( sep[0] )
				EndIf
			Else
				array( arrayIndex ) &= Chr( text[i] )
			EndIf
		Next
	EndIf
End Sub
Sub stringSplitExEx(text As String, separator As String, array() As String)
	Dim As Integer i, arrayIndex, lb = LBound(array)
	
	If @array(lb) = 0 Then
		arrayIndex = 0
		ReDim array(1)
		For i = 0 To Len(text) - 1
			If separator[1] = 0 Then
				If text[i] = separator[0] Then
					arrayIndex += 1
					ReDim Preserve array(arrayIndex)
				Else
					array(arrayIndex) &= Chr(text[i])
				EndIf
			Else
				If Mid(text, i + 1, Len(separator)) = separator Then
					arrayIndex += 1
					i += Len(separator) - 1
					ReDim Preserve array(arrayIndex)
				Else
					array(arrayIndex) &= Chr(text[i])
				EndIf
			EndIf
		Next
	Else
		Dim As Integer ub = UBound(array)
		arrayIndex = lb
		For i = 0 To Len(text) - 1
			If separator[1] = 0 Then
				If text[i] = separator[0] Then
					If arrayIndex < ub Then
						arrayIndex += 1
					Else
						array(arrayIndex) &= Chr(separator[0])
					EndIf
				Else
					array(arrayIndex) &= Chr(text[i])
				EndIf
			Else
				If Mid(text, i + 1, Len(separator)) = separator Then
					If arrayIndex < ub Then
						arrayIndex += 1
						i += Len(separator) - 1
					Else
						array(arrayIndex) &= Chr(text[i])
					EndIf
				Else
					array(arrayIndex) &= Chr(text[i])
				EndIf
			EndIf
		Next
	EndIf
End Sub
