다음과 같은 번역 파일이 있습니다.
#: this is just some comment
msgid ""
"this is a line.\n"
"this is a newline.\n"
"this is another newLine".
msgstr ""
"this can be filled in.\n"
"or left blank."
#: just another comment
msgid "Pizza"
msgstr ""
보시다시피 여러 줄 msgid
또는 단일 줄 수 있습니다. 동일은 간다 msgstr
.
내 모든 번역 파일은 다음과 같습니다. 위의 데이터 예제를 사용하여 이와 같은 두 개의 키를 포함하는 json 객체를 어떻게 만들 수 있습니까?
[
{
"msgid": "this is a line.\nthis is a newline.\nthis is another newLine.",
"msgstr": "this can be filled in.\n or left blank."
},
{
"msgid": "Pizza",
"msgstr": ""
}
]
사용 방법을 알고있는 json 라이브러리에 액세스 할 수 있습니다. 데이터를 반복하는 for (each) 루프로 고생하고 있습니다.
현재 다음 코드가 있습니다.
std::ifstream input(findFile("language.po"));
Json::Value jsonRoot = Json:arrayValue;
for( std::string line; getline( input, line ); )
{
Json::Value Translation = Json::objectValue;
if(line.find("msgid") == 0) {
//messageId found
Translation["msgid"] = line;
} else if(line.find("msgstr") == 0) {
//translated string was found
Translation["msgstr"] = line;
}
jsonRoot.append(Translation);
}
그러나 이것은 내가 원하지 않는 모든 라인에 대해 새로운 json 배열을 생성합니다.
이 순간 전류 출력 (테스트되지 않음)은 다음과 같아야합니다.
[
{
"msgid": ""
},
{
"msgstr": ""
},
{
"msgid": "Pizza"
},
{
"msgstr": ""
}
]
루프는 각 줄의 내용에 관계없이 각 줄을 배열에 추가합니다. 필요한 것은 상태 머신이므로 완료된 개체 만 배열에 추가하고 다음 필드 시작에 도달 할 때까지 이전 줄에 연속 줄을 추가해야하며 줄 접두사와 따옴표를 제거하기 위해 줄을 구문 분석해야합니다.
다음과 같이 더 시도하십시오.
std::ifstream input(findFile("language.po").string());
std::string msgid, msgstr;
std::string *field = NULL;
std::string::size_type start, end;
Json::Value jsonRoot = Json::arrayValue;
for( std::string line; std::getline( input, line ); )
{
if (line.compare(0, 1, "#") == 0)
continue;
if (line.compare(0, 6, "msgid ") == 0)
{
if (!msgid.empty())
{
Json::Value Translation = Json::objectValue;
Translation["msgid"] = msgid;
Translation["msgstr"] = msgstr;
jsonRoot.append(Translation);
}
msgid.clear();
msgstr.clear();
field = &msgid;
start = 6;
}
else if (!field)
{
continue;
}
else if (line.compare(0, 7, "msgstr ") == 0)
{
field = &msgstr;
start = 7;
}
else
{
start = 0;
}
start = line.find('\"', start);
if (start == std::string::npos)
continue;
++start;
end = line.find('\"', start);
if (end != std::string::npos)
*field += line.substr(start, end-start);
else
*field += line.substr(start);
}
if (!msgid.empty())
{
Json::Value Translation = Json::objectValue;
Translation["msgid"] = msgid;
Translation["msgstr"] = msgstr;
jsonRoot.append(Translation);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다