Introduction
If you ever tried to distribute an international (multilingual) application
you know maybe the obstacles to translate all the texts.
Some dialog controls like those ones of the MessageBox
can't be transformed
into another language, they are part of the Windows installed on your machine.
So the 'Cancel' button of a German Windows OS will always be "Abbrechen" or
"Annuler" in a French Windows OS.
However, this is not the problem, but the entire rest of your application should
be in your customers' language. Title bar, menus, tooltips, dialogs and its controls
can get accessed and modified at runtime.
Print preview problem
But the print preview gave me headache, because normally you have to define
at compilation time the printing resources you'd like to link.
e.g.
#include "l.deu\afxprint.rc"
#include "l.fra\afxprint.rc"
#include "l.ita\afxprint.rc"
After linking one of the indicated examples, the preview buttons seem to be
invariant. So, I consulted MSDN and figured out, they are accessible in a
certain way.
Solution
Derive from CPreviewView
your own class, where you should have access to the
desired buttons. Then make sure the framework calls your derived class.
void CLangPreviewView::OnActivateView(BOOL bActivate, CView*, CView*)
{
if (bActivate)
{
if (m_pToolBar) {
m_pToolBar->GetDlgItem(AFX_ID_PREVIEW_PRINT)
->SetWindowText(CDic::GetText(_T("Preview"),
_T("PRINT"), _T("&Print...")));
m_pToolBar->GetDlgItem(AFX_ID_PREVIEW_NEXT)
->SetWindowText(CDic::GetText(_T("Preview"),
_T("NEXT"), _T("&Next Page")));
m_pToolBar->GetDlgItem(AFX_ID_PREVIEW_PREV)
->SetWindowText(CDic::GetText(_T("Preview"),
_T("PREV"), _T("Pre&v Page")));
SetWindowText(CDic::GetText(_T("Preview"),
_T("NUMPAGE"), _T("")));
m_pToolBar->GetDlgItem(AFX_ID_PREVIEW_ZOOMIN)
->SetWindowText(CDic::GetText(_T("Preview"),
_T("ZOOMIN"), _T("Zoom &In")));
m_pToolBar->GetDlgItem(AFX_ID_PREVIEW_ZOOMOUT)->
SetWindowText(CDic::GetText(_T("Preview"),
_T("ZOOMOUT"), _T("Zoom &Out")));
m_pToolBar->GetDlgItem(AFX_ID_PREVIEW_CLOSE) ->
SetWindowText(CDic::GetText(_T("Preview"),
_T("CLOSE"), _T("&Close")));
}
}
}
void CLangPreviewView::OnUpdateNumPageChange(CCmdUI* pCmdUI)
{
CPreviewView::OnUpdateNumPageChange(pCmdUI);
UINT nPages = (m_nZoomState == ZOOM_OUT ? m_nPages : m_nZoomOutPages);
if (nPages == 1)
pCmdUI->SetText(CDic::GetText(_T("Preview"),
_T("TWOPAGE"), _T("&Two Page")));
else
pCmdUI->SetText(CDic::GetText(_T("Preview"),
_T("ONEPAGE"), _T("&One Page")));
}
Steps
- Add "LangPreviewView.cpp/.h" to your application
- Add "Dic.cpp/.h" to your application (a dictionary class, see below)
- Call "CDic::LoadLanguage(LPCTSTR)" to load/reload application language specific texts (e.g. in CYourAppDocument::OnNewDocument)
CDic::LoadLanguage(_T("Lang_de.txt"));
Override the OnFilePrintPreview
function of your view class to call CPreviewView
ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
void CYourAppView::OnFilePrintPreview()
{
CPrintPreviewState* pState = new CPrintPreviewState;
if (!DoPrintPreview(AFX_IDD_PREVIEW_TOOLBAR, this,
RUNTIME_CLASS(CYourAppView), pState))
{
TRACE0("Error: DoPrintPreview failed.\n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
delete pState;
}
}
Modify the OnPreparePrinting
function of your view class to enable localized (language depending) status bar page display
BOOL CYourAppView::OnPreparePrinting(CPrintInfo* pInfo)
{
BOOL bReturn = DoPreparePrinting(pInfo);
pInfo->m_strPageDesc = CDic::GetText(_T("Preview"),
_T("PAGEDESC"), _T("Page %u\nPages %u-%u\n"), true);
return bReturn;
}
(I prefer collecting localized texts in several external text files to
having everything in internal stringtable resources. If there is a new language added
to your application, you don't have to recompile the entire project or part of it,
simply add the language file to your distribution.)
Now, we are ready to test the application.
Test
Select one of the five languages by clicking the flag of the toolbar and then
start the print preview.
Preview in German
Preview in French
Preview in Italian
Preview in Spanish
Point of interest
CDic
There is a handy class to retrieve localized texts from a file.
Its member functions are static, therefore they can be called from almost
everywhere.
bool CDic::LoadLanguage(LPCTSTR strFilepath)
CString CDic::GetText(LPCTSTR strSection, LPCTSTR strID,
LPCTSTR strDefaultText, bool bTransform)
History
- 1.00 04-July-2004 Initial version
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.