2015-12-06 01:24:51 +00:00
|
|
|
#include "Specter/Translator.hpp"
|
|
|
|
#include <LogVisor/LogVisor.hpp>
|
|
|
|
|
|
|
|
namespace Specter
|
|
|
|
{
|
|
|
|
static LogVisor::LogModule Log("Specter::Translator");
|
|
|
|
|
|
|
|
Locale::Locale(const std::string& name, const std::string& fullName,
|
|
|
|
const unsigned char* yamlSource, size_t yamlLength)
|
|
|
|
: m_name(name), m_fullName(fullName)
|
|
|
|
{
|
|
|
|
Athena::io::YAMLDocReader reader;
|
2016-01-04 23:58:38 +00:00
|
|
|
yaml_parser_set_input_string(reader.getParser(), yamlSource, yamlLength);
|
|
|
|
reader.parse();
|
2015-12-06 01:24:51 +00:00
|
|
|
m_rootNode = std::move(reader.releaseRootNode());
|
2016-01-05 07:50:35 +00:00
|
|
|
if (m_rootNode)
|
|
|
|
{
|
|
|
|
m_langNode = m_rootNode->findMapChild(name.c_str());
|
|
|
|
if (!m_langNode)
|
|
|
|
Log.report(LogVisor::FatalError, "no root node '%s' found in locale", name.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Log.report(LogVisor::Warning, "locale empty");
|
2015-12-06 01:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Translator::setLocale(const Locale* targetLocale)
|
|
|
|
{
|
|
|
|
if (!targetLocale)
|
|
|
|
Log.report(LogVisor::FatalError, "null locale");
|
|
|
|
m_targetLocale = targetLocale;
|
|
|
|
}
|
|
|
|
|
2016-01-05 22:03:40 +00:00
|
|
|
static const std::string* RecursiveLookup(const Athena::io::YAMLNode* node,
|
2015-12-06 01:24:51 +00:00
|
|
|
std::string::const_iterator start,
|
|
|
|
std::string::const_iterator end)
|
|
|
|
{
|
|
|
|
for (std::string::const_iterator it = start ; it != end ; ++it)
|
|
|
|
{
|
|
|
|
if (*it == '/')
|
|
|
|
{
|
2016-01-05 22:03:40 +00:00
|
|
|
const Athena::io::YAMLNode* ch = node->findMapChild(std::string(start, it).c_str());
|
2015-12-06 01:24:51 +00:00
|
|
|
if (!ch)
|
|
|
|
return nullptr;
|
2016-01-05 22:03:40 +00:00
|
|
|
return RecursiveLookup(ch, it+1, end);
|
2015-12-06 01:24:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-05 22:03:40 +00:00
|
|
|
const Athena::io::YAMLNode* ch = node->findMapChild(std::string(start, end).c_str());
|
2015-12-06 01:24:51 +00:00
|
|
|
if (!ch)
|
|
|
|
return nullptr;
|
|
|
|
return &ch->m_scalarString;
|
|
|
|
}
|
|
|
|
|
2015-12-31 03:19:03 +00:00
|
|
|
const std::string* Translator::translate(const std::string& key) const
|
2015-12-06 01:24:51 +00:00
|
|
|
{
|
2016-01-05 22:03:40 +00:00
|
|
|
if (!m_targetLocale->rootNode())
|
2016-01-05 07:50:35 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2015-12-06 01:24:51 +00:00
|
|
|
return RecursiveLookup(m_targetLocale->rootNode(), key.cbegin(), key.cend());
|
|
|
|
}
|
|
|
|
|
2015-12-31 03:19:03 +00:00
|
|
|
std::string Translator::translateOr(const std::string& key, const char* vor) const
|
|
|
|
{
|
2016-01-05 22:03:40 +00:00
|
|
|
const std::string* find = translate(key);
|
2015-12-31 03:19:03 +00:00
|
|
|
if (find)
|
|
|
|
return *find;
|
|
|
|
return vor;
|
|
|
|
}
|
|
|
|
|
2015-12-06 01:24:51 +00:00
|
|
|
}
|