Localizing the own code - some recommendations?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Localizing the own code - some recommendations?

27 Feb 2016, 15:20

I think the localization of AHK-code (also compiled) could be done (not so hard) with a LNG-file in INI-style and placeholders in the code, which look for the right strings in the LNG-file.

Before I reinvent the localization-wheel, are there some recommendations for existing AHK-codes or snippets?

Have a fine Sunday!
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Localizing the own code - some recommendations?

27 Feb 2016, 20:03

I think the easiest way would be to have a file as you suggest, something like this (e.g. "lang.ini"):

Code: Select all

[english]
string1=Hello
string2=Example
[french]
string1=Bonjour
string2=Exemple
Then in your script, a fairly simple piece of code can read all the strings for a particular language:

Code: Select all

language := "english" ;change this line to change language loaded
IniRead, language, lang.ini, % language
loop, parse, language, `r`n
{
   StringSplit, words, A_LoopField, =
   %words1% := words2
}
MsgBox % string1 "`r`n" string2
return
So basically, depending on the selected language, the relevant strings will be loaded into variables string1, string2 etc. Of course, you can change the string names in the ini file, and then the corresponding variable name will change in the code. For example if you had welcome_message=Welcome to the script in the ini file, within the program, the variable "welcome_message" would contain the string "Welcome to the script".

And of course, you can base the language selection on user input, or you could base it off the built-in variable A_Language (see https://autohotkey.com/docs/misc/Languages.htm for reference). And obviously you can also change the section names in the INI file to whatever is most manageable for you too.

EDIT: Provided the INI file is supplied with the EXE, this should work fine with the compiled EXE also. You can use FileInstall to include the INI into the EXE (will be extracted when the EXE is run), or you can simply supply it alongside the EXE. You may want to add either a default setting for each string within the code, or else an error if the file is not present, just in case.
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Localizing the own code - some recommendations?

28 Feb 2016, 04:56

Thanks for your quick and helpful reply.
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Localizing the own code - some recommendations?

28 Feb 2016, 06:14

thank you SifJar , I played with an example

Code: Select all

;- Iniread / -IniWrite - Example
;Modified= 20160228
;Created = 20160228
filename1= IniTest
SetWorkingDir, %A_ScriptDir%

RSSINI=%A_scriptdir%\lang_NEW.ini
ifnotexist,%rssini%
    {
    IniWrite,Hello    , %rssini% ,english   , string1
    IniWrite,Example  , %rssini% ,english   , string2
    IniWrite,Bonjour  , %rssini% ,french    , string1
    IniWrite,Exemple  , %rssini% ,french    , string2
    }
IniRead, E1, %rssini% , english , string1
IniRead, E2, %rssini% , english , string2
IniRead, F1, %rssini% , french  , string1
IniRead, F2, %rssini% , french  , string2

Gui,2: Add, text, x10  y10 , English-Line1
Gui,2: Add, text, x10  y35 , English-Line2
Gui,2: Add, text, x10  y60 , French -Line1
Gui,2: Add, text, x10  y85 , French -Line2

Gui,2: Add, Edit, x100 y10 w100 vC1,%E1%
Gui,2: Add, Edit, x100 y35 w100 vC2,%E2%
Gui,2: Add, Edit, x100 y60 w100 vC3,%F1%
Gui,2: Add, Edit, x100 y85 w100 vC4,%F2%

Gui,2: Add, Radio, x10  y120 w70 h20 vR1 gCheckR, English
Gui,2: Add, Radio, x120 y120 w70 h20 vR2 gCheckR, French
Gui,2: Show,x100 y10 w250 h150,%filename1%
return


save1:
IniWrite, %C1%  , %rssini% ,english   , string1
IniWrite, %C2%  , %rssini% ,english   , string2
IniWrite, %C3%  , %rssini% ,french    , string1
IniWrite, %C4%  , %rssini% ,french    , string2
return


CheckR:
Gui,2:submit,nohide
if R1=1
  language=english
if R2=1
  language=french
gosub,lang
return

LANG:
IniRead, language, %rssini%, % language
loop, parse, language, `r`n
{
   StringSplit, words, A_LoopField, =
   %words1% := words2
}
MsgBox % string1 "`r`n" string2
return


2Guiclose:
Gui,2:submit,nohide
gosub,save1
exitapp

about language code

Code: Select all

;https://autohotkey.com/docs/misc/Languages.htm

;- a failure in help-file (?) :
;the_language := languageCode_%A_Language%    ; Get the name of the system's default language.  ;- wrong
; replace :=   with  =
; or
the_language := "languageCode_" . A_Language  ; Get the name of the system's default language.
MsgBox,The_Language is  = %the_language%`nA_language is  =%a_language%
EDIT :
I was wrong , must copy the whole text from help file , not only the last 2 lines
https://autohotkey.com/docs/misc/Languages.htm

Code: Select all

;-------- https://autohotkey.com/docs/misc/Languages.htm ---
;..... continue , here only last lines ............................
languageCode_0843 = Uzbek_Cyrillic
languageCode_042a = Vietnamese
;----- above all countries language code ....
the_language := languageCode_%A_Language%  ; Get the name of the system's default language.
MsgBox, %the_language%`n%a_language%       ; Display the language name and code ( example : German_Standard   0407 )
return

another example with date format

Code: Select all

Time := A_Now
FormatTime,suomi, %time% L1035, dddd MMMM dd-MM-yyyy  ;-- SF 1035 suomi finland
FormatTime,here , %time%      , dddd MMMM dd-MM-yyyy  ;
msgbox,Language-Code Suomi Finland 1035 = %suomi%`nLanguage-Code here %a_language% = %here%
return

;-- see locale codes for language
; https://autohotkey.com/docs/misc/Languages.htm
; http://msdn.microsoft.com/en-us/library/aa912040
; http://www.science.co.il/language/locale-codes.asp
Last edited by garry on 29 Feb 2016, 04:04, edited 3 times in total.
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Localizing the own code - some recommendations?

28 Feb 2016, 08:03

garry wrote:thank you SifJar , I played with an example
The main issue I see with your example is that you manually read each individual string; this is going to be a pain if you have a lot of strings. My method of using a loop to automatically read all the strings seems a bit cleaner IMO.
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Localizing the own code - some recommendations?

28 Feb 2016, 08:43

I use your example , only the language was selected with radiobox .
Example combined iniwrite / iniread and it's possible to change content from xy.ini
( this example used only 2 languages with 2 words each )
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Localizing the own code - some recommendations?

28 Feb 2016, 18:55

garry wrote:I use your example , only the language was selected with radiobox .
Example combined iniwrite / iniread and it's possible to change content from xy.ini
( this example used only 2 languages with 2 words each )
Ah I see what you mean, I only looked at it very briefly before.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Localizing the own code - some recommendations?

29 Feb 2016, 01:32

garry wrote:;- a failure in help-file (?) :
;the_language := languageCode_%A_Language% ; Get the name of the system's default language. ;- wrong
; replace := with =
; or
the_language := "languageCode_" . A_Language ; Get the name of the system's default language.
No... you are wrong.

The original code uses a double-dereference to get from, for example, "0409" to "English_United_States". It relies on all of the languageCode_xxxx variables being defined in your script (i.e. copied from the help file).

Your code does not produce "the name of the system's default language"; it produces the language code with the nonsense prefix "languageCode_". If you just want the language code, just use A_Language.
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Localizing the own code - some recommendations?

29 Feb 2016, 02:52

this is a copy from https://autohotkey.com/docs/misc/Languages.htm which not works
( yes , I need only a_language )
EDIT : I was wrong , see below

Code: Select all

;-------- https://autohotkey.com/docs/misc/Languages.htm ---
the_language := languageCode_%A_Language%  ; Get the name of the system's default language.
MsgBox %the_language%
Last edited by garry on 29 Feb 2016, 03:37, edited 1 time in total.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Localizing the own code - some recommendations?

29 Feb 2016, 03:18

No, it is not a copy of the script from the documentation. It is the last two lines of a script from the documentation.
I wrote: It relies on all of the languageCode_xxxx variables being defined in your script (i.e. copied from the help file).
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Localizing the own code - some recommendations?

29 Feb 2016, 03:24

aargh, sorry , yes I was wrong
when I copy the whole text it works fine ...
to be not confused I added this

Code: Select all

;-------- https://autohotkey.com/docs/misc/Languages.htm ---
;..... continue , here only last lines ............................
languageCode_0843 = Uzbek_Cyrillic
languageCode_042a = Vietnamese
;----- above all countries language code ....
the_language := languageCode_%A_Language%  ; Get the name of the system's default language.
MsgBox, %the_language%`n%a_language%       ; Display the language name and code ( example : German_Standard   0407 )
return
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Localizing the own code - some recommendations?

29 Feb 2016, 04:16

Btw, A_Language returns the system's default language, i.e. GetSystemDefaultUILanguage().

It may be more appropriate to use GetUserDefaultUILanguage(), at least on Vista and above, where that function works.

Code: Select all

MsgBox % format("{:04X}", DllCall("GetUserDefaultUILanguage"))
There is another function GetUserPreferredUILanguages(), but it is rubbish. If I set three languages in Control Panel (something you can do either in Windows 10, or in MUI versions of Windows), the function returns the first one, ignores the other two and returns English United States as a second option, even though it is not one of my preferences. (My system was installed from English United Kingdom media but is set to Australia.)
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Localizing the own code - some recommendations?

29 Feb 2016, 04:24

thank you Lexikos , example above 'GetUserDefaultUILanguage' works also with XP , like a_language ( example : 0407 )
https://autohotkey.com/docs/misc/Languages.htm
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Localizing the own code - some recommendations?

29 Feb 2016, 05:48

As usual, I'm fighting with more trivial questions ..

I have a GUI where I want to write the "translated text". I define the GUI and call a function:

Code: Select all

    Gui, Add, Text, x+15  Section                          ,   % callmsg(1)
....

callmsg(nummer)    ; Nummer wird übergeben, Sprache kommt vom Dialog, Sprachdatei ist fixiert
{
    global langfile
    global language
    IniRead, mystring, %langfile%, %language%, %nummer%, ???
    MsgBox %mystring%
    return
}
The current state is:
- the GUI is defined
- the function is called
- the function searches the string from file and display it with the msgbox
- everything's fine, BUT: nothing is returned to the GUI

I'm again lost between expressions, calls, functions and returns ....

Thanks for help!
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Localizing the own code - some recommendations?

29 Feb 2016, 06:00

... nothing is returned to the GUI
don't really know , just a gui example

Code: Select all

gui,2: add,edit ,x10 y10 h25 w150 vED1
gosub,a1
Gui,2:show
return

2Guiclose:
exitapp

a1:
mystring=abcd
GuiControl,2:,ED1,%mystring%
return
tried something with a function

Code: Select all

gui,2: add,edit ,x10 y10 h25 w150 vED1
gosub,a1
Gui,2:show
return

2Guiclose:
exitapp

a1:
aa:=justAnotherFunction()
GuiControl,2:,ED1,%aa%
return

justAnotherFunction(){
   Return, "Function Test-1"
}
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Localizing the own code - some recommendations?

29 Feb 2016, 06:18

Peter2 wrote:everything's fine, BUT: nothing is returned to the GUI
You didn't return anything from the function! You need to use return mystring
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Localizing the own code - some recommendations?

29 Feb 2016, 06:27

thank you SifJar ( I think like the 2nd example above )
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Localizing the own code - some recommendations?

29 Feb 2016, 06:52

SifJar wrote:...You need to use return mystring
If you need the creation of a trivial problem, call me: :wave:

Thanks SifJar!
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Localizing the own code - some recommendations?

29 Feb 2016, 09:15

garry wrote:thank you SifJar ( I think like the 2nd example above )
Sort of, but your example is hideously over-complicated for such a simple task. There is absolutely no need to have a subroutine involved, or to assign the return value of the function to a variable - you ccould quite simply replace gosub,a1 with GuiControl,2:,ED1,% justAnotherFunction() and save about 4 lines (which considering the length of the script, is significant) as well as probably being more efficient code (not having to jump back and forth, assign variables etc.) [I'm not sure about the efficiency, that's mostly a guess].

However, even simpler and therefore generally better is something like Peter2 posted: Gui, Add, Text, x+15 Section , % callmsg(1) - just put the value straight into the gui control.

For the sake of completing my previous example, here's a GUI example using my original code:

Code: Select all

langFile := "lang.ini"
MsgBox, 1, Select Language, % "Press OK for English, Cancel for French"
IfMsgBox, OK
   language := "english" 
else
   language := "french"

gosub LoadLang
gosub DrawGui
return

GuiClose:
ExitApp

LoadLang:
IniRead, language, % langFile, % language
loop, parse, language, `r`n
{
   StringSplit, words, A_LoopField, =
   %words1% := words2
}
return

toFrench:
language := "french"
gosub LoadLang
gosub DrawGui
return

toEnglish:
language := "english"
gosub LoadLang
gosub DrawGui
return

DrawGui:
Gui, destroy
Gui, Add, Text, x30 y20 w140 h20 , % string1
Gui, Add, Text, x30 y50 w140 h20 , % string2
Gui, Add, Button, x30 y80 w65 h20 gtoEnglish, % english
Gui, Add, Button, x100 y80 w65 h20 gtoFrench, % french
Gui, Show, w200 h120, Language Demo
return
The advantage of this over using a function to look up a particular string when it's need is that all strings are loaded at once. So say I'm going to use string1, say 100 times throughout my script; I don't want to have to load it from the file every single time. If you're only going to use each string once, it may be better to go with a function to load them individually when you need them.

With the approach I've taken here (using a subroutine to load language), you can easily switch to another language by changing the "language" variable and calling the subroutine again. I have demonstrated this also. The lang.ini for use with this script:

Code: Select all

[english]
string1=Hello
string2=Example
english=English
french=French
[french]
string1=Bonjour
string2=Exemple
english=Anglais
french=Francais
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: Localizing the own code - some recommendations?

29 Feb 2016, 14:43

thank you SifJar , gosub is not needed

Code: Select all

gui,2: add,edit ,x10 y10 h25 w150 vED1
Gui,2:show
GuiControl,2:,ED1,% justAnotherFunction()
return

2Guiclose:
exitapp

justAnotherFunction(){
   Return, "Function Test-1"
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RussF and 186 guests