注意文件有编码个和结尾字符,Windows和Linux不太一样,解析处理的中文可能乱码或者结尾符号无法截断正常解析,下面就是csv文件的文本打开
ResID,Chinese_Simplified,Chinese_Traditional,Japanese,Korean,English
1,启用,較佳,望ましい,??,Enable
2,光电探头信号,一般,一般,??,Photoelectric probe signal
3,外接信号,經濟,経済,??,External signal
4,识别(读码)结果:,強烈,ストロング,??,Recognition (code reading) result:
5,生产线速度,一般,一般,??,Production line speed
6,相机型号:,柔和,ソフト,???,Camera model:
我随便填写的文字不要在,程序可能有漏洞
其实用一个lan.csv文件, Excel文件编辑一下就行了,新语言就增加一列,新增翻译就往下添加行
解析csv文件1...6...n就获取一行的语言,首先指定英语项,在使用id拿到英文
下面代码运行的结果
Chinese_Simplified, Chinese_Traditional, Japanese, Korean, English,
启用
光电探头信号
外接信号
识别(读码)结果:
生产线速度
Chinese_Simplified, Chinese_Traditional, Japanese, Korean, English,
Enable
Photoelectric probe signal
External signal
Recognition (code reading) result:
Production line speed
#include <iostream>
#include <istream>
#include <streambuf>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <map>
#include <list>
class multilingual_manage
{
public:
multilingual_manage();
~multilingual_manage();
static int init_language_res(const std::string &lan);
static std::string get_language_content(const std::string& id);
static std::list<std::string> get_multilingual_name() {
return multilingual;
}
private:
static void read_language_content(std::ifstream& csv_data, const std::string& lan);
private:
static std::map<std::string/*id*/, std::string/*text*/> languages;
static std::list<std::string> multilingual;
};
std::map<std::string/*id*/, std::string/*text*/> multilingual_manage::languages;
std::list<std::string> multilingual_manage::multilingual;
multilingual_manage::multilingual_manage()
{
}
multilingual_manage::~multilingual_manage()
{
}
void multilingual_manage::read_language_content(std::ifstream &csv_data, const std::string &lan)
{
std::string word;
std::string line;
std::istringstream sin;//将整行字符串line读入到字符串istringstream中
int currentLanIndex = -1;
// 读取数据
std::string lanId = "";
unsigned int rows = 0;//行
unsigned int columns = 0;//列
while (std::getline(csv_data, line))
{
rows++;
columns = 0;
sin.clear();
sin.str(line);
while (std::getline(sin, word, ',')) //将字符串流sin中的字符读到field字符串中,以逗号为分隔符
{
columns++;
//加入当前列的语言
if (columns == currentLanIndex) {
//std::cout << word << " ";
languages.emplace(std::pair<std::string/*id*/, std::string/*text*/>(lanId, word));
}
//获取语言文本对应的id
if ((columns==1)&&(rows!=1)) {
lanId = word;
//std::cout << word << " ";
}
//获取所有语言
if ((rows==1)&&(columns!=1)) {
multilingual.push_back(word);
//找到当前语言所在列
if (word == lan) {
currentLanIndex = columns;
}
}
//std::cout << rows << "," << columns << std::endl;
}
}
}
std::string multilingual_manage::get_language_content(const std::string &id)
{
std::map<std::string/*id*/, std::string/*text*/>::iterator iter = languages.find(id);
if (iter != languages.end()) {
return iter->second;
}
return "@#";//未翻译
}
#define LANGUAGE_RES_ID(__id__) multilingual_manage::get_language_content(__id__)
#define DEF_LANG_Enable_Use2 LANGUAGE_RES_ID("1") //QObject::tr("启用")
#define DEF_LANG_Vision_Photoelectric_Probe2 LANGUAGE_RES_ID("2") //QObject::tr("光电探头信号")
#define DEF_LANG_Vision_External_Signal3 LANGUAGE_RES_ID("3") //QObject::tr("外接信号")
#define DEF_LANG_Vision_Recognition_Result2 LANGUAGE_RES_ID("4") //QObject::tr("识别(读码)结果:")
#define DEF_LANG_Production_Line_Speed2 LANGUAGE_RES_ID("5") //QObject::tr("生产线速度")
#define DEF_LANG_Camera_Model2 LANGUAGE_RES_ID("6") //QObject::tr("相机型号:")
int multilingual_manage::init_language_res(const std::string& lan)
{
languages.clear();
multilingual.clear();
std::ifstream csv_data("lan.csv"/*文件名你自己的就好*/, std::ios::in);
std::string line;
if (!csv_data.is_open())
{
std::cout << "Error: opening file fail" << std::endl;
std::exit(1);
}
std::istringstream sin;
std::vector<std::string> words;
std::string word;
// 读取
read_language_content(csv_data, lan);
std::list<std::string>::const_iterator citer = multilingual.cbegin();
for (; citer != multilingual.cend(); citer++)
{
std::cout << *citer << ", ";
}
std::cout << std::endl;
std::cout << DEF_LANG_Enable_Use2 << std::endl;
std::cout << DEF_LANG_Vision_Photoelectric_Probe2 << std::endl;
std::cout << DEF_LANG_Vision_External_Signal3 << std::endl;
std::cout << DEF_LANG_Vision_Recognition_Result2 << std::endl;
std::cout << DEF_LANG_Production_Line_Speed2 << std::endl;
csv_data.close();
return 0;
}
int main()
{
multilingual_manage::init_language_res("Chinese_Simplified");
multilingual_manage::init_language_res("English");
return 0;
}