NO

Author Topic: How to Implement Multiple Languages  (Read 4503 times)

PhilG57

  • Guest
How to Implement Multiple Languages
« on: February 15, 2016, 04:30:41 PM »
I've been thinking about providing multiple language support in a Windows program.  Rather than provide a separately compiled version for each supported language, I'd like to offer a menu item to select from among a couple of languages. 

My question is how best for the program to respond in the selected language.  Do I place all program messages in stringtables with different versions for each supported language?  Then at program execution time, LoadString the appropriate message in the selected language and display that string?  Most of my user communication is done writing to a ListBox during program executon and to a disk file for later program behavior review.

I've also looked at the message compiler and the ".mc" file but believe that approach only applies to FormatMessage.

Also, if there is written or online help in this area, I would greatly appreciate the reference.

Many thanks.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: How to Implement Multiple Languages
« Reply #1 on: February 16, 2016, 09:20:06 AM »
With resources an another language is possible to insert afterwards.
Or with language dll.

Attachment is simple project with runtime language change.

In compiled C ?
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
char *aMsgEN[] = {"MsgEN1\n","MsgEN2\n"};
char *aMsgGE[] = {"MsgGE1\n","MsgGE2\n"};
char **aMsg = aMsgEN;
int main(int argc, char **argv)
{
char *lang = getenv("LANG");
printf(aMsg[0]);
printf("%s %04X\n", lang, *(unsigned short*)lang);
//if (lang[0] == 'G' && lang[1] == 'E') {
if (*(unsigned short*)lang == *(unsigned short*)"GE") {
aMsg = aMsgGE;
printf(aMsg[0]);
}
return 0;
}
strings in dll in console program? exercise?
« Last Edit: February 20, 2016, 03:00:11 AM by TimoVJL »
May the source be with you

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: How to Implement Multiple Languages
« Reply #2 on: February 16, 2016, 10:28:09 AM »
Great project, Timo, I didn't know that was possible!

Grincheux

  • Guest
Re: How to Implement Multiple Languages
« Reply #3 on: February 17, 2016, 12:57:43 PM »
That's exactly what I was searching. Very usefull Big Thanks.

AB

  • Guest
Re: How to Implement Multiple Languages
« Reply #4 on: January 13, 2017, 09:44:46 PM »
Sorry for bumping but I have exactly the same problem so I thought making a new thread would be wasteful.

With inspiration from TimoVJL's work in WSDIMenuExLang2.zip, I have my translated "duplicates" ready in the Resource. However unlike him, I use the following functions instead of SetThreadLocale():
Unfortunately I am stuck: the user is supposed to select the language in a child dialog, and I thought that when the selection is ready the child could SendMessage() to the parent, so that the parent could update its UI. The problem is that the parent never receives the user message from the child, and therefore I cannot update the parent's menu.

I also cannot update the parent dialog's menu from within the child dialog's procedure (using either GetParent() or GetAncestor()).

So to simplify my thoughts and request for help:
  • how to make it so that the child dialog can message the parent dialog to update its UI, or
  • how to make it so that the child can directly change the parent, or
  • is there another method to select Language from a child dialog?

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: How to Implement Multiple Languages
« Reply #5 on: January 14, 2017, 03:07:28 AM »
Your method should work. If it doesn't, debug the problem:
- create a console with AllocConsole
- after SendMessage, use GetLastError and prinf() etc to check what went wrong
- you may try PostMessage instead, sometimes SendMessage gets "access denied".

For comparison: SmallEditor
« Last Edit: January 14, 2017, 03:09:09 AM by jj2007 »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: How to Implement Multiple Languages
« Reply #6 on: January 14, 2017, 10:17:05 AM »
Use global HWND variable for parent.
Both SendMessage and PostMessage should work.
May the source be with you

AB

  • Guest
Re: How to Implement Multiple Languages
« Reply #7 on: January 14, 2017, 04:59:25 PM »
Thanks for the help so far! The suggested global HWND works fine. Now I have another question: how do I properly update the main dialog itself?

To clarify my question: I am translating the main dialog as well, so when processing the MSG_CHANGE_LANG message in MainDlgProc(), I do the following things:
  • call EndDialog(hwndDlg, 0);
  • get the translated dialog template from Resource
  • call ResizableDialogBoxIndirect(/* ... */); // Thanks Pelle
My problem is: the new dialog doesn't receive the WM_INITDIALOG message, so its controls are not set up by my "init" functions, as needed.

EDIT: I was wrong about the strikeout above and the fault lied with my "init" functions. Still there remains the question if the above approach is safe (even though it clearly works), and if there are better ways to do this.
« Last Edit: January 14, 2017, 05:09:33 PM by AB »