Search code examples
c++internationalizationwxwidgets

wxWidgets translations do not load


I am trying to learn how to internationalize text using wxWidgets. I have wxWidgets 3.1.0 set up and working, and am using Visual C++ 2015 on Windows 10.

I did get this working at one point on accident, then I tried translating more text and it stopped working. I am not sure what I am doing wrong, but I appear to have something misconfigured or am missing a step.

I wrote up a SSCCE to test this out and it still fails, indicating I am probably doing something wrong and it is not a problem with the real project I am working on.

C++ code:

#include <iostream>
#include <wx/wx.h>
class Application : public ::wxApp {
public:
  virtual bool OnInit() override {
    wxMessageBox(_("MyTranslation"), "Test", wxOK | wxICON_INFORMATION);
    return false;
  }
};
wxIMPLEMENT_APP(Application);

In the project root (working directory) I have a folder "en" containing the PO/MO files.

Test.po:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: Test\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-04-25 00:59-0400\n"
"PO-Revision-Date: 2016-04-25 16:36-0400\n"
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Last-Translator: \n"
"Language-Team: \n"
"X-Generator: Poedit 1.8.7\n"

#
msgid "MyTranslation"
msgstr "Hello world!"

Poedit has no problem with this file, and msgfmt emits no warning or errors.

I then run the following command to create the mo file:

msgfmt test.po -otest.mo

When I run the application, I get the message box to pop up but the contents are "MyTranslation" instead of "Hello world!"

enter image description here

Also note that I am using the default locale (en) so for now I am not messing with a wxLocale instance. Eventually I will need to, but I am taking baby steps here - no point in heaping more code onto a broken process unless I need to.

What am I doing wrong, and what do I need to do to fix it?


Solution

  • According to wxWiki you need to initialize language support, just like you'd do with plain gettext.

    Here's your working demo updated:

    #include <iostream>
    #include <wx/wx.h>
    class Application : public ::wxApp {
    public:
      virtual bool OnInit() override {
        wxLocale* locale = new wxLocale(wxLANGUAGE_DEFAULT);
        locale->AddCatalogLookupPathPrefix(wxT("."));
        locale->AddCatalog(wxT("test"));
        wxMessageBox(_("MyTranslation"), "Test", wxOK | wxICON_INFORMATION);
        return false;
      }
    };
    wxIMPLEMENT_APP(Application);