如何为C++程序添加多语言支持?我想让用户在打开应用程序时在两种语言之间进行选择。没有任何外部库的最简单方法是什么?
回复我的评论,"你可以制作一个字典,其中它的键是一个表示单词的枚举,然后值可以是一个包含该语言和该语言中实际字符串的结构数组。示例:(伪代码(字典:-WORD1_ENUM=>structure[LANGUAGE1_ENUM,WORD_IN_LANGUAGE1]、structure[LANGUAGE2_ENUM,WORD_IN_LANGUAGE2]";,你可以制作一本字典,根据选定的语言检索你的世界。
#include <initializer_list>
#include <string>
#include <vector>
#include <iostream>
enum STRINGS
{
HELLO_WORLD,
GOODBYE_WORLD
};
#define SELECTED_LANGUAGE SLOVENIAN
#define NOT_FOUND_STR "Not Found"
enum LANGUAGE
{
ENGLISH,
SLOVENIAN
};
struct DICTIONARY_VAL
{
LANGUAGE lang;
std::string text;
};
struct DICTIONARY_BLOCK_t{
STRINGS key;
std::vector<DICTIONARY_VAL> values;
};
class DICTIONARY_t
{
private:
std::vector<DICTIONARY_BLOCK_t> data;
public:
DICTIONARY_t (std::initializer_list<DICTIONARY_BLOCK_t> var)
{
for (DICTIONARY_BLOCK_t val : var)
{
data.push_back(val);
}
}
std::string get_value(STRINGS key, LANGUAGE lang)
{
std::string l_ret = NOT_FOUND_STR;
for (uint32_t i = 0; i < data.size() ; i++)
{
if (data[i].key == key)
{
for (uint32_t j = 0; j < data[i].values.size(); j++)
{
if (data[i].values[j].lang == lang)
{
l_ret = data[i].values[j].text;
break;
}
}
break;
}
}
return l_ret;
}
};
DICTIONARY_t dict =
{
{HELLO_WORLD, { {ENGLISH, "Hello World"}, {SLOVENIAN, "Zivjo svet" } }},
{GOODBYE_WORLD, { {ENGLISH, "Goodbye World"}, {SLOVENIAN, "Nasvidenje svet"} }}
};
int main()
{
LANGUAGE selected_language;
std::cout << "Select your lanugagen0.) ENGLISHn1.) Slovenian" << std::endl;
int tmp;
std::cin >> tmp;
selected_language = (LANGUAGE) tmp;
std::cout << dict.get_value(HELLO_WORLD, selected_language) << std::endl;
return 0;
}