GNU gettext & iconv & mingw
От: Antrix  
Дата: 15.01.09 11:45
Оценка:
Почему-то bind_textdomain_codeset не изминяет кодировку, вот когда я пишу так:

#include <iconv.h>
#include <cassert>
#include <cerrno>
#include <string>
#include <stdexcept>
#include <iostream>


#include "windows.h"


#include <libintl.h>
#include <locale.h>
#include <winuser.h>

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
    string package("test");    
    SetEnvironmentVariable("LANG", "uk_UK");
    putenv("LANG=uk_UK");
    cout << setlocale(LC_ALL, "") << endl;

    cout << bindtextdomain(package.c_str(), "./locale") << endl;
    cout << textdomain(package.c_str()) << endl;
    cout << gettext (package.c_str()) << endl;

    cout << gettext("Hello, world!") << endl;
    cout << "codeset = " << bind_textdomain_codeset(package.c_str(), "UTF-8") << endl;
    cout << gettext("Hello, world!") << endl;

    return 0;
}

То в консоли выводится абракадабра, а если я использую явно ф-и iconv (хотя bind_textdomain_codeset сам его использует) то всенормально, вот работающий код:

#include <iconv.h>
#include <cassert>
#include <cerrno>
#include <string>
#include <stdexcept>
#include <iostream>


#include "windows.h"


#include <libintl.h>
#include <locale.h>
#include <winuser.h>

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
    string package("test");

    SetEnvironmentVariable("LANG", "uk_UK");
    putenv("LANG=uk_UK");
    
    cout << setlocale(LC_ALL, "Russian_Russia") << endl;

    cout << bindtextdomain(package.c_str(), "./locale") << endl;
    
    cout << textdomain(package.c_str()) << endl;
    
    cout << gettext (package.c_str()) << endl;

    

      iconv_t cd;
      size_t k, f, t;
      int se;
      const char *code = gettext("Hello, world!");
      const char* in = code;
      char buf[100];
      char* out = buf;

      cd = iconv_open("cp1251", "UTF-8");
      if( cd == (iconv_t)(-1) )
           cout << "iconv return" << cd << endl;
      f = strlen(code);
      t = sizeof buf;
      memset( &buf, 0, sizeof buf );
      errno = 0;
      k = iconv(cd, &in, &f, &out, &t);
      se = errno;
      printf( "\n\n\nconverted: %u,error=%d\n", (unsigned) k, se );
      printf("string: %s\n", buf);

  iconv_close(cd);


    return 0;
}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.