axmol/extensions/fairygui/TranslationHelper.cpp

296 lines
8.9 KiB
C++
Raw Normal View History

2020-08-04 12:31:33 +08:00
#include "TranslationHelper.h"
#include "PackageItem.h"
#include "UIPackage.h"
#if defined(AXIS_VERSION)
#include "pugixml/pugixml.hpp"
2020-08-04 12:31:33 +08:00
#else
#include "tinyxml2/tinyxml2.h"
#endif
#include "utils/ByteBuffer.h"
2021-07-17 23:32:31 +08:00
#include "base/format.h"
2020-08-04 12:31:33 +08:00
USING_NS_CC;
NS_FGUI_BEGIN
using namespace std;
2021-12-31 15:49:45 +08:00
hlookup::string_map<hlookup::string_map<std::string> > TranslationHelper::strings;
2020-08-04 12:31:33 +08:00
void TranslationHelper::loadFromXML(const char* xmlString, size_t nBytes)
{
2021-07-17 23:32:31 +08:00
TranslationHelper::strings.clear();
2020-08-04 12:31:33 +08:00
#if defined(AXIS_VERSION)
2020-08-04 12:31:33 +08:00
pugi::xml_document doc;
if (doc.load_buffer(xmlString, nBytes)) {
auto root = doc.document_element();
2021-12-31 15:49:45 +08:00
auto ele = doc.child("string"sv);
2020-08-04 12:31:33 +08:00
while (ele)
{
2021-12-31 15:49:45 +08:00
auto key = ele.attribute("name"sv).value();
auto text = ele.text().as_string();
2020-08-04 12:31:33 +08:00
size_t i = key.find("-");
if (i == std::string::npos)
continue;
2021-12-31 15:49:45 +08:00
auto key2 = key.substr(0, i);
auto key3 = key.substr(i + 1);
auto& col = TranslationHelper::strings[key2];
2020-08-04 12:31:33 +08:00
col[key3] = text;
2021-12-31 15:49:45 +08:00
ele = ele.next_sibling("string"sv);
2020-08-04 12:31:33 +08:00
}
}
#else
tinyxml2::XMLDocument* xml = new tinyxml2::XMLDocument();
xml->Parse(xmlString, nBytes);
tinyxml2::XMLElement* root = xml->RootElement();
tinyxml2::XMLElement* ele = root->FirstChildElement("string");
while (ele)
{
std::string key = ele->Attribute("name");
std::string text = ele->GetText();
size_t i = key.find("-");
if (i == std::string::npos)
continue;
std::string key2 = key.substr(0, i);
std::string key3 = key.substr(i + 1);
std::unordered_map<std::string, std::string>& col = strings[key2];
col[key3] = text;
ele = ele->NextSiblingElement("string");
}
delete xml;
#endif
}
void TranslationHelper::translateComponent(PackageItem* item)
{
2021-07-17 23:32:31 +08:00
if (TranslationHelper::strings.empty())
2020-08-04 12:31:33 +08:00
return;
2021-07-17 23:32:31 +08:00
auto col = TranslationHelper::strings.find(item->owner->getId() + item->id);
if (col == TranslationHelper::strings.end())
2020-08-04 12:31:33 +08:00
return;
2021-07-17 23:32:31 +08:00
auto& sstrings = col->second;
2020-08-04 12:31:33 +08:00
ByteBuffer* buffer = item->rawData;
buffer->seek(0, 2);
2021-07-17 23:32:31 +08:00
std::string fmtbuf;
2020-08-04 12:31:33 +08:00
int childCount = buffer->readShort();
for (int i = 0; i < childCount; i++)
{
2021-07-17 19:51:27 +08:00
int dataLen = buffer->readUshort();
2020-08-04 12:31:33 +08:00
int curPos = buffer->getPos();
buffer->seek(curPos, 0);
ObjectType baseType = (ObjectType)buffer->readByte();
ObjectType type = baseType;
buffer->skip(4);
const string& elementId = buffer->readS();
if (type == ObjectType::COMPONENT)
{
if (buffer->seek(curPos, 6))
type = (ObjectType)buffer->readByte();
}
buffer->seek(curPos, 1);
2021-07-17 23:32:31 +08:00
auto it = sstrings.find(elementId + "-tips");
if (it != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
buffer->seek(curPos, 2);
int gearCnt = buffer->readShort();
for (int j = 0; j < gearCnt; j++)
{
2021-07-17 19:51:27 +08:00
int nextPos = buffer->readUshort();
2020-08-04 12:31:33 +08:00
nextPos += buffer->getPos();
if (buffer->readByte() == 6) //gearText
{
buffer->skip(2); //controller
int valueCnt = buffer->readShort();
for (int k = 0; k < valueCnt; k++)
{
const string& page = buffer->readS();
if (!page.empty())
{
2021-07-17 23:32:31 +08:00
if ((it = sstrings.find(fmt::format_to(fmtbuf, "{}-texts_{}", elementId, k))) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
else
buffer->skip(2);
}
}
2021-07-17 23:32:31 +08:00
if (buffer->readBool() && (it = sstrings.find(elementId + "-texts_def")) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
}
buffer->setPos(nextPos);
}
if (baseType == ObjectType::COMPONENT && buffer->version >= 2)
{
buffer->seek(curPos, 4);
buffer->skip(2); //pageController
buffer->skip(4 * buffer->readShort());
int cpCount = buffer->readShort();
for (int k = 0; k < cpCount; k++)
{
std::string target = buffer->readS();
int propertyId = buffer->readShort();
2021-07-17 23:32:31 +08:00
if (propertyId == 0 && (it = sstrings.find(elementId + "-cp-" + target)) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
else
buffer->skip(2);
}
}
switch (type)
{
case ObjectType::TEXT:
case ObjectType::RICHTEXT:
case ObjectType::INPUTTEXT:
{
2021-07-17 23:32:31 +08:00
if ((it = sstrings.find(elementId)) != sstrings.end())
2020-08-04 12:31:33 +08:00
{
buffer->seek(curPos, 6);
buffer->writeS(it->second);
}
2021-07-17 23:32:31 +08:00
if ((it = sstrings.find(elementId + "-prompt")) != sstrings.end())
2020-08-04 12:31:33 +08:00
{
buffer->seek(curPos, 4);
buffer->writeS(it->second);
}
break;
}
case ObjectType::LIST:
{
buffer->seek(curPos, 8);
buffer->skip(2);
int itemCount = buffer->readShort();
for (int j = 0; j < itemCount; j++)
{
int nextPos = buffer->readUshort();
nextPos += buffer->getPos();
buffer->skip(2); //url
if (type == ObjectType::TREE)
buffer->skip(2);
//title
2021-07-17 23:32:31 +08:00
if ((it = sstrings.find(fmt::format_to(fmtbuf, "{}-{}", elementId, j))) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
else
buffer->skip(2);
//selected title
2021-07-17 23:32:31 +08:00
if ((it = sstrings.find(fmt::format_to(fmtbuf, "{}-{}-0", elementId, j))) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
else
buffer->skip(2);
if (buffer->version >= 2)
{
buffer->skip(6);
buffer->skip(buffer->readShort() * 4); //controllers
int cpCount = buffer->readShort();
for (int k = 0; k < cpCount; k++)
{
std::string target = buffer->readS();
int propertyId = buffer->readShort();
2021-07-17 23:32:31 +08:00
if (propertyId == 0 && (it = sstrings.find(fmt::format_to(fmtbuf, "{}-{}-{}", elementId, j,
target))) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
else
buffer->skip(2);
}
}
buffer->setPos(nextPos);
}
break;
}
case ObjectType::LABEL:
{
if (buffer->seek(curPos, 6) && (ObjectType)buffer->readByte() == type)
{
2021-07-17 23:32:31 +08:00
if ((it = sstrings.find(elementId)) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
else
buffer->skip(2);
buffer->skip(2);
if (buffer->readBool())
buffer->skip(4);
buffer->skip(4);
2021-07-17 23:32:31 +08:00
if (buffer->readBool() && (it = sstrings.find(elementId + "-prompt")) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
}
break;
}
case ObjectType::BUTTON:
{
if (buffer->seek(curPos, 6) && (ObjectType)buffer->readByte() == type)
{
2021-07-17 23:32:31 +08:00
if ((it = sstrings.find(elementId)) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
else
buffer->skip(2);
2021-07-17 23:32:31 +08:00
if ((it = sstrings.find(elementId + "-0")) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
}
break;
}
case ObjectType::COMBOBOX:
{
if (buffer->seek(curPos, 6) && (ObjectType)buffer->readByte() == type)
{
int itemCount = buffer->readShort();
for (int j = 0; j < itemCount; j++)
{
2021-07-17 19:51:27 +08:00
int nextPos = buffer->readUshort();
2020-08-04 12:31:33 +08:00
nextPos += buffer->getPos();
2021-07-17 23:32:31 +08:00
if ((it = sstrings.find(fmt::format_to(fmtbuf, "{}-{}", elementId, j))) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
buffer->setPos(nextPos);
}
2021-07-17 23:32:31 +08:00
if ((it = sstrings.find(elementId)) != sstrings.end())
2020-08-04 12:31:33 +08:00
buffer->writeS(it->second);
}
break;
}
default:
break;
}
buffer->setPos(curPos + dataLen);
}
}
NS_FGUI_END