Changing labels on Mac menu bar
Posted: Wed Oct 24, 2012 1:36 pm
I have to translate label in standard application menu bar, on MAC OS X. Using FindItem doesn't works, because menu items are in different menu than those of main wxMenuBar of my frame. The menu items are added by wxMenuBar::Init() function (in wxWidgets sources). I don't want to subclass just to change labels but they are added "by code" and are not good for internationalization. Here's the wxWidgets code:
In Italy, we need the word "Nascondi" instead of "Hide", "Nascondi tutte" instead of "Hide others" and so on. I think in other countries they have the same problem.
So, it would be good to change this fixed strings ("Hide" etc) in wxWidgets sources to use something like string resources that can be internationalized. For now i've patched somehow the code, this way:
This is a very bad solution... but it works. It requires to change menu.h header, to make m_rootMenu a public member... and this is the worst working solution that i've found in all my life 
Code: Select all
hideLabel = wxString::Format(_("Hide %s"), wxTheApp ? wxTheApp->GetAppDisplayName() : _("Application"));
m_appleMenu->Append( wxID_OSX_HIDE, hideLabel + "\tCtrl+H" );
m_appleMenu->Append( wxID_OSX_HIDEOTHERS, _("Hide Others")+"\tAlt+Ctrl+H" );
m_appleMenu->Append( wxID_OSX_SHOWALL, _("Show All") );
So, it would be good to change this fixed strings ("Hide" etc) in wxWidgets sources to use something like string resources that can be internationalized. For now i've patched somehow the code, this way:
Code: Select all
void MainDialog::OnActivate( wxActivateEvent& event )
{
#if defined(__WXMAC__) && !wxOSX_USE_CARBON
if(wxMenu * pAppleMenu=GetMenuBar()->m_rootMenu)
{
typedef struct
{
int idItem;
const wxChar *newText;
} tagTranslateMenuItem;
tagTranslateMenuItem aryTranslate[] = { { wxID_OSX_HIDE, wxT("Nascondi Ammortamenti\tCtrl+H") },
{ wxID_OSX_HIDEOTHERS, wxT("Nascondi altre\tAlt+Ctrl+H")} ,
{ wxID_OSX_SHOWALL, wxT("Mostra tutte") },
{ wxApp::s_macExitMenuItemId, wxT("Esci da Ammortamenti\tCtrl+Q") }
};
for(size_t i=0; i<ARRAY_COUNT(aryTranslate); i++)
{
// GetMenuBar()->SetLabel(aryTranslate[i].idItem, aryTranslate[i].newText);
if(wxMenuItem *pMenuItem = pAppleMenu->FindItem(aryTranslate[i].idItem))
{
pMenuItem->SetItemLabel(aryTranslate[i].newText);
}
}
}
#endif
event.Skip();
}
