1

I was used boost::locale to make a multilanguage exe, but it doesn't work. The exe always output "Hello World". How can it output "您好"?

I used the example code from http://www.boost.org/doc/libs/1_53_0/libs/locale/doc/html/messages_formatting.html

    #include <boost/locale.hpp>
    #include <iostream>

    using namespace std;
    using namespace boost::locale;

    int main()
    {
        generator gen;

        // Specify location of dictionaries
        gen.add_messages_path(".");
        gen.add_messages_domain("hello");

        // Generate locales and imbue them to iostream
        locale::global(gen(""));
        cout.imbue(locale());

        // Display a message using current system locale
        cout << translate("Hello World") << endl;
    }

And make a po file and a mo file. Po file is:

# 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: messages\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-04-26 20:50+0800\n"
"PO-Revision-Date: 2013-04-26 21:44+0800\n"
"Last-Translator: \n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.5.5\n"
"X-Poedit-SourceCharset: UTF-8\n"

#: main.cpp:21
msgid "Hello World"
msgstr "您好"
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
Alberl
  • 43
  • 1
  • 6

1 Answers1

1

Im beginning with boost_locale too, here is how i make it work...

  1. First make sure your .mo file is named exactly as the domain your refering to, in this case hello.mo

  2. Put the .mo file into the correct file structure, for example if your trying to translate to spanish this would be ./es_ES/LC_MESSAGES/hello.mo

  3. Make sure you instantiate the global locale like this, locale::global(gen("es_ES.UTF-8"));

Hope this helps.

roboli
  • 1,418
  • 20
  • 24
  • Thanks, you are right. I have solved it, I looked the source code of cppcms. – Alberl Jun 08 '13 at 12:23
  • Also, when defining a locale::global(gen("es_ES.UTF-8")); the charset used by the hello.mo file should match. In my case it was locale::global(gen("es_ES.ISO-8859-1")); – vengy Mar 12 '21 at 17:26
  • I do exactly the same, but nothing is translated... What might be another reason? – Dmitry Jan 18 '22 at 13:03