user1741867
user1741867

Reputation: 21

Hard-coding Russian Translations in qt C++

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)

Upvotes: 2

Views: 1358

Answers (2)

NG_
NG_

Reputation: 7181

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("Справка");

Upvotes: 2

Anycorn
Anycorn

Reputation: 51505

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.

Upvotes: 3

Related Questions