2016-03-04 23:03:47 +00:00
|
|
|
#include "specter/Translator.hpp"
|
|
|
|
#include "logvisor/logvisor.hpp"
|
2015-12-06 01:24:51 +00:00
|
|
|
|
2016-03-04 23:03:47 +00:00
|
|
|
namespace specter
|
2015-12-06 01:24:51 +00:00
|
|
|
{
|
2016-03-04 23:03:47 +00:00
|
|
|
static logvisor::Module Log("specter::Translator");
|
2015-12-06 01:24:51 +00:00
|
|
|
|
|
|
|
Locale::Locale(const std::string& name, const std::string& fullName,
|
|
|
|
const unsigned char* yamlSource, size_t yamlLength)
|
|
|
|
: m_name(name), m_fullName(fullName)
|
|
|
|
{
|
2016-03-04 23:03:47 +00:00
|
|
|
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)
|
2016-03-04 23:03:47 +00:00
|
|
|
Log.report(logvisor::Fatal, "no root node '%s' found in locale", name.c_str());
|
2016-01-05 07:50:35 +00:00
|
|
|
}
|
|
|
|
else
|
2016-03-04 23:03:47 +00:00
|
|
|
Log.report(logvisor::Warning, "locale empty");
|
2015-12-06 01:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Translator::setLocale(const Locale* targetLocale)
|
|
|
|
{
|
|
|
|
if (!targetLocale)
|
2016-03-04 23:03:47 +00:00
|
|
|
Log.report(logvisor::Fatal, "null locale");
|
2015-12-06 01:24:51 +00:00
|
|
|
m_targetLocale = targetLocale;
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:03:47 +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-03-04 23:03:47 +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-03-04 23:03:47 +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
|
|
|
}
|