2

I'm An entry level programmer and new to QT too. I have a project where i am hard coding my programing into a three different languages. I am having an issue on figuring out how to hard code Russian instead of letters i get???. my code is broken down into FR.h SP.h German.h Russian.h ->i understand the others use a Latin codec but Russian doesn't English.h example below of how files look-each word is defined

    #ifndef EN_H
#define EN_H

#define TR_PRG_EN                 "Program"
#define TR_HELP_EN                "Help"
#define TR_PASSRW_EN              "Pass: R/W"
#define TR_ALLOWWRITE_EN          "Allow Write"
#define TR_SERIAL_EN              "Serial #"
#define TR_ALLOWREAD_EN           "Allow Read"
#define TR_PRO_EN                 "Product #"
#define TR_MEMSIZE_EN             "Mem. Size"
#define TR_TRIP_EN                "Trip #"
#define TR_RANGE_EN               "Range"
#define TR_DESCR_EN                "Description"
#define TR_LOGSET_EN              "Logger Settings"
#define TR_INTTEMP_EN             "Int. Temp (NTC)"
#define TR_HHIGH_EN               "HHigh(°F)"
#define TR_HIGH_EN                "High(°F)"
#define TR_LOW_EN                 "Low(°F)"
#define TR_LLOW_EN                "LLow(°F)"

with russian.h

    #ifndef RU_H
#define RU_H

#define TR_ENCODING               "utf-8"
#define TR_PRG_RU                 "??????????"
#define TR_HELP_RU                "Help"
#define TR_PASSRW_RU              "?????????"
#define TR_ALLOWWRITE_RU          "????????"
#define TR_SERIAL_RU              "???????? #"
#define TR_ALLOWREAD_RU           "????????? ??????"
#define TR_PRO_RU                 "??????? #"
#define TR_MEMSIZE_RU             "?????? ?????? "
#define TR_TRIP_RU                "??????? #"
#define TR_RANGE_RU               "????????"
#define TR_DESCR_RU                "????????"

i then have Clang.cpp(class,with.h file) file which i put the words into a Qlist>(2d array)->below is an example

const char *trOK[NB_LAN]={TR_OK_EN,TR_OK_FR,TR_OK_SP,TR_OK_IT,TR_OK_RU,TR_OK_GR,TR_OK_PR};
const char *trName[NB_LAN]={TR_NAME_EN,TR_NAME_FR,TR_NAME_SP,TR_NAME_IT,TR_NAME_RU,TR_NAME_GR,TR_NAME_PR};
const char *trDesc[NB_LAN]={TR_DESC_EN,TR_DESC_FR,TR_DESC_SP,TR_DESC_IT,TR_DESC_RU,TR_DESC_GR,TR_DESC_PR};
const char *trLL[NB_LAN]={TR_LL_EN,TR_LL_FR,TR_LL_SP,TR_LL_IT,TR_LL_RU,TR_LL_GR,TR_LL_PR};
const char *trL[NB_LAN]={TR_L_EN,TR_L_FR,TR_L_SP,TR_L_IT,TR_L_RU,TR_L_GR,TR_L_PR};
const char *trH[NB_LAN]={TR_H_EN,TR_H_FR,TR_H_SP,TR_H_IT,TR_H_RU,TR_H_GR,TR_H_PR};
const char *trHH[NB_LAN]={TR_HH_EN,TR_HH_FR,TR_HH_SP,TR_HH_IT,TR_HH_RU,TR_HH_GR,TR_HH_PR};
CLang::CLang()
{
    for(int i=0;i<NB_LAN;i++)
    {

        strTab << QStringList();
        strTab[i] << trPrg[i];            //m_tabLogger
        strTab[i] << trHelp[i];           //m_textBrowserHelp
        strTab[i] << trPassrw[i];         //label_13
        strTab[i] << trAllowWrite[i];     //m_checkBoxLoggerAllowWrite
        strTab[i] << trSerial[i];         //label_2
        strTab[i] << trAllowRead[i];      //m_checkBoxLoggerAllowRead
        strTab[i] << trPro[i];            //label_3
        strTab[i] << trMemSize[i];        //label_5
        strTab[i] << trTrip[i];           //label_6
        strTab[i] << trRange[i];          //m_labelRange
        strTab[i] << trDescr[i];           //label_7
        strTab[i] << trLogSet[i];         //m_groupBoxSettings

then i call it in the main window , so my question how do i make the russian language show the letters another question i have is i mulitple windows(GUI) how do i call these translations on those different windows(GUI)

Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
  • You've chosen the most difficult to maintain method of translating. While QT itself provides the easiest to maintain one! – Jan Hudec Oct 12 '12 at 17:30
  • it want what i wanted to i would have like to used qLing. but it gave me issues in the sense that i had text that was in a resource in html – user1741867 Oct 12 '12 at 18:08
  • Qt provides support for embedding resources into the binary, so you can do that with the `.qm` files if you want to have single binary. – Jan Hudec Oct 14 '12 at 09:12
  • The major problem with string constants like this is maintenance. The strings tend to change a lot during program lifetime. With defines like you have whenever you change something, you must carefully update all languages, because otherwise you won't know what was updated and what not. When you add a language you don't know, you are in trouble. With the QT system the English strings are used as keys in the translation catalogue, so when you change them, they won't match and the linguist tool will easily tell you what needs to be re-translated. – Jan Hudec Oct 14 '12 at 09:37

2 Answers2

3

Almost every framework has a facility to handle translations, including QT: http://doc.qt.digia.com/4.7-snapshot/linguist-programmers.html

You should use that instead of hard coding UTF8 in source files

And to answer why it fails - you are trying to put UTF8 into char. Not gonna work. You need to use QString or similar.

Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • Almost every framework has a facility to handle translations, but the QT one and GNU gettext are best ones around! – Jan Hudec Oct 12 '12 at 17:27
  • 1
    UTF-8 obviously *will* work in char. However it has to be done carefully so that compiler recognizes it's looking at utf-8 and Microsoft Visual C++ (possibly except Visual C++ 2012) insists on screwing up. – Jan Hudec Oct 12 '12 at 17:29
  • Utf-8 encoded files work perfectly with QString::fromUtf8. Я гарантирую это. You just have to setup your IDE correctly. – Lol4t0 Oct 12 '12 at 18:21
  • @Lol4t0: The problem with "setting up your IDE correctly" in case of Visual Studio is, that if you tell the compiler the sources are UTF-8, it will correctly compile wide literals, but convert narrow literals back to cp12??. You can tell it it's cp12?? and actually have utf-8, but than it will be displayed incorrectly and wide literals won't work. Other compilers generally leave narrow strings intact, so utf-8 encoded source compiles with utf-8 encoded literals. – Jan Hudec Oct 14 '12 at 09:09
2

I see 2 solutions. More true-way is to write your defines in this way:

1.Use const char* constants:

const char* TR_HELP_RU = "Справка";

and then in your main.cpp add:

QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForCStrings(codec);

2.Use QString constants:

 const QString TR_HELP_RU = QString("Справка");
NG_
  • 6,895
  • 7
  • 45
  • 67
  • i have tried both ways and still no reults. my trans are stored into a 2d array and then picked from that array – user1741867 Oct 12 '12 at 18:22
  • Check encoding of your source files, and if needed convert to UTF-8, and try again. – NG_ Oct 12 '12 at 18:34
  • still nothing, so i am using option 2 and i run and get this error Clang.cpp:19: error: cannot convert 'const QString' to 'const char*' in initialization – user1741867 Oct 12 '12 at 18:45
  • Of course, you must change your code respectively to chosen type. As for me, it is more applyable to use 1 case, try it. – NG_ Oct 12 '12 at 19:26
  • Visual Studio, if you tell it the source is utf-8, converts it back to cp12?? and you can't tell it to which codepage you want to convert. The only way around I found is to write it as escape sequences. The above would be "\xd0\xa1\xd0\xbf\xd1\x80\xd0\xb0\xd0\xb2\xd0\xba\xd0\xb0". Other compilers don't have this problem, but the asker obviously comes from Windows environment (the `C` prefix for classes is Microsoft thing) – Jan Hudec Oct 14 '12 at 09:23
  • here is the thing i put my trans in a 2d array a list within a list with Qstringlist so anything with QString will create an issueni think i might be wrong – user1741867 Oct 16 '12 at 13:24