#include-once
#include <String.au3>

#cs ----------------------------------------------------------------------------

	AutoIt Version: 3.3.15.0 (Beta)
	Author:         mataran

	LanguageSupport UDF
	A simple XML-based language/data UDF. Consistency checker and string transfer
	programs available.

	NOTICE:
	I haven't had time to implement one of the "true" XML UDFs here,
	so if anyone adds it instead of _StringBetween(), let me know :)
	The LanguageTransmitter however uses a XML UDF, so the XML files created
	should are valid!

#ce ----------------------------------------------------------------------------
Global $_LangSupport_bError = False

;===============================================================================
; Function Name:	_LangInit($sFilepath)
; Description:		Initializes the LanguageSupport UDF
; Parameter(s):		$sFilepath Path to the XML File
; Syntax:			_LangInit($sFilepath)
; Author(s):		mataran
; Return Value(s)	NO Return, Global $_LangSupportArray that contains the matching strings, as well as $_LangSupportFile (FilePath)
;===============================================================================
Func _LangInit($sFilepath)
	Global $_LangSupportError = False
	If Not IsDeclared('sFilepath') Then
		MsgBox(16, 'LangError', 'Filepath for the LanguageFile is not set. See _LangInit($sFilepath)')
		Return SetError(1)
	EndIf
	Global $_LangSupportFile = $sFilepath
	Local $_LangSupportData = FileRead($sFilepath)
	If @error Then
		MsgBox(16, 'Error', 'Could not read Language File: ' & $sFilepath & '. Error Code: ' & @error & @CRLF & 'Unable to display strings correctly.')
		Return SetError(2)
	EndIf
	Global $_LangSupportArray = _LangCreateArray($_LangSupportData)
	If @error Then Return SetError(3)
EndFunc   ;==>_LangInit

Func _parseXML($sString)
	$sString = StringReplace($sString, '&amp;', '&')
	$sString = StringReplace($sString, '&lt; ', '<')
	$sString = StringReplace($sString, '&gt;', '>')
	$sString = StringReplace($sString, '&quot;', '"')
	$sString = StringReplace($sString, '&apos;', "'")
	Return $sString
EndFunc

;===============================================================================
; Function Name:	_xmlReadString($sData, $sString)
; Description:		reads the String from the XML file
; Parameter(s):		$sData		The string that contains the data read from the xml file
;					$sString	The Key to extract
; Syntax:			_xmlReadString($sData, $sString)
; Author(s):		mataran
; Return Value(s)	Value associated with the key
;===============================================================================
Func _xmlReadString($sData, $sString)
	$aString = _StringBetween($sData, '<string name="' & $sString & '">', '</string>')
	If @error Then
		SetError(1)
		Return '[' & $sString & ']' ; error return
	EndIf
	Return _parseXML($aString[0])
EndFunc   ;==>_xmlReadString

;===============================================================================
; Function Name:	_LangCreateArray($sData)
; Description:		Creates an Array with matching Key-Value pairs of the XML file
; Parameter(s):		$sData		The string that contains the data read from the xml file
; Syntax:			_LangCreateArray($sData)
; Author(s):		mataran
; Return Value(s)	Array with matching Key-Value pairs
;===============================================================================
Func _LangCreateArray($sData)
	Local $aTmp = _StringBetween($sData, '<string name="', '">')
	If @error Then
		MsgBox(16, 'Error', 'Error: The selected language file seems corrupt!')
		Return SetError(2)
	EndIf
	Local $_LangSupportArray[UBound($aTmp)][2]
	For $i = 0 To UBound($aTmp) - 1
		$_LangSupportArray[$i][0] = $aTmp[$i]
		$_LangSupportArray[$i][1] = _xmlReadString($sData, $aTmp[$i])
		If @error Then SetError(1)
	Next
	Return $_LangSupportArray
EndFunc   ;==>_LangCreateArray


;===============================================================================
; Function Name:	s($sString)
; Description:		Gets the Value associated with the key
; Parameter(s):		$sString	key to find value to
; Example:			MsgBox(0, '', 's('Test')) reads the string matching the key "Test"
; Syntax:			s($sString)
; Author(s):		mataran
; Return Value(s)	found Value
;===============================================================================
Func s($sString)
	If Not IsDeclared('sString') Or $sString = '' Then Return ''
	If Not IsDeclared('_LangSupportArray') Then
		If Not $_LangSupport_bError Then
			MsgBox(16, 'LangError', '_LangSupportArray not initialized. See _LangInit($sFilepath)')
			$_LangSupport_bError = True
		EndIf
		SetError(1)
		Return $sString
	EndIf
	For $i = 0 To UBound($_LangSupportArray) - 1
		If $sString = $_LangSupportArray[$i][0] Then Return $_LangSupportArray[$i][1]
	Next
	MsgBox(16, 'Error: Update Required!', 'There are translations missing in your language file!' & @CRLF & 'The program will not be able to display the data correctly. Please update the language file!' & @CRLF & @CRLF & 'Missing Content: ' & @CRLF & $sString)
	ConsoleWrite($_LangSupportFile & '_outdated! Data: ' & @CRLF & $sString & @CRLF)
;~ 	FileDelete($_LangSupportFile)
	SetError(1)
	Return '[' & $sString & ']'
EndFunc   ;==>s
