Click here to Skip to main content
15,897,273 members
Articles / Programming Languages / C++

Multilingual support for applications

Rate me:
Please Sign up or sign in to vote.
4.33/5 (29 votes)
6 Jun 20055 min read 212.5K   4.3K   114  
An easy solution to add multilingual support to your application in 10 minutes.
// ApplicationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Application.h"
#include "ApplicationDlg.h"

#include <direct.h>				// For getting path to current folder

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

/////////////////////////////////////////////////////////////////////////////
// CApplicationDlg dialog

CApplicationDlg::CApplicationDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CApplicationDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

//=====================================================================

CApplicationDlg::~CApplicationDlg()
{
	// Release the language
	m_pLanguage->ReleaseLanguage();
}

//=====================================================================

void CApplicationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, lblExample, m_lblExample);
}

//=====================================================================

BEGIN_MESSAGE_MAP(CApplicationDlg, CDialog)
	ON_BN_CLICKED(btnLoadLanguage, OnBtnLoadLanguage)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CApplicationDlg message handlers

BOOL CApplicationDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Init language object
	m_pLanguage = CLanguage::Instance();

	// Init language folder and default language
	m_pLanguage->Init("lng", "enlgish");

	// Load default system language
	m_pLanguage->LoadLanguage(GetSystemDefaultLangID());

	// Call the function that is responsible for translating all controls
	TranslateAllControls();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

//=====================================================================

void CApplicationDlg::OnBtnLoadLanguage() 
{
	// Declare variables
	string sDLLPath;
	char map[_MAX_PATH]; 

	// Get working path
    _getcwd(map, _MAX_PATH);
	sDLLPath = map;

	// Set up dll path
	sDLLPath += "\\lng\\dutch.dll";

	// Load the dutch example language
	m_pLanguage->LoadLanguage(sDLLPath, "Dutch");
	
	// Call the function that is responsible for translating all controls
	TranslateAllControls();
}

//=====================================================================

void CApplicationDlg::TranslateAllControls()
{
	// Load all strings for controls
	m_lblExample.SetWindowText(m_pLanguage->GetString(IDS_EXAMPLE).c_str());
}

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
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions