Working YellowNotes on Windows.
This commit is contained in:
94
tr.cpp
Normal file
94
tr.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "tr.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
void tr::add(const char *sentence, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, sentence);
|
||||
|
||||
std::string sent(sentence);
|
||||
|
||||
const char *tr = va_arg(args, char *);
|
||||
while (tr != nullptr) {
|
||||
std::string t(tr);
|
||||
size_t pos = t.find(":");
|
||||
std::string lang = t.substr(0, pos);
|
||||
std::string str = t.substr(pos + 1);
|
||||
|
||||
std::unordered_map<std::string, Translations_t *>::iterator it;
|
||||
it = _translations.find(lang);
|
||||
if (it != _translations.end()) {
|
||||
Translations_t *m = it->second;
|
||||
std::pair p = { sent, str };
|
||||
m->insert(p);
|
||||
} else {
|
||||
Translations_t *m = new Translations_t();
|
||||
std::pair p = { sent, str };
|
||||
m->insert(p);
|
||||
std::pair lp = { lang, m };
|
||||
_translations.insert(lp);
|
||||
}
|
||||
|
||||
tr = va_arg(args, char *);
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
const char *tr::translate(const char *str)
|
||||
{
|
||||
std::unordered_map<std::string, Translations_t *>::iterator it;
|
||||
|
||||
it = _translations.find(_lang);
|
||||
|
||||
if (it == _translations.end()) {
|
||||
return str;
|
||||
} else {
|
||||
Translations_t *m = it->second;
|
||||
Translations_t::iterator n_it;
|
||||
std::string s(str);
|
||||
n_it = m->find(s);
|
||||
if (n_it == m->end()) {
|
||||
return str;
|
||||
} else {
|
||||
return n_it->second.c_str();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tr::setLang(const std::string &l)
|
||||
{
|
||||
_lang = l;
|
||||
}
|
||||
|
||||
tr::tr()
|
||||
{
|
||||
add("Quit", "nl:Beëindigen", nullptr);
|
||||
add("New Note", "nl:Nieuwe Notitie", nullptr);
|
||||
add("Show Notes", "nl:Notities Presenteren", nullptr);
|
||||
add("Hide Notes", "nl:Notities Verbergen", nullptr);
|
||||
add("Reload Notes", "nl:Notities opnieuw laden", nullptr);
|
||||
add("Setup", "nl:Instellingen", nullptr);
|
||||
_lang = "en";
|
||||
}
|
||||
|
||||
static tr *_t = nullptr;
|
||||
|
||||
static void init()
|
||||
{
|
||||
if (_t == nullptr) {
|
||||
_t = new tr();
|
||||
}
|
||||
}
|
||||
|
||||
const char *_(const char *str)
|
||||
{
|
||||
init();
|
||||
return _t->translate(str);
|
||||
}
|
||||
|
||||
void setLang(const std::string &lang)
|
||||
{
|
||||
init();
|
||||
_t->setLang(lang);
|
||||
}
|
||||
Reference in New Issue
Block a user