gettext define solution

Hello,
i ve defined the error codes and text.
now i want to use gettext to get the error text in dependence from the selected language.

this example works
1
2
3
4
5
6
7
8
9
10
11
12

#define ERR_NBR_1 1
#define ERR_TXT_1 gettext("turn off")
#define ERR_NBR_2 2
#define ERR_TXT_2 gettext("turn on")

int main(void)
{
    cout << ERR_NBR_1 << ERR_TXT_1 << endl;
    //change language
    cout << ERR_NBR_1 << ERR_TXT_1 << endl;
}


but i want something like this

example is not working, because the def is in memory and gettext didnt work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#define ERR_NBR_1 1
#define ERR_TXT_1 gettext("turn off")
#define ERR_NBR_2 2
#define ERR_TXT_2 gettext("turn on")

typedef struct{
 int error;
 string text;
}ER;

vector<ER> errors({
{ERR_NBR_1 , ERR_TXT_1},
{ERR_NBR_2 , ERR_TXT_2},
});

int main(void)
{
    cout << error.at(0).text << endl;
    //change language
    cout << error.at(0).text << endl;
}
Last edited on
I don't see a need for those defines if you're going to have a function, gettext, that will do the translating for you.

Just put the error text in the error struct, fetch the text, pass it to gettext for the translated text.

cout << gettext( error.at(0).text);
Hello cn00by,

Your code does not compile. it is missing the header files. The function "gettext()" is missing.

It makes it hard to understand what yo are trying to do.

lines 12 -14 art trying to set u the vector with a "#define" that calls a function which I hope returns a string, but it can not find the function.

I think line 21 should be "at(1)" not "at(0)".

Having more to work with i could give you better answwer with a program that will actually run.

Hope that helps,

Andy
Hello,
sry that was just an example more like an pseudo code.
i did not test the examples.
Hello cn00by,

I really would not think of that as pseudo code. With just a little more work you would have a working program.

This may not what you may have thought of at first, but it is a working example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <vector>

std::string gettext(std::string line);

#define ERR_NBR_1 1
#define ERR_TXT_1 gettext("turn off")
#define ERR_NBR_2 2
#define ERR_TXT_2 gettext("turn on")

typedef struct
{
	int error;
	std::string text;
}ER;

std::vector<ER> errors({
	{ ERR_NBR_1 , ERR_TXT_1 },
	{ ERR_NBR_2 , ERR_TXT_2 },
});

int main(void)
{
	std::cout << "\n Error # " << errors.at(0).error << " " << errors.at(0).text << std::endl;
	//change language
	std::cout << "\n Error # " << errors.at(1).error << " " << errors.at(1).text << std::endl;

	std::cin.get();

	return 0;
}

std::string gettext(std::string line)
{
	return line;
}


I use a "#define" that is similar to your "ERR_TXT_1" that calls a function to clear the screen.

As Repeater said there is a better way to do this unless the point was to play with #define to see how they work in which case you did well.

Hope that helps,

Andy

Edit:
Last edited on
Hello,
wow Andy thx for the code edit.

the cout message is all the time in english and not in germany.
if i change the code from:
cout << errors.at(1).error to
cout << ERR_TXT_1
it works

if you want that the example work with gettext you need an additioan file for gettext.
the translated code in the mo file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <vector>

std::string gettext(std::string line);

#define ERR_NBR_1 1
#define ERR_TXT_1 gettext("turn off")
#define ERR_NBR_2 2
#define ERR_TXT_2 gettext("turn on")

typedef struct
{
	int error;
	std::string text;
}ER;

std::vector<ER> errors({
	{ ERR_NBR_1 , ERR_TXT_1 },
	{ ERR_NBR_2 , ERR_TXT_2 },
});

int main(void)
{
	std::cout << "\n Error # " << errors.at(0).error << " " << errors.at(0).text << std::endl;
	  /* Change language.  */
          setenv ("LANGUAGE", "de", 1);
           /* Make change known.  */
          {
              extern int  _nl_msg_cat_cntr;
              ++_nl_msg_cat_cntr;
          }
	 std::cout << "\n Error # " << errors.at(1).error << " " << errors.at(1).text << std::endl;

	std::cin.get();

	return 0;
}

std::string gettext(std::string line)
{
	return line;
}
Hello cn00by,

You are welcome. Bitte schön. Glad it made sense to you. I see you have added some new code that I will have to check out because I have not seen this before.

Auf Wiedersehen,

Andy
Topic archived. No new replies allowed.