Revision 4fcc2b1a-8dfe-4c8a-a346-13e4a144227b - Stack Overflow

I have the following [gettext][1] .po file, which has been translated from a .pot file. I am working on a Linux system ([openSUSE][2] if it matters), running gettext 0.17.

 # 
 # <[email protected]>, 2011
 # transer <[email protected]>, 2011
 msgid ""
 msgstr ""
 "Project-Id-Version: transtest\n"
 "Report-Msgid-Bugs-To: \n"
 "POT-Creation-Date: 2011年05月24日 22:47+0100\n"
 "PO-Revision-Date: 2011年05月30日 23:03+0100\n"
 "Last-Translator: \n"
 "Language-Team: German (Germany)\n"
 "MIME-Version: 1.0\n"
 "Content-Type: text/plain; charset=UTF-8\n"
 "Content-Transfer-Encoding: 8bit\n"
 "Language: de_DE\n"
 "Plural-Forms: nplurals=2; plural=(n != 1)\n"
 
 #: transtest.cpp:12
 msgid "Min Size"
 msgstr "Min Größe"

Now, when I create the .mo file via 

 msgfmt -c transtest_de_DE.po -o transtest.mo

I then check the encoding with the "file" command,

 file --mime transtest_de_DE.po
 transtest_de_DE.po: text/x-po; charset=utf-8

and then install it to my locale folder and run the program after exporting `LANG` and `LC_CTYPE`, I end up with garbage where the two non-ASCII chars are. 

If I set my terminal encoding to [ISO-8859-2][3], rather than [UTF-8][4], then I see the two characters correctly.

Looking inside the generated .mo file with a text editor the file appears to be in UTF-8 as well (I can see the symbols if I set my editor encoding to UTF-8).

The program is very simple, and it looks like so:

 #include <iostream>
 #include <locale>
 const char *PROGRAM_NAME="transtest";
 
 using namespace std;
 
 int main()
 {
 	setlocale (LC_ALL, "");
 	bindtextdomain( PROGRAM_NAME, "/usr/share/locale" );
 	textdomain( PROGRAM_NAME );
 	cerr << gettext("Min Size") << endl;
 }

I am installing the .mo file to `/usr/share/locale/de_DE/LC_MESSAGES/transstest.mo`, and I have exported `LC_CTYPE` and `LANG` as "de_DE". 

 $ echo $LC_CTYPE; echo $LANG
 de_DE
 de_DE

Where am I going wrong? Why is gettext giving me the wrong encoding (ISO-8859-2) for my strings, rather than the requested (in the .po file) UTF-8?


###Edit:

The solution was in Stack&nbsp;Overflow question *[Can't make (UTF-8) traditional Chinese character to work in PHP gettext extension (.po and .mo files created in poEdit)][5]* and it appears that I needed to explicitly call 

 bind_textdomain_codeset(PROGRAM_NAME, "utf-8");

The final program looks like so:

 #include <iostream>
 #include <locale>
 const char *PROGRAM_NAME="transtest";
 
 using namespace std;
 
 int main()
 {
 	setlocale (LC_ALL, "");
 	bindtextdomain( PROGRAM_NAME, "/usr/share/locale" );
 	bind_textdomain_codeset(PROGRAM_NAME, "utf-8");
 	textdomain( PROGRAM_NAME );
 	cerr << gettext("Min Size") << endl;
 }

No changes to any of my gettext files were needed.

 [1]: http://en.wikipedia.org/wiki/Gettext
 [2]: http://en.wikipedia.org/wiki/OpenSUSE
 [3]: http://en.wikipedia.org/wiki/ISO/IEC_8859-2
 [4]: http://en.wikipedia.org/wiki/UTF-8
 [5]: https://stackoverflow.com/questions/2264740/cant-make-utf-8-traditional-chinese-character-to-work-in-php-gettext-extension

AltStyle によって変換されたページ (->オリジナル) /