Jump to content

Handling script logging and onscreen messages in multiple languages


Recommended Posts

Curious what the best approach to this might be? Or if perhaps I'm already using it.

So its standard procedure where I work to include detailed logging/reports/prompts to the end user as the script is being executed to perform its function. Living here in Canada means that I need to respect the possibility of language barriers in Quebec for instance, where it may include offices that are operating completely in French rather than English.

My current approach is leveraging a simple @OSLang check and an IF statement to relay a preconfigured message in the case of an English return, and an already translated version of the same message in the case a French Windows installation is detected.

Looking for a more sophisticated method....if there is any available? I'm guessing it's likely a pretty tall order to expect autoit to suddenly translate text for me but because I'm still learning, I figured I'd ask and see what options there might be out there.

Thanks!

Edited by Karnalsyn
Link to comment
Share on other sites

Rather than spreading Switch @OSLang ... Case ... Case ... Else ... EndSwitch everywhere in your code, create a 2D array of translated strings where dimension 1 is @OSLang and dimension 2 is string "index". Then all you have to do is check once for all that @OSLang is within accepted languages, store the index of this accepted language (0, 1, ...) in a global variable $lang and do MsgBox(..., $Strings[$lang][17]) to display the 18th string in the correct language.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

20 hours ago, jchd said:

Rather than spreading Switch @OSLang ... Case ... Case ... Else ... EndSwitch everywhere in your code, create a 2D array of translated strings where dimension 1 is @OSLang and dimension 2 is string "index". Then all you have to do is check once for all that @OSLang is within accepted languages, store the index of this accepted language (0, 1, ...) in a global variable $lang and do MsgBox(..., $Strings[$lang][17]) to display the 18th string in the correct language.

Thanks for offering up a potential method for me to explore.

I am dealing with more than one notification point per script run. We log multiple line entries as the script continues to run through each of its steps.

Is your suggestion based more around having only 1 message in multiple languages. Do you see any issues adapting your suggestion for multiple messages in multiple languages?

Example of logging per script would be:

-Extracting files

-Loading prerequisite software

-Installing X....

-Installation of X completed successfully

 

Thanks again

Edited by Karnalsyn
Link to comment
Share on other sites

Here's how I would do it. Study the comments and code to see that you can support essentially every dialect Windows supports, yet are able to produce the same message for all English (or Spanish or ...) user OR produce distinct messages for US and GB and any dialect you have to support.

#include <Array.au3>

; This enumeration lists all applicative strings "short names".
; Expand as necessary.
Global Enum $_AppMsgHello, $_AppMsgHowAreYou, $_AppMsgHopeYouEnjoyTheTrip, $_AppMsgSeeYouSoon

; This lists the (generic) languages for which we have strings : English, French, Spanish, Italian, Japanese
; For instance, here we confuse en-US and en-GB and simply group them under "English" for this application
; This is an example but is not mandatory: finer-grain regional strings can be similarly dealt with
; $_AppLangLast is a marker to limit list; keep it last
Global Enum $_AppLangEn, $_AppLangFr, $_AppLangEs, $_AppLangIt, $_AppLangJa, $_AppLangLast


; This function will return the requested message  for the current language
; $iAppMsg MUST be one of the $_App*** constants
; $iForceAppLang may be used to override @OSLang but must be within supported languages constants
Func _GetMsg($iAppMsg, $iForceAppLang = Default)

    ; All the Static initializations will only be done ONCE and will be skipped after the first call

    ; Array holding couples of [Regional language variant code (see @OSLang help), AppLanguage]
    ; Entries can be in any order. Expand as needed.
    Static $aLangCodes = [ _
        ["0409", $_AppLangEn], _    ; en-US
        ["0809", $_AppLangEn], _    ; en-GB
        ["0C09", $_AppLangEn], _    ; en-AU
        ["0410", $_AppLangIt], _    ; it-IT
        ["0810", $_AppLangIt], _    ; it-CH
        ["1009", $_AppLangEn], _    ; en-CA
        ["1409", $_AppLangEn], _    ; en-NZ
        ["1809", $_AppLangEn], _    ; en-IE
        ["1C09", $_AppLangEn], _    ; en-ZA
        ["040C", $_AppLangFr], _    ; fr-FR
        ["080C", $_AppLangFr], _    ; fr-BE
        ["4C0A", $_AppLangEs], _    ; es-NI
        ["0C0C", $_AppLangFr], _    ; fr-CA
        ["100C", $_AppLangFr], _    ; fr-CH
        ["140C", $_AppLangFr], _    ; fr-LU
        ["180C", $_AppLangFr], _    ; fr-MC
        ["040A", $_AppLangEs], _    ; es-ES_tradnl
        ["080A", $_AppLangEs], _    ; es-MX
        ["0C0A", $_AppLangEs], _    ; es-ES
        ["0411", $_AppLangJa] _     ; ja-JP
    ]
    ; Note that almost the same setup could be simplified by ignoring the first 2 hex digits
    ; of @OSLang, but doing so would preclude distinction between language dialects.
    ; For instance, @OSLang for all English dialects is xx04, all French is xx0C, a.s.o.


    ; 2D array holding app strings in every supported app language in turn
    ; App-supported languages (first dimension) in same order as $_AppLang enumeration
    ; and strings (second dimension) in same order as $_AppMsg enumeration
    Static $aStrings = [ _
        [ _     ; En
            "Good morning, dear Sir", _
            "How are you today?", _
            "We hope you have enjoyed the trip", _
            "See you soon!" _
        ], _
        [ _     ; Fr
            "Bonjour cher monsieur", _
            "Comment allez-vous aujourd'hui ?", _
            "Nous espérons que vous avez apprécié le voyage", _
            "À bientôt !" _
        ], _
        [ _     ; Es
            "Hola estimado Sr.", _
            "¿Cómo estás hoy?", _
            "Esperamos que hayas disfrutado del viaje", _
            "¡Nos vemos pronto!" _
        ], _
        [ _     ; It
            "Ciao caro Mr.", _
            "Come stai oggi?", _
            "Ci auguriamo che abbia mai goduto il viaggio", _
            "A presto!" _
        ], _
        [ _     ; Ja
            "こんにちは親愛なる氏", _
            "今日は元気ですか?", _
            "旅行を楽しんでいただければ幸いです", _
            "また近いうちにお会いしましょう!" _
        ] _
    ]

    ; Determine application language from @OSLang and the tables above
    Static $OSLang = _ArraySearch($aLangCodes, @OSLang)
    Static $iLang = ($OSLang < 0) ? -1 : $aLangCodes[$OSLang][1]

    ; Get the required message
    ; This is the only active part of code of the function after first invokation!
    ; Only if absolutely necessary we could check here that $AppMsg is a valid $_AppMsg*** constant
    ; but it's simpler to insure that the invoking code always uses a valid $_AppMsg*** value
    If $iForceAppLang = Default Then
        Return ($iLang < 0 ? "@OSLang code not supported yet!" : $aStrings[$iLang][$iAppMsg])
    Else
        Return (($iForceAppLang < 0) Or ($iForceAppLang >= $_AppLangLast) ? "Forced language " & $iForceAppLang & " is not supported" : $aStrings[$iForceAppLang][$iAppMsg])
    EndIf

EndFunc

; Tests
;   use default @OSLang
MsgBox(0, "Test strings", _GetMsg($_AppMsgHowAreYou))
MsgBox(0, "Test strings", _GetMsg($_AppMsgHopeYouEnjoyTheTrip))
MsgBox(0, "Test strings", _GetMsg($_AppMsgHello))
MsgBox(0, "Test strings", _GetMsg($_AppMsgSeeYouSoon))
;   temporarily force a different applicative language
MsgBox(0, "Test strings", _GetMsg($_AppMsgHowAreYou, $_AppLangIt))
;   forcing was temporary
MsgBox(0, "Test strings", _GetMsg($_AppMsgHowAreYou))
MsgBox(0, "Test strings", _GetMsg($_AppMsgHowAreYou, $_AppLangJa))
;  check that forcing an unsupported language still works
MsgBox(0, "Test strings", _GetMsg($_AppMsgHowAreYou, 143))

Forgot to mention that Google translate goes wrong at times!

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

My example probably does more than you expect[ed], being able to differentiate at will language dialects.

You can safely simplify it (only using the last 2 hex digit of @OSLang which encodes the "généric" language) iff you only need a "coarse" en-CA and fr-CA support or something close. But in case your application might be used elsewhere or be used by people having set their PC to some other/stranger language or demand precise wording --just think en-US, vs. en-GB (colour/color and such) or fr-CA vs. fe-BE (e.g. septante, octante, nonante-- then you may be happy to be able to satisfy linguistic subtlilities.

Outside this linguistic "mess", I recommend you keep the single_function_with_almost_everything_enclosed paradigm, since it avoids pollution of the global space. Don't be afraid of significant initializations as the Static modifier cause them to be run only once at first invokation. Subsequent runs will just skip that part.

If you need the translated phrases to be dynamic (e.g. stored as an easily editable structured text companion file), do keep a similar approach, but then Static won't work as well and you'll have to load data explicitely at first run of the function. Being a big fan of SQLite, I'd recommend switching to a database approach if ever you need to support a very large number of phrases/languages, but you're probably not there.

Don't hesitate to chime if you need to.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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...