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

 AutoIt Version: 3.3.14.1
 Author:         mataran

 Script Function:
	Example2 Language UDF: change Language

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <File.au3>

#include <LanguageSupport.au3>

Global $sPathLang = @ScriptDir & '\Languages\'  ;Path to the LanguageFolder
Global $sPathIni = @ScriptDir & '\Example2.ini' ;Path to the ini-File

;=====================
; example only
If FileExists($sPathIni) Then
	$MsgBox = MsgBox(292,"Example","Reset Language?")
	If $MsgBox = 6 Then IniDelete($sPathIni, 'Language', 'LanguageFile')
EndIf
;=====================

$sLangFile = IniRead($sPathIni, 'Language', 'LanguageFile', '0') ;Read the selected Language from the ini file
If Not FileExists($sPathLang & $sLangFile) Then $sLangFile = GUI_SelectLang() ;In case the Language File (which is defined in the ini file) does not exist -> select one
_LangInit($sPathLang & $sLangFile) ;Load the Language File
If @error Then
	IniDelete($sPathIni, 'Language') ;Reset the selected Language in case the LangFile is corrupt (ofc you could use IniDelete too)
	Exit
EndIf


MsgBox(64, s('Title1'), s('Msg1'))
MainScript()

Func MainScript()
MsgBox(64, s('Title2'), StringFormat(s('MsgMain')))
EndFunc





Func GUI_SelectLang()
DirCreate($sPathLang) ;in case the folder does not exist yet, create it

Local $sFileList = ''
$aFileList = _FileListToArray($sPathLang, '*.xml', 1) ;check if there are files in the folder
If UBound($aFileList) < 2 Then
	ShellExecute($sPathLang)
	Sleep(1000)
	MsgBox(16, 'Error!', 'Please copy the language files to: ' & $sPathLang)
	Exit
EndIf
For $i = 1 To UBound($aFileList)-1
	$sFileList &= StringReplace($aFileList[$i], '.xml', '') & '|'
Next
$sFileList = StringTrimRight($sFileList, 1)

$GUI_Lang = GUICreate("Select Language", 250, 180)
GUISetFont(9, 400, 0, "MS Reference Sans Serif")
GUICtrlCreateLabel("Select Language", 70, 15, 109, 20)
$Combo_LangFiles = GUICtrlCreateCombo("", 20, 48, 209, 25, BitOR($CBS_DROPDOWN,$WS_VSCROLL))
GUICtrlSetData(-1, $sFileList)
GUICtrlSetTip(-1, 'Select Language from: ' & $sPathLang)
$Button_Save = GUICtrlCreateButton("Save", 83, 88, 83, 33)
$Button_FileOpen = GUICtrlCreateButton("Open", 166, 139, 35, 25)
GUICtrlSetFont(-1, 8, 400, 0, "MS Reference Sans Serif")
GUICtrlCreateLabel("Or select Language File:", 20, 144, 142, 19)
GUICtrlSetFont(-1, 8, 400, 0, "MS Reference Sans Serif")
GUISetState(@SW_SHOW)


While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $Button_Save
			If GUICtrlRead($Combo_LangFiles) <> '' Then
			$tmp = GUICtrlRead($Combo_LangFiles) &'.xml'
			GUIDelete($GUI_Lang)
			IniWrite($sPathIni, 'Language', 'LanguageFile', $tmp)
			Return $tmp ;gebe Dateinamen zurück
			EndIf
		Case $Button_FileOpen
			$sLangFile = FileOpenDialog('Select Language File', $sPathLang, 'Language File (*.xml)', 3, 'English.xml')
			If (Not @error) And ($sLangFile <> '') Then
				$tmp = StringSplit($sLangFile, '\', 2)
				FileCopy($sLangFile, $sPathLang & $tmp[UBound($tmp)-1]) ;kopiere Datei in den Ordner hinein
				GUIDelete($GUI_Lang)
				IniWrite($sPathIni, 'Language', 'LanguageFile', $tmp[UBound($tmp)-1])
				Return $tmp[UBound($tmp)-1] ;gebe Dateinamen zurück
			EndIf
	EndSwitch
WEnd
EndFunc
