Click here to Skip to main content
15,893,161 members
Articles / Desktop Programming / MFC

Internationalization and Multiple Language Support

Rate me:
Please Sign up or sign in to vote.
4.65/5 (44 votes)
14 Dec 20025 min read 1.1M   4.3K   129  
Changing the language of a Windows program on the fly using resource only DLLs.
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "MultiLang.h"

#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_SETFOCUS()
	ON_COMMAND(ID_LANGUAGE_DEFAULT, OnLanguageDefault)
	ON_COMMAND(ID_LANGUAGE_ENGLISH, OnLanguageEnglish)
	ON_COMMAND(ID_LANGUAGE_GERMAN, OnLanguageGerman)
	ON_COMMAND(ID_LANGUAGE_FRENCH, OnLanguageFrench)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	m_bLoadedDLL	= FALSE;

	m_pMenuNew		= NULL;
}

CMainFrame::~CMainFrame()
{
		// Just tidy up
	if(m_pMenuNew)
		delete m_pMenuNew;
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	// create a view to occupy the client area of the frame
	if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
		CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
	{
		TRACE0("Failed to create view window\n");
		return -1;
	}
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
	cs.lpszClass = AfxRegisterWndClass(0);
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnSetFocus(CWnd* /*pOldWnd*/)
{
	// forward focus to the view window
	m_wndView.SetFocus();
}

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
	// let the view have first crack at the command
	if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
		return TRUE;

	// otherwise, do default handling
	return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}


void CMainFrame::OnLanguageDefault() 
{
	SetLanguage(Default);
}

void CMainFrame::OnLanguageEnglish() 
{
	SetLanguage(English);
}

void CMainFrame::OnLanguageFrench() 
{
	SetLanguage(French);	
}

void CMainFrame::OnLanguageGerman() 
{
	SetLanguage(German);	
}
/**
 *
 * Simple error report to somewhere
 *
 * szMessage	[IN]	Our message
 * dwLastErro	[IN]	The error number
 *
 */
void CMainFrame::ReportError(LPCTSTR szMessage, DWORD dwLastError)
{
	CString		strError = _T("");

	strError.Format(_T("%s %d\n"), szMessage, dwLastError);
	AfxMessageBox(strError);
}

/**
 *
 * Change the language
 * 
 * nLanguage	[IN]	The new language
 *
 */
void CMainFrame::SetLanguage(LANGUAGES nLanguage)
{
	CString		strBuffer	= _T("");

	HINSTANCE	hInst			= NULL;

	LCID			lcid			= NULL;

		// Free the resource Libray, we check one is loaded because
		// of problems with 98, not needed NT/XP
	if(m_bLoadedDLL)
		{
		if(!FreeLibrary(AfxGetResourceHandle()))
			ReportError(_T("FreeLibrary(AfxGetResourceHandle())"), GetLastError());

			// Reset to English i.e. the default language
		AfxSetResourceHandle(AfxGetInstanceHandle());
		}

	if(nLanguage == Default)
		{
		lcid= GetThreadLocale();
			// We only want the low order 8 bits
		lcid &= 0xFF;
			// Evaluate what the locale is and map onto the languages we have
			// 
		switch(lcid)
			{
			default:
			case LANG_ENGLISH:
				nLanguage = English;
				break;

			case LANG_FRENCH:
				nLanguage = French;
				break;

			case LANG_GERMAN:
				nLanguage = German;
				break;
			}
		}

		// Load the relevant resource DLL
	switch(nLanguage)
		{
		case French:
			hInst = LoadLibrary(_T("LangFRA.dll"));
			break;

		case German:
			hInst = LoadLibrary(_T("LangGER.dll"));
			break;
		}

		// If we have succesfully loaded a resource DLL, use it, otherwise
		// load the resources from the EXE
	if(hInst)
		{
		AfxSetResourceHandle(hInst);
		m_bLoadedDLL = TRUE;
		}
	else
		{
		AfxSetResourceHandle(AfxGetInstanceHandle());
		m_bLoadedDLL = FALSE;
		}

		// we MUST get a handle to the menu in the Mainframe
		// YOU may need to use a pointer to the Frame:-

		// CMainFrame* pFrame = (CMainFrame *) AfxGetApp()->m_pMainWnd;
		// i.e.	pFrame->GetMenu()
		//			pFrame->m_hMenu()
		//
		// Or without using CMainFrame, using Win32 calls
		//		HMENU hMenu= ::GetMenu(AfxGetApp()->m_pMainWnd->GetSafeHwnd());
		//		CMenu *pMenuCurrent = CMenu::FromHandle(hMenu);

	CMenu *pMenuCurrent = GetMenu();

		// Create a new menu instance, we need to use this in SetMenu
	m_pMenuNew = new CMenu;

		// has the menu changed?
		// m_hMenuDefault is the default menu resource for this frame see AFXWIN.H
	if(pMenuCurrent->m_hMenu != m_hMenuDefault)
		{
			// Destroy the "New" menu and delete the resource
			// We, after all created it!
		pMenuCurrent->DestroyMenu();
		delete pMenuCurrent;
		}

		// Load our new resource, menu and 
	m_pMenuNew->LoadMenu(IDR_MAINFRAME);
		// Displauy the new menu
	SetMenu(m_pMenuNew);

		// Update any other text strings that are displayed
		// e.g. The Status bar
	strBuffer.LoadString(AFX_IDS_IDLEMESSAGE);
	m_wndStatusBar.SetPaneText(0, strBuffer);

		// Our view, force a repaint to update the text
	m_wndView.Invalidate();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
Ted wrote his first program c. 1972, and has been writing software professionally since 1975. For his sins he has been a user of Visual C++ from version 1.0 days.

On the odd days that he leaves his PC he is into walking in the Peak District, but tends to get waylaid at the lunch time pub.

Comments and Discussions