[Home]  [Prev]  [Next]    A guide, a tutorial for developing well-designed cross-platform applications

8. Internationalization

Sooner or later you are confronted with users asking for localized version of your application, at least if it's successful. So it is better plan to implement international support right from the beginning.


8.1 Language support

The base for any language support is to mark each text a user ever sees for translation even if you don't plan or provide any. This gives your users a chance to start their own translation which might flow back to you. When you use text in your application immediately decide if a user ever will see it or not.

There are three different kind of text:

  1. Text a user sees and has to interpret. This is the most common case of text in an application. This text has to be marked for translation.
  2. Text which is a name or a common notion. Never translate names unless there is a common translation of it. Text that is used only internally in your application and a user normally doesn't see (like debug information's). This text should not be translated but marked for multi byte support.
  3. Text has to be plain ASCII. This almost only the case for text which is used in the internal program flow (i.e. filenames).
Mark text for translation with the macro _("..."), for text not to be translated use _T("...").

The localization code within wxWidgets tries hard to discover the localization files (catalogs) but it might still not work. In this case handle it like the help files and use the application directory (g_appDirectory) in the preferences (see also the Application location description).

Sample code

#include <wx/intl.h>     // internationalization

class App: public wxApp {
    ...
private:
    wxLocale m_locale;
    ...
}

bool App::OnInit () {
    ...

    wxString appPath = g_appDirectory; // gets the configured application directory
    if (appPath.IsEmpty()) appPath = wxFileName(argv[0]).GetPath (wxPATH_GET_VOLUME);
    wxString appName = wxFileName(argv[0]).GetName();
#ifdef __WXMAC__
    appPath += wxFileName::GetPathSeparator() + appName + _T(".app/Contents/SharedSupport"));
#endif

    ...
    m_locale.Init();
    m_locale.AddCatalogLookupPathPrefix (appPath);
    m_locale.AddCatalog (appName);

    ...
}

Note: The application directory (g_appDirectory) can be configured in the preferences, the code can looked up in the demo application.

String samples

// no translation, using _T("...")
const wxString APP_MAINT = _T("Otto Wyss");
const wxString APP_VERSION = _T("1.0.3");
const wxString APP_LICENCE = _T("wxWindows");
const wxString APP_COPYRIGTH = _T("(C) 2004 Otto Wyss");

   // will be translated, using _("...")
    aboutinfo->Add (new wxStaticText(this, -1, _("Written by: ")), 0, wxALIGN_LEFT);
    aboutinfo->Add (new wxStaticText(this, -1, APP_MAINT), 1, wxEXPAND | wxALIGN_LEFT);
    aboutinfo->Add (new wxStaticText(this, -1, _("Version: ")), 0, wxALIGN_LEFT);
    aboutinfo->Add (new wxStaticText(this, -1, APP_VERSION), 1, wxEXPAND | wxALIGN_LEFT);
    aboutinfo->Add (new wxStaticText(this, -1, _("Licence type: ")), 0, wxALIGN_LEFT);
    aboutinfo->Add (new wxStaticText(this, -1, APP_LICENCE), 1, wxEXPAND | wxALIGN_LEFT);
    aboutinfo->Add (new wxStaticText(this, -1, _("Copyright: ")), 0, wxALIGN_LEFT);
    aboutinfo->Add (new wxStaticText(this, -1, APP_COPYRIGTH), 1, wxEXPAND | wxALIGN_LEFT);


8.2 Translations