Working with INI files, there must be a more efficient way to do it.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Working with INI files, there must be a more efficient way to do it.

28 Mar 2018, 08:43

Hi,

This is my first time checking out the IniRead function and I think I'm doing too much double.

Is there a more efficient way of doing this ?

Here is an example of my code:

Code: Select all

IniRead, TEXT1, language.ini, DUTCH, TEXT1
IniRead, TEXT2, language.ini, DUTCH, TEXT2

Gui, Add, Button, x447 y37 vDutch   gLangDutch, NL
Gui, Add, Button, x475 y37 vEnglish gLangEnglish, EN
Gui, Add, Text, x10 y50 vText , %TEXT1%
Gui, Add, Button, vButton, %TEXT2%
Gui, Show, w521 h370, INI TEST
return

LangDutch:
IniRead, TEXT1, language.ini, DUTCH, TEXT1
IniRead, TEXT2, language.ini, DUTCH, TEXT2
GuiControl,, Text, %TEXT1%
GuiControl,, Button, %TEXT2%
return

LangEnglish:
IniRead, TEXT1, language.ini, ENGLISH, TEXT1
IniRead, TEXT2, language.ini, ENGLISH, TEXT2
GuiControl,, Text, %TEXT1%
GuiControl,, Button, %TEXT2%
return
Example of language.ini

Code: Select all

[DUTCH]
TEXT1 = Hallo
TEXT2 = Wereld

[ENGLISH]
TEXT1 = Hello
TEXT2 = World
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Working with INI files, there must be a more efficient way to do it.

28 Mar 2018, 09:08

Code: Select all

#SingleInstance, Force

IniRead, TEXT1, language.ini, DUTCH, TEXT1
IniRead, TEXT2, language.ini, DUTCH, TEXT2

Gui, Add, Radio,	x447	y37 vRB1			,% "NL"
Gui, Add, Radio,	x475	y37 vRB2			,% "EN"
Gui, Add, Text,		x10		y50 vText			,% TEXT1
Gui, Add, Button,				vButton	gLang	,% TEXT2
Gui, Show, 			w521	h370				,% "INI TEST"
Return

Lang:
	Gui, Submit, NoHide
	lang := (RB1 = 1) ? "DUTCH" : "ENGLISH"
	IniRead, TEXT1, language.ini,% lang, TEXT1
	IniRead, TEXT2, language.ini,% lang, TEXT2
	GuiControl,, Text,% TEXT1
	GuiControl,, Button,% TEXT2
	Return
Not tested (corrected).
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Working with INI files, there must be a more efficient way to do it.

28 Mar 2018, 09:18

Thanks for your quick answer BoBo, but it doesn't seem to be working, also I'd like to keep using the original
buttons and not use Radio buttons.

In the meantime I tried to create a function (I always have problem understanding them but I'll keep trying :) ).

I tried this, but when I call it it doesn't return anything. I'm probably doing something stupid, but can't figure it out.

Code: Select all

SelectLanguage(Lang)
{
	IniRead, TEXT1, language.ini, %Lang%, TEXT1
	IniRead, TEXT2, language.ini, %Lang%, TEXT2
	return
}

LangDutch:

SelectLanguage("DUTCH")
MsgBox %Lang%                       ;;; THIS RETURNS A BLANK MESSAGE BOX, WHY IS THAT ?

GuiControl,, Text, %TEXT1%
GuiControl,, Button, %TEXT2%
return
}
Guest

Re: Working with INI files, there must be a more efficient way to do it.

28 Mar 2018, 09:24

Add global to your function like so

Code: Select all

SelectLanguage(Lang)
{
	global ; <- add this
	IniRead, TEXT1, language.ini, %Lang%, TEXT1
	IniRead, TEXT2, language.ini, %Lang%, TEXT2
	return
}
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Working with INI files, there must be a more efficient way to do it.

28 Mar 2018, 09:27

You can also shorten the code like this:

Code: Select all

IniRead, TEXT1, language.ini, DUTCH, TEXT1
IniRead, TEXT2, language.ini, DUTCH, TEXT2

Gui, Add, Button, x447 y37 vDutch   gLangDutch, NL
Gui, Add, Button, x475 y37 vEnglish gLangEnglish, EN
Gui, Add, Text, x10 y50 vText , %TEXT1%
Gui, Add, Button, vButton, %TEXT2%
Gui, Show, w521 h370, INI TEST
return

LangEnglish:
LangDutch:
SelectLanguage(substr(A_ThisLabel, 5))
GuiControl,, Text, %TEXT1%
GuiControl,, Button, %TEXT2%
return


SelectLanguage(Lang) {
	global Text1, Text2
	IniRead, TEXT1, language.ini, %Lang%, TEXT1
	IniRead, TEXT2, language.ini, %Lang%, TEXT2
	return
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Working with INI files, there must be a more efficient way to do it.

28 Mar 2018, 09:41

WalkerOfTheDay wrote:Thanks for your quick answer BoBo, but it doesn't seem to be working, also I'd like to keep using the original
buttons and not use Radio buttons.
Changed variable from RB to RB1 (so it should work now). JFTR :)
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Working with INI files, there must be a more efficient way to do it.

28 Mar 2018, 09:47

@ Bobo: Thanks for the addition.
@Guest: global indeed fixed it.
@Odlanir: Thanks that's a pretty smart solution !
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Working with INI files, there must be a more efficient way to do it.

28 Mar 2018, 09:50

What is the purpose of this and how flexible should this be?
Im guessing only 2 languages will be supported or are you perhaps thinking to add more? I dont think buttons is good for selecting languages. Why not use DDL (drop-down list) instead? Because lots of languages and GUI gets cluttered badly with buttons.

In any case, heres my quick version.
Just copy-paste and it should work.
Variables, ini section and ini keys are not perhaps named properly, you can do that yourself. I arbitrary named them. xD

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


; Simulates ini file.
/*
[LangList]
ListLng=DUTCH||ENGLISH|GERMAN|SERBIAN|
SelectedLng=DUTCH

[DUTCH]
TEXT1 = Hallo
TEXT2 = Wereld

[ENGLISH]
TEXT1 = Hello
TEXT2 = World

[GERMAN]
TEXT1 = Halo
TEXT2 = Welt

[SERBIAN]
TEXT1 = Halo
TEXT2 = Svet
*/

; Initial reading.
IniRead, ListLng , %A_ScriptDir%\%A_ScriptName%, LangList, ListLng, ERROR
IniRead, SelectedLng , %A_ScriptDir%\%A_ScriptName%, LangList, SelectedLng, ERROR
IniRead, TEXT1, %A_ScriptDir%\%A_ScriptName%, %SelectedLng%, TEXT1
IniRead, TEXT2, %A_ScriptDir%\%A_ScriptName%, %SelectedLng%, TEXT2

; GUI
Gui, Add, DDL, w100 vSelectLang gSelectLang, %ListLng% ; Drop down list
Gui, Add, Text, w100 vText Center, %TEXT1% ; Text
Gui, Add, Button, w100 vButton, %TEXT2% ; Button
Gui, Show
return

; DDL SUB
SelectLang:
	Gui, Submit, NoHide
	IniRead, TEXT1, %A_ScriptDir%\%A_ScriptName%, %SelectLang%, TEXT1
	IniRead, TEXT2, %A_ScriptDir%\%A_ScriptName%, %SelectLang%, TEXT2
	GuiControl,, Text, %TEXT1%
	GuiControl,, Button, %TEXT2%
	
	StringReplace, ListLng, ListLng, %SelectedLng%||, %SelectedLng%|
	StringReplace, ListLng, ListLng, %SelectLang%, %SelectLang%|
	SelectedLng := SelectLang
	IniWrite, %ListLng%, %A_ScriptDir%\%A_ScriptName%, LangList, ListLng
	IniWrite, %SelectedLng%, %A_ScriptDir%\%A_ScriptName%, LangList, SelectedLng
return

; GUI events
GuiEscape:
GuiClose:
exitapp

All you have too do here is list as much languages as you want and in any order. :)
Make sure to select a default starting one tho.
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Working with INI files, there must be a more efficient way to do it.

28 Mar 2018, 10:36

@theimmersion : Notice that this code will not work in a compiled script ( at least the IniWrite part ). You can even shorten your vars :

Code: Select all

%A_ScriptDir%\%A_ScriptName%
with this:

Code: Select all

%A_ScriptFullpath%
P.S: Maybe I'm wrong but looks like you've read a recent post about the inline ini coding. ( https://autohotkey.com/boards/viewtopic.php?f=5&t=45333 )
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Working with INI files, there must be a more efficient way to do it.

28 Mar 2018, 10:55

@Odlanir, I know it wont work for compiled scripts. I just showed example while keeping everything in one place. Easier to work with for test/demo scripts.
About the "%A_ScriptFullpath%", i keep forgetting this one. Thanks. xD

P.S: nope, i did that long before that for another project of mine. I even think i have a post with my approach (similar) somewhere where i asked how to create .ini as utf-16 for serbian/russian language letters. :)
But thanks, that post will surly also come in handy.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Working with INI files, there must be a more efficient way to do it.

29 Mar 2018, 02:08

theimmersion wrote:What is the purpose of this and how flexible should this be?
Im guessing only 2 languages will be supported or are you perhaps thinking to add more? I dont think buttons is good for selecting languages. Why not use DDL (drop-down list) instead? Because lots of languages and GUI gets cluttered badly with buttons.
Well. I am working on a program which stores the data of our personel and keeps track of the hours of our temps.
Years ago I wrote an application in CA Clipper (16 bit), which we still use by running it through DOSBox.
I am trying to replace this application with an Autohotkey application.

I found, for me it's a big challenge to complete it because I still lack a lot of AHK knowledge but with the help of
this great community :bravo: :bravo: I've already gotten very far with software.

To give something back to the community, once finished (hopefully this year :lol: ) I'd like to share the software with the
community for free. But since it's in Dutch.. not many people would/could benefit from it.
So yesterday the thought crossed in my mind to make it multi language.
I only speak Dutch, English and German so I was thinking of these three languages first. Was thinking of doing Country flags in the scripts.
Perhaps I'll take your advise and do a DDL instead of buttons. Maybe I'll make a 'preference' in a seperate tab where users can
choose their settings.

theimmersion wrote: In any case, heres my quick version.
Just copy-paste and it should work.
Variables, ini section and ini keys are not perhaps named properly, you can do that yourself. I arbitrary named them. xD
Spoiler
All you have too do here is list as much languages as you want and in any order. :)
Make sure to select a default starting one tho.
Thank you so much for your script.
I do have some questions: how is it possible that the script reads the commented section ??

And eventually I am wanting to compile the script, so if I use an external ini file it should work right ?

Many thanks for your help guys !
Rene Yertini

Re: Working with INI files, there must be a more efficient way to do it.

29 Mar 2018, 02:40

I do have some questions: how is it possible that the script reads the commented section ??
great question
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Working with INI files, there must be a more efficient way to do it.

29 Mar 2018, 04:07

With your great examples, I came up with this (might change the buttons later).

Code: Select all

SelectLanguage(Lang)

{
	global
	IniRead, TEXT1, language.ini, %Lang%, TEXT1
	IniRead, TEXT2, language.ini, %Lang%, TEXT2
	GuiControl,, Text, %TEXT1%
	GuiControl,, Button, %TEXT2%
	IniWrite, %TEXT1%, language.ini, DEFAULT, TEXT1
	IniWrite, %TEXT2%, language.ini, DEFAULT, TEXT2
	return
}
IniRead, TEXT1, language.ini, DEFAULT, TEXT1
IniRead, TEXT2, language.ini, DEFAULT, TEXT2

Gui, Add, Button, x442 y37 vDutch   gLangDutch, NL
Gui, Add, Button, x470 y37 vEnglish gLangEnglish, EN
Gui, Add, Button, x498 y37 vGerman  gLangGerman, DE
Gui, Add, Text, x10 y50 vText , %TEXT1%
Gui, Add, Button, vButton, %TEXT2%
Gui, Show, w521 h370, INI TEST
return

LangDutch:
LangEnglish:
LangGerman:
SelectLanguage(substr(A_ThisLabel, 5))
return

GuiClose:
ExitApp
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Working with INI files, there must be a more efficient way to do it.

29 Mar 2018, 04:54

Is there a specifc (well known but I've never heard of) reason why you've positioned the function above/before its initial call?? :wtf:
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Working with INI files, there must be a more efficient way to do it.

29 Mar 2018, 04:57

Actually, no :)
I always put my functions on top of my scripts tbo.
Must be a remainder of my CA Clipper days :).

Why, is it bad ?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Working with INI files, there must be a more efficient way to do it.

29 Mar 2018, 05:34

WalkerOfTheDay wrote:Actually, no :)
I always put my functions on top of my scripts tbo.
Must be a remainder of my CA Clipper days :).

Why, is it bad ?
Nun, auf Deutsch, weils (für mich einfacher ist). Der funktionsaufruf agiert als referenz auf die jeweilige funktion. Was sich IMHO ähnlich verhält, wie eine referenz in einer wissenschaftlichen ausführung (zb einer doktor-/diplomarbeit), welche auf eine jeweilige fussnote oder einen glossar (am ende eines buches) verweist. Die scriptbezogene steigerung des ganzen ist dann noch die auslagerung von funktionen in externe "bibliotheken", welche nur sukkzessive 'inkludiert' werden. Damit bleibt der code übersichtlicher, sowie modular - was die wartung/maintenance erleichtert.

It's probably a matter of taste but AFAIK it's kinda common sense that, if collecting all functions within a section of the script, doing this at the end of the script. With AHK it might be even more special bc of the [AutoExecute]-section!
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Working with INI files, there must be a more efficient way to do it.

29 Mar 2018, 06:26

Okay, you now I am not German, right ? :mrgreen:

Fortunately, I can read it but it took me some time to understand what you were saying.
I get what you're saying, and maybe I should adapt my coding style.

I just always have been putting my functions on top beneath eachother to not 'clutter' the main code.
I find that it looks somewhat 'cleaner'.

When you do it, do you put all functions beneath eachothers, or do you put your functions beneath the
relevant piece of code ?
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Working with INI files, there must be a more efficient way to do it.

29 Mar 2018, 06:29

@BoBo Was ist mit dir und Deutsch? We get it, Your german. Everyone knows by now. :P
But not everyone knows german language tho.

@WalkerOfTheDay well, iniread is reading any plain text file and an ahk script is a plain text file anyway.
In simple terms, IniRead/Write only knows for 3 things: Section | Key | Comment Line (;)
So we CHEAT THE SYSTEM by adding the multi-line comment (/* */) in ahk script so AHK does not interpret those because it would throw an error on sections because ahk does not know what that is and also skip the keys because for ahk, those are the same like normal variables, but skip it while the IniRead winAPI detects the sections and keys normally.
A neat thing sometimes.

Of course you would have to move it from the ahk script to external ini file, its a convenience thing for me tho for quick testing, and sometimes its annoying when i have to copy-paste stuff into an ahk script and a separate ini file as well. Dont ask, im just weird. xD
Also, use utf-16 if you ever lan on adding a language that use Cyrillic letters (like Russian, Serbian, Croatian etc...). GL! :)
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Working with INI files, there must be a more efficient way to do it.

29 Mar 2018, 07:08

theimmersion wrote:@BoBo Was ist mit dir und Deutsch? We get it, Your german. Everyone knows by now. :P
But not everyone knows german language tho.
Well, I thought to myself ...
WalkerOfTheDay wrote:So yesterday the thought crossed in my mind to make it multi language.
I only speak Dutch, English and German so I was thinking of these three languages first. Was thinking of doing Country flags in the scripts.
... taking WalkerOfTheDay's word/statement for granted wouldn't be a problem!? :mrgreen: and for the reason that not everyone is speaking German I've left an additional English statement just for your pleasure :thumbup:
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Working with INI files, there must be a more efficient way to do it.

29 Mar 2018, 07:13

.. But since it's in Dutch.. not many people would/could benefit from it.
I like dutch ... can work with , laatste nieuws :
https://download.omroep.nl/nos/content/ ... _web02.mp4

I used dBase_III+ , was able to replace with autohohotkey (listview)

a script from wolf_II , Language_demo.ahk :
https://autohotkey.com/boards/viewtopic.php?f=5&t=14883

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Google [Bot], Nerafius and 108 guests