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