Jump to content

Support of language files in the program


AZJIO
 Share

Recommended Posts

Support of language files in the program

;  coded by AZJIO

#include <ComboConstants.au3>

;=============================================================
; This is not necessarily in the script
If Not FileExists(@ScriptDir & '\Lang') Then DirCreate(@ScriptDir & '\Lang')
$hFile = FileOpen(@ScriptDir & '\Lang\En.lng', 2 + 32)
If $hFile <> -1 Then
    FileWrite($hFile, _
            '[lng]' & @CRLF & _
            '1=My Programs' & @CRLF & _
            '2=Open' & @CRLF & _
            '3=Open File' & @CRLF & _
            '4=Example choice language' & @CRLF & _
            '5=Select' & @CRLF & _
            '6=Language')
    FileClose($hFile)
EndIf

$hFile = FileOpen(@ScriptDir & '\Lang\Ru.lng', 2 + 32)
If $hFile <> -1 Then
    FileWrite($hFile, _
            '[lng]' & @CRLF & _
            '1=Моя программа' & @CRLF & _
            '2=Открыть' & @CRLF & _
            '3=Открыть файл' & @CRLF & _
            '4=Пример выбора языка' & @CRLF & _
            '5=Выбор' & @CRLF & _
            '6=Языковой файл')
    FileClose($hFile)
EndIf
;=============================================================

#NoTrayIcon
Global $LangPath, $Ini = @ScriptDir & '\prog_set.ini'

; Create a configuration file with the language parameter. Used when you first start.
If Not FileExists($Ini) Then
    $hFile = FileOpen($Ini, 2)
    If $hFile <> -1 Then
        FileWrite($hFile, _
                '[Set]' & @CRLF & _
                'Lang=none')
        FileClose($hFile)
    EndIf
EndIf

; By default, set the English language interface, in the absence of language files.
Global $aLngDef[7][2] = [[ _
        6, 6],[ _
        '1', 'My Programs'],[ _
        '2', 'Open'],[ _
        '3', 'Open File'],[ _
        '4', 'Example choice language'],[ _
        '5', 'Select'],[ _
        '6', 'Language']]

; Ru
; This is optional, but the convenience of the mother tongue language file is not required
If @OSLang = 0419 Then
    Dim $aLngDef[7][2] = [[ _
            6, 6],[ _
            '1', 'Моя программа'],[ _
            '2', 'Открыть'],[ _
            '3', 'Открыть файл'],[ _
            '4', 'Пример выбора языка'],[ _
            '5', 'Выбор'],[ _
            '6', 'Языковой файл']]
EndIf

Global $aLng[7] = [6]

_SetLangCur($aLngDef) ; initially set by default, in case the language file will be invalid and will not apply to all items

; apply the language file, if specified.
$LangPath = IniRead($Ini, 'Set', 'Lang', 'none')
If $LangPath <> 'none' And FileExists(@ScriptDir & '\Lang\' & $LangPath) Then
    $aLngINI = IniReadSection(@ScriptDir & '\Lang\' & $LangPath, 'lng')
    If Not @error Then _SetLangCur($aLngINI)
EndIf
$aLngINI = 0

$hGui = GUICreate($aLng[1], 250, 100)
$Button = GUICtrlCreateButton($aLng[2], 10, 60, 99, 22)
GUICtrlSetTip(-1, $aLng[3])
$Label = GUICtrlCreateLabel($aLng[4], 10, 5, 153, 15)
; $Checkbox = GUICtrlCreateCheckbox ($aLng[5], 10, 50, 55, 22)

; Search for language files to add to the list of Combo
$LangList = 'none'
$search = FileFindFirstFile(@ScriptDir & '\Lang\*.lng')
If $search <> -1 Then
    While 1
        $hFile = FileFindNextFile($search)
        If @error Then ExitLoop
        $LangList &= '|' & $hFile
    WEnd
EndIf
FileClose($search)

GUICtrlCreateLabel('Language', 10, 33, 75, 17) ; This text is usually not recommended to translate, since switching of unintelligible language will lead to difficulties in restoring the native language, since it is not clear what item in the menu to click.
$ComboLang = GUICtrlCreateCombo('', 85, 30, 70, 22, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, $LangList, $LangPath)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $ComboLang
            _SetLang()
        Case -3
            Exit
    EndSwitch
WEnd

; function change of language
Func _SetLang()
    Local $aLngINI
    $LangPath = GUICtrlRead($ComboLang)
    If $LangPath <> 'none' And FileExists(@ScriptDir & '\Lang\' & $LangPath) Then
        $aLngINI = IniReadSection(@ScriptDir & '\Lang\' & $LangPath, 'lng')
        If Not @error Then
            _SetLangCur($aLngINI)
            _SetLang2()
            IniWrite($Ini, 'Set', 'Lang', $LangPath)
        EndIf
    Else
        _SetLangCur($aLngDef)
        _SetLang2()
        $LangPath = 'none'
        IniWrite($Ini, 'Set', 'Lang', 'none')
    EndIf
EndFunc   ;==>_SetLang

Func _SetLangCur($aLng2D)
    Local $tmp
    For $i = 1 To $aLng2D[0][0]
        If StringInStr($aLng2D[$i][1], '\n') Then $aLng2D[$i][1] = StringReplace($aLng2D[$i][1], '\n', @CRLF) ; Enables line wrapping, which does not support ini
        $tmp = Number($aLng2D[$i][0])
        If $tmp > 0 And $tmp <= $aLng[0] Then $aLng[$tmp] = $aLng2D[$i][1] ; Add text if its parameter is a number used as array index
        ; In fact if the passed in the $tmp parameter is not a number in the range of indexes of the array, it will be discarded. The array doesn't cause a mistake as the number won't exceed quantity of elements of the array.
    Next
EndFunc   ;==>_SetLangCur

; update function of texts in the GUI
Func _SetLang2()
    ; not enough to update the names of variables, they need to change the visible GUI
    WinSetTitle($hGui, '', $aLng[1])
    GUICtrlSetData($Label, $aLng[4])
    GUICtrlSetData($Button, $aLng[2])
    GUICtrlSetTip($Button, $aLng[3])
    ; language change depending on the State of the checkbox
    ; If $TrCh = 0 Then
        ; GUICtrlSetTip($Checkbox, $aLng[5])
    ; Else
        ; GUICtrlSetTip($Checkbox, $aLng[6])
    ; EndIf
EndFunc   ;==>_SetLang2

Just an example

Edited by AZJIO
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...