Skip to content
⌘ NSIS Forum Archive

function for showing diff translated strings

9 posts

coco_vc#

function for showing diff translated strings

Hi all,

I have in .onInit to do different checks and in case smth fails I have to show MsgBoxes in the specific language. For this I do the ${Switch} $LANGUAGE trick. So I have several times the same code, for instance:


${Switch} $LANGUAGE
${Case} ${LANG_ENGLISH}
StrCpy $msg "${OLDER_VERSION_ALREADY_INSTALLED_ENGLISH}"
${Break}
${Case} ${LANG_FRENCH}
StrCpy $msg "${OLDER_VERSION_ALREADY_INSTALLED_FRENCH}"
${Break}
${Default} ; if we forget one language, show the english msgbox
StrCpy $msg "${OLDER_VERSION_ALREADY_INSTALLED_ENGLISH}"
${Break}
${EndSwitch}
and


${Switch} $LANGUAGE
${Case} ${LANG_ENGLISH}
StrCpy $msg "${NEED_TO_BE_ADMIN_LANG_ENGLISH}"
${Break}
${Case} ${LANG_FRENCH}
StrCpy $msg "${NEED_TO_BE_ADMIN_LANG_FRENCH}"
${Break}
${Default} ; if we forget one language, show the english msgbox
StrCpy $msg "${NEED_TO_BE_ADMIN_LANG_ENGLISH}"
${Break}
${EndSwitch}
and so on.

Those chunks of code look the same with the exception of the define which are in the above sample: OLDER_VERSION_ALREADY_INSTALLED_ENGLISH, OLDER_VERSION_ALREADY_INSTALLED_FRENCH, NEED_TO_BE_ADMIN_LANG_ENGLISH, NEED_TO_BE_ADMIN_LANG_FRENCH

Is there a way to create smth like a function and as a parameter to pass for instance OLDER_VERSION_ALREADY_INSTALLED_ or NEED_TO_BE_ADMIN_LANG_ so that I don't have the same code written over and over again? (and in this way for a new language I just have to add one line in the function not to change in each piece of code)

Thx,
Viv
Afrow UK#
!macro OLDER_VERSION_ALREADY_INSTALLED Language
${Case} ${LANG_${Language}}
StrCpy $msg "${OLDER_VERSION_ALREADY_INSTALLED_${Language}}"
${Break}
!macroend

!macro NEED_TO_BE_ADMIN Language
${Case} ${LANG_${Language}}
StrCpy $msg "${NEED_TO_BE_ADMIN_LANG_${Language}}"
${Break}
!macroend
...
${Switch} $LANGUAGE
!insertmacro OLDER_VERSION_ALREADY_INSTALLED ENGLISH
!insertmacro OLDER_VERSION_ALREADY_INSTALLED FRENCH
${Default} ; if we forget one language, show the english msgbox
StrCpy $msg "${OLDER_VERSION_ALREADY_INSTALLED_ENGLISH}"
${Break}
${EndSwitch}
...

${Switch} $LANGUAGE
!insertmacro NEED_TO_BE_ADMIN ENGLISH
!insertmacro NEED_TO_BE_ADMIN FRENCH
${Default} ; if we forget one language, show the english msgbox
StrCpy $msg "${NEED_TO_BE_ADMIN_LANG_ENGLISH}"
${Break}
${EndSwitch}
-Stu
coco_vc#
Hi Stu,

Thanks for your reply. Still in your sample whenever I add a new language I have to add a line in _each_ chunk of code, in your sample in the last 2 chunks like:


!insertmacro OLDER_VERSION_ALREADY_INSTALLED NEW_LANGUAGE
respectivelly


!insertmacro NEED_TO_BE_ADMIN NEW_LANGUAGE
which is what I would like to avoid.
The algorithm I was thinking about would look smth like:


function call_MsgBox(a_define)
${Switch} $LANGUAGE
${Case} ${LANG_ENGLISH}
StrCpy $msg "${a_define_ENGLISH}"
${Break}
${Case} ${LANG_FRENCH}
StrCpy $msg "${a_define_FRENCH}"
${Break}
${Default} ; if we forget one language, show the english msgbox
StrCpy $msg "${a_define_ENGLISH}"
${Break}
${EndSwitch}
MessageBox MB_ICONEXCLAMATION|MB_OK "$msg"
end function
and when calling the function would be smth like:


call_MsgBox(OLDER_VERSION_ALREADY_INSTALLED)
and


call_MsgBox(NEED_TO_BE_ADMIN)

which means whenever a new language is added the _only_ place I would have to change is in the first chunk of code by just adding:


${Case} ${LANG_NEW_LANGUAGE}
StrCpy $msg "${{a_define_NEW_LANGUAGE}"
${Break}
Is smth like this possible?

Thx,
Viv
coco_vc#
I didn't get any answer to this one. Does this mean this is not possible? or does this mean that no NSIS-guru has yet read this post? or maybe I wasn't clear enough with my question?

Thx,
Viv
coco_vc#
Thx guys for your answer.

> but you can create a macro from your switch.
Does this mean I would have to change only in one place when a new language is added? Or the solution you are talking about is similar with Stu's suggestion? (which btw is nice, but I am trying to get rid of the fact that I have to change in several similar places when I add a new language)

Thx,
Viv
kichik#
Yes, one place. It's a combination of Afrow's macros and your pseudo-function. Instead of your function, you'll have:
!macro lang_define var name
${Switch} $LANGUAGE
${Case} ${LANG_ENGLISH}
StrCpy ${var} "${${name}_ENGLISH}"
${Break}
${Case} ${LANG_FRENCH}
StrCpy ${var} "${${name}_FRENCH}"
${Break}
${Default} ; if we forget one language, show the english msgbox
StrCpy ${var} "${${name}_ENGLISH}"
${Break}
${EndSwitch}
!macroend
Then you can use it like this:
!insertmacro lang_define $0 somelangdefine
MessageBox MB_OK $0
You only have to a few lines for each new language, all in one place.